Use the refcnt API with struct plimit.

OK?

Index: kern/kern_resource.c
===================================================================
RCS file: src/sys/kern/kern_resource.c,v
retrieving revision 1.71
diff -u -p -r1.71 kern_resource.c
--- kern/kern_resource.c        8 Feb 2021 10:51:01 -0000       1.71
+++ kern/kern_resource.c        17 Mar 2022 15:59:52 -0000
@@ -582,7 +582,7 @@ lim_startup(struct plimit *limit0)
        limit0->pl_rlimit[RLIMIT_RSS].rlim_max = lim;
        limit0->pl_rlimit[RLIMIT_MEMLOCK].rlim_max = lim;
        limit0->pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = lim / 3;
-       limit0->pl_refcnt = 1;
+       refcnt_init(&limit0->pl_refcnt);
 }
 
 /*
@@ -598,14 +598,14 @@ lim_copy(struct plimit *lim)
        newlim = pool_get(&plimit_pool, PR_WAITOK);
        memcpy(newlim->pl_rlimit, lim->pl_rlimit,
            sizeof(struct rlimit) * RLIM_NLIMITS);
-       newlim->pl_refcnt = 1;
+       refcnt_init(&newlim->pl_refcnt);
        return (newlim);
 }
 
 void
 lim_free(struct plimit *lim)
 {
-       if (atomic_dec_int_nv(&lim->pl_refcnt) > 0)
+       if (refcnt_rele(&lim->pl_refcnt) == 0)
                return;
        pool_put(&plimit_pool, lim);
 }
@@ -617,7 +617,7 @@ lim_fork(struct process *parent, struct 
 
        mtx_enter(&parent->ps_mtx);
        limit = parent->ps_limit;
-       atomic_inc_int(&limit->pl_refcnt);
+       refcnt_take(&limit->pl_refcnt);
        mtx_leave(&parent->ps_mtx);
 
        child->ps_limit = limit;
@@ -650,7 +650,7 @@ lim_write_begin(void)
         */
 
        limit = p->p_p->ps_limit;
-       if (P_HASSIBLING(p) || limit->pl_refcnt > 1)
+       if (P_HASSIBLING(p) || refcnt_shared(&limit->pl_refcnt))
                limit = lim_copy(limit);
 
        return (limit);
@@ -703,7 +703,7 @@ lim_read_enter(void)
        if (limit != pr->ps_limit) {
                mtx_enter(&pr->ps_mtx);
                limit = pr->ps_limit;
-               atomic_inc_int(&limit->pl_refcnt);
+               refcnt_take(&limit->pl_refcnt);
                mtx_leave(&pr->ps_mtx);
                if (p->p_limit != NULL)
                        lim_free(p->p_limit);
Index: sys/resourcevar.h
===================================================================
RCS file: src/sys/sys/resourcevar.h,v
retrieving revision 1.24
diff -u -p -r1.24 resourcevar.h
--- sys/resourcevar.h   21 Jun 2019 09:39:48 -0000      1.24
+++ sys/resourcevar.h   17 Mar 2022 15:59:52 -0000
@@ -35,6 +35,7 @@
 #ifndef        _SYS_RESOURCEVAR_H_
 #define        _SYS_RESOURCEVAR_H_
 
+#include <sys/refcnt.h>
 #include <sys/timeout.h>
 
 /*
@@ -44,7 +45,7 @@
  */
 struct plimit {
        struct  rlimit pl_rlimit[RLIM_NLIMITS];
-       u_int   pl_refcnt;              /* number of references */
+       struct  refcnt pl_refcnt;
 };
 
 /* add user profiling from AST */

Reply via email to