git: 45f988b6abfd - stable/15 - pwddesc: add sharecount

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Wed, 11 Nov 2026 01:06:59 UTC
The branch stable/15 has been updated by kib:

URL: https://cgit.FreeBSD.org/src/commit/?id=45f988b6abfdf1b7b62f429f5fc1eb89296c1327

commit 45f988b6abfdf1b7b62f429f5fc1eb89296c1327
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2026-10-21 22:54:59 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2026-11-10 19:53:56 +0000

    pwddesc: add sharecount
    
    (cherry picked from commit 5276fba9fd55bf2e79c54df18a70b46d11cc15a1)
---
 sys/kern/kern_descrip.c | 9 ++++++++-
 sys/sys/filedesc.h      | 3 ++-
 2 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/sys/kern/kern_descrip.c b/sys/kern/kern_descrip.c
index 764368b36cd3..ea033a7f598a 100644
--- a/sys/kern/kern_descrip.c
+++ b/sys/kern/kern_descrip.c
@@ -2418,6 +2418,7 @@ pdinit(struct pwddesc *pdp, bool keeplock)
 
 	PWDDESC_LOCK_INIT(newpdp);
 	refcount_init(&newpdp->pd_refcount, 1);
+	refcount_init(&newpdp->pd_sharecount, 1);
 	newpdp->pd_cmask = CMASK;
 
 	if (pdp == NULL) {
@@ -2499,6 +2500,7 @@ pddrop(struct pwddesc *pdp)
 		PWDDESC_XUNLOCK(pdp);
 		return;
 	}
+	MPASS(refcount_load(&pdp->pd_sharecount) == 0);
 	pwd = PWDDESC_XLOCKED_LOAD_PWD(pdp);
 	pwd_set(pdp, NULL);
 	PWDDESC_XUNLOCK(pdp);
@@ -2525,7 +2527,11 @@ fdshare(struct filedesc *fdp)
 struct pwddesc *
 pdshare(struct pwddesc *pdp)
 {
+	u_int old __diagused;
+
 	refcount_acquire(&pdp->pd_refcount);
+	old = refcount_acquire(&pdp->pd_sharecount);
+	MPASS(old > 0);
 	return (pdp);
 }
 
@@ -2557,7 +2563,7 @@ pdunshare(struct thread *td)
 
 	p = td->td_proc;
 	/* Not shared. */
-	if (refcount_load(&p->p_pd->pd_refcount) == 1)
+	if (refcount_load(&p->p_pd->pd_sharecount) == 1)
 		return;
 
 	pdp = pdcopy(p->p_pd);
@@ -2843,6 +2849,7 @@ pdescfree(struct thread *td)
 	atomic_thread_fence_seq_cst();
 	PROC_WAIT_UNLOCKED(p);
 
+	(void)refcount_release(&pdp->pd_sharecount);
 	pddrop(pdp);
 }
 
diff --git a/sys/sys/filedesc.h b/sys/sys/filedesc.h
index a2bb6c671dbb..d985641eb475 100644
--- a/sys/sys/filedesc.h
+++ b/sys/sys/filedesc.h
@@ -103,7 +103,8 @@ typedef SMR_POINTER(struct pwd *) smrpwd_t;
 struct pwddesc {
 	struct mtx	pd_lock;	/* protects members of this struct */
 	smrpwd_t	pd_pwd;		/* directories */
-	u_int		pd_refcount;
+	u_int		pd_refcount;	/* lifetime */
+	u_int		pd_sharecount;	/* processes sharing the descriptor */
 	u_short		pd_cmask;	/* mask for file creation */
 };