Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/sys Add pool_cache_setpredestruct(), which allows a pool...



details:   https://anonhg.NetBSD.org/src/rev/8282ce8b184f
branches:  trunk
changeset: 1029104:8282ce8b184f
user:      thorpej <thorpej%NetBSD.org@localhost>
date:      Tue Dec 21 18:59:22 2021 +0000

description:
Add pool_cache_setpredestruct(), which allows a pool cache to specify
a function to be called before the destructor for a batch of one or more
objects is called.  This can be used as a synchronization point by
subsystems that rely on the type-stable nature of pool cache objects or
subsystems that use other forms of passive serialization.

diffstat:

 distrib/sets/lists/comp/mi  |   5 ++++-
 share/man/man9/Makefile     |   5 +++--
 share/man/man9/pool_cache.9 |  20 +++++++++++++++++---
 sys/kern/subr_pool.c        |  39 +++++++++++++++++++++++++++++++++++++--
 sys/sys/param.h             |   4 ++--
 sys/sys/pool.h              |   6 ++++--
 6 files changed, 67 insertions(+), 12 deletions(-)

diffs (240 lines):

diff -r 413c96ed0aaf -r 8282ce8b184f distrib/sets/lists/comp/mi
--- a/distrib/sets/lists/comp/mi        Tue Dec 21 16:50:11 2021 +0000
+++ b/distrib/sets/lists/comp/mi        Tue Dec 21 18:59:22 2021 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: mi,v 1.2399 2021/12/07 17:39:53 brad Exp $
+#      $NetBSD: mi,v 1.2400 2021/12/21 18:59:22 thorpej Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 ./etc/mtree/set.comp                           comp-sys-root
@@ -12282,6 +12282,7 @@
 ./usr/share/man/cat9/pool_cache_sethardlimit.0 comp-sys-catman         .cat
 ./usr/share/man/cat9/pool_cache_sethiwat.0     comp-sys-catman         .cat
 ./usr/share/man/cat9/pool_cache_setlowat.0     comp-sys-catman         .cat
+./usr/share/man/cat9/pool_cache_setpredestruct.0       comp-sys-catman         .cat
 ./usr/share/man/cat9/pool_create.0             comp-sys-catman         .cat
 ./usr/share/man/cat9/pool_destroy.0            comp-sys-catman         .cat
 ./usr/share/man/cat9/pool_get.0                        comp-sys-catman         .cat
@@ -20474,6 +20475,7 @@
 ./usr/share/man/html9/pool_cache_sethardlimit.html     comp-sys-htmlman        html
 ./usr/share/man/html9/pool_cache_sethiwat.html comp-sys-htmlman        html
 ./usr/share/man/html9/pool_cache_setlowat.html comp-sys-htmlman        html
+./usr/share/man/html9/pool_cache_setpredestruct.html   comp-sys-htmlman        html
 ./usr/share/man/html9/pool_create.html         comp-sys-htmlman        html
 ./usr/share/man/html9/pool_destroy.html                comp-sys-htmlman        html
 ./usr/share/man/html9/pool_get.html            comp-sys-htmlman        html
@@ -28827,6 +28829,7 @@
 ./usr/share/man/man9/pool_cache_sethardlimit.9 comp-sys-man            .man
 ./usr/share/man/man9/pool_cache_sethiwat.9     comp-sys-man            .man
 ./usr/share/man/man9/pool_cache_setlowat.9     comp-sys-man            .man
+./usr/share/man/man9/pool_cache_setpredestruct.9       comp-sys-man            .man
 ./usr/share/man/man9/pool_create.9             comp-sys-man            .man
 ./usr/share/man/man9/pool_destroy.9            comp-sys-man            .man
 ./usr/share/man/man9/pool_get.9                        comp-sys-man            .man
diff -r 413c96ed0aaf -r 8282ce8b184f share/man/man9/Makefile
--- a/share/man/man9/Makefile   Tue Dec 21 16:50:11 2021 +0000
+++ b/share/man/man9/Makefile   Tue Dec 21 18:59:22 2021 +0000
@@ -1,4 +1,4 @@
-#       $NetBSD: Makefile,v 1.458 2021/04/28 00:49:22 thorpej Exp $
+#       $NetBSD: Makefile,v 1.459 2021/12/21 18:59:22 thorpej Exp $
 
 #      Makefile for section 9 (kernel function and variable) manual pages.
 
