mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Pedro Falcato <pfalcato@suse.de>
To: Kees Cook <kees@kernel.org>
Cc: "Vlastimil Babka" <vbabka@kernel.org>,
	"Harry Yoo" <harry@kernel.org>,
	"Andrew Morton" <akpm@linux-foundation.org>,
	"Hao Li" <hao.li@linux.dev>, "Christoph Lameter" <cl@gentwo.org>,
	"David Rientjes" <rientjes@google.com>,
	"Roman Gushchin" <roman.gushchin@linux.dev>,
	linux-mm@kvack.org, "Kuniyuki Iwashima" <kuniyu@google.com>,
	linux-hardening@vger.kernel.org,
	"Jakub Kicinski" <kuba@kernel.org>,
	"David S. Miller" <davem@davemloft.net>,
	"Eric Dumazet" <edumazet@google.com>,
	"Paolo Abeni" <pabeni@redhat.com>,
	"Simon Horman" <horms@kernel.org>,
	"Jason Xing" <kerneljasonxing@gmail.com>,
	"Björn Töpel" <bjorn@kernel.org>,
	"Jiayuan Chen" <jiayuan.chen@linux.dev>,
	"Willem de Bruijn" <willemb@google.com>,
	linux-kernel@vger.kernel.org, netdev@vger.kernel.org
Subject: Re: [PATCH v4 5/7] mm/slab: Provide kmalloc type fallback for bucket allocations
Date: Tue, 22 Sep 2026 11:11:58 +0100	[thread overview]
Message-ID: <arJSz0r5u_78NWKe@pedro-suse.tail5790ac.ts.net> (raw)
In-Reply-To: <20260921075820.1718334-5-kees@kernel.org>

Hi Kees,

Big thanks for continuing this effort :))

On Mon, Sep 21, 2026 at 12:58:16AM -0700, Kees Cook wrote:
> kmem_buckets_create() clones kmalloc_caches[KMALLOC_NORMAL].
> kmalloc_slab() figures out the kmalloc type the caller asks for, but
> then ignored it whenever a bucket set was in use, returning a normal
> cache regardless. This would be a problem if a caller asked for GFP_DMA,
> __GFP_ACCOUNT, etc. None of the current users do this, so there is
> problem, but it makes adding new users fragile. For example, skb data[1]
> needs to handle GFP_DMA (rarely) and __GFP_ACCOUNT (often).
> 
> Send those allocations to the general caches instead so nothing breaks and
> regular allocations remain isolated with the bucket. The kmem_bucket_type
> enum contains only a single item here, but will be expanded in the next
> patch.
> 
> Built and tests pass with ARCH=x86_64 defconfig with GCC 16.2.0, with
> CONFIG_SLAB_BUCKETS as y and n.
> 
> Assisted-by: LLM
> Link: https://lore.kernel.org/all/04debe19-bbe8-4b5f-9668-753d1f97832d@redhat.com/ [1]
> Signed-off-by: Kees Cook <kees@kernel.org>
> ---
> Cc: Vlastimil Babka <vbabka@kernel.org>
> Cc: Harry Yoo <harry@kernel.org>
> Cc: Andrew Morton <akpm@linux-foundation.org>
> Cc: Hao Li <hao.li@linux.dev>
> Cc: Christoph Lameter <cl@gentwo.org>
> Cc: David Rientjes <rientjes@google.com>
> Cc: Roman Gushchin <roman.gushchin@linux.dev>
> Cc: <linux-mm@kvack.org>
> Cc: Pedro Falcato <pfalcato@suse.de>
> Cc: Kuniyuki Iwashima <kuniyu@google.com>
> Cc: <linux-hardening@vger.kernel.org>
> ---
>  include/linux/slab.h   | 13 +++++++++++
>  mm/slab.h              | 23 ++++++++++++++++--
>  lib/tests/slub_kunit.c | 53 ++++++++++++++++++++++++++++++++++++++++++
>  3 files changed, 87 insertions(+), 2 deletions(-)
> 
> diff --git a/include/linux/slab.h b/include/linux/slab.h
> index 18a2351f9084..ab9ab3d34847 100644
> --- a/include/linux/slab.h
> +++ b/include/linux/slab.h
> @@ -742,6 +742,19 @@ typedef struct kmem_cache * kmem_buckets[KMALLOC_SHIFT_HIGH + 1];
>  
>  extern kmem_buckets kmalloc_caches[NR_KMALLOC_TYPES];
>  
> +/*
> + * The kmalloc types a bucket set can hold a copy of. This is deliberately not
> + * enum kmalloc_cache_type: the KMALLOC_PARTITION copies are all "normal" to a
> + * bucket set, which already separates what they were there to separate, so
> + * indexing by those would mean up to KMALLOC_PARTITION_CACHES_NR unusable
> + * rows per set. Allocations of any type not listed here are served by the
> + * general caches.
> + */

