Commit dfdc8d25 authored by Christian Brauner's avatar Christian Brauner
Browse files

Merge patch series "fs,mm: add kmem_cache_create_rcu()"

Christian Brauner <brauner@kernel.org> says:

When a kmem cache is created with SLAB_TYPESAFE_BY_RCU the free pointer
must be located outside of the object because we don't know what part of
the memory can safely be overwritten as it may be needed to prevent
object recycling.

That has the consequence that SLAB_TYPESAFE_BY_RCU may end up adding a
new cacheline. This is the case for e.g., struct file. After having it
shrunk down by 40 bytes and having it fit in three cachelines we still
have SLAB_TYPESAFE_BY_RCU adding a fourth cacheline because it needs to
accommodate the free pointer.

Add a new kmem_cache_create_rcu() function that allows the caller to
specify an offset where the free pointer is supposed to be placed.

Before this series cat /proc/slabinfo:

filp                1198   1248    256   32    2 : tunables    0    0    0 : slabdata     39     39      0
                                   ^^^

After this series cat /proc/slabinfo:

filp                1323   1323    192   21    1 : tunables    0    0    0 : slabdata     63     63      0
                                   ^^^
* patches from https://lore.kernel.org/r/20240828-work-kmem_cache-rcu-v3-0-5460bc1f09f6@kernel.org:
  fs: use kmem_cache_create_rcu()
  mm: add kmem_cache_create_rcu()
  mm: remove unused root_cache argument

Link: https://lore.kernel.org/r/20240828-work-kmem_cache-rcu-v3-0-5460bc1f09f6@kernel.org


Signed-off-by: default avatarChristian Brauner <brauner@kernel.org>
parents c0390d54 ea566e18
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -511,9 +511,9 @@ EXPORT_SYMBOL(__fput_sync);

void __init files_init(void)
{
	filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0,
				SLAB_TYPESAFE_BY_RCU | SLAB_HWCACHE_ALIGN |
				SLAB_PANIC | SLAB_ACCOUNT, NULL);
	filp_cachep = kmem_cache_create_rcu("filp", sizeof(struct file),
				offsetof(struct file, f_freeptr),
				SLAB_HWCACHE_ALIGN | SLAB_PANIC | SLAB_ACCOUNT);
	percpu_counter_init(&nr_files, 0, GFP_KERNEL);
}