@@ -758,7 +758,8 @@
        pool_cache.9 pool_cache_invalidate.9 \
        pool_cache.9 pool_cache_sethiwat.9 \
        pool_cache.9 pool_cache_setlowat.9 \
-       pool_cache.9 pool_cache_sethardlimit.9
+       pool_cache.9 pool_cache_sethardlimit.9 \
+       pool_cache.9 pool_cache_setpredestruct.9
 MLINKS+=powerhook_establish.9 powerhook_disestablish.9
 MLINKS+=preempt.9 yield.9
 MLINKS+=pserialize.9 pserialize_create.9 \
diff -r 413c96ed0aaf -r 8282ce8b184f share/man/man9/pool_cache.9
--- a/share/man/man9/pool_cache.9       Tue Dec 21 16:50:11 2021 +0000
+++ b/share/man/man9/pool_cache.9       Tue Dec 21 18:59:22 2021 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: pool_cache.9,v 1.22 2020/04/13 08:59:14 wiz Exp $
+.\"    $NetBSD: pool_cache.9,v 1.23 2021/12/21 18:59:22 thorpej Exp $
 .\"
 .\" Copyright (c)2003 YAMAMOTO Takashi,
 .\" All rights reserved.
@@ -53,7 +53,7 @@
 .\" POSSIBILITY OF SUCH DAMAGE.
 .\"
 .\" ------------------------------------------------------------
-.Dd April 12, 2020
+.Dd December 21, 2021
 .Dt POOL_CACHE 9
 .Os
 .\" ------------------------------------------------------------
@@ -69,7 +69,8 @@
 .Nm pool_cache_invalidate ,
 .Nm pool_cache_sethiwat ,
 .Nm pool_cache_setlowat ,
-.Nm pool_cache_sethardlimit
+.Nm pool_cache_sethardlimit ,
+.Nm pool_cache_setpredestruct
 .Nd resource-pool cache manager
 .\" ------------------------------------------------------------
 .Sh SYNOPSIS
@@ -119,6 +120,10 @@
 .Fn pool_cache_sethardlimit \
 "pool_cache_t pc" "int n" "const char *warnmess" "int ratecap"
 .\" ------------------------------------------------------------
+.Ft void
+.Fn pool_cache_setpredestruct \
+"pool_cache_t pc" "void (*fn)(void *)"
+.\" ------------------------------------------------------------
 .Sh DESCRIPTION
 These utility routines provide management of pools of fixed-sized
 areas of memory.
@@ -347,6 +352,15 @@
 represents the minimal interval (in seconds) after which another warning
 message is issued when the pool hits its hard limit again.
 .\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+.It Fn pool_cache_setpredestruct "fn"
+Sets a pre-destructor hook for the pool cache.
+This hook will be invoked
+.Pq with the same argument passed to the destructor
+before a batch of one or more objects is destructed.
+Pool cache users who rely on type-stability or use other
+forms of passive serialization may use this hook as a
+synchronization point.
+.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 .It Fn pool_cache_prime "pc" "n"
 Set the minimum number of total items (both free and allocated) for the backing
 .Xr pool 9
