From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-yx1-f53.google.com (mail-yx1-f53.google.com [74.125.224.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 070213B3895 for ; Tue, 28 Jul 2026 05:24:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.224.53 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785216274; cv=none; b=VxaWpmbHsbQlxdF4ZOUhY3QUewCvRbaamguFz9mmWvmFhTvW5RrmoSHHmj1uBiMfEsMPFLGIKlbklaeWh+Z7YzmVKuXkTN6m4wnIZJtshBqpr+n08uXUQSFE3wHFPPs/ewCjtxIRxwr3UarmScFDDkvkQiXHffwWOmln6gy6Aps= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785216274; c=relaxed/simple; bh=0TCr9DTmYv1uI8WVYyHvYyk0lI0kGpXMf8BLGRUvttk=; h=Date:From:To:cc:Subject:Message-ID:MIME-Version:Content-Type; b=g26HEshA1TCUFWDWrryo2jfOvPISq16eRNRYh6L3oKcNm6Ligc2aPWyENJecvqvDCoZ2i2ZIK+iTl4gAY3EcsN8TvFKvhsotveQSEieCVhHMNDQ/sTHdOwVeaXwjJmuqLtuFagS5iblF+pdkKUmei/ArEfr5DzWMLOhVRJjaNXE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Jrr5R6Tv; arc=none smtp.client-ip=74.125.224.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Jrr5R6Tv" Received: by mail-yx1-f53.google.com with SMTP id 956f58d0204a3-6681e7911b0so4753808d50.0 for ; Mon, 27 Jul 2026 22:24:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1785216269; x=1785821069; darn=vger.kernel.org; h=content-type:mime-version:message-id:subject:cc:to:from:date:from :to:cc:subject:date:message-id:reply-to:content-type; bh=+dP2n6X89PfagInpqwKnt0atKqO22OfQKc1DqTtxeCs=; b=Jrr5R6Tv+E8Dee+VWfh018cTPyOvLtBE7tRTh3KESitfrYHzQ8YFXRpE24FMgl99oh hHiFBu3NrOuRqVWzi3hzOE3nABaRrxZ2A0VbrJeRqG1RFo3FASp7RPWq7so3NOLgPyqD 6nCqS/0fFHknGC/p/gVv45T5sGhCGlqFB6kqC/+gjv1OWV+Z2SNjRXOX6wraua4QCTlp 0tpTXCBzZn9MS0yC3i61sqKBDDcrAEtfsYVCqjtNWJmxm1mXdEeAmnUyS0YmWY0IPrXp kilDKZyfaf2D+ZclIeIGEaetuJ/aXioPOyotqH0UEaA6Ybc7eiazp7SmD5dc587Kz9CJ Icig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785216269; x=1785821069; h=content-type:mime-version: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=+dP2n6X89PfagInpqwKnt0atKqO22OfQKc1DqTtxeCs=; b=fBD1gIZQ/Ux8BHDy6LnxSIj0iKE0AdJLaBPvyAzprQq/dBNbwAXG0QofOVC6pOF+Vo mCR6obisRBXOXZg89I37DygbwjLSfRPH/cbREzaQFiOgNcf/98bxamgCISadk+3NVqE/ qLZ+CBnrUSFiATGrMFQkN6AFHwffycaXZKvr9SiEBXIGdPpWqpuTDZI6mlVXLGYfdSsx Xtmxpl+1dztGW28vvklrVb7cOFBXcleujSSbhscACBlVNtU7twM0JgnvCEsl8lQ5U69Y WCI35agVLVAFVUg7llPLLuG1SmGYhNbZYG/cmJ7sSLj683909Qt/KN2mwYBH//RSfU4Q QKPg== X-Forwarded-Encrypted: i=1; AHgh+RrxzHkIY8IcM3xAeRDtg77waeKv7xFHyMkkNIzs//wFhIhW/bAP6aNDG/CtCYF4kVoPefKMJoplx3q0fw8=@vger.kernel.org X-Gm-Message-State: AOJu0YzaMoSVEgKVAIohY+TkY/uiyhX1LA2VmSYi0X9tIyw/7JSls2JL RPQI1WID6F0yYuNqW6Ph6VrH+PtD3muGchM2lNk+I3HByEr5Or9MOVzLVtXHExf1Gw== X-Gm-Gg: AR+sD11BDZ7IFB84X1I9SQHfx4rEM9VeMPiE5gwTItSnqyJ80NrEFXJMxsfbAeQ7J8g K2wDHy4Qemyd+2T6bmFhRTHtnuf96Yj7UTbabBQdYin6OXZ9rZTHazIQCHZWrOlSBi2gau6Vn5R EltPUtDAeS0Hl6BG6GxoBqAJEH9yJ4JG/Tl9UVI0P1jOdltheJBC3nzw1bOcX5hjJenMz8xJZKt w5xxPAFOai0vuudhOsviWLCNVGlICOE7EoPtLdY7/XgJFlSIoMPQCuaKMTOAMlgBc3xVH/TTHfN fg27TzEZpFoYGFhGuByu5YtYztql42XfWlCvDg3q+miAs89VJ9LfpVJ9Cz1t/b6/kizdnPvD/QF 8gjPoNLaKwj7etJYWAxSlDQccaCrgcgaVes7Hfnkq4AUOfTR1VxJ7bDJALbWJ8KQKOO3PpEAqIs NKf3WqOYWyxDOVPkib1vpTqtRZgHYLa5Vxt4RrCMK9Gk6PXgDL0HvWPynJCkMrLA== X-Received: by 2002:a05:690e:4088:b0:662:f28b:7c63 with SMTP id 956f58d0204a3-669057a7895mr344755d50.63.1785216268371; Mon, 27 Jul 2026 22:24:28 -0700 (PDT) Received: from darker.attlocal.net (172-10-233-147.lightspeed.sntcca.sbcglobal.net. [172.10.233.147]) by smtp.gmail.com with ESMTPSA id 956f58d0204a3-668c6d2f9e0sm4496668d50.2.2026.07.27.22.24.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Jul 2026 22:24:27 -0700 (PDT) Date: Mon, 27 Jul 2026 22:24:14 -0700 (PDT) From: Hugh Dickins To: Andrew Morton cc: Zi Yan , Kairui Song , Matthew Wilcox , Jan Kara , David Hildenbrand , Kiryl Shutsemau , Chris Arges , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH] mm/filemap: __filemap_add_folio() restore index before retrying Message-ID: <562fbfa6-dd6d-0b6a-2461-ed2ff1173bc8@google.com> 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 In __filemap_add_folio()'s split-a-conflict loop, xas_set_order() is applied repeatedly: each application modifies xas.xa_index, rounding it down according to the split_order attempted at that stage: and if all goes as intended, it eventually (or immediately) converges on an xas_try_split() to the required folio_order, with xas.xa_index now the same as index: then xas_store() puts the new folio into the xarray there. But if a new node was needed, and GFP_NOWAIT allocation did not get one, the lock is dropped, xas_nomem() used to allocate, and sequence retried. If (that part of) the xarray is unchanged when the lock is reacquired, no problem. But what if the conflict was meanwhile resolved by another thread (perhaps even doing the same thing, inserting a folio at that same index)? Isn't there a danger of now putting our folio into the xarray at an intermediate rounded-down index? With !folio_contains() bug to follow, when CONFIG_DEBUG_VM=y is checking for that. Fix this with an xas_set_order() to restore the original xas.xa_index at the bottom of the loop, so the retry does a full re-evaluation after reacquiring the lock, and cannot reach xas_store() with the wrong index. Production was suffering from rare SIGILLs and SIGSEGVs, executable text found a page away from where it belonged, !folio_contains() bug hit when debug enabled: symptoms not seen since this patch went in. Fixes: 200a89c159a7 ("mm/filemap: use xas_try_split() in __filemap_add_folio()") Signed-off-by: Hugh Dickins Cc: stable@vger.kernel.org --- mm/filemap.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/mm/filemap.c b/mm/filemap.c index 58eb9d240643..d721986d5f46 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -931,6 +931,12 @@ noinline int __filemap_add_folio(struct address_space *mapping, if (!xas_nomem(&xas, gfp)) break; + + /* + * Lock has been dropped: start again with the original index + * and order (but now with the memory reserved by xas_nomem()). + */ + xas_set_order(&xas, index, forder); } if (xas_error(&xas))