git: 5276fba9fd55 - main - pwddesc: add sharecount

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Wed, 04 Nov 2026 16:23:21 UTC
The branch main has been updated by kib:

URL: https://cgit.FreeBSD.org/src/commit/?id=5276fba9fd55bf2e79c54df18a70b46d11cc15a1

commit 5276fba9fd55bf2e79c54df18a70b46d11cc15a1
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2026-10-21 22:54:59 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2026-11-04 16:23:11 +0000

    pwddesc: add sharecount
    
    The pd_refcount field counts both sharing processes and the transient
    pdhold() references taken by sysctls and mountcheckdirs(), so it
    cannot answer "is this pwddesc shared".  As a result pdunshare() made
    needless copies while the pwddesc was held, and a copy made while
    mountcheckdirs() held it lost the cwd/root update.
    
    Add pd_sharecount, counting only processes using the pwddesc, and use
    it for the sharing decision. Existing pd_refcount still manages the
    lifetime.
    
    Reviewed by:    markj
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
    Differential revision:  https://reviews.freebsd.org/D60963
---
 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 0aafaa8bdb9f..0fa8210b71c0 100644
--- a/sys/kern/kern_descrip.c
+++ b/sys/kern/kern_descrip.c
@@ -2446,6 +2446,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) {
@@ -2527,6 +2528,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);
@@ -2553,7 +2555,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);
 }
 
@@ -2585,7 +2591,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);
@@ -2871,6 +2877,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 */
 };