diff -r 413c96ed0aaf -r 8282ce8b184f sys/kern/subr_pool.c
--- a/sys/kern/subr_pool.c      Tue Dec 21 16:50:11 2021 +0000
+++ b/sys/kern/subr_pool.c      Tue Dec 21 18:59:22 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: subr_pool.c,v 1.277 2021/07/25 06:00:31 simonb Exp $   */
+/*     $NetBSD: subr_pool.c,v 1.278 2021/12/21 18:59:22 thorpej Exp $  */
 
 /*
  * Copyright (c) 1997, 1999, 2000, 2002, 2007, 2008, 2010, 2014, 2015, 2018,
@@ -33,7 +33,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: subr_pool.c,v 1.277 2021/07/25 06:00:31 simonb Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_pool.c,v 1.278 2021/12/21 18:59:22 thorpej Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_ddb.h"
@@ -2119,6 +2119,7 @@
        pc->pc_partgroups = NULL;
        pc->pc_ctor = ctor;
        pc->pc_dtor = dtor;
+       pc->pc_pre_dtor = NULL;
        pc->pc_arg  = arg;
        pc->pc_refcnt = 0;
        pc->pc_freecheck = NULL;
@@ -2164,6 +2165,19 @@
 }
 
 /*
+ * pool_cache_setpredestruct:
+ *
+ *     Set a pre-destructor hook for the specified pool cache.
+ */
+void
+pool_cache_setpredestruct(pool_cache_t pc, void (*fn)(void *))
+{
+       KASSERT(pc->pc_pre_dtor == NULL);
+       pc->pc_pre_dtor = fn;
+       membar_sync();
+}
+
+/*
  * pool_cache_destroy:
  *
  *     Destroy a pool cache.
@@ -2290,6 +2304,20 @@
        return pool_reclaim(&pc->pc_pool);
 }
 
+static inline void
+pool_cache_pre_destruct(pool_cache_t pc)
+{
+       /*
+        * Call the pre-destruct hook before destructing a batch
+        * of objects.  Users of this hook can perform passive
+        * serialization other other activities that need to be
+        * performed once-per-batch (rather than once-per-object).
+        */
+       if (__predict_false(pc->pc_pre_dtor != NULL)) {
+               (*pc->pc_pre_dtor)(pc->pc_arg);
+       }
+}
+
 static void
 pool_cache_destruct_object1(pool_cache_t pc, void *object)
 {
@@ -2309,6 +2337,7 @@
 
        FREECHECK_IN(&pc->pc_freecheck, object);
 
+       pool_cache_pre_destruct(pc);
        pool_cache_destruct_object1(pc, object);
 }
 
@@ -2325,6 +2354,12 @@
        pcg_t *next;
        int i, n;
 
+       if (pcg == NULL) {
+               return 0;
+       }
+
+       pool_cache_pre_destruct(pc);
+
        for (n = 0; pcg != NULL; pcg = next, n++) {
                next = pcg->pcg_next;
 
diff -r 413c96ed0aaf -r 8282ce8b184f sys/sys/param.h
--- a/sys/sys/param.h   Tue Dec 21 16:50:11 2021 +0000
+++ b/sys/sys/param.h   Tue Dec 21 18:59:22 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: param.h,v 1.705 2021/10/20 03:09:45 thorpej Exp $      */
+/*     $NetBSD: param.h,v 1.706 2021/12/21 18:59:22 thorpej Exp $      */
 
 /*-
  * Copyright (c) 1982, 1986, 1989, 1993
@@ -67,7 +67,7 @@
  *     2.99.9          (299000900)
  */
 
-#define        __NetBSD_Version__      999009200       /* NetBSD 9.99.92 */
+#define        __NetBSD_Version__      999009300       /* NetBSD 9.99.93 */
 
 #define __NetBSD_Prereq__(M,m,p) (((((M) * 100000000) + \
     (m) * 1000000) + (p) * 100) <= __NetBSD_Version__)
diff -r 413c96ed0aaf -r 8282ce8b184f sys/sys/pool.h
--- a/sys/sys/pool.h    Tue Dec 21 16:50:11 2021 +0000
+++ b/sys/sys/pool.h    Tue Dec 21 18:59:22 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pool.h,v 1.94 2021/07/25 06:00:31 simonb Exp $ */
+/*     $NetBSD: pool.h,v 1.95 2021/12/21 18:59:22 thorpej Exp $        */
 
 /*-
  * Copyright (c) 1997, 1998, 1999, 2000, 2007, 2020
@@ -265,7 +265,8 @@
        int             pc_ncpu;        /* number cpus set up */
        int             (*pc_ctor)(void *, void *, int);
        void            (*pc_dtor)(void *, void *);
-       void            *pc_arg;        /* for ctor/ctor */
+       void            (*pc_pre_dtor)(void *);
+       void            *pc_arg;        /* for ctor/dtor/pre_dtor */
        unsigned int    pc_refcnt;      /* ref count for pagedaemon, etc */
        void            *pc_cpus[MAXCPUS];
 
@@ -341,6 +342,7 @@
                    const char *, struct pool_allocator *, int,
                    int (*)(void *, void *, int), void (*)(void *, void *),
                    void *);
+void           pool_cache_setpredestruct(pool_cache_t, void (*)(void *));
 void           pool_cache_destroy(pool_cache_t);
 void           pool_cache_bootstrap_destroy(pool_cache_t);
 void           *pool_cache_get_paddr(pool_cache_t, int, paddr_t *);



Home | Main Index | Thread Index | Old Index