Home | History | Annotate | Line # | Download | only in sys
      1 /*	$NetBSD: pool.h,v 1.97 2026/08/23 17:49:11 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997, 1998, 1999, 2000, 2007, 2020
      5  *     The NetBSD Foundation, Inc.
      6  * All rights reserved.
      7  *
      8  * This code is derived from software contributed to The NetBSD Foundation
      9  * by Paul Kranenburg; by Jason R. Thorpe of the Numerical Aerospace
     10  * Simulation Facility, NASA Ames Research Center.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31  * POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 #ifndef _SYS_POOL_H_
     35 #define _SYS_POOL_H_
     36 
     37 #include <sys/stdbool.h>
     38 #include <sys/stdint.h>
     39 
     40 struct pool_sysctl {
     41 	char pr_wchan[16];
     42 	uint64_t pr_flags;
     43 	uint64_t pr_size;
     44 	uint64_t pr_pagesize;
     45 	uint64_t pr_itemsperpage;
     46 	uint64_t pr_nitems;
     47 	uint64_t pr_nout;
     48 	uint64_t pr_hardlimit;
     49 	uint64_t pr_npages;
     50 	uint64_t pr_minpages;
     51 	uint64_t pr_maxpages;
     52 
     53 	uint64_t pr_nget;
     54 	uint64_t pr_nfail;
     55 	uint64_t pr_nput;
     56 	uint64_t pr_npagealloc;
     57 	uint64_t pr_npagefree;
     58 	uint64_t pr_hiwat;
     59 	uint64_t pr_nidle;
     60 
     61 	uint64_t pr_cache_meta_size;
     62 	uint64_t pr_cache_nfull;
     63 	uint64_t pr_cache_npartial;
     64 	uint64_t pr_cache_nempty;
     65 	uint64_t pr_cache_ncontended;
     66 	uint64_t pr_cache_nmiss_global;
     67 	uint64_t pr_cache_nhit_global;
     68 	uint64_t pr_cache_nmiss_pcpu;
     69 	uint64_t pr_cache_nhit_pcpu;
     70 };
     71 
     72 #ifdef _KERNEL
     73 #define __POOL_EXPOSE
     74 #endif
     75 
     76 #ifdef __POOL_EXPOSE
     77 #include <sys/param.h>
     78 #include <sys/mutex.h>
     79 #include <sys/condvar.h>
     80 #include <sys/queue.h>
     81 #include <sys/time.h>
     82 #include <sys/tree.h>
     83 #include <sys/callback.h>
     84 
     85 #ifdef _KERNEL_OPT
     86 #include "opt_pool.h"
     87 #endif
     88 
     89 #define POOL_PADDR_INVALID	((paddr_t) -1)
     90 
     91 struct pool;
     92 
     93 struct pool_allocator {
     94 	void		*(*pa_alloc)(struct pool *, int);
     95 	void		(*pa_free)(struct pool *, void *);
     96 	unsigned int	pa_pagesz;
     97 
     98 	/* The following fields are for internal use only. */
     99 	kmutex_t	pa_lock;
    100 	TAILQ_HEAD(, pool) pa_list;	/* list of pools using this allocator */
    101 	uint32_t	pa_refcnt;	/* number of pools using this allocator */
    102 	int		pa_pagemask;
    103 	int		pa_pageshift;
    104 };
    105 
    106 LIST_HEAD(pool_pagelist,pool_item_header);
    107 SPLAY_HEAD(phtree, pool_item_header);
    108 
    109 #define POOL_QUARANTINE_DEPTH	128
    110 typedef struct {
    111 	size_t rotor;
    112 	intptr_t list[POOL_QUARANTINE_DEPTH];
    113 } pool_quar_t;
    114 
    115 struct pool {
    116 	TAILQ_ENTRY(pool)
    117 			pr_poollist;
    118 	struct pool_pagelist
    119 			pr_emptypages;	/* Empty pages */
    120 	struct pool_pagelist
    121 			pr_fullpages;	/* Full pages */
    122 	struct pool_pagelist
    123 			pr_partpages;	/* Partially-allocated pages */
    124 	struct pool_item_header	*pr_curpage;
    125 	struct pool	*pr_phpool;	/* Pool item header pool */
    126 	struct pool_cache *pr_cache;	/* Cache for this pool */
    127 	unsigned int	pr_size;	/* Size of item */
    128 	unsigned int	pr_align;	/* Requested alignment, must be 2^n */
    129 	unsigned int	pr_itemoffset;	/* offset of the item space */
    130 	unsigned int	pr_minitems;	/* minimum # of free items to keep */
    131 	unsigned int	pr_maxitems;	/* maximum # of free items to keep */
    132 	unsigned int	pr_minpages;	/* minimum # of pages to keep */
    133 	unsigned int	pr_maxpages;	/* maximum # of pages to keep */
    134 	unsigned int	pr_npages;	/* # of pages allocated */
    135 	unsigned int	pr_itemsperpage;/* # items that fit in a page */
    136 	unsigned int	pr_poolid;	/* id of the pool */
    137 	unsigned int	pr_nitems;	/* number of free items in pool */
    138 	unsigned int	pr_nout;	/* # items currently allocated */
    139 	unsigned int	pr_hardlimit;	/* hard limit to number of allocated
    140 					   items */
    141 	unsigned int	pr_refcnt;	/* ref count for pagedaemon, etc */
    142 	struct pool_allocator *pr_alloc;/* back-end allocator */
    143 	TAILQ_ENTRY(pool) pr_alloc_list;/* link on allocator's pool list */
    144 
    145 	/* Drain hook. */
    146 	void		(*pr_drain_hook)(void *, int);
    147 	void		*pr_drain_hook_arg;
    148 
    149 	const char	*pr_wchan;	/* tsleep(9) identifier */
    150 	unsigned int	pr_flags;	/* r/w flags */
    151 	unsigned int	pr_roflags;	/* r/o flags */
    152 #define PR_WAITOK	0x01	/* Note: matches KM_SLEEP */
    153 #define PR_NOWAIT	0x02	/* Note: matches KM_NOSLEEP */
    154 #define PR_WANTED	0x04	/* waiting for free objects */
    155 #define PR_PHINPAGE	0x40	/* page header in page */
    156 #define PR_LIMITFAIL	0x100	/* even if waiting, fail if we hit limit */
    157 #define PR_RECURSIVE	0x200	/* pool contains pools, for vmstat(8) */
    158 #define PR_NOTOUCH	0x400	/* don't use free items to keep internal state*/
    159 #define PR_NOALIGN	0x800	/* don't assume backend alignment */
    160 #define PR_LARGECACHE	0x1000	/* use large cache groups */
    161 #define PR_GROWING	0x2000	/* pool_grow in progress */
    162 #define PR_ZERO		0x8000	/* zero data before returning */
    163 #define PR_USEBMAP	0x10000	/* use a bitmap to manage freed items */
    164 #define PR_PSERIALIZE	0x20000	/* needs pserialize sync point before free */
    165 
    166 	/*
    167 	 * `pr_lock' protects the pool's data structures when removing
    168 	 * items from or returning items to the pool, or when reading
    169 	 * or updating read/write fields in the pool descriptor.
    170 	 *
    171 	 * We assume back-end page allocators provide their own locking
    172 	 * scheme.  They will be called with the pool descriptor _unlocked_,
    173 	 * since the page allocators may block.
    174 	 */
    175 	kmutex_t	pr_lock;
    176 	kcondvar_t	pr_cv;
    177 	int		pr_ipl;
    178 
    179 	struct phtree	pr_phtree;
    180 
    181 	int		pr_maxcolor;	/* Cache colouring */
    182 	int		pr_curcolor;
    183 	int		pr_phoffset;	/* unused */
    184 
    185 	/*
    186 	 * Warning message to be issued, and a per-time-delta rate cap,
    187 	 * if the hard limit is reached.
    188 	 */
    189 	const char	*pr_hardlimit_warning;
    190 	struct timeval	pr_hardlimit_ratecap;
    191 	struct timeval	pr_hardlimit_warning_last;
    192 
    193 	/*
    194 	 * Instrumentation
    195 	 */
    196 	unsigned long	pr_nget;	/* # of successful requests */
    197 	unsigned long	pr_nfail;	/* # of unsuccessful requests */
    198 	unsigned long	pr_nput;	/* # of releases */
    199 	unsigned long	pr_npagealloc;	/* # of pages allocated */
    200 	unsigned long	pr_npagefree;	/* # of pages released */
    201 	unsigned int	pr_hiwat;	/* max # of pages in pool */
    202 	unsigned long	pr_nidle;	/* # of idle pages */
    203 
    204 	/*
    205 	 * Diagnostic aides.
    206 	 */
    207 	void		*pr_freecheck;
    208 	void		*pr_qcache;
    209 	bool		pr_redzone;
    210 	size_t		pr_reqsize;
    211 	size_t		pr_reqsize_with_redzone;
    212 #ifdef POOL_QUARANTINE
    213 	pool_quar_t	pr_quar;
    214 #endif
    215 };
    216 
    217 /*
    218  * Cache group sizes, assuming 4-byte paddr_t on !_LP64.
    219  * All groups will be aligned to COHERENCY_UNIT.
    220  */
    221 #ifdef _LP64
    222 #define PCG_NOBJECTS_NORMAL	15	/* 256 byte group */
    223 #define PCG_NOBJECTS_LARGE	63	/* 1024 byte group */
    224 #else
    225 #define PCG_NOBJECTS_NORMAL	14	/* 124 byte group */
    226 #define PCG_NOBJECTS_LARGE	62	/* 508 byte group */
    227 #endif
    228 
    229 typedef struct pcgpair {
    230 	void	*pcgo_va;		/* object virtual address */
    231 	paddr_t	pcgo_pa;		/* object physical address */
    232 } pcgpair_t;
    233 
    234 /* The pool cache group. */
    235 typedef struct pool_cache_group {
    236 	struct pool_cache_group	*pcg_next;	/* link to next group */
    237 	u_int			pcg_avail;	/* # available objects */
    238 	u_int			pcg_size;	/* max number objects */
    239 	pcgpair_t 		pcg_objects[1];	/* the objects */
    240 } pcg_t;
    241 
    242 /* Pool cache CPU.  Sized to 64 bytes on _LP64. */
    243 typedef struct pool_cache_cpu {
    244 	struct pool_cache_group	*cc_current;
    245 	struct pool_cache_group	*cc_previous;
    246 	pcg_t *volatile 	*cc_pcgcache;
    247 	uint64_t		cc_misses;
    248 	uint64_t		cc_hits;
    249 	uint64_t		cc_pcmisses;
    250 	uint64_t		cc_contended;
    251 	uint32_t		cc_nfull;
    252 	uint32_t		cc_npart;
    253 } pool_cache_cpu_t;
    254 
    255 struct pool_cache {
    256 	/* Pool layer. */
    257 	struct pool	pc_pool;
    258 
    259 	/* Cache layer. */
    260 	TAILQ_ENTRY(pool_cache)
    261 			pc_cachelist;	/* entry on global cache list */
    262 	struct pool	*pc_pcgpool;	/* Pool of cache groups */
    263 	pcg_t *volatile *pc_pcgcache;	/* list of empty cache groups */
    264 	int		pc_pcgsize;	/* Use large cache groups? */
    265 	int		pc_ncpu;	/* number cpus set up */
    266 	int		(*pc_ctor)(void *, void *, int);
    267 	void		(*pc_dtor)(void *, void *);
    268 	void		*pc_arg;	/* for ctor/dtor */
    269 	unsigned int	pc_refcnt;	/* ref count for pagedaemon, etc */
    270 	unsigned int	pc_roflags;	/* r/o cache flags */
    271 	void		*pc_cpus[MAXCPUS];
    272 
    273 	/* Diagnostic aides. */
    274 	void		*pc_freecheck;
    275 	bool		pc_redzone;
    276 	size_t		pc_reqsize;
    277 
    278 	/* Hot items. */
    279 	pcg_t *volatile pc_fullgroups	/* list of full cache groups */
    280 	    __aligned(CACHE_LINE_SIZE);
    281 	pcg_t *volatile pc_partgroups;	/* groups for reclamation */
    282 
    283 	/* Boot cpu. */
    284 	pool_cache_cpu_t pc_cpu0 __aligned(CACHE_LINE_SIZE);
    285 };
    286 
    287 #endif /* __POOL_EXPOSE */
    288 
    289 typedef struct pool_cache *pool_cache_t;
    290 
    291 #ifdef _KERNEL
    292 /*
    293  * pool_allocator_kmem is the default that all pools get unless
    294  * otherwise specified.  pool_allocator_nointr is provided for
    295  * pools that know they will never be accessed in interrupt
    296  * context.
    297  */
    298 extern struct pool_allocator pool_allocator_kmem;
    299 extern struct pool_allocator pool_allocator_nointr;
    300 extern struct pool_allocator pool_allocator_meta;
    301 
    302 void		pool_subsystem_init(void);
    303 
    304 void		pool_init(struct pool *, size_t, u_int, u_int,
    305 		    int, const char *, struct pool_allocator *, int);
    306 void		pool_destroy(struct pool *);
    307 
    308 void		pool_set_drain_hook(struct pool *,
    309 		    void (*)(void *, int), void *);
    310 
    311 void		*pool_get(struct pool *, int);
    312 void		pool_put(struct pool *, void *);
    313 int		pool_reclaim(struct pool *);
    314 
    315 void		pool_prime(struct pool *, int);
    316 void		pool_setlowat(struct pool *, int);
    317 void		pool_sethiwat(struct pool *, int);
    318 void		pool_sethardlimit(struct pool *, int, const char *, int);
    319 bool		pool_drain(struct pool **);
    320 int		pool_totalpages(void);
    321 int		pool_totalpages_locked(void);
    322 
    323 unsigned int	pool_nget(struct pool *);
    324 unsigned int	pool_nput(struct pool *);
    325 
    326 /*
    327  * Debugging and diagnostic aides.
    328  */
    329 void		pool_printit(struct pool *, const char *,
    330     void (*)(const char *, ...) __printflike(1, 2));
    331 void		pool_printall(const char *, void (*)(const char *, ...)
    332     __printflike(1, 2));
    333 int		pool_chk(struct pool *, const char *);
    334 
    335 /*
    336  * Pool cache routines.
    337  */
    338 pool_cache_t	pool_cache_init(size_t, u_int, u_int, u_int, const char *,
    339 		    struct pool_allocator *, int, int (*)(void *, void *, int),
    340 		    void (*)(void *, void *), void *);
    341 void		pool_cache_bootstrap(pool_cache_t, size_t, u_int, u_int, u_int,
    342 		    const char *, struct pool_allocator *, int,
    343 		    int (*)(void *, void *, int), void (*)(void *, void *),
    344 		    void *);
    345 void		pool_cache_destroy(pool_cache_t);
    346 void		pool_cache_bootstrap_destroy(pool_cache_t);
    347 void		*pool_cache_get_paddr(pool_cache_t, int, paddr_t *);
    348 void		pool_cache_put_paddr(pool_cache_t, void *, paddr_t);
    349 void		pool_cache_destruct_object(pool_cache_t, void *);
    350 void		pool_cache_invalidate(pool_cache_t);
    351 bool		pool_cache_reclaim(pool_cache_t);
    352 void		pool_cache_set_drain_hook(pool_cache_t,
    353 		    void (*)(void *, int), void *);
    354 void		pool_cache_setlowat(pool_cache_t, int);
    355 void		pool_cache_sethiwat(pool_cache_t, int);
    356 void		pool_cache_sethardlimit(pool_cache_t, int, const char *, int);
    357 void		pool_cache_prime(pool_cache_t, int);
    358 void		pool_cache_cpu_init(struct cpu_info *);
    359 
    360 unsigned int	pool_cache_nget(pool_cache_t);
    361 unsigned int	pool_cache_nput(pool_cache_t);
    362 
    363 #define		pool_cache_get(pc, f) pool_cache_get_paddr((pc), (f), NULL)
    364 #define		pool_cache_put(pc, o) pool_cache_put_paddr((pc), (o), \
    365 				          POOL_PADDR_INVALID)
    366 
    367 void		pool_whatis(uintptr_t, void (*)(const char *, ...)
    368     __printflike(1, 2));
    369 #endif /* _KERNEL */
    370 
    371 #endif /* _SYS_POOL_H_ */
    372