1.\" $NetBSD: kmem.9,v 1.28 2021/03/06 14:44:02 rin Exp $ 2.\" 3.\" Copyright (c)2006 YAMAMOTO Takashi, 4.\" All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 15.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25.\" SUCH DAMAGE. 26.\" 27.\" ------------------------------------------------------------ 28.Dd January 24, 2021 29.Dt KMEM 9 30.Os 31.\" ------------------------------------------------------------ 32.Sh NAME 33.Nm kmem 34.Nd kernel wired memory allocator 35.\" ------------------------------------------------------------ 36.Sh SYNOPSIS 37.In sys/kmem.h 38.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 39.Ft void * 40.Fn kmem_alloc \ 41"size_t size" "km_flag_t kmflags" 42.Ft void * 43.Fn kmem_zalloc \ 44"size_t size" "km_flag_t kmflags" 45.Ft void 46.Fn kmem_free \ 47"void *p" "size_t size" 48.\" --- 49.Ft void * 50.Fn kmem_intr_alloc \ 51"size_t size" "km_flag_t kmflags" 52.Ft void * 53.Fn kmem_intr_zalloc \ 54"size_t size" "km_flag_t kmflags" 55.Ft void 56.Fn kmem_intr_free \ 57"void *p" "size_t size" 58.\" --- 59.Ft char * 60.Fn kmem_asprintf \ 61"const char *fmt" "..." 62.\" --- 63.Ft char * 64.Fn kmem_strdupsize \ 65"const char *str" "size_t *size" "km_flag_t kmflags" 66.Ft char * 67.Fn kmem_strdup \ 68"const char *str" "km_flag_t kmflags" 69.Ft char * 70.Fn kmem_strndup \ 71"const char *str" "size_t manxlen" "km_flag_t kmflags" 72.Ft void 73.Fn kmem_strfree \ 74"char *str" 75.\" ------------------------------------------------------------ 76.Ft void * 77.Fn kmem_tmpbuf_alloc \ 78"size_t size" "void *stackbuf" "size_t stackbufsize" "km_flag_t kmflags" 79.Ft void 80.Fn kmem_tmpbuf_free \ 81"void *p" "size_t size" "void *stackbuf" 82.\" ------------------------------------------------------------ 83.Pp 84.Cd "options KMEM_SIZE" 85.Sh DESCRIPTION 86.Fn kmem_alloc 87allocates kernel wired memory. 88It takes the following arguments. 89.Bl -tag -width kmflags 90.It Fa size 91Specify the size of allocation in bytes. 92.It Fa kmflags 93Either of the following: 94.Bl -tag -width KM_NOSLEEP 95.It Dv KM_SLEEP 96If the allocation cannot be satisfied immediately, sleep until enough 97memory is available. 98If 99.Dv KM_SLEEP 100is specified, then the allocation cannot fail. 101.It Dv KM_NOSLEEP 102Don't sleep. 103Immediately return 104.Dv NULL 105if there is not enough memory available. 106It should only be used when failure to allocate will not have harmful, 107user-visible effects. 108.Pp 109.Bf -symbolic 110Use of 111.Dv KM_NOSLEEP 112is strongly discouraged as it can create transient, hard to debug failures 113that occur when the system is under memory pressure. 114.Ef 115.Pp 116In situations where it is not possible to sleep, for example because locks 117are held by the caller, the code path should be restructured to allow the 118allocation to be made in another place. 119.El 120.El 121.Pp 122The contents of allocated memory are uninitialized. 123.Pp 124Unlike Solaris, kmem_alloc(0, flags) is illegal. 125.Pp 126.\" ------------------------------------------------------------ 127.Fn kmem_zalloc 128is the equivalent of 129.Fn kmem_alloc , 130except that it initializes the memory to zero. 131.Pp 132.\" ------------------------------------------------------------ 133.Fn kmem_asprintf 134functions as the well known 135.Fn asprintf 136function, but allocates memory using 137.Fn kmem_alloc . 138This routine can sleep during allocation. 139The size of the allocated area is the length of the returned character string, plus one (for the NUL terminator). 140This must be taken into consideration when freeing the returned area with 141.Fn kmem_free . 142.Pp 143.\" ------------------------------------------------------------ 144.Fn kmem_free 145frees kernel wired memory allocated by 146.Fn kmem_alloc 147or 148.Fn kmem_zalloc 149so that it can be used for other purposes. 150It takes the following arguments. 151.Bl -tag -width kmflags 152.It Fa p 153The pointer to the memory being freed. 154It must be the one returned by 155.Fn kmem_alloc 156or 157.Fn kmem_zalloc . 158.It Fa size 159The size of the memory being freed, in bytes. 160It must be the same as the 161.Fa size 162argument used for 163.Fn kmem_alloc 164or 165.Fn kmem_zalloc 166when the memory was allocated. 167.El 168.Pp 169Freeing 170.Dv NULL 171is illegal. 172.Pp 173.\" ------------------------------------------------------------ 174.Fn kmem_intr_alloc , 175.Fn kmem_intr_zalloc 176and 177.Fn kmem_intr_free 178are the equivalents of the above kmem routines which can be called 179from the interrupt context. 180These routines are for the special cases. 181Normally, 182.Xr pool_cache 9 183should be used for memory allocation from interrupt context. 184.Pp 185The 186.Fn kmem_strdupsize 187function is a utility function that can be used to copy the string in the 188.Fa str 189argument to a new buffer allocated using 190.Fn kmem_alloc 191and optionally return the size of the allocation (the length of the string 192plus the trailing 193.Dv NUL ) 194in the 195.Fa size 196argument if that is not 197.Dv NULL . 198.Pp 199The 200.Fn kmem_strdup 201function is a simplified version of 202.Fn kmem_strdupsize 203that does not return the size of the allocation. 204.Pp 205The 206.Fn kmem_strndup 207function is variation of 208.Fn kmem_strdup 209that copies at most 210.Fa maxlen 211characters from the string 212.Fa str 213always NUL terminating the copied string. 214.Pp 215The 216.Fn kmem_strfree 217function can be used to free a 218.Dv NUL 219terminated string computing the length of the string using 220.Xr strlen 3 221and adding one for the 222.Dv NUL 223and then using 224.Fn kmem_free . 225.Pp 226The 227.Fn kmem_tmpbuf_alloc 228function is a utility function for allocating memory for temporary 229use, where allocation on the stack is desirable, but only up to a 230certain size. 231If the requested size fits within the specified stack buffer, the 232stack buffer is returned. 233Otherwise, memory is allocated with 234.Fn kmem_alloc . 235The 236.Fn kmem_tmpbuf_free 237function compares the result of a previous call to 238.Fn kmem_tmpbuf_alloc 239and frees the memory using 240.Fn kmem_free 241if it is not the specified stack buffer. 242.\" ------------------------------------------------------------ 243.Sh NOTES 244Making 245.Dv KM_SLEEP 246allocations while holding mutexes or reader/writer locks is discouraged, as the 247caller can sleep for an unbounded amount of time in order to satisfy the 248allocation. 249This can in turn block other threads that wish to acquire locks held by the 250caller. 251It should be noted that 252.Fn kmem_free 253may also block. 254.Pp 255For some locks this is permissible or even unavoidable. 256For others, particularly locks that may be taken from soft interrupt context, 257it is a serious problem. 258As a general rule it is better not to allow this type of situation to develop. 259One way to circumvent the problem is to make allocations speculative and part 260of a retryable sequence. 261For example: 262.Bd -literal 263 retry: 264 /* speculative unlocked check */ 265 if (need to allocate) { 266 new_item = kmem_alloc(sizeof(*new_item), KM_SLEEP); 267 } else { 268 new_item = NULL; 269 } 270 mutex_enter(lock); 271 /* check while holding lock for true status */ 272 if (need to allocate) { 273 if (new_item == NULL) { 274 mutex_exit(lock); 275 goto retry; 276 } 277 consume(new_item); 278 new_item = NULL; 279 } 280 mutex_exit(lock); 281 if (new_item != NULL) { 282 /* did not use it after all */ 283 kmem_free(new_item, sizeof(*new_item)); 284 } 285.Ed 286.\" ------------------------------------------------------------ 287.Sh OPTIONS 288.Ss KMEM_SIZE 289Kernels compiled with the 290.Dv KMEM_SIZE 291option ensure the size given in 292.Fn kmem_free 293matches the actual allocated size. 294On 295.Fn kmem_alloc , 296the kernel will allocate an additional contiguous kmem page of eight 297bytes in the buffer, will register the allocated size in the first kmem 298page of that buffer, and will return a pointer to the second kmem page 299in that same buffer. 300When freeing, the kernel reads the first page, and compares the 301size registered with the one given in 302.Fn kmem_free . 303Any mismatch triggers a panic. 304.Pp 305.Dv KMEM_SIZE 306is enabled by default on 307.Dv DIAGNOSTIC . 308.Sh RETURN VALUES 309On success, 310.Fn kmem_alloc , 311.Fn kmem_asprintf , 312.Fn kmem_intr_alloc , 313.Fn kmem_intr_zalloc , 314.Fn kmem_strdupsize , 315and 316.Fn kmem_zalloc 317return a pointer to allocated memory. 318Otherwise, 319.Dv NULL 320is returned. 321.\" ------------------------------------------------------------ 322.Sh CODE REFERENCES 323The 324.Nm 325subsystem is implemented within the file 326.Pa sys/kern/subr_kmem.c . 327.\" ------------------------------------------------------------ 328.Sh SEE ALSO 329.Xr intro 9 , 330.Xr memoryallocators 9 , 331.Xr percpu 9 , 332.Xr pool_cache 9 , 333.Xr uvm_km 9 334.\" ------------------------------------------------------------ 335.Sh CAVEATS 336The 337.Fn kmem_alloc , 338.Fn kmem_asprintf , 339.Fn kmem_free , 340.Fn kmem_strdupsize , 341.Fn kmem_strfree , 342and 343.Fn kmem_zalloc 344functions cannot be used from interrupt context, from a soft interrupt, 345or from a callout. 346Use 347.Xr pool_cache 9 348in these situations. 349.\" ------------------------------------------------------------ 350.Sh SECURITY CONSIDERATIONS 351As the memory allocated by 352.Fn kmem_alloc 353is uninitialized, it can contain security-sensitive data left by its 354previous user. 355It is the caller's responsibility not to expose it to the world. 356