From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.161.53 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796054126; b=aOGQLXyUm48K8HAu0kqOp6P0SZYk6N2d0qH8HR9wP2jM8aYdX9t/4blioekOCBX8Mqa3 iJrwsKfD3OHNPxWOuk8ThDtko1l1Xz9dBHnVZMnr1dg6QON8YdjvtnN/DXNfT+1cmuwtq ep6cPFsCLxRP6XY6yg5zqc+tM8X3QNSCZpPGYSGbA939u4CmmiQHp2ZroT7dPVLBVabMb vRJ2ivj/ebgGmPOg6s2ucd4D5rXXmMSx1aDIcL5L5Qrm3jkzg3UMiNmEWS6KtuQB64bxL DB+zsydXzAQjCZTyLDszSJ4DZFBYsjjkBBnglVa89FY3tNd05NOrX2M49Lp6t9kbD8Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796054126; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: Date:From:To:Cc:Subject:Message-ID:Mail-Followup-To:References: MIME-Version:Content-Type:Content-Disposition:In-Reply-To; bh=RFtCN3hBbgYmHbKXPMbh4xn9UudSR2VF5rq3RIzp7Y4=; b=DU57vjkhPWjuken75jIjIVHympN5exNi6pp86hdDH2Y5kzT8F0kb5FT049Mhxpzc/1VC B7FV+pYmNqqZivbro1qO/Kd7mFraxGksOC/OO+i8b60Dpty1trTcLTJaJ/dlSxWA6Jgrw Xx7C7D9LuFZX2EV1ZQsznBbBeycWHoYmOJQAm/jBcQpYRFxpVWKHwbGL0UYE9OVJpi8BB FDiPt3K9dYTu770z+f+Y/8k5WAdDvlKljfHIUnfQHDk9JwaD290/vrXyAdj7+mgBUy58i sDMdn0YIXx5MQBdP0fW/sSP6CbYPxpXrGW97WNaSIW+owR6vXMAxVtjeWMX1fOqXtIw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b="Af1GyG/v"; arc=none smtp.remote-ip=209.85.161.53 Received: from mail-oo1-f53.google.com (mail-oo1-f53.google.com [209.85.161.53]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A3E28606A329 for ; Mon, 30 Nov 2026 15:55:25 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A3E28606A329 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=Af1GyG/v Received: by mail-oo1-f53.google.com with SMTP id 006d021491bc7-7190d9bdb2cso6454110eaf.0 for ; Mon, 30 Nov 2026 07:55:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796054124; x=1796658924; darn=vger.kernel.org; h=in-reply-to:content-disposition:content-type:mime-version :references:mail-followup-to:message-id:subject:cc:to:from:date:from :to:cc:subject:date:message-id:reply-to:content-type; bh=RFtCN3hBbgYmHbKXPMbh4xn9UudSR2VF5rq3RIzp7Y4=; b=Af1GyG/v1xn/UaIaGPx7yxAiVymkEMryb2quf/bO0AddcsUizyJJ5vTSwfE7PdbTMW DJu4u53lQUrax32RkXkneRLosNlVrcz3d/Aw0CmEVmj2OAV8XHt2Xm8ozXiX2Ao6f+0p fndCIAz0on9ji6SP4GEoZ+nixYXT6+VjUrqFEW8gbSYH2UQtZQMUvMVjt9eDai9XGsYm Y2yV0KtXIsdxDih+mpmNiju2ExACscs3RmBG6I9t4Tz2EjP7BXaU/whyRYCSYSwKG2M8 7COq7EFekNMv4Eov1vsR0j0Dz2wqNW/NrXLFVsC+oYH7TiNKQJosr8GeU0c0fPzvbLXu TqAQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796054124; x=1796658924; h=in-reply-to:content-disposition:content-type:mime-version :references:mail-followup-to:message-id:subject:cc:to:from:date :x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to:content-type; bh=RFtCN3hBbgYmHbKXPMbh4xn9UudSR2VF5rq3RIzp7Y4=; b=1awN9ufkxIoLZdNkz2FtO8HskGWJokj+wBgJVd22MLfuv8dqtG/kmp55XiIyUp3MLR A49zjV191aU6MbV/v/qthIIJz0l/IAjAZx7kty+9mRCn3JjcJWfb7wHcudZ5b3YtRvNt OQ2Q7Ej6SQCmAgVzpY4lq5KIAfGh6LttZTX5YBIi4pFMMdR+5KOn2qefjTPGb6Wl6Hyc KwZQLZ12yALWtozfLleCI5FslrbPwGJUT5TD95YMLYyqxEiHMKKGHmhsLVkYnGUBGbQI V/QhpBB829n+l1TVw1I26cW5oTi/j1Id6Og+UwThjcyqMs+TFb7EOpbNPlpFo+e/XfId Ya8g== X-Forwarded-Encrypted: i=1; AL+hErFC2YNCBXEbbnbpgvfBaZzkTwuHtWhpscCRPNfvhFLERKxh420GnAefyrPtUlv3Fv/e7SGvfcMnYuI=@vger.kernel.org X-Gm-Message-State: APuU4Z+dorpsO6PPpt4gMBpFUzAbDoCVXdjK0xHN59PdgcEMoi7Og/qx C4l+ER4SD20q/W3OVhUYSLOoQx4LpTnEt3tIufnfSP529x6j/iW8sEsa X-Gm-Gg: AZFdTZfl2Wl+z/ainoPRg2TVoqO7fd/JwaowTxGaYZ3x7WaDW/rx+iUkQyvU3j9T8Sk vueiEJ1sKfjx3cPjaDhO+v4X0KvCIZZCL6zRXZUE1+24J/KxFAq2Er5C347536agwU0qEypY3Jz zZ+yEiO9Pl0AHmGr1GEy8bl4zbEkT5zGhPK9m7zfcYTwzOpDGhNLfZdy0LUb45uBNtQwFd3HchB Bj1vKzBqpx7EymY548AaX4rfPWRr+EplxfB0bZFh65wrxdsQftd7PhcVuh87h4wrKKQtflSmv1H AZh/zY8cFxQbsvIfn0Tns8rG3MAO+UH0Mm8sy/cbS0IiST4JJT7CLiSHf3ORqyCOiCkYW+q3M0z BRtaJEFRtJmi7OreTO4n7R+rGL40UClGAtw9ZFrCUAExr0k9gqQ0TZOSVpe15RgxhXRWtNVuIlF rQLnRs4dc7+ok2N5VMuYJy/c0/mKgREs3ZBwQvCtAGXdANRlWDpVevn/JaGAEg8tLH2pgxQm9hA qh+RlFk7O29vtBhXV7Q/RU8q31MFvowdbID9TF4AdVjqLp/0SNflJDPPgGayih2cueHcNjRzLzS QVR8sJsEZjw6A4sZZaKU4LdXTKybqWSpMTMNYoIDOtQeD1fu7hOsDxjoN4gb X-Received: by 2002:a05:6820:441a:b0:6da:9dae:a2a with SMTP id 006d021491bc7-71b39571fdbmr26894275eaf.55.1796054124419; Mon, 30 Nov 2026 07:55:24 -0800 (PST) Received: from localhost ([2a03:2880:10ff:44::]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-71b380c6815sm22645860eaf.12.2026.11.30.07.55.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 07:55:24 -0800 (PST) Date: Mon, 30 Nov 2026 07:55:23 -0800 From: Joshua Hahn To: "David Hildenbrand (Arm)" Cc: Hugh Dickins , Baolin Wang , Andrew Morton , Mike Rapoport , Peter Xu , Michal Hocko , Kiryl Shutsemau , Manfred Spraul , Davidlohr Bueso , Cyrill Gorcunov , Zi Yan , "Liam R. Howlett" , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Usama Arif , Lorenzo Stoakes , Rik van Riel , Vlastimil Babka , Harry Yoo , Jann Horn , Yang Shi , Zach O'Keefe , Song Liu , David Stevens , Kefeng Wang , Alex Williamson , Suren Baghdasaryan , Dave Jiang , Matthew Wilcox , Miaohe Lin , David Rientjes , Shakeel Butt , Jan Kara , Mel Gorman , Jason Gunthorpe , John Hubbard , Gerd Hoffmann , Vivek Kasireddy , Dave Airlie , Matthew Brost , Byungchul Park , Gregory Price , Ying Huang , Alistair Popple , Hagen Paul Pfeifer , James Bottomley , Felix Kuehling , Alex Sierra , Balbir Singh , Kairui Song , Qi Zheng , Axel Rasmussen , Yuanchu Xie , Wei Xu , Baoquan He , Oscar Salvador , Muchun Song , Dan Williams , Rakie Kim , Hao Li , Christoph Lameter , Roman Gushchin , Sebastian Andrzej Siewior , Clark Williams , Steven Rostedt , Alexei Starovoitov , Xu Xin , Chengming Zhou , Shivank Garg , linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-cxl@vger.kernel.org, linux-rt-devel@lists.linux.dev, SJ Park , stable@vger.kernel.org, Chris Mason Subject: Re: [PATCH 25/32] mm/mempolicy: don't wait for mems_allowed_seq in kmalloc_nolock() Message-ID: Mail-Followup-To: "David Hildenbrand (Arm)" , Hugh Dickins , Baolin Wang , Andrew Morton , Mike Rapoport , Peter Xu , Michal Hocko , Kiryl Shutsemau , Manfred Spraul , Davidlohr Bueso , Cyrill Gorcunov , Zi Yan , "Liam R. Howlett" , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Usama Arif , Lorenzo Stoakes , Rik van Riel , Vlastimil Babka , Harry Yoo , Jann Horn , Yang Shi , Zach O'Keefe , Song Liu , David Stevens , Kefeng Wang , Alex Williamson , Suren Baghdasaryan , Dave Jiang , Matthew Wilcox , Miaohe Lin , David Rientjes , Shakeel Butt , Jan Kara , Mel Gorman , Jason Gunthorpe , John Hubbard , Gerd Hoffmann , Vivek Kasireddy , Dave Airlie , Matthew Brost , Byungchul Park , Gregory Price , Ying Huang , Alistair Popple , Hagen Paul Pfeifer , James Bottomley , Felix Kuehling , Alex Sierra , Balbir Singh , Kairui Song , Qi Zheng , Axel Rasmussen , Yuanchu Xie , Wei Xu , Baoquan He , Oscar Salvador , Muchun Song , Dan Williams , Rakie Kim , Hao Li , Christoph Lameter , Roman Gushchin , Sebastian Andrzej Siewior , Clark Williams , Steven Rostedt , Alexei Starovoitov , Xu Xin , Chengming Zhou , Shivank Garg , linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-cxl@vger.kernel.org, linux-rt-devel@lists.linux.dev, SJ Park , stable@vger.kernel.org, Chris Mason References: <20261129-fixes_core_3-v1-0-f66094116801@kernel.org> <20261129-fixes_core_3-v1-25-f66094116801@kernel.org> X-Mailing-List: linux-cxl@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20261129-fixes_core_3-v1-25-f66094116801@kernel.org> On Sun, Nov 29, 2026 at 11:06:56AM +0100, David Hildenbrand (Arm) wrote: > kmalloc_nolock() must not wait for locks, including seqlocks. > get_from_any_partial() therefore skips read_mems_allowed_begin() if > spinning is not allowed. > > However, with an MPOL_INTERLEAVE or MPOL_WEIGHTED_INTERLEAVE task policy, > mempolicy_slab_node() calls interleave_nodes() or > weighted_interleave_nodes(), which both spin on tsk->mems_allowed_seq > since commit 274519ed414b ("mm/mempolicy: protect task interleave > functions with tsk->mems_allowed_seq"). The same applies to > apply_strict_numa_policy() with slab_strict_numa. > > Consequently, a BPF program that allocates using kmalloc_nolock() within > the write section of that seqcount, for example in > cpuset_change_task_nodemask(), can deadlock. On PREEMPT_RT, waiting for > the seqcount can then even sleep in atomic context. > > Let's fix it by passing whether spinning is allowed to > mempolicy_slab_node(), simply using the local node for interleave > policies if disallowed. > > While not perfect, it shouldn't matter in practice, is in line with > !in_task() handling, and slab interleaving is best-effort either way, > unless slab_strict_numa is set. In general this one looks good to me but I just want to note that below, we turn the strict slab_strict_numa into a best-effort case when spinning is not allowed. Maybe something that is worth documenting? Otherwise, LGTM: Reviewed-by: Joshua Hahn > This has low urgency, as it requires a BPF program to allocate with > kmalloc_nolock() while the task's mems_allowed are being updated. > > Fixes: af92793e52c3 ("slab: Introduce kmalloc_nolock() and kfree_nolock().") > Cc: stable@vger.kernel.org > Reported-by: Chris Mason > Signed-off-by: David Hildenbrand (Arm) > --- > include/linux/mempolicy.h | 2 +- > mm/mempolicy.c | 8 +++++++- > mm/slub.c | 15 +++++++++------ > 3 files changed, 17 insertions(+), 8 deletions(-) > > diff --git a/include/linux/mempolicy.h b/include/linux/mempolicy.h > index 828e7de6dee73..93ad83fe25a81 100644 > --- a/include/linux/mempolicy.h > +++ b/include/linux/mempolicy.h > @@ -147,7 +147,7 @@ extern int huge_node(struct vm_area_struct *vma, > extern bool init_nodemask_of_mempolicy(nodemask_t *mask); > extern bool mempolicy_in_oom_domain(struct task_struct *tsk, > const nodemask_t *mask); > -extern unsigned int mempolicy_slab_node(void); > +extern unsigned int mempolicy_slab_node(bool allow_spin); > > extern enum zone_type policy_zone; > > diff --git a/mm/mempolicy.c b/mm/mempolicy.c > index 746fd0a137515..c93a278d840b3 100644 > --- a/mm/mempolicy.c > +++ b/mm/mempolicy.c > @@ -2236,7 +2236,7 @@ static unsigned int interleave_nodes(struct mempolicy *policy) > * Depending on the memory policy provide a node from which to allocate the > * next slab entry. > */ > -unsigned int mempolicy_slab_node(void) > +unsigned int mempolicy_slab_node(bool allow_spin) > { > struct mempolicy *policy; > int node = numa_mem_id(); > @@ -2253,9 +2253,15 @@ unsigned int mempolicy_slab_node(void) > return first_node(policy->nodes); > > case MPOL_INTERLEAVE: > + /* We'd have to spin on tsk->mems_allowed_seq. */ > + if (!allow_spin) > + return node; > return interleave_nodes(policy); > > case MPOL_WEIGHTED_INTERLEAVE: > + /* We'd have to spin on tsk->mems_allowed_seq. */ > + if (!allow_spin) > + return node; > return weighted_interleave_nodes(policy); > > case MPOL_BIND: > diff --git a/mm/slub.c b/mm/slub.c > index 5c33d12470601..f970f1e5a9e6a 100644 > --- a/mm/slub.c > +++ b/mm/slub.c > @@ -4027,7 +4027,8 @@ static void *get_from_any_partial(struct kmem_cache *s, gfp_t gfp_flags, > if (allow_spin) > cpuset_mems_cookie = read_mems_allowed_begin(); > > - zonelist = node_zonelist(mempolicy_slab_node(), gfp_flags); > + zonelist = node_zonelist(mempolicy_slab_node(allow_spin), > + gfp_flags); > for_each_zone_zonelist(zone, z, zonelist, highest_zoneidx) { > struct kmem_cache_node *n; > > @@ -4640,12 +4641,14 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node, > return object; > } > > -static __always_inline int apply_strict_numa_policy(int node) > +static __always_inline int apply_strict_numa_policy(int node, > + unsigned int alloc_flags) > { > #ifdef CONFIG_NUMA > if (static_branch_unlikely(&strict_numa) && > node == NUMA_NO_NODE) { > > + const bool allow_spin = alloc_flags_allow_spinning(alloc_flags); > struct mempolicy *mpol = current->mempolicy; > > if (mpol) { > @@ -4658,7 +4661,7 @@ static __always_inline int apply_strict_numa_policy(int node) > */ > if (mpol->mode != MPOL_BIND || > !node_isset(numa_mem_id(), mpol->nodes)) > - node = mempolicy_slab_node(); > + node = mempolicy_slab_node(allow_spin); > } > } > #endif > @@ -5016,7 +5019,7 @@ static __fastpath_inline void *slab_alloc_node(struct kmem_cache *s, > if (unlikely(object)) > goto out; > > - node = apply_strict_numa_policy(node); > + node = apply_strict_numa_policy(node, ac->alloc_flags); > > object = alloc_from_pcs(s, gfpflags, ac->alloc_flags, node); > > @@ -5577,7 +5580,7 @@ static void *__kmalloc_nolock_noprof(DECL_TOKEN_PARAMS(size, token), gfp_t gfp_f > if (!can_spin_trylock()) > return NULL; > > - node = apply_strict_numa_policy(node); > + node = apply_strict_numa_policy(node, ac->alloc_flags); > > retry: > if (unlikely(size > KMALLOC_MAX_CACHE_SIZE)) > @@ -7481,7 +7484,7 @@ __refill_objects_any(struct kmem_cache *s, void **p, gfp_t gfp, unsigned int min > > do { > cpuset_mems_cookie = read_mems_allowed_begin(); > - zonelist = node_zonelist(mempolicy_slab_node(), gfp); > + zonelist = node_zonelist(mempolicy_slab_node(/* allow_spin */ true), gfp); > for_each_zone_zonelist(zone, z, zonelist, highest_zoneidx) { > struct kmem_cache_node *n; > unsigned int r; > > -- > 2.56.1 >