From nobody Sat Nov 01 00:45:01 2025 X-Original-To: dev-commits-src-branches@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4cyzdp0hCGz6FWqS; Sat, 01 Nov 2025 00:45:02 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R12" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4cyzdp08phz3grG; Sat, 01 Nov 2025 00:45:02 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1761957902; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=krFM72Lf340FUHVzDUl1cwDCyTrwliyLJDRSnSZgCfw=; b=Nmng82pZ7UMWfPw5SvPGgoyzEWj2Heu6TGI93OEmSItPUdckurs4tjHMh5RVawn4GpUc+2 tPjbp9zuGh+JZJXqVB4B1rHEH+TwcSEQpeJgBYy1J5m73UW6q7z0yrXk2fLuv8YH9f0c0H upNBAF+CBfF4yHtFuMD58dJLcMwLvqlr1nTPW1dBpjYWgpmkGHpz0nX+KxEN6ZpSuM8R0n YGxP4j6QqaWN6uVoBJQ2zNApvcGIINfTMbB1XxBFeMEEYaX4L/5l4z0dZLnh+BjUKewPpk Fdw+gFFp/pj3M2c0eVeUo6ZYDcBB5yJAkoHCmiTZsPKHyoRHRiffnAFRo8864A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1761957902; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=krFM72Lf340FUHVzDUl1cwDCyTrwliyLJDRSnSZgCfw=; b=dZz3AuSySOSI1j9YtkBQVFGJHPsMhNWNBY6TTymGpsnrr4zkd6CT5jo9Tv0C8vAWJYJpjj e4xwwcsd4Nw2D5a/cMOOdQCOf7+CcpLBhvQkFC72uS+1k2RvsH/wXYtlyVjuRBYtCkSYGK Q60snvxMPFmFnKwk2JOTSq+v7ytnrUGjYCM0N24GJbMAVlF7V54EOH1vyduGIEJ9ywnkRQ DOHvwob5IT7SbuRAt5Sm2B7k4q5VvvWicLjSvV6Xjldtm4YV20AOx7YFM1ww5uRH08JhrO oYACsecOeHUs9IOKwKHvVOIEnNmoCmhIwdn1iWxGfBf98KIP3IlxOQjVLvW5VA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1761957902; a=rsa-sha256; cv=none; b=aHebDygE1eLHuhjomFjkhN+gkLV0Qc3VL8NE0+WRtF3he0Ta2bmdeEbI8hr343kR1CUMWQ wzOZS5VHgNgg612mrsMUTnPkMNMMvXQIIxFwpN9hKsoHht7bquA9cQ6jlv4hKufrsPUZAO Osi/Rkdo6h7FeYMbG78f7xoeDMuGgMsH0xEkKpLhJvE8b2ZNHa0/P430QLdLZ7Fdt0iHz+ +TPga151GLwJPNY40NMJCCEJYMHqm2SW0an3jHAVtdIQi0/aP5xObW/7aKX0uaIbYCUvRB Da0fvrnu+S1+pTHLaeu8MNCXXB/YFP1E2ybZGOnjLtqAi6vJm2gJQoRC14xp/A== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4cyzdn6hrrz13b; Sat, 01 Nov 2025 00:45:01 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 5A10j19Q000671; Sat, 1 Nov 2025 00:45:01 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 5A10j1rq000668; Sat, 1 Nov 2025 00:45:01 GMT (envelope-from git) Date: Sat, 1 Nov 2025 00:45:01 GMT Message-Id: <202511010045.5A10j1rq000668@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Konstantin Belousov Subject: git: 77a5c63f7923 - stable/15 - struct file: add DFLAG_FORK, indicate copying on fork List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-branches@freebsd.org Sender: owner-dev-commits-src-branches@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kib X-Git-Repository: src X-Git-Refname: refs/heads/stable/15 X-Git-Reftype: branch X-Git-Commit: 77a5c63f79236301dece4f3045e254ccc5f49716 Auto-Submitted: auto-generated The branch stable/15 has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=77a5c63f79236301dece4f3045e254ccc5f49716 commit 77a5c63f79236301dece4f3045e254ccc5f49716 Author: Konstantin Belousov AuthorDate: 2025-08-20 19:07:43 +0000 Commit: Konstantin Belousov CommitDate: 2025-11-01 00:43:13 +0000 struct file: add DFLAG_FORK, indicate copying on fork (cherry picked from commit 1265516c5c610fbc0d65451140e3f17b358cd015) --- sys/kern/kern_descrip.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++-- sys/sys/file.h | 6 +++++- 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/sys/kern/kern_descrip.c b/sys/kern/kern_descrip.c index 057235574eb5..6934a5a83081 100644 --- a/sys/kern/kern_descrip.c +++ b/sys/kern/kern_descrip.c @@ -2510,10 +2510,13 @@ fdcopy(struct filedesc *fdp) { struct filedesc *newfdp; struct filedescent *nfde, *ofde; + struct file *fp; int i, lastfile; + bool fork_pass; MPASS(fdp != NULL); + fork_pass = false; newfdp = fdinit(); FILEDESC_SLOCK(fdp); for (;;) { @@ -2524,10 +2527,35 @@ fdcopy(struct filedesc *fdp) fdgrowtable(newfdp, lastfile + 1); FILEDESC_SLOCK(fdp); } - /* copy all passable descriptors (i.e. not kqueue) */ + + /* + * Copy all passable descriptors (i.e. not kqueue), and + * prepare to handle copyable but not passable descriptors + * (kqueues). + * + * The pass to handle copying is performed after all passable + * files are installed into the new file descriptor's table, + * since kqueues need all referenced file descriptors already + * valid, including other kqueues. For the same reason the + * copying is done in two passes by itself, first installing + * not fully initialized ('empty') copyable files into the new + * fd table, and then giving the subsystems a second chance to + * really fill the copied file backing structure with the + * content. + */ newfdp->fd_freefile = fdp->fd_freefile; FILEDESC_FOREACH_FDE(fdp, i, ofde) { - if ((ofde->fde_file->f_ops->fo_flags & DFLAG_PASSABLE) == 0 || + const struct fileops *ops; + + ops = ofde->fde_file->f_ops; + fp = NULL; + if ((ops->fo_flags & DFLAG_FORK) != 0 && + (ofde->fde_flags & UF_FOCLOSE) == 0) { + if (ops->fo_fork(newfdp, ofde->fde_file, &fp, p1, + curthread) != 0) + continue; + fork_pass = true; + } else if ((ops->fo_flags & DFLAG_PASSABLE) == 0 || (ofde->fde_flags & UF_FOCLOSE) != 0 || !fhold(ofde->fde_file)) { if (newfdp->fd_freefile == fdp->fd_freefile) @@ -2536,11 +2564,30 @@ fdcopy(struct filedesc *fdp) } nfde = &newfdp->fd_ofiles[i]; *nfde = *ofde; + if (fp != NULL) + nfde->fde_file = fp; filecaps_copy(&ofde->fde_caps, &nfde->fde_caps, true); fdused_init(newfdp, i); } MPASS(newfdp->fd_freefile != -1); FILEDESC_SUNLOCK(fdp); + + /* + * Now handle copying kqueues, since all fds, including + * kqueues, are in place. + */ + if (__predict_false(fork_pass)) { + FILEDESC_FOREACH_FDE(newfdp, i, nfde) { + const struct fileops *ops; + + ops = nfde->fde_file->f_ops; + if ((ops->fo_flags & DFLAG_FORK) == 0 || + nfde->fde_file == NULL) + continue; + ops->fo_fork(newfdp, NULL, &nfde->fde_file, p1, + curthread); + } + } return (newfdp); } diff --git a/sys/sys/file.h b/sys/sys/file.h index 9a072121e25f..b2bd62f244da 100644 --- a/sys/sys/file.h +++ b/sys/sys/file.h @@ -137,6 +137,8 @@ typedef int fo_fspacectl_t(struct file *fp, int cmd, off_t *offset, off_t *length, int flags, struct ucred *active_cred, struct thread *td); typedef int fo_cmp_t(struct file *fp, struct file *fp1, struct thread *td); +typedef int fo_fork_t(struct filedesc *fdp, struct file *fp, struct file **fp1, + struct proc *p1, struct thread *td); typedef int fo_spare_t(struct file *fp); typedef int fo_flags_t; @@ -161,12 +163,14 @@ struct fileops { fo_fallocate_t *fo_fallocate; fo_fspacectl_t *fo_fspacectl; fo_cmp_t *fo_cmp; - fo_spare_t *fo_spares[8]; /* Spare slots */ + fo_fork_t *fo_fork; + fo_spare_t *fo_spares[7]; /* Spare slots */ fo_flags_t fo_flags; /* DFLAG_* below */ }; #define DFLAG_PASSABLE 0x01 /* may be passed via unix sockets. */ #define DFLAG_SEEKABLE 0x02 /* seekable / nonsequential */ +#define DFLAG_FORK 0x04 /* copy on fork */ #endif /* _KERNEL */ #if defined(_KERNEL) || defined(_WANT_FILE)