+2 −0
Original line number Diff line number Diff line
@@ -1011,6 +1011,7 @@ static inline int ra_has_index(struct file_ra_state *ra, pgoff_t index)
 * @f_task_work: task work entry point
 * @f_llist: work queue entrypoint
 * @f_ra: file's readahead state
 * @f_freeptr: Pointer used by SLAB_TYPESAFE_BY_RCU file cache (don't touch.)
 */
struct file {
	atomic_long_t			f_count;
@@ -1042,6 +1043,7 @@ struct file {
		struct callback_head	f_task_work;
		struct llist_node	f_llist;
		struct file_ra_state	f_ra;
		freeptr_t		f_freeptr;
	};
	/* --- cacheline 3 boundary (192 bytes) --- */
} __randomize_layout
+9 −0
Original line number Diff line number Diff line
@@ -212,6 +212,12 @@ enum _slab_flag_bits {
#define SLAB_NO_OBJ_EXT		__SLAB_FLAG_UNUSED
#endif

/*
 * freeptr_t represents a SLUB freelist pointer, which might be encoded
 * and not dereferenceable if CONFIG_SLAB_FREELIST_HARDENED is enabled.
 */
typedef struct { unsigned long v; } freeptr_t;

/*
 * ZERO_SIZE_PTR will be returned for zero sized kmalloc requests.
 *
@@ -242,6 +248,9 @@ struct kmem_cache *kmem_cache_create_usercopy(const char *name,
			slab_flags_t flags,
			unsigned int useroffset, unsigned int usersize,
			void (*ctor)(void *));
struct kmem_cache *kmem_cache_create_rcu(const char *name, unsigned int size,
					 unsigned int freeptr_offset,
					 slab_flags_t flags);
void kmem_cache_destroy(struct kmem_cache *s);
int kmem_cache_shrink(struct kmem_cache *s);

+2 −0
Original line number Diff line number Diff line
@@ -261,6 +261,8 @@ struct kmem_cache {
	unsigned int object_size;	/* Object size without metadata */
	struct reciprocal_value reciprocal_size;
	unsigned int offset;		/* Free pointer offset */
	/* Specific free pointer requested (if not UINT_MAX) */
	unsigned int rcu_freeptr_offset;
#ifdef CONFIG_SLUB_CPU_PARTIAL
	/* Number of per cpu partial objects to keep around */
	unsigned int cpu_partial;
+98 −41
Original line number Diff line number Diff line
@@ -202,10 +202,10 @@ struct kmem_cache *find_mergeable(unsigned int size, unsigned int align,
}

static struct kmem_cache *create_cache(const char *name,
		unsigned int object_size, unsigned int align,
		slab_flags_t flags, unsigned int useroffset,
		unsigned int usersize, void (*ctor)(void *),
		struct kmem_cache *root_cache)
		unsigned int object_size, unsigned int freeptr_offset,
		unsigned int align, slab_flags_t flags,
		unsigned int useroffset, unsigned int usersize,
		void (*ctor)(void *))
{
	struct kmem_cache *s;
	int err;
@@ -213,6 +213,13 @@ static struct kmem_cache *create_cache(const char *name,
	if (WARN_ON(useroffset + usersize > object_size))
		useroffset = usersize = 0;

	/* If a custom freelist pointer is requested make sure it's sane. */
	err = -EINVAL;
	if (freeptr_offset != UINT_MAX &&
	    (freeptr_offset >= object_size || !(flags & SLAB_TYPESAFE_BY_RCU) ||
	     !IS_ALIGNED(freeptr_offset, sizeof(freeptr_t))))
		goto out;

	err = -ENOMEM;
	s = kmem_cache_zalloc(kmem_cache, GFP_KERNEL);
	if (!s)
@@ -220,13 +227,13 @@ static struct kmem_cache *create_cache(const char *name,

	s->name = name;
	s->size = s->object_size = object_size;
	s->rcu_freeptr_offset = freeptr_offset;
	s->align = align;
	s->ctor = ctor;
#ifdef CONFIG_HARDENED_USERCOPY
	s->useroffset = useroffset;
	s->usersize = usersize;
#endif

	err = __kmem_cache_create(s, flags);
	if (err)
		goto out_free_cache;
@@ -241,38 +248,10 @@ static struct kmem_cache *create_cache(const char *name,
	return ERR_PTR(err);
}

/**
 * kmem_cache_create_usercopy - Create a cache with a region suitable
 * for copying to userspace
 * @name: A string which is used in /proc/slabinfo to identify this cache.
 * @size: The size of objects to be created in this cache.
 * @align: The required alignment for the objects.
 * @flags: SLAB flags
 * @useroffset: Usercopy region offset
 * @usersize: Usercopy region size
 * @ctor: A constructor for the objects.
 *
 * Cannot be called within a interrupt, but can be interrupted.
 * The @ctor is run when new pages are allocated by the cache.
 *
 * The flags are
 *
 * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
 * to catch references to uninitialised memory.
 *
 * %SLAB_RED_ZONE - Insert `Red` zones around the allocated memory to check
 * for buffer overruns.
 *
 * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
 * cacheline.  This can be beneficial if you're counting cycles as closely
 * as davem.
 *
 * Return: a pointer to the cache on success, NULL on failure.
 */
struct kmem_cache *
kmem_cache_create_usercopy(const char *name,
		  unsigned int size, unsigned int align,
		  slab_flags_t flags,
static struct kmem_cache *
do_kmem_cache_create_usercopy(const char *name,
		  unsigned int size, unsigned int freeptr_offset,
		  unsigned int align, slab_flags_t flags,
		  unsigned int useroffset, unsigned int usersize,
		  void (*ctor)(void *))
{
@@ -332,9 +311,9 @@ kmem_cache_create_usercopy(const char *name,
		goto out_unlock;
	}

	s = create_cache(cache_name, size,
	s = create_cache(cache_name, size, freeptr_offset,
			 calculate_alignment(flags, align, size),
			 flags, useroffset, usersize, ctor, NULL);
			 flags, useroffset, usersize, ctor);
	if (IS_ERR(s)) {
		err = PTR_ERR(s);
		kfree_const(cache_name);
@@ -356,6 +335,45 @@ kmem_cache_create_usercopy(const char *name,
	}
	return s;
}

/**
 * kmem_cache_create_usercopy - Create a cache with a region suitable
 * for copying to userspace
 * @name: A string which is used in /proc/slabinfo to identify this cache.
 * @size: The size of objects to be created in this cache.
 * @freeptr_offset: Custom offset for the free pointer in RCU caches
 * @align: The required alignment for the objects.
 * @flags: SLAB flags
 * @useroffset: Usercopy region offset
 * @usersize: Usercopy region size
 * @ctor: A constructor for the objects.
 *
 * Cannot be called within a interrupt, but can be interrupted.
 * The @ctor is run when new pages are allocated by the cache.
 *
 * The flags are
 *
 * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
 * to catch references to uninitialised memory.
 *
 * %SLAB_RED_ZONE - Insert `Red` zones around the allocated memory to check
 * for buffer overruns.
 *
 * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
 * cacheline.  This can be beneficial if you're counting cycles as closely
 * as davem.
 *
 * Return: a pointer to the cache on success, NULL on failure.
 */
struct kmem_cache *
kmem_cache_create_usercopy(const char *name, unsigned int size,
			   unsigned int align, slab_flags_t flags,
			   unsigned int useroffset, unsigned int usersize,
			   void (*ctor)(void *))
{
	return do_kmem_cache_create_usercopy(name, size, UINT_MAX, align, flags,
					     useroffset, usersize, ctor);
}
EXPORT_SYMBOL(kmem_cache_create_usercopy);

/**
@@ -387,11 +405,50 @@ struct kmem_cache *
kmem_cache_create(const char *name, unsigned int size, unsigned int align,
		slab_flags_t flags, void (*ctor)(void *))
{
	return kmem_cache_create_usercopy(name, size, align, flags, 0, 0,
					  ctor);
	return do_kmem_cache_create_usercopy(name, size, UINT_MAX, align, flags,
					     0, 0, ctor);
}
EXPORT_SYMBOL(kmem_cache_create);

/**
 * kmem_cache_create_rcu - Create a SLAB_TYPESAFE_BY_RCU cache.
 * @name: A string which is used in /proc/slabinfo to identify this cache.
 * @size: The size of objects to be created in this cache.
 * @freeptr_offset: The offset into the memory to the free pointer
 * @flags: SLAB flags
 *
 * Cannot be called within an interrupt, but can be interrupted.
 *
 * See kmem_cache_create() for an explanation of possible @flags.
 *
 * By default SLAB_TYPESAFE_BY_RCU caches place the free pointer outside
 * of the object. This might cause the object to grow in size. Callers
 * that have a reason to avoid this can specify a custom free pointer
 * offset in their struct where the free pointer will be placed.
 *
 * Note that placing the free pointer inside the object requires the
 * caller to ensure that no fields are invalidated that are required to
 * guard against object recycling (See SLAB_TYPESAFE_BY_RCU for
 * details.).
 *
 * Using zero as a value for @freeptr_offset is valid. To request no
 * offset UINT_MAX must be specified.
 *
 * Note that @ctor isn't supported with custom free pointers as a @ctor
 * requires an external free pointer.
 *
 * Return: a pointer to the cache on success, NULL on failure.
 */
struct kmem_cache *kmem_cache_create_rcu(const char *name, unsigned int size,
					 unsigned int freeptr_offset,
					 slab_flags_t flags)
{
	return do_kmem_cache_create_usercopy(name, size, freeptr_offset, 0,
					     flags | SLAB_TYPESAFE_BY_RCU, 0, 0,
					     NULL);
}
EXPORT_SYMBOL(kmem_cache_create_rcu);

static struct kmem_cache *kmem_buckets_cache __ro_after_init;

/**
Loading