Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (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 05547246783; Sat, 12 Sep 2026 13:23:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789219417; cv=none; b=cMLVxFXEm2lrueXyM3MHYWnT8L4k+YpIh+0NDwY/+oaa0GlYcXthZ/AEyeDoXuvTuWzZut8ND9Hl1gz/4RMUaJaPUm+l9ohNhCxkbVuwSKGmen/LMFzPeLD7nijUHA5SJrgVw2rQZ8eaRIidi7Vb1fcQDm0yobu5pR5dJ1Ab/t0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789219417; c=relaxed/simple; bh=9xTjomEKHCi7JR4K6zeO5oZIKa+ED4rh9WmSbTuu3TY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ECFkI2+HT34epnzmxyk0NlxDqaMxN1yVoE8a1C91gNKXZ+VV8tQQVHya1hq6shq7cwByn16TEEQgJO8PfOW6nHt9YKkUhJG0eV++tTKB8HPHdg+rzpVO/2p+ognr/VgeDZFc+ofTCOXPDN8XHcHn+U3CElr45uR3Jv56on8aoJw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=NTrunptr; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="NTrunptr" Received: by smtp.kernel.org (Postfix) with ESMTPSA id B32681F000FF; Sat, 12 Sep 2026 13:23:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1789219415; bh=AQRejnC5cvrQL05kiHIHb+Jis/zE53lHt4jEdseueCI=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=NTrunptrlvGOEorJKv4H6Vxq/Ble1uJe1yEGxOqjYbvKgyvviPioPo/n4mcuVsaJN NEKu+vhe3iloTpE6zTf+1J+1VsZySe0ZHvqUejEiH5HWEckPfZRDFX3W0bmuOQT/J8 X8w6CCuFtq+rmkf/mc+A76Xhls96ZFuPwPuuDQDE= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, "Liam R. Howlett (Oracle)" , Chris Mason , Chuck Lever , Boqun Feng , Breno Leitao , Ingo Molnar , Jason Gunthorpe , Joe Perches , Peter Zijlstra , Rik van Riel , Waiman Long , Will Deacon , Andrew Morton , Sasha Levin Subject: [PATCH 6.12 1293/1376] maple_tree: catch race in mas_alloc_cyclic() Date: Sat, 12 Sep 2026 09:01:59 +0200 Message-ID: <20260912065636.353407811@linuxfoundation.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260912065607.535295758@linuxfoundation.org> References: <20260912065607.535295758@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: patches@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.12-stable review patch. If anyone has any objections, please let me know. ------------------ From: Liam R. Howlett (Oracle) [ Upstream commit 35f1342e5b893a740eff2ef9ab337bfaa63ab76d ] If mas_alloc_cyclic() is called during a low memory situation, it is possible the lock may be dropped so reclaim can occur. There is a window where some other task may allocate the same id and cause the mas_insert() to fail with -EEXIST. In this scenario the function will return -EEXIST, which is not expected. Modifying the retry on mas_nomem() to re-search for a slot means that any race with other writes will not matter as the lock will be held between finding the index and writing the index. Moving the flag logic avoids cases where the flag is modified on drop lock/reacquire or when the write fails after clearing the flag. No existing users are exposed to this issue. Link: https://lore.kernel.org/20260821192627.4085470-13-liam@infradead.org Fixes: 9b6713cc7522 ("maple_tree: Add mtree_alloc_cyclic()") Signed-off-by: Liam R. Howlett (Oracle) Reported-by: Chris Mason Reviewed-by: Chuck Lever Cc: Boqun Feng Cc: Breno Leitao Cc: Ingo Molnar Cc: Jason Gunthorpe Cc: Joe Perches Cc: Peter Zijlstra Cc: Rik van Riel Cc: Waiman Long Cc: Will Deacon Signed-off-by: Andrew Morton Signed-off-by: Sasha Levin --- lib/maple_tree.c | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/lib/maple_tree.c b/lib/maple_tree.c index e4b68b19ae9ad..c7e3f91fd91c6 100644 --- a/lib/maple_tree.c +++ b/lib/maple_tree.c @@ -4348,35 +4348,40 @@ int mas_alloc_cyclic(struct ma_state *mas, unsigned long *startp, void *entry, unsigned long range_lo, unsigned long range_hi, unsigned long *next, gfp_t gfp) { - unsigned long min = range_lo; - int ret = 0; - - range_lo = max(min, *next); - ret = mas_empty_area(mas, range_lo, range_hi, 1); - if ((mas->tree->ma_flags & MT_FLAGS_ALLOC_WRAPPED) && ret == 0) { - mas->tree->ma_flags &= ~MT_FLAGS_ALLOC_WRAPPED; - ret = 1; - } - if (ret < 0 && range_lo > min) { - mas_reset(mas); - ret = mas_empty_area(mas, min, range_hi, 1); - if (ret == 0) - ret = 1; - } - if (ret < 0) - return ret; + int ret; + unsigned long min; + min = range_lo; do { + range_lo = max(min, *next); + ret = mas_empty_area(mas, range_lo, range_hi, 1); + if (ret < 0 && range_lo > min) { + mas_reset(mas); + ret = mas_empty_area(mas, min, range_hi, 1); + if (ret == 0) + ret = 1; + } + if (ret < 0) + goto out; + mas_insert(mas, entry); } while (mas_nomem(mas, gfp)); - if (mas_is_err(mas)) - return xa_err(mas->node); + if (mas_is_err(mas)) { + ret = xa_err(mas->node); + goto out; + } + + if ((mas->tree->ma_flags & MT_FLAGS_ALLOC_WRAPPED) && ret == 0) { + mas->tree->ma_flags &= ~MT_FLAGS_ALLOC_WRAPPED; + ret = 1; + } *startp = mas->index; *next = *startp + 1; if (*next == 0) mas->tree->ma_flags |= MT_FLAGS_ALLOC_WRAPPED; +out: mas_destroy(mas); return ret; } -- 2.53.0