From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795947271; b=Ku1AwMf5+x1tCSfzH9lC/hwiWrsu4NFeTugZTuYov6ELB3oZqFDpUdluFuCd7bUmw62P amEJ8uKM7fsHxHo4Y6FaQzZQF5FqAMVnFck8p5QrunUnNJQwqIQVVgGvMS6IKwf19VWvv qMDnRwYbyD1nGkWecMSjJbYeG4aL5ZGxJbfn7WzkzzuxlHp6xCPfHqfHEU8M6lweMskb5 6rLqp6jyFDSct5xEl12y/b5VsMctrX7AaHsRTLpHl852GiBnIdm/mmImP3t26czy+KoGA nPizlA0FCGWCUHhoS2jbIrOew8D6oN+afl7y8vzlDNwGbjT6SYMODXvMWBa/rkdSzFQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795947271; h=Received:DKIM-Signature:From:Date:Subject:MIME-Version:Content-Type: Content-Transfer-Encoding:Message-Id:References:In-Reply-To:To:Cc: X-Mailer:X-Developer-Signature:X-Developer-Key; bh=t0uki+pj+GtYc1TGW8FgFtjZzhjBfl96ESE1J9zMFV8=; b=ZRRqY6nOwh9J4lRaxjCfLXFxVahlwO6h5kRoFYzn+CgXN6k5dcq3F0HzYsLr0JK/i8Xa eXthLrTWiPtewRuDWJEkU5K0cQR86q9+GdPS7JHqHOWcm3kdM5oxnmAPdQhzNCPFnEEjY EX1bKD4qBxWteAhbCEf1X/9TOCnM5rJC6WsGaK6v/ZWJ5MmAJO/63203Cm+OiNOfqlpfB YBwzRAL5lRZ3ikIIoI/pj8P+/LsftmK0o7zDSmqJCHbMIMJF6J+zSzHpLJsxSXFe4pIU3 OwTNtyokH5YCbV7IWl+ZS4ACyzN/XAGa2snoiDxV4k4FpWMymzNpC+SRJoJwYG64nSQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b="P/uhLPYd"; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 33C80606A31C; Sun, 29 Nov 2026 10:14:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=P/uhLPYd Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2ABA51F000C1; Sun, 29 Nov 2026 10:14:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795947270; bh=t0uki+pj+GtYc1TGW8FgFtjZzhjBfl96ESE1J9zMFV8=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=P/uhLPYdArtmfIQ/EDCidv+rqI2sN3WJAWbJZWB+GiiahZJuaEIVM3SSYaSN9LtUz hbgs5veHKjCC+JTwuha89uawNhPfRd0T5OUZmicwiHVI+wtVWUkwIYa4Rjew8nXMhu ZRzHPV2OCxqMRKbmwhLAz3yju/eMepR8F2KSRBzdBlEm65I4U6maT/tCDAUyXvEqRQ TC27qkGY4KCJg0GEQPbz2AqZ28I8o1PvjxXOUWfEU+Bh8X/QcaTi6HkzrobXf+F47y UpHeLE0Ezx43w8yBsYrRhgn3Yzo8BwPzb/vM0egpfHQC7lJ52b1bLrPyfXCUgJOL0y SPxzBsNT8CbEQ== From: "David Hildenbrand (Arm)" Date: Sun, 29 Nov 2026 11:06:55 +0100 Subject: [PATCH 24/32] mm/mempolicy: reject an empty set of allowed nodes X-Mailing-List: linux-kernel@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="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261129-fixes_core_3-v1-24-f66094116801@kernel.org> References: <20261129-fixes_core_3-v1-0-f66094116801@kernel.org> In-Reply-To: <20261129-fixes_core_3-v1-0-f66094116801@kernel.org> To: 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 , Joshua Hahn , 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 Cc: 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 , "David Hildenbrand (Arm)" , Chris Mason , stable@vger.kernel.org X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2854; i=david@kernel.org; h=from:subject:message-id; bh=4bnDqxAHNwg1glIouRze3Aer2wfME55p8a04BHM5+iE=; b=owGbwMvMwCF2/OVpCdfQ3ZWMp9WSGLK5f+r+P6Fgc1OgY/Ks0PJdhYsq7F4+L1LLlFr0c9FEe /5lMkxeHaUsDGIcDLJiiixBwfcNXr5cV1vtxscMM4eVCWQIAxenAExEuZThn431tW87PFNZ5+le WrzigcDrfeZXNap9NO7lCIjuO6D1zp/hf0jBrllsxw/E+F6SXZCdwH1S4rFozm9ZLjetR6zSfRY m3AA= X-Developer-Key: i=david@kernel.org; a=openpgp; fpr=1BD9CAAD735C4C3A460DFCCA4DDE10F700FF835A From: Chris Mason Installing a mempolicy with MPOL_F_RELATIVE_NODES can trigger a division by zero when racing with memory offlining. I managed to reproduce this in a small VM with excessive set_mempolicy() calls while offlining memory: Oops: divide error: 0000 [#1] SMP RIP: 0010:bitmap_fold+0x43/0x60 Call Trace: ? mpol_relative_nodemask+0x26/0x40 ? mpol_set_nodemask+0x43/0x90 ? do_set_mempolicy+0x5e/0xe0 ? __x64_sys_set_mempolicy+0x6c/0x80 mpol_set_nodemask() intersects current->mems_allowed with N_MEMORY, and mpol_relative_nodemask() folds the user's nodemask modulo the weight of the result. If memory offlining just cleared the task's only allowed node in N_MEMORY, but cpuset did not update mems_allowed yet, the intersection is empty and nodes_fold() divides by zero. do_mbind() and mpol_shared_policy_init() call mpol_set_nodemask() as well. Let's fix it by failing with -EINVAL early if there is no allowed node with memory, just like ->create() would later fail for an empty nodemask. The same division by zero was originally reported by syzkaller for mpol_rebind_mm() [1]. The discussion of Yury's fix attempt [2] also identified this path in mpol_set_nodemask() [3], but only the rebind path got fixed, by commit b983c5642638 ("cgroup/cpuset: rebind mm mempolicy to effective_mems, not mems_allowed"). This has low urgency, as it is extremely unlikely to hit in practice. Fixes: 8fe23e057172 ("mm: clear node in N_HIGH_MEMORY and stop kswapd when all memory is offlined") Cc: stable@vger.kernel.org Reported-by: David Hildenbrand (Arm) Closes: https://lore.kernel.org/all/fe33c767-ea11-43e2-8732-f752c9c1205c@kernel.org/ [3] Link: https://lore.kernel.org/all/CA+0ovCgxbZkXa+OU8w3s84R3KNPNxxRfmsNR-udh+afQBbGNmw@mail.gmail.com/ [1] Link: https://lore.kernel.org/all/20260528190337.878027-1-ynorov@nvidia.com/ [2] Signed-off-by: Chris Mason [ david: update description and comment; add reproduction details ] Signed-off-by: David Hildenbrand (Arm) --- mm/mempolicy.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/mm/mempolicy.c b/mm/mempolicy.c index 1f68a463b3bc7..746fd0a137515 100644 --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -491,9 +491,13 @@ static int mpol_set_nodemask(struct mempolicy *pol, if (!pol || pol->mode == MPOL_LOCAL) return 0; - /* Check N_MEMORY */ - nodes_and(nsc->mask1, - cpuset_current_mems_allowed, node_states[N_MEMORY]); + /* + * If we just offlined the last memory on a node, it might be gone from + * N_MEMORY before cpuset updates mems_allowed. Just fail in that case. + */ + if (!nodes_and(nsc->mask1, + cpuset_current_mems_allowed, node_states[N_MEMORY])) + return -EINVAL; VM_BUG_ON(!nodes); -- 2.56.1