From nobody Sun Feb 8 18:18:34 2026 Received: from mail-qk1-f172.google.com (mail-qk1-f172.google.com [209.85.222.172]) (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 DE18D296BD3 for ; Mon, 19 Jan 2026 16:11:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768839092; cv=none; b=NHtV8AguQGhJMjXuLWCN3H3f0c5f1uJzliFMUM42X7uU+eupOTOyNgxkOH9zPsj4MdY1HK8sElgvdmlnnYf6VWUO5pVFsnhZ93o9HUrRLMcRe0nt297WHdW2sLf8y78SqwdQ7xyMWhpWxBDayRa2NqwSsEz+6gAeoujfqtA+Axo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768839092; c=relaxed/simple; bh=LjPrxhlXashuskkdS1QinyAYIMs9j65wkVv5w4j2L5o=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To:Cc; b=EGJuDoWMljc6nEmZNcV8jYAeKl5CyNjpygMNJWvEHoDf+ve1+jv4abaKdDKAeE+4DbQQLs8Kcdd202JDGLTtZqKRflSqTQlpBDtxPDG/xwn6FCb8fsZ8cN/s9h6w/hoRnV36+Q1T9z3cKJCzLTMn4g5+bySxCh+aOIjAwQ63Q0o= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=G5Kr307Z; arc=none smtp.client-ip=209.85.222.172 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.b="G5Kr307Z" Received: by mail-qk1-f172.google.com with SMTP id af79cd13be357-8c2c36c10dbso515486285a.2 for ; Mon, 19 Jan 2026 08:11:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1768839089; x=1769443889; darn=vger.kernel.org; h=cc:to:message-id:content-transfer-encoding:mime-version:subject :date:from:from:to:cc:subject:date:message-id:reply-to; bh=WjJfvLBGT103l6zGUDrj7QF6QyCPGm8jtIUaKkXRKrc=; b=G5Kr307ZszSjrqEClIqtEM5nko+kCRMJL/vZyPxv4jQYL28thVG3F2mWkA9W6qrm5S tWTlcwhNnFkyZJ2FD3ilCGHg8M9hBmCKN3o2Mcp7Z1vqY8YNpGxYc3ydnMaYNCqh/bWT fh4AS/GqFnenFgYmHWNkW+Ems1p4T8WvcB2fL0Ld9hFNOAbteIctEdTYQXdTnJFzZQUT wMNGuJuCFaZ+CUfxyVSuaFW4j2+AwSnr0+KS6Rm5jlTSW4y7vXDu9H/lNxVw4pHxxyxQ JFjLdmLix952znSSPaKRbNrGaUuXqbJ2EoIL3oDaHp9rXaY/NzJxvdC1zNa8HFmcfPy8 Jp/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768839089; x=1769443889; h=cc:to:message-id:content-transfer-encoding:mime-version:subject :date:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=WjJfvLBGT103l6zGUDrj7QF6QyCPGm8jtIUaKkXRKrc=; b=Qg24M1DV1PdjDyXvSU1TEBBm3SRcYHnrpmH/w63uFxT7C7TpMznqAIkdFaJmznYHyZ kkOeIXjFKmJ4Gl44qZtYebTNyR1XpSNx1JHqh8f0DYfsSIqx7DEJLfRVrm5SPZespYV+ Y8yn3Kn/IbEu4M80nj/DeujfGxXHKXKQEWjZHwBXRvCVtggSI/tuGJIebSvn6lpsmKUL UC+lcBR61+nASb8shuJaFTSIyYIwkSuVQ+uhVH12XWYTq2oJmygFbf6+f1+GXjHHpMkG utAulJgHtBIJ03lOS6u2bU3TkbzhlA5aLQZGzhJxmlrvFHIgwbRsfe+cuUjO20IlJDm2 MrDA== X-Forwarded-Encrypted: i=1; AJvYcCUSGHuUy+APVZZhBpinSFC8ODKAOoUzvgv5i7CJY8IQfrPHUAnrQbG+sbz0HEsvciHgPlN9+sg+4Q3EVf4=@vger.kernel.org X-Gm-Message-State: AOJu0YwaP20ZXmUBiKGHVKJ4wwtBq+bKMEOBb0TsuLtL9P98ggkThxva 7j0ZJVqsAf6Nj81ye1+tvBVoCRcG5s8wEcuN6GSF+VfO/FnnFwvWOCDNmwPAMSRItYU= X-Gm-Gg: AY/fxX6O7ZKzvwkjr8nKQMg/riSxDchni00zOfw3a4IRtl649buAowzUmB+Ctq8OADf 4pGpG7XxOF0nqt1Tk9VUqbiL/A+qdshkLUSbRpEhU6zx4ZKgaZ3n6wGbuLLQznS0f208chrlT7r 0SN5ra0LczCdlhht3DJQT2X5pUo6kUBOMBJ2ZY+mi9tMIoGVFcoxGBswhhlEchSDsZUMUWVgu9M Ysmp/4jTeXPmpFAKCSkJAYpIpWYeCAky56ehEJcj1fq3wR9OXh7q2aFwlVJu2i5/xxF6Sxs/BRQ kVIxQcCJ3G8j88N8befTgszS0VeoGvyM4yKDYK/bLXxfOs52Elle91jWMaztPah8gMk0+7yXjCE YWeOtR+x1EmfQ07700nhPPb0LshyzLCaSJhX7phzAxdDgTv01WwDO0AZ8nK81WjiX5JHbohQ2SM 5PdlIuN88NaqIjoXMEY039LX4swGpH768zaMkMTMdznfsfoYeD X-Received: by 2002:a05:620a:284b:b0:8b2:dfb3:dc2a with SMTP id af79cd13be357-8c6a67a2256mr1560628385a.75.1768839089399; Mon, 19 Jan 2026 08:11:29 -0800 (PST) Received: from [127.0.0.1] ([101.32.222.185]) by smtp.gmail.com with ESMTPSA id af79cd13be357-8c6a72985a8sm815336085a.54.2026.01.19.08.11.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Jan 2026 08:11:28 -0800 (PST) From: Kairui Song Date: Tue, 20 Jan 2026 00:11:21 +0800 Subject: [PATCH v3] mm/shmem, swap: fix race of truncate and swap entry split 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="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260120-shmem-swap-fix-v3-1-3d33ebfbc057@tencent.com> X-B4-Tracking: v=1; b=H4sIAAAAAAAC/2XMSw7CIBSF4a0Yxl7DBVrFkfswDipcLIPSBhrUN N27tImJj+F/kvNNLFH0lNhxM7FI2SffhxJyu2GmbcKNwNvSTHBRc0SE1HbUQbo3Azj/gIPlJHi D6lpZVk5DpDKv4PlSuvVp7ONz9TMu65sSv1RGQOBOqr1TrtaVOI0UDIVxZ/qOLVgWn4D+A8QCS GW0qp2VWn4D8zy/AKtrZZzvAAAA X-Change-ID: 20260111-shmem-swap-fix-8d0e20a14b5d To: linux-mm@kvack.org Cc: Hugh Dickins , Baolin Wang , Andrew Morton , Kemeng Shi , Nhat Pham , Chris Li , Kemeng Shi , Nhat Pham , Baoquan He , Barry Song , linux-kernel@vger.kernel.org, Kairui Song , stable@vger.kernel.org X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1768839084; l=4919; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=ysQSLUoYhwKPncamfXxkacphx1e+w1AheyJG0ahsEz8=; b=dpOk1lOYoQzKSE19Lc0fcCL6U4W81gszY7m55ZxxOMel9fsuqr7+PYmLX5KDsSrL9y1B9a+Wp osakKrgfLb1B/t56TuAvbwlAv2kxar3ZXjv9QbkK5p9baeoQUoDoXWA X-Developer-Key: i=kasong@tencent.com; a=ed25519; pk=kCdoBuwrYph+KrkJnrr7Sm1pwwhGDdZKcKrqiK8Y1mI= From: Kairui Song The helper for shmem swap freeing is not handling the order of swap entries correctly. It uses xa_cmpxchg_irq to erase the swap entry, but it gets the entry order before that using xa_get_order without lock protection, and it may get an outdated order value if the entry is split or changed in other ways after the xa_get_order and before the xa_cmpxchg_irq. And besides, the order could grow and be larger than expected, and cause truncation to erase data beyond the end border. For example, if the target entry and following entries are swapped in or freed, then a large folio was added in place and swapped out, using the same entry, the xa_cmpxchg_irq will still succeed, it's very unlikely to happen though. To fix that, open code the Xarray cmpxchg and put the order retrieval and value checking in the same critical section. Also, ensure the order won't exceed the end border, skip it if the entry goes across the border. Skipping large swap entries crosses the end border is safe here. Shmem truncate iterates the range twice, in the first iteration, find_lock_entries already filtered such entries, and shmem will swapin the entries that cross the end border and partially truncate the folio (split the folio or at least zero part of it). So in the second loop here, if we see a swap entry that crosses the end order, it must at least have its content erased already. I observed random swapoff hangs and kernel panics when stress testing ZSWAP with shmem. After applying this patch, all problems are gone. Fixes: 809bc86517cc ("mm: shmem: support large folio swap out") Cc: stable@vger.kernel.org Signed-off-by: Kairui Song Acked-by: Chris Li Reported-by: Chris Mason Reviewed-by: Baolin Wang Reviewed-by: Nhat Pham --- Changes in v3: - Rebased on top of mainline. - Fix nr_pages calculation [ Baolin Wang ] - Link to v2: https://lore.kernel.org/r/20260119-shmem-swap-fix-v2-1-034c94= 6fd393@tencent.com Changes in v2: - Fix a potential retry loop issue and improvement to code style thanks to Baoling Wang. I didn't split the change into two patches because a separate patch doesn't stand well as a fix. - Link to v1: https://lore.kernel.org/r/20260112-shmem-swap-fix-v1-1-0f347f= 4f6952@tencent.com --- mm/shmem.c | 45 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index ec6c01378e9d..6c3485d24d66 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -962,17 +962,29 @@ static void shmem_delete_from_page_cache(struct folio= *folio, void *radswap) * being freed). */ static long shmem_free_swap(struct address_space *mapping, - pgoff_t index, void *radswap) + pgoff_t index, pgoff_t end, void *radswap) { - int order =3D xa_get_order(&mapping->i_pages, index); - void *old; + XA_STATE(xas, &mapping->i_pages, index); + unsigned int nr_pages =3D 0; + pgoff_t base; + void *entry; =20 - old =3D xa_cmpxchg_irq(&mapping->i_pages, index, radswap, NULL, 0); - if (old !=3D radswap) - return 0; - free_swap_and_cache_nr(radix_to_swp_entry(radswap), 1 << order); + xas_lock_irq(&xas); + entry =3D xas_load(&xas); + if (entry =3D=3D radswap) { + nr_pages =3D 1 << xas_get_order(&xas); + base =3D round_down(xas.xa_index, nr_pages); + if (base < index || base + nr_pages - 1 > end) + nr_pages =3D 0; + else + xas_store(&xas, NULL); + } + xas_unlock_irq(&xas); + + if (nr_pages) + free_swap_and_cache_nr(radix_to_swp_entry(radswap), nr_pages); =20 - return 1 << order; + return nr_pages; } =20 /* @@ -1124,8 +1136,8 @@ static void shmem_undo_range(struct inode *inode, lof= f_t lstart, uoff_t lend, if (xa_is_value(folio)) { if (unfalloc) continue; - nr_swaps_freed +=3D shmem_free_swap(mapping, - indices[i], folio); + nr_swaps_freed +=3D shmem_free_swap(mapping, indices[i], + end - 1, folio); continue; } =20 @@ -1191,12 +1203,23 @@ static void shmem_undo_range(struct inode *inode, l= off_t lstart, uoff_t lend, folio =3D fbatch.folios[i]; =20 if (xa_is_value(folio)) { + int order; long swaps_freed; =20 if (unfalloc) continue; - swaps_freed =3D shmem_free_swap(mapping, indices[i], folio); + swaps_freed =3D shmem_free_swap(mapping, indices[i], + end - 1, folio); if (!swaps_freed) { + /* + * If found a large swap entry cross the end border, + * skip it as the truncate_inode_partial_folio above + * should have at least zerod its content once. + */ + order =3D shmem_confirm_swap(mapping, indices[i], + radix_to_swp_entry(folio)); + if (order > 0 && indices[i] + (1 << order) > end) + continue; /* Swap was replaced by page: retry */ index =3D indices[i]; break; --- base-commit: 24d479d26b25bce5faea3ddd9fa8f3a6c3129ea7 change-id: 20260111-shmem-swap-fix-8d0e20a14b5d Best regards, --=20 Kairui Song