Skip to content

Commit 4b7ff9a

Browse files
committed
mm, slab: restore kerneldoc for kmem_cache_create()
As kmem_cache_create() became a _Generic() wrapper macro, it currently has no kerneldoc despite being the main API to use. Add it. Also adjust kmem_cache_create_usercopy() kerneldoc to indicate it is now a legacy wrapper. Also expand the kerneldoc for struct kmem_cache_args, especially for the freeptr_offset field, where important details were removed with the removal of kmem_cache_create_rcu(). Signed-off-by: Vlastimil Babka <[email protected]> Reviewed-by: Christian Brauner <[email protected]>
1 parent a6711d1 commit 4b7ff9a

File tree

2 files changed

+98
-26
lines changed

2 files changed

+98
-26
lines changed

include/linux/slab.h

Lines changed: 91 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -242,19 +242,72 @@ bool slab_is_available(void);
242242

243243
/**
244244
* struct kmem_cache_args - Less common arguments for kmem_cache_create()
245-
* @align: The required alignment for the objects.
246-
* @useroffset: Usercopy region offset
247-
* @usersize: Usercopy region size
248-
* @freeptr_offset: Custom offset for the free pointer in RCU caches
249-
* @use_freeptr_offset: Whether a @freeptr_offset is used
250-
* @ctor: A constructor for the objects.
245+
*
246+
* Any uninitialized fields of the structure are interpreted as unused. The
247+
* exception is @freeptr_offset where %0 is a valid value, so
248+
* @use_freeptr_offset must be also set to %true in order to interpret the field
249+
* as used. For @useroffset %0 is also valid, but only with non-%0
250+
* @usersize.
251+
*
252+
* When %NULL args is passed to kmem_cache_create(), it is equivalent to all
253+
* fields unused.
251254
*/
252255
struct kmem_cache_args {
256+
/**
257+
* @align: The required alignment for the objects.
258+
*
259+
* %0 means no specific alignment is requested.
260+
*/
253261
unsigned int align;
262+
/**
263+
* @useroffset: Usercopy region offset.
264+
*
265+
* %0 is a valid offset, when @usersize is non-%0
266+
*/
254267
unsigned int useroffset;
268+
/**
269+
* @usersize: Usercopy region size.
270+
*
271+
* %0 means no usercopy region is specified.
272+
*/
255273
unsigned int usersize;
274+
/**
275+
* @freeptr_offset: Custom offset for the free pointer
276+
* in &SLAB_TYPESAFE_BY_RCU caches
277+
*
278+
* By default &SLAB_TYPESAFE_BY_RCU caches place the free pointer
279+
* outside of the object. This might cause the object to grow in size.
280+
* Cache creators that have a reason to avoid this can specify a custom
281+
* free pointer offset in their struct where the free pointer will be
282+
* placed.
283+
*
284+
* Note that placing the free pointer inside the object requires the
285+
* caller to ensure that no fields are invalidated that are required to
286+
* guard against object recycling (See &SLAB_TYPESAFE_BY_RCU for
287+
* details).
288+
*
289+
* Using %0 as a value for @freeptr_offset is valid. If @freeptr_offset
290+
* is specified, %use_freeptr_offset must be set %true.
291+
*
292+
* Note that @ctor currently isn't supported with custom free pointers
293+
* as a @ctor requires an external free pointer.
294+
*/
256295
unsigned int freeptr_offset;
296+
/**
297+
* @use_freeptr_offset: Whether a @freeptr_offset is used.
298+
*/
257299
bool use_freeptr_offset;
300+
/**
301+
* @ctor: A constructor for the objects.
302+
*
303+
* The constructor is invoked for each object in a newly allocated slab
304+
* page. It is the cache user's responsibility to free object in the
305+
* same state as after calling the constructor, or deal appropriately
306+
* with any differences between a freshly constructed and a reallocated
307+
* object.
308+
*
309+
* %NULL means no constructor.
310+
*/
258311
void (*ctor)(void *);
259312
};
260313

@@ -275,30 +328,20 @@ __kmem_cache_create(const char *name, unsigned int size, unsigned int align,
275328
}
276329

