git: 2832d06f8b42 - main - kern/kern_descrip.c: add pdswap() helper

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Mon, 23 Nov 2026 19:09:17 UTC
The branch main has been updated by kib:

URL: https://cgit.FreeBSD.org/src/commit/?id=2832d06f8b428c7bdedaa21724ca4d8a196529db

commit 2832d06f8b428c7bdedaa21724ca4d8a196529db
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2026-11-23 01:39:14 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2026-11-23 19:05:46 +0000

    kern/kern_descrip.c: add pdswap() helper
    
    It stores the new pwddesc into the target process's p_pd, then runs the
    tail of pdescfree() on the old one.
    
    Reviewed by:    markj
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
    Differential revision:  https://reviews.freebsd.org/D61934
---
 sys/kern/kern_descrip.c | 32 +++++++++++++++++++++++++++++++-
 sys/sys/filedesc.h      |  2 ++
 2 files changed, 33 insertions(+), 1 deletion(-)

diff --git a/sys/kern/kern_descrip.c b/sys/kern/kern_descrip.c
index c36fc16ef19b..d1cc35894cd2 100644
--- a/sys/kern/kern_descrip.c
+++ b/sys/kern/kern_descrip.c
@@ -119,7 +119,7 @@ static void	filecaps_copy_finish(const struct filecaps *src,
 		    struct filecaps *dst, u_long *ioctls);
 static u_long 	*filecaps_free_prep(struct filecaps *fcaps);
 static void	filecaps_free_finish(u_long *ioctls);
-
+static void	pdescfree_tail(struct proc *p, struct pwddesc *pdp);
 static struct pwd *pwd_alloc(void);
 
 /*
@@ -2581,6 +2581,22 @@ fdunshare(struct thread *td)
 	p->p_fd = tmp;
 }
 
+/*
+ * Used by the caller to replace its own p_pd while no other writer
+ * exists.  old_pdp must be the current value, and its share and
+ * reference are consumed.
+ */
+void
+pdswap(struct proc *p, struct pwddesc *new_pdp, struct pwddesc *old_pdp)
+{
+	MPASS(p->p_pd == old_pdp);
+
+	/* Make the initialized copy visible before the pointer to it. */
+	atomic_store_rel_ptr((volatile uintptr_t *)&p->p_pd,
+	    (uintptr_t)new_pdp);
+	pdescfree_tail(p, old_pdp);
+}
+
 /*
  * Unshare a pwddesc structure.
  */
@@ -2861,6 +2877,20 @@ fdescfree(struct thread *td)
 	fdescfree_fds(td, fdp);
 }
 
+static void
+pdescfree_tail(struct proc *p, struct pwddesc *pdp)
+{
+	/*
+	 * The p_pd store must be visible before the process lock is
+	 * observed unlocked, see the comment above fdhold().
+	 */
+	atomic_thread_fence_seq_cst();
+	PROC_WAIT_UNLOCKED(p);
+
+	(void)refcount_release(&pdp->pd_sharecount);
+	pddrop(pdp);
+}
+
 void
 pdescfree(struct thread *td)
 {
diff --git a/sys/sys/filedesc.h b/sys/sys/filedesc.h
index 3dfac6f0ab14..178c3fee96cb 100644
--- a/sys/sys/filedesc.h
+++ b/sys/sys/filedesc.h
@@ -370,6 +370,8 @@ struct pwddesc *pdcopy(struct pwddesc *pdp);
 void	pdescfree(struct thread *td);
 struct pwddesc *pdinit(struct pwddesc *pdp, bool keeplock);
 struct pwddesc *pdshare(struct pwddesc *pdp);
+void	pdswap(struct proc *p, struct pwddesc *new_pdp,
+	    struct pwddesc *old_pdp);
 void	pdunshare(struct thread *td);
 
 int	pwd_altroot(struct thread *td, struct vnode *altroot_vp);