This sounds odd. Is there a good reason why KMALLOC_PARTITIONs are kmalloc_cache_types?
Perhaps that bit should be reworked instead?

> +enum kmem_bucket_type {
> +	KMEM_BUCKET_NORMAL = 0,
> +	NR_KMEM_BUCKET_TYPES
> +};
> +
>  /*
>   * Define gfp bits that should not be set for KMALLOC_NORMAL.
>   */
> diff --git a/mm/slab.h b/mm/slab.h
> index 8fd6835e4235..7f1bfee83b92 100644
> --- a/mm/slab.h
> +++ b/mm/slab.h
> @@ -421,6 +421,26 @@ static inline unsigned int size_index_elem(unsigned int bytes)
>  	return (bytes - 1) / 8;
>  }
>  
> +/*
> + * Which set of buckets to use for the given kmalloc_cache_type. If not
> + * handled by the kmem_buckets, fall back to general caches.
> + */
> +static inline kmem_buckets *
> +kmalloc_choose_bucket(kmem_buckets *bucket, enum kmalloc_cache_type type)
> +{
> +	enum kmem_bucket_type btype;
> +
> +	if (!bucket)
> +		return &kmalloc_caches[type];
> +
> +	if (type <= KMALLOC_PARTITION_END)
> +		btype = KMEM_BUCKET_NORMAL;
> +	else
> +		return &kmalloc_caches[type];	/* No set holds a row for it. */

Hitting this case sounds like a bug in the kernel. WARN_ON_ONCE()?

Otherwise LGTM.

-- 
Pedro

  reply	other threads:[~2026-09-22 10:12 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-21  7:58 [PATCH v4 0/7] net: skb: isolate skb data area allocations into a separate bucket Kees Cook
2026-09-21  7:58 ` [PATCH v4 1/7] mm/slab: Mark the kmem_buckets_create() context as a Context: section Kees Cook
2026-09-21 12:03   ` Harry Yoo
2026-09-22  9:58   ` Pedro Falcato
2026-09-21  7:58 ` [PATCH v4 2/7] mm/slab: Give bucket caches the alignment of the caches they mirror Kees Cook
2026-09-21 13:17   ` Harry Yoo
2026-09-21 23:25     ` Kees Cook
2026-09-21  7:58 ` [PATCH v4 3/7] mm/slab: Add kmem_buckets_destroy() Kees Cook
2026-09-21  7:58 ` [PATCH v4 4/7] mm/slab: Add tests for the existing kmem_buckets behaviour Kees Cook
2026-09-21  7:58 ` [PATCH v4 5/7] mm/slab: Provide kmalloc type fallback for bucket allocations Kees Cook
2026-09-22 10:11   ` Pedro Falcato [this message]
2026-09-21  7:58 ` [PATCH v4 6/7] mm/slab: Let a bucket set handle __GFP_ACCOUNT Kees Cook
2026-09-21 13:25   ` Harry Yoo
2026-09-21 23:26     ` Kees Cook
2026-09-22 10:20   ` Pedro Falcato
2026-09-21  7:58 ` [PATCH v4 7/7] net: skb: isolate skb data area allocations into a separate bucket Kees Cook

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=arJSz0r5u_78NWKe@pedro-suse.tail5790ac.ts.net \
    --to=pfalcato@suse.de \
    --cc=akpm@linux-foundation.org \
    --cc=bjorn@kernel.org \
    --cc=cl@gentwo.org \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=hao.li@linux.dev \
    --cc=harry@kernel.org \
    --cc=horms@kernel.org \
    --cc=jiayuan.chen@linux.dev \
    --cc=kees@kernel.org \
    --cc=kerneljasonxing@gmail.com \
    --cc=kuba@kernel.org \
    --cc=kuniyu@google.com \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=rientjes@google.com \
    --cc=roman.gushchin@linux.dev \
    --cc=vbabka@kernel.org \
    --cc=willemb@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®