277330
/**
278-
* kmem_cache_create_usercopy - Create a cache with a region suitable
279-
* for copying to userspace
331+
* kmem_cache_create_usercopy - Create a kmem cache with a region suitable
332+
* for copying to userspace.
280333
* @name: A string which is used in /proc/slabinfo to identify this cache.
281334
* @size: The size of objects to be created in this cache.
282335
* @align: The required alignment for the objects.
283336
* @flags: SLAB flags
284337
* @useroffset: Usercopy region offset
285338
* @usersize: Usercopy region size
286-
* @ctor: A constructor for the objects.
287-
*
288-
* Cannot be called within a interrupt, but can be interrupted.
289-
* The @ctor is run when new pages are allocated by the cache.
290-
*
291-
* The flags are
339+
* @ctor: A constructor for the objects, or %NULL.
292340
*
293-
* %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
294-
* to catch references to uninitialised memory.
295-
*
296-
* %SLAB_RED_ZONE - Insert `Red` zones around the allocated memory to check
297-
* for buffer overruns.
298-
*
299-
* %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
300-
* cacheline. This can be beneficial if you're counting cycles as closely
301-
* as davem.
341+
* This is a legacy wrapper, new code should use either KMEM_CACHE_USERCOPY()
342+
* if whitelisting a single field is sufficient, or kmem_cache_create() with
343+
* the necessary parameters passed via the args parameter (see
344+
* &struct kmem_cache_args)
302345
*
303346
* Return: a pointer to the cache on success, NULL on failure.
304347
*/
@@ -333,6 +376,31 @@ __kmem_cache_default_args(const char *name, unsigned int size,
333376
return __kmem_cache_create_args(name, size, &kmem_default_args, flags);
334377
}
335378

379+
/**
380+
* kmem_cache_create - Create a kmem cache.
381+
* @__name: A string which is used in /proc/slabinfo to identify this cache.
382+
* @__object_size: The size of objects to be created in this cache.
383+
* @__args: Optional arguments, see &struct kmem_cache_args. Passing %NULL
384+
* means defaults will be used for all the arguments.
385+
*
386+
* This is currently implemented as a macro using ``_Generic()`` to call
387+
* either the new variant of the function, or a legacy one.
388+
*
389+
* The new variant has 4 parameters:
390+
* ``kmem_cache_create(name, object_size, args, flags)``
391+
*
392+
* See __kmem_cache_create_args() which implements this.
393+
*
394+
* The legacy variant has 5 parameters:
395+
* ``kmem_cache_create(name, object_size, align, flags, ctor)``
396+
*
397+
* The align and ctor parameters map to the respective fields of
398+
* &struct kmem_cache_args
399+
*
400+
* Context: Cannot be called within a interrupt, but can be interrupted.
401+
*
402+
* Return: a pointer to the cache on success, NULL on failure.
403+
*/
336404
#define kmem_cache_create(__name, __object_size, __args, ...) \
337405
_Generic((__args), \
338406
struct kmem_cache_args *: __kmem_cache_create_args, \

mm/slab_common.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,13 +239,17 @@ static struct kmem_cache *create_cache(const char *name,
239239
}
240240

241241
/**
242-
* __kmem_cache_create_args - Create a kmem cache
242+
* __kmem_cache_create_args - Create a kmem cache.
243243
* @name: A string which is used in /proc/slabinfo to identify this cache.
244244
* @object_size: The size of objects to be created in this cache.
245-
* @args: Arguments for the cache creation (see struct kmem_cache_args).
245+
* @args: Additional arguments for the cache creation (see
246+
* &struct kmem_cache_args).
246247
* @flags: See %SLAB_* flags for an explanation of individual @flags.
247248
*
248-
* Cannot be called within a interrupt, but can be interrupted.
249+
* Not to be called directly, use the kmem_cache_create() wrapper with the same
250+
* parameters.
251+
*
252+
* Context: Cannot be called within a interrupt, but can be interrupted.
249253
*
250254
* Return: a pointer to the cache on success, NULL on failure.
251255
*/

0 commit comments

Comments
 (0)