From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-183.mta1.migadu.com (out-183.mta1.migadu.com [95.215.58.183]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0B8D33FA5E9 for ; Thu, 18 Jun 2026 15:22:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.183 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781796142; cv=none; b=BNY8eQwaoaretf0FkiNdRAARZqAFiOulVsNT3uRXSv5r1X6DZ2R/mRle3O1u80c5FW953zAKT4dGccoDX1huQoF1cFxLJmqBweqOP3b83jyuxG1P1U4wnKcEpj2yakXx1asl6IC8Ey4/gvpLhhZsgVZa2f3Ba25IZUvm3vZCH8Q= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781796142; c=relaxed/simple; bh=AaRNd0RWc4PK/tT/kKv1T1Y47ICHmh86fFeVOFKOwus=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=ebsTFoZFsiejy648Ewu8XrzGwTEkHuLYCxvFqLvjEVDuG5BE2Hq6Ud14j3y5oancRGJl6buZLIhzBTpdJlNCMlQyy4LfSne/wVDcgiI77PorbU22rsF9/od2rMxkLiEYv3QXt8C/aUtYKtsDUYSrE0WvewVnBJMKLGjYGGpOrvQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=xdh7NdOu; arc=none smtp.client-ip=95.215.58.183 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="xdh7NdOu" Date: Thu, 18 Jun 2026 23:21:51 +0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1781796138; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=LPnF9ie3GWjL02NStVwtdTUxsTUElaL+pC+C/sA2hk8=; b=xdh7NdOu/tRjnJz9YQ8g2oGsRsA3yrNpsKLJz5LZDvOwlLTsbkuh37pzim51iPz2sfz+uH I8qoUs1l+2Migi7fW4rZ7TzuWhhnVp5dSqLasjbuvFCa5LRuJWwQQwdmohk46yppJRzh2j A31lwqxFrVQHdf3pk0SKGPZi7u+d2kk= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Hao Li To: vbabka@kernel.org, harry@kernel.org Cc: akpm@linux-foundation.org, cl@gentwo.org, rientjes@google.com, roman.gushchin@linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] mm/slub: deduplicate NUMA policy calculation in allocation paths Message-ID: References: <20260618100913.346636-1-hao.li@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20260618100913.346636-1-hao.li@linux.dev> X-Migadu-Flow: FLOW_OUT On Thu, Jun 18, 2026 at 06:08:52PM +0800, Hao Li wrote: > Currently, alloc_from_pcs() and __slab_alloc_node() both calculate the > NUMA policy independently. Since they are called consecutively in paths > like __kmalloc_nolock_noprof() and slab_alloc_node(), this leads to > redundant computations. > > Introduce a helper function to resolve the NUMA policy once, eliminating > the duplicated code and reducing execution overhead. > > Signed-off-by: Hao Li > --- > mm/slub.c | 72 ++++++++++++++++++++++--------------------------------- > 1 file changed, 29 insertions(+), 43 deletions(-) > > diff --git a/mm/slub.c b/mm/slub.c > index 62e9cd46916f..45e9f379b7da 100644 > --- a/mm/slub.c > +++ b/mm/slub.c > @@ -4523,32 +4523,36 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node, > return object; > } > > +static __always_inline int apply_numa_policy(int node) > +{ > +#ifdef CONFIG_NUMA > + if (static_branch_unlikely(&strict_numa) && > + node == NUMA_NO_NODE) { > + > + struct mempolicy *mpol = current->mempolicy; > + > + if (mpol) { > + /* > + * Special BIND rule support. If the local node > + * is in permitted set then do not redirect > + * to a particular node. > + * Otherwise we apply the memory policy to get > + * the node we need to allocate on. > + */ > + if (mpol->mode != MPOL_BIND || > + !node_isset(numa_mem_id(), mpol->nodes)) > + node = mempolicy_slab_node(); > + } > + } > +#endif > + return node; > +} > + > static void *__slab_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node, > const struct slab_alloc_context *ac) > { > void *object; > > -#ifdef CONFIG_NUMA > - if (static_branch_unlikely(&strict_numa) && > - node == NUMA_NO_NODE) { > - > - struct mempolicy *mpol = current->mempolicy; > - > - if (mpol) { > - /* > - * Special BIND rule support. If the local node > - * is in permitted set then do not redirect > - * to a particular node. > - * Otherwise we apply the memory policy to get > - * the node we need to allocate on. > - */ > - if (mpol->mode != MPOL_BIND || > - !node_isset(numa_mem_id(), mpol->nodes)) > - node = mempolicy_slab_node(); > - } > - } > -#endif > - > object = ___slab_alloc(s, gfpflags, node, ac); > > return object; oh!, __slab_alloc_node is almost an empty wrapper Maybe I need to eliminate it completely... > @@ -4756,28 +4760,6 @@ void *alloc_from_pcs(struct kmem_cache *s, gfp_t gfp, unsigned int alloc_flags, > bool node_requested; > void *object; > > -#ifdef CONFIG_NUMA > - if (static_branch_unlikely(&strict_numa) && > - node == NUMA_NO_NODE) { > - > - struct mempolicy *mpol = current->mempolicy; > - > - if (mpol) { > - /* > - * Special BIND rule support. If the local node > - * is in permitted set then do not redirect > - * to a particular node. > - * Otherwise we apply the memory policy to get > - * the node we need to allocate on. > - */ > - if (mpol->mode != MPOL_BIND || > - !node_isset(numa_mem_id(), mpol->nodes)) > - > - node = mempolicy_slab_node(); > - } > - } > -#endif > - > node_requested = IS_ENABLED(CONFIG_NUMA) && node != NUMA_NO_NODE; > > /* > @@ -4927,6 +4909,8 @@ static __fastpath_inline void *slab_alloc_node(struct kmem_cache *s, > if (unlikely(object)) > goto out; > > + node = apply_numa_policy(node); > + > object = alloc_from_pcs(s, gfpflags, ac->alloc_flags, node); > > if (unlikely(!object)) > @@ -5430,6 +5414,8 @@ static void *__kmalloc_nolock_noprof(DECL_TOKEN_PARAMS(size, token), gfp_t gfp_f > */ > return NULL; > > + node = apply_numa_policy(node); > + > ret = alloc_from_pcs(s, gfp_flags, ac->alloc_flags, node); > if (ret) > goto success; > -- > 2.54.0 >