1 KMEM_ALLOC(9F)           Kernel Functions for Drivers           KMEM_ALLOC(9F)
   2 
   3 
   4 
   5 NAME
   6        kmem_alloc, kmem_zalloc, kmem_free - allocate kernel memory
   7 
   8 SYNOPSIS
   9        #include <sys/types.h>
  10        #include <sys/kmem.h>
  11 
  12 
  13 
  14        void *kmem_alloc(size_t size, int flag);
  15 
  16 
  17        void *kmem_zalloc(size_t size, int flag);
  18 
  19 
  20        void kmem_free(void *buf, size_t size);
  21 
  22 
  23 INTERFACE LEVEL
  24        Architecture independent level 1 (DDI/DKI).
  25 
  26 PARAMETERS
  27        size
  28                Number of bytes to allocate.
  29 
  30 
  31        flag
  32                Determines whether caller can sleep for memory. Possible flags
  33                are KM_SLEEP to allow sleeping until memory is available, or
  34                KM_NOSLEEP to return NULL immediately if memory is not
  35                available.
  36 
  37 
  38        buf
  39                Pointer to allocated memory.
  40 
  41 
  42 DESCRIPTION
  43        The kmem_alloc() function allocates size bytes of kernel memory and
  44        returns a pointer to the allocated memory. The allocated memory is at
  45        least double-word aligned, so it can hold any C data structure. No
  46        greater alignment can be assumed. flag determines whether the caller
  47        can sleep for memory.  KM_SLEEP allocations may sleep but are
  48        guaranteed to succeed.  KM_NOSLEEP allocations are guaranteed not to
  49        sleep but may fail (return NULL) if no memory is currently available.
  50        The initial contents of memory allocated using kmem_alloc() are random
  51        garbage.
  52 
  53 
  54        The kmem_zalloc() function is like kmem_alloc() but returns zero-filled
  55        memory.
  56 
  57 
  58        The kmem_free() function frees previously allocated kernel memory. The
  59        buffer address and size must exactly match the original allocation.
  60        Memory cannot be returned piecemeal.
  61 
  62 RETURN VALUES
  63        If successful, kmem_alloc() and kmem_zalloc() return a pointer to the
  64        allocated memory. If KM_NOSLEEP is set and memory cannot be allocated
  65        without sleeping, kmem_alloc() and kmem_zalloc() return NULL.
  66 
  67 CONTEXT
  68        The kmem_alloc() and kmem_zalloc() functions can be called from
  69        interrupt context only if the KM_NOSLEEP flag is set. They can be
  70        called from user context with any valid flag. The kmem_free() function
  71        can be called from from user, interrupt, or kernel context.
  72 
  73 SEE ALSO
  74        copyout(9F), freerbuf(9F), getrbuf(9F)
  75 
  76 
  77        Writing Device Drivers
  78 
  79 WARNINGS
  80        Memory allocated using kmem_alloc() is not paged. Available memory is
  81        therefore limited by the total physical memory on the system. It is
  82        also limited by the available kernel virtual address space, which is
  83        often the more restrictive constraint on large-memory configurations.
  84 
  85 
  86        Excessive use of kernel memory is likely to affect overall system
  87        performance.  Overcommitment of kernel memory will cause the system to
  88        hang or panic.
  89 
  90 
  91        Misuse of the kernel memory allocator, such as writing past the end of
  92        a buffer, using a buffer after freeing it, freeing a buffer twice, or
  93        freeing a null or invalid pointer, will corrupt the kernel heap and may
  94        cause the system to corrupt data or panic.
  95 
  96 
  97        The initial contents of memory allocated using kmem_alloc() are random
  98        garbage. This random garbage may include secure kernel data. Therefore,
  99        uninitialized kernel memory should be handled carefully. For example,
 100        never copyout(9F) a potentially uninitialized buffer.
 101 
 102 NOTES
 103        kmem_alloc(0, flag) always returns NULL, but if KM_SLEEP is set, this
 104        behavior is considered to be deprecated; the system may be configured
 105        to explicitly panic in this case in lieu of returning NULL.
 106        kmem_free(NULL, 0) is legal, however.
 107 
 108 
 109 
 110                                November 20, 2019                KMEM_ALLOC(9F)