From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.161.42 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796054126; b=voVrVp9gNrXepvc4FBkhfYzMce3Er5bboCmCM2gSgYD2eX4NBwpAK7qlSLNAV6waOSbs Pip9/5/2hvGhWCFpMndnLlbyI6BEwAcfedjYcVuw4+anYj+VVB/GZYvIE/mRK2ip/3l/L M8a8puHoE+2VktfXoRnStkjLaHJl0Id7g5CM99MngfG6ouXdk1/O0x7yP+isSWbuq3qTA 5KWk0Y6JGhhWwsUGBXb8sHbgPtww71Yi11BzgytBlrJ2rApVF9zve7OGIaZarNrjD6MU2 jLa2kLyRQHPAIS/uCakGq3c9i7CQSGPLtzY0Wjs4soHzNyEiZOIsXz6px0PcYsteK/w== 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=fm0Nk4VrFe1TSvrwJ/Vs8QdH1fmvlfRA+EFjfGLanjKbh0y7e6wvCrzSxAvPzKOu1UmN hxSrvrr9nW7Ji5+OKpxswcyEVUju649+AyOyoR0vr92tcDdHTHMEsFroB0d354EnQpZrb KyG8Xl2mO/TmlJut6acX4vSweMjT+X0mXKfKM4qQml+2eUn0nCK1kJZd0uZ9y7PhvU41C CiDiRmNRuIOGr8z4hwSRRL+CI2rII/Vsh9vmB/kbxgjlmUEnv+b12R5D3pl0DnaMzHXje t3pTtAAXjBS6U0wuyQB8L7Jp4YVVnlSiFar+ths1zShggaZgYKzU5ash7EvMFSZGjAw== 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=sX41RizQ; arc=none smtp.remote-ip=209.85.161.42 Received: from mail-oo1-f42.google.com (mail-oo1-f42.google.com [209.85.161.42]) (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 A0A00606A327 for ; Mon, 30 Nov 2026 15:55:25 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A0A00606A327 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=sX41RizQ Received: by mail-oo1-f42.google.com with SMTP id 006d021491bc7-7190d9bdb2cso6454115eaf.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=lists.linux.dev; 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=sX41RizQUxdFpuG8XfpqIhQvOaaaTp+vduHGq7XcyxL7/ZE8m7kHxWy2VNEvw/ONH+ lk0GAqIKIDiU/w1racWMNsQFaBOrrrd6kGKeBRgqbudChBHE80EK6n6HgwMpGsm0IlNn xxrwVfGAHyBlq/GZaGSi9SeQIIyDzOl/zdJTXHGRGis8OE04ADfkaJt1PMaDomgq4nhG rilAzqYFU1pPT11+QCWl5g3WBx22/THK3t6HYCjeQPotOK9G3geJpkQNl/ZWp3y9kNIV 3S8xeIYZhPdnQmR4X375Z1HVQqaww7sJrGi1Wq84ObQy6KfQL1YY+aGC+FJCqZbavl1M 4ssA== 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=gEonU4m6jxFgOcnQoLJ47jD0+HvOBHLIABZxZ0bUe6VT0xGxtxIdx9JlYIaAW76MtJ QFT8QQSwfCIA+LzeZsurlTO660qfHdENpe43TLvpZAPxsKEfBVMaiKjy0gjk0RikhKmt /aQ1DcZhsDrqgpfUxcVh9yn9h9KkvmEwxPUY/Ef/nj3MxKUQ/c/Y3qKKL1LpEasNzT3R 4F5tRGlXHVxzx1fYmanYqfrw94ZzCRg0/FVEOn5bNS2+CGVXDYBH6f92d5L6ykJdUQvR O3yPCc8nHQj5yjZcbuAFKbTNRE0nzI4okEDV7AFEQ9h84jTh0wEzlrP1zS1sT6a/SCeB ToSQ== X-Forwarded-Encrypted: i=1; AL+hErF1YBgX6a8QEtF26Gm1bLjfw5Xd0lvWOuEF6S3WqzmISgIRQ7OLrfNrjIp6SZv3PezOMNYBdliaf25Jd7AOzg==@lists.linux.dev X-Gm-Message-State: APuU4Z8zesDIPhis4kt1u9SahVpr3eP/1PPeCd0DpDHTtRztKcWTExFJ qVEITmOHP3zJPH+TFw2MY6+G4c8DZtYVieJ85wG31HxVvtzD8bIU9/dK X-Gm-Gg: AZFdTZeF/BoLlPyk/O1S8ekt+ItBEqEH+0V9+x99SjKGiU7ytE4NIlH6W56e9DfD/If M0kiLbRDXBzS5aCHDQadIkcPEu32nHFG6PQx2Yzn1DcOBqXha+XcDvmUttSLKnFpg3fuVTMll6z TQe4I4LbE6G+Hs/LX932/5eTkVpm2PZn5U4gGGO/TrUEot3taH6PRoJa6AnRlsF4RgMoxhyLIbb 0wCBY5HRNNOzO9y18cKhF8HHh5znIDsmZtiMKw+Ikv794SBPJ9NlTiNcLy2Soja/6xGAsxYtNLy dV3EYFPnTxg74glYuP37FE5iYrQfhZi1tDxh4zq//KTMSqzO/TxMBCRMYuEjeGphVqkILtMc0Sa jKzWLK/pWVzvVZle0GOR5lhlcYDaXZngzrxZvx23A1tEOdYfubeCSt250MRyh5PrB1794zhfify gqHoY3m5CSIL3kbWqMR1d57BeTw5wXO0KJmXp9lTcCMLkMJRDIL/Ds9sgBLdpn9blWbJQROt68d i4Ocrd4GFB+v+sRDNTef3rTRVsYW3ylOEjsR+pznpJjULkARPFb5vQdQt9bqXDwYA0dyaruyQzt cu7OLqbveK1qCd3G13QFTuESpyXk9/1IUwr7cjaOam1vBn5QwF8pg3Dzoljg 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-rt-devel@lists.linux.dev 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 >