Use the refcnt API with struct ucred. OK?
Index: nfs/nfs_socket.c =================================================================== RCS file: src/sys/nfs/nfs_socket.c,v retrieving revision 1.139 diff -u -p -r1.139 nfs_socket.c --- nfs/nfs_socket.c 22 Feb 2022 01:15:02 -0000 1.139 +++ nfs/nfs_socket.c 16 Mar 2022 15:42:05 -0000 @@ -1493,7 +1493,7 @@ nfs_getreq(struct nfsrv_descript *nd, st nfsm_adv(nfsm_rndup(len)); nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED); memset(&nd->nd_cr, 0, sizeof (struct ucred)); - nd->nd_cr.cr_ref = 1; + refcnt_init(&nd->nd_cr.cr_refcnt); nd->nd_cr.cr_uid = fxdr_unsigned(uid_t, *tl++); nd->nd_cr.cr_gid = fxdr_unsigned(gid_t, *tl++); len = fxdr_unsigned(int, *tl); Index: kern/kern_fork.c =================================================================== RCS file: src/sys/kern/kern_fork.c,v retrieving revision 1.238 diff -u -p -r1.238 kern_fork.c --- kern/kern_fork.c 10 Dec 2021 05:34:42 -0000 1.238 +++ kern/kern_fork.c 16 Mar 2022 15:42:05 -0000 @@ -190,7 +190,8 @@ process_initialize(struct process *pr, s /* give the process the same creds as the initial thread */ pr->ps_ucred = p->p_ucred; crhold(pr->ps_ucred); - KASSERT(p->p_ucred->cr_ref >= 2); /* new thread and new process */ + /* new thread and new process */ + KASSERT(p->p_ucred->cr_refcnt.r_refs >= 2); LIST_INIT(&pr->ps_children); LIST_INIT(&pr->ps_orphans); Index: kern/kern_prot.c =================================================================== RCS file: src/sys/kern/kern_prot.c,v retrieving revision 1.78 diff -u -p -r1.78 kern_prot.c --- kern/kern_prot.c 24 Oct 2021 00:02:25 -0000 1.78 +++ kern/kern_prot.c 16 Mar 2022 15:42:05 -0000 @@ -57,7 +57,7 @@ inline void crset(struct ucred *newcr, const struct ucred *cr) { - KASSERT(cr->cr_ref > 0); + KASSERT(cr->cr_refcnt.r_refs > 0); memcpy( (char *)newcr + offsetof(struct ucred, cr_startcopy), (const char *)cr + offsetof(struct ucred, cr_startcopy), @@ -945,7 +945,7 @@ crget(void) struct ucred *cr; cr = pool_get(&ucred_pool, PR_WAITOK|PR_ZERO); - cr->cr_ref = 1; + refcnt_init(&cr->cr_refcnt); return (cr); } @@ -956,7 +956,7 @@ crget(void) struct ucred * crhold(struct ucred *cr) { - atomic_inc_int(&cr->cr_ref); + refcnt_take(&cr->cr_refcnt); return (cr); } @@ -967,8 +967,7 @@ crhold(struct ucred *cr) void crfree(struct ucred *cr) { - - if (atomic_dec_int_nv(&cr->cr_ref) == 0) + if (refcnt_rele(&cr->cr_refcnt)) pool_put(&ucred_pool, cr); } @@ -980,12 +979,12 @@ crcopy(struct ucred *cr) { struct ucred *newcr; - if (cr->cr_ref == 1) + if (!refcnt_shared(&cr->cr_refcnt)) return (cr); newcr = crget(); *newcr = *cr; crfree(cr); - newcr->cr_ref = 1; + refcnt_init(&newcr->cr_refcnt); return (newcr); } @@ -999,7 +998,7 @@ crdup(struct ucred *cr) newcr = crget(); *newcr = *cr; - newcr->cr_ref = 1; + refcnt_init(&newcr->cr_refcnt); return (newcr); } @@ -1011,7 +1010,7 @@ crfromxucred(struct ucred *cr, const str { if (xcr->cr_ngroups < 0 || xcr->cr_ngroups > NGROUPS_MAX) return (EINVAL); - cr->cr_ref = 1; + refcnt_init(&cr->cr_refcnt); cr->cr_uid = xcr->cr_uid; cr->cr_gid = xcr->cr_gid; cr->cr_ngroups = xcr->cr_ngroups; Index: sys/ucred.h =================================================================== RCS file: src/sys/sys/ucred.h,v retrieving revision 1.13 diff -u -p -r1.13 ucred.h --- sys/ucred.h 21 Jun 2018 13:58:21 -0000 1.13 +++ sys/ucred.h 16 Mar 2022 15:42:05 -0000 @@ -35,13 +35,14 @@ #ifndef _SYS_UCRED_H_ #define _SYS_UCRED_H_ +#include <sys/refcnt.h> #include <sys/syslimits.h> /* * Credentials. */ struct ucred { - u_int cr_ref; /* reference count */ + struct refcnt cr_refcnt; /* reference count */ /* The following fields are all copied by crset() */ #define cr_startcopy cr_uid