@@ -242,19 +242,72 @@ bool slab_is_available(void);
242
242
243
243
/**
244
244
* 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.
251
254
*/
252
255
struct kmem_cache_args {
256
+ /**
257
+ * @align: The required alignment for the objects.
258
+ *
259
+ * %0 means no specific alignment is requested.
260
+ */
253
261
unsigned int align ;
262
+ /**
263
+ * @useroffset: Usercopy region offset.
264
+ *
265
+ * %0 is a valid offset, when @usersize is non-%0
266
+ */
254
267
unsigned int useroffset ;
268
+ /**
269
+ * @usersize: Usercopy region size.
270
+ *
271
+ * %0 means no usercopy region is specified.
272
+ */
255
273
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
+ */
256
295
unsigned int freeptr_offset ;
296
+ /**
297
+ * @use_freeptr_offset: Whether a @freeptr_offset is used.
298
+ */
257
299
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
+ */
258
311
void (* ctor )(void * );
259
312
};
260
313
@@ -275,30 +328,20 @@ __kmem_cache_create(const char *name, unsigned int size, unsigned int align,
275
328
}
276
329
277
330
/**
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.
280
333
* @name: A string which is used in /proc/slabinfo to identify this cache.
281
334
* @size: The size of objects to be created in this cache.
282
335
* @align: The required alignment for the objects.
283
336
* @flags: SLAB flags
284
337
* @useroffset: Usercopy region offset
285
338
* @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.
292
340
*
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)
302
345
*
303
346
* Return: a pointer to the cache on success, NULL on failure.
304
347
*/
@@ -333,6 +376,31 @@ __kmem_cache_default_args(const char *name, unsigned int size,
333
376
return __kmem_cache_create_args (name , size , & kmem_default_args , flags );
334
377
}
335
378
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
+ */
336
404
#define kmem_cache_create (__name , __object_size , __args , ...) \
337
405
_Generic((__args), \
338
406
struct kmem_cache_args *: __kmem_cache_create_args, \
0 commit comments