From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id BCC6EC021B6 for ; Tue, 25 Feb 2025 01:08:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3EEDD280004; Mon, 24 Feb 2025 20:08:06 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 39F55280002; Mon, 24 Feb 2025 20:08:06 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2B469280004; Mon, 24 Feb 2025 20:08:06 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 0F3EB280002 for ; Mon, 24 Feb 2025 20:08:06 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id BDF57B1479 for ; Tue, 25 Feb 2025 01:08:05 +0000 (UTC) X-FDA: 83156680530.17.32E64A9 Received: from out30-98.freemail.mail.aliyun.com (out30-98.freemail.mail.aliyun.com [115.124.30.98]) by imf30.hostedemail.com (Postfix) with ESMTP id A4C3C8000A for ; Tue, 25 Feb 2025 01:08:02 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=gbw6aLh2; spf=pass (imf30.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.98 as permitted sender) smtp.mailfrom=baolin.wang@linux.alibaba.com; dmarc=pass (policy=none) header.from=linux.alibaba.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1740445684; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=2wtspKnVg9vRGSfU0IMP9tf/T/MZmprrqJBQEvw5r2s=; b=ltlgHFOb5bKPPpgsgXw+QYoVdAGyoSMAQlvJ3lTsDn8EqkpHGGIZ8IptZ755qLWvn3EvV/ OD45Jpif/Tibdiu9oZlqmV77Jqe21XUEoSdg90WooACl0Wkrwr9+lgl+dfQEVdlwO/Hac6 zZdZxefYAECcn/Rmt+/FoRk+iy/JOSo= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=gbw6aLh2; spf=pass (imf30.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.98 as permitted sender) smtp.mailfrom=baolin.wang@linux.alibaba.com; dmarc=pass (policy=none) header.from=linux.alibaba.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1740445684; a=rsa-sha256; cv=none; b=4HD7QgxsVqcFPD2JvuFAyRG1lcNM1GD86KwyRHvTWqXtGdhkih0Isc5RLc1ecnZms5DSwl 6xaEjXOlv/WjLPWF0N4y3tJPne65O8XeGKw4dePI+1l2xcPZFbgFqm2uEm8K/tC4skWtQa 5r6/ZGsaw4kW8ojH0VIiTV0A6ZGlP/Q= DKIM-Signature:v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1740445679; h=Message-ID:Date:MIME-Version:Subject:To:From:Content-Type; bh=2wtspKnVg9vRGSfU0IMP9tf/T/MZmprrqJBQEvw5r2s=; b=gbw6aLh2J/RLqODuf2HWULB0IFsoFNnntmZ9osEJVtMpyimDRfRl4aMx+UVOUTnbGvEqEnESi9/nlioW78PtXFbOvdVNrNsTF989Hd6WqMb0UoxGtQ++3S18BccuybPbmnZ6ET6MH7Y79IhTW5h4diYWU/5x/B3VCtu3ldCZUpI= Received: from 30.74.144.116(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0WQCd.v6_1740445677 cluster:ay36) by smtp.aliyun-inc.com; Tue, 25 Feb 2025 09:07:58 +0800 Message-ID: Date: Tue, 25 Feb 2025 09:07:57 +0800 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] mm: shmem: fix potential data corruption during shmem swapin To: Kairui Song Cc: akpm@linux-foundation.org, alex_y_xu@yahoo.ca, baohua@kernel.org, da.gomez@samsung.com, david@redhat.com, hughd@google.com, ioworker0@gmail.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, ryan.roberts@arm.com, wangkefeng.wang@huawei.com, willy@infradead.org, ziy@nvidia.com References: <731904cf-d862-4c0e-ae5b-26444faff253@linux.alibaba.com> <53e610af72302667475821e5b3c84c382da4efbc.1740386576.git.baolin.wang@linux.alibaba.com> From: Baolin Wang In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Rspam-User: X-Stat-Signature: xde9nbc3qzb35e58fc55f3tzg166rf4u X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: A4C3C8000A X-HE-Tag: 1740445682-503450 X-HE-Meta: U2FsdGVkX199In16ED4lc2T0yUA/JNeVq5ra+SlbIEpYixx3EkEiEGoJFlLLPAViwknoqQ385p34UUPcRvFzlu2Y7u7ZmVmyTh4q12GuCsLLecapi9dFFJsy8RW/4qnU2YHxNKGl6QtSSg8zJmZEgGPA6y68TaSc+78urLPGlRUhw1aK6hLHO3504KGLfThMuF2LeXkZTjFvWOjpCloyRl9gyrcGCTSKOi0ljZmwBD7uiIvBJQwQkvo9FmulWAbQlSKdOQr2wtAsFKgIjZ2mCmP+KHNqNxTnOB/0jQ4/Oc6ZpNy3O0ysx/mUpJbZFCqgw2OjBrQ8VS0FyjyOUl35/Uysx/poHwFTxt+JVWdmYvXifM0npceYNCjCFBr28TRfA4a7M2H8vRC0AGtsI903+7M0k01WZp+j1h9KjtbGakwGoDHHH4/i3+8zPIS70HLNYDqBdM8GPGrT6vbY9kXlNp1/+yiNIyoPzKDjSBe6ISpQ/rWkbZxWtmgi2aniX7NZo6Z9W8TaH5NOBInNI8aFL9XBdfS9a2WVU9YncWqu+5SqSdTofZ/DLzDzRA2mutwZl96GfCI7BhjxMQpchCZdxF4Rmc0E3rgw5Byrslk75IPU9FvEqJBGujLy9OCpi/PbLaKJOR2mC4rkn9rsKfuMdubEodh//aplLyKDDkF9fqpJT4HI4XvQQ8Ui2ikWBypHvxR7zOcV27aitDM3QJXdWWCgbalHuKHHDnYO4oSENn+NO1DGptpL+td1OlSoAheFacFQxdL7aEymofee20xEzT6hzjkbggXzQOw3Hfdy3WsVGjUNhalyZGBIUZp3iOXVIK09W5H2OKgx8Ag1Yx9lXjkLEvHGO2evJsYvD1PXgUvtPmqCNZ4miEoKNT2TV2vXS4czkX6iFpFVRfKLcJsdO4u2QEzjGPbIQJOejr4ierPzOA4cuJZ3LE511/sjRp0CLapOHJhXmz50YeEFJI5 KLF7inCH RGas0ERlNIUKCKZf0DhC3EBjntoSasp2jYZCUm0BGY0AKIDq6QWVuoyZapvwq0JPzlLLPMUE0UoXDICwHt9aFpZYHweKTyLvQW0NyckfM4yy31WGCnXcgPWPN2hcsa/w2SC+mLPr6JtrjNiqGxgBEO3uHw3RMZQIeUmZeADX2ofSe103d/6xQklyZAeB3X+yk0tQRWLCob+qvSaAg+v+Uuwxr/CT0zN5jnfFUS6DKBiqOu4LTzQ1Vusru48j79r8jDqY4BW8Z9/1xKrHDgk53ImKW/+8RlRthZqsjxvbe0/JyjANWn5cSoCpLgHqI5BZJKQW1iElIo9vBWyozX0UGVkaTLZKrfCLjDWjQvoVh/Bfj3vEaiwHfsMVquJAwnxvTgew4DI6+vBpwWo4CxYjcODPrXX7AdhKuvGwbyDdicjM9jbSag0SWtpNiNA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On 2025/2/25 01:50, Kairui Song wrote: > On Mon, Feb 24, 2025 at 4:47 PM Baolin Wang > wrote: >> >> Alex and Kairui reported some issues (system hang or data corruption) when >> swapping out or swapping in large shmem folios. This is especially easy to >> reproduce when the tmpfs is mount with the 'huge=within_size' parameter. >> Thanks to Kairui's reproducer, the issue can be easily replicated. >> >> The root cause of the problem is that swap readahead may asynchronously >> swap in order 0 folios into the swap cache, while the shmem mapping can >> still store large swap entries. Then an order 0 folio is inserted into >> the shmem mapping without splitting the large swap entry, which overwrites >> the original large swap entry, leading to data corruption. >> >> When getting a folio from the swap cache, we should split the large swap >> entry stored in the shmem mapping if the orders do not match, to fix this >> issue. >> >> Fixes: 809bc86517cc ("mm: shmem: support large folio swap out") >> Reported-by: Alex Xu (Hello71) >> Reported-by: Kairui Song > > Maybe you can add a Closes:? Yes. Hope Andrew can help add this: Closes: https://lore.kernel.org/all/1738717785.im3r5g2vxc.none@localhost/ >> Signed-off-by: Baolin Wang >> --- >> mm/shmem.c | 31 +++++++++++++++++++++++++++---- >> 1 file changed, 27 insertions(+), 4 deletions(-) >> >> diff --git a/mm/shmem.c b/mm/shmem.c >> index 4ea6109a8043..cebbac97a221 100644 >> --- a/mm/shmem.c >> +++ b/mm/shmem.c >> @@ -2253,7 +2253,7 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index, >> struct folio *folio = NULL; >> bool skip_swapcache = false; >> swp_entry_t swap; >> - int error, nr_pages; >> + int error, nr_pages, order, split_order; >> >> VM_BUG_ON(!*foliop || !xa_is_value(*foliop)); >> swap = radix_to_swp_entry(*foliop); >> @@ -2272,10 +2272,9 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index, >> >> /* Look it up and read it in.. */ >> folio = swap_cache_get_folio(swap, NULL, 0); >> + order = xa_get_order(&mapping->i_pages, index); >> if (!folio) { >> - int order = xa_get_order(&mapping->i_pages, index); >> bool fallback_order0 = false; >> - int split_order; >> >> /* Or update major stats only when swapin succeeds?? */ >> if (fault_type) { >> @@ -2339,6 +2338,29 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index, >> error = -ENOMEM; >> goto failed; >> } >> + } else if (order != folio_order(folio)) { >> + /* >> + * Swap readahead may swap in order 0 folios into swapcache >> + * asynchronously, while the shmem mapping can still stores >> + * large swap entries. In such cases, we should split the >> + * large swap entry to prevent possible data corruption. >> + */ >> + split_order = shmem_split_large_entry(inode, index, swap, gfp); >> + if (split_order < 0) { >> + error = split_order; >> + goto failed; >> + } >> + >> + /* >> + * If the large swap entry has already been split, it is >> + * necessary to recalculate the new swap entry based on >> + * the old order alignment. >> + */ >> + if (split_order > 0) { >> + pgoff_t offset = index - round_down(index, 1 << split_order); >> + >> + swap = swp_entry(swp_type(swap), swp_offset(swap) + offset); >> + } >> } >> >> alloced: >> @@ -2346,7 +2368,8 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index, >> folio_lock(folio); >> if ((!skip_swapcache && !folio_test_swapcache(folio)) || >> folio->swap.val != swap.val || >> - !shmem_confirm_swap(mapping, index, swap)) { >> + !shmem_confirm_swap(mapping, index, swap) || >> + xa_get_order(&mapping->i_pages, index) != folio_order(folio)) { >> error = -EEXIST; >> goto unlock; >> } >> -- >> 2.43.5 >> > > Thanks for the fix, it works for me. > > Tested-by: Kairui Song Thanks for testing :)