Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

mm/slab: document kfree() as allowed for kmem_cache_alloc() objects

This will make it easier to free objects in situations when they can
come from either kmalloc() or kmem_cache_alloc(), and also allow
kfree_rcu() for freeing objects from kmem_cache_alloc().

For the SLAB and SLUB allocators this was always possible so with SLOB
gone, we can document it as supported.

Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
Reviewed-by: Mike Rapoport (IBM) <rppt@kernel.org>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: "Paul E. McKenney" <paulmck@kernel.org>
Cc: Frederic Weisbecker <frederic@kernel.org>
Cc: Neeraj Upadhyay <quic_neeraju@quicinc.com>
Cc: Josh Triplett <josh@joshtriplett.org>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: Lai Jiangshan <jiangshanlai@gmail.com>
Cc: Joel Fernandes <joel@joelfernandes.org>

+18 -10
+13 -4
Documentation/core-api/memory-allocation.rst
··· 170 170 After the cache is created kmem_cache_alloc() and its convenience 171 171 wrappers can allocate memory from that cache. 172 172 173 - When the allocated memory is no longer needed it must be freed. You can 174 - use kvfree() for the memory allocated with `kmalloc`, `vmalloc` and 175 - `kvmalloc`. The slab caches should be freed with kmem_cache_free(). And 176 - don't forget to destroy the cache with kmem_cache_destroy(). 173 + When the allocated memory is no longer needed it must be freed. 174 + 175 + Objects allocated by `kmalloc` can be freed by `kfree` or `kvfree`. Objects 176 + allocated by `kmem_cache_alloc` can be freed with `kmem_cache_free`, `kfree` 177 + or `kvfree`, where the latter two might be more convenient thanks to not 178 + needing the kmem_cache pointer. 179 + 180 + The same rules apply to _bulk and _rcu flavors of freeing functions. 181 + 182 + Memory allocated by `vmalloc` can be freed with `vfree` or `kvfree`. 183 + Memory allocated by `kvmalloc` can be freed with `kvfree`. 184 + Caches created by `kmem_cache_create` should be freed with 185 + `kmem_cache_destroy` only after freeing all the allocated objects first.
+4 -2
include/linux/rcupdate.h
··· 976 976 * either fall back to use of call_rcu() or rearrange the structure to 977 977 * position the rcu_head structure into the first 4096 bytes. 978 978 * 979 - * Note that the allowable offset might decrease in the future, for example, 980 - * to allow something like kmem_cache_free_rcu(). 979 + * The object to be freed can be allocated either by kmalloc() or 980 + * kmem_cache_alloc(). 981 + * 982 + * Note that the allowable offset might decrease in the future. 981 983 * 982 984 * The BUILD_BUG_ON check must not involve any function calls, hence the 983 985 * checks are done in macros here.
+1 -4
mm/slab_common.c
··· 989 989 990 990 /** 991 991 * kfree - free previously allocated memory 992 - * @object: pointer returned by kmalloc. 992 + * @object: pointer returned by kmalloc() or kmem_cache_alloc() 993 993 * 994 994 * If @object is NULL, no operation is performed. 995 - * 996 - * Don't free memory not originally allocated by kmalloc() 997 - * or you will run into trouble. 998 995 */ 999 996 void kfree(const void *object) 1000 997 {