All of lore.kernel.org
 help / color / mirror / Atom feed
From: 马超 <machao26@xiaomi.com>
To: Baolin Wang <baolin.wang@linux.alibaba.com>,
	Kairui Song <ryncsn@gmail.com>
Cc: "akpm@linux-foundation.org" <akpm@linux-foundation.org>,
	"hughd@google.com" <hughd@google.com>,
	"stable@vger.kernel.org" <stable@vger.kernel.org>,
	"baohua@kernel.org" <baohua@kernel.org>,
	"linux-mm@kvack.org" <linux-mm@kvack.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	田孝斌 <tianxiaobin@xiaomi.com>, 俞东斌 <yudongbin@xiaomi.com>,
	李鹏程 <xiaoyaoli@xiaomi.com>
Subject: 回复: [External Mail]Re: [PATCH 6.18.y] mm: shmem: fix potential livelock issue for shmem direct swapin
Date: Tue, 7 Jul 2026 02:47:42 +0000	[thread overview]
Message-ID: <1ac8eb5743e244f58efa28ea46aeb4d0@xiaomi.com> (raw)
In-Reply-To: <8ef0b72e-a0e8-4913-8d30-519335305260@linux.alibaba.com>

> On 7/6/26 1:59 PM, Kairui Song wrote:
>> On Mon, Jul 6, 2026 at 11:25 AM Baolin Wang
>> <baolin.wang@linux.alibaba.com> wrote:
>>>
>>> When skipping swapcache for synchronous IO swap devices,
>>> swapcache_prepare() is used to prevent parallel swapin from proceeding with the swap cache flag.
>>> However, on PREEMPT kernels this can lead to a livelock, as reported by Chao[1]:
>>>
>>> Thread A starts direct swapin of a shmem folio and calls
>>> swapcache_prepare() to set SWAP_HAS_CACHE. It may then be preempted inside workingset_refault().
>>> Meanwhile, a higher priority thread B also attempts direct swapin of
>>> the same shmem swap entry. Since swapcache_prepare() already marks
>>> the entry, thread B repeatedly gets -EEXIST and busy-loops waiting
>>> for thread A to finish. But as thread B runs at higher priority,
>>> thread A cannot preempt it, resulting in starvation and a livelock.
>>>
>>> Fix it by yielding the CPU with schedule_timeout_uninterruptible(1)
>>> when
>>> swapcache_prepare() fails, following the same approach used in
>>> commits 029c4628b2eb ("mm: swap: get rid of livelock in swapin
>>> readahead") and
>>> 13ddaf26be32 ("mm/swap: fix race when skipping swapcache").
>>>
>>> Note that mainline does not have this potential issue, which has
>>> already been resolved by Kairui's swap refactoring work[2].
>>>
>>> [1]
>>> https://lore.kernel.org/all/700a2cbf90a2484f979aac858f08f5d4@xiaomi.c
>>> om/ [2]
>>> https://lore.kernel.org/all/20260517-swap-table-p4-v5-0-88ae43e064c7@
>>> tencent.com/
>>> Fixes: 1dd44c0af4fa ("mm: shmem: skip swapcache for swapin of
>>> synchronous swap device")
>>> Reported-by: Ma Chao <machao26@xiaomi.com>
>>> Closes:
>>> https://lore.kernel.org/all/700a2cbf90a2484f979aac858f08f5d4@xiaomi.c
>>> om/
>>> Signed-off-by: Baolin Wang <baolin.wang@linux.alibaba.com>
>>> ---
>>> Hi Chao, could you try this patch to check if it fixes your issue? Thanks.
>>> ---
>>>   mm/shmem.c | 2 ++
>>>   1 file changed, 2 insertions(+)
>>>
>>> diff --git a/mm/shmem.c b/mm/shmem.c
>>> index 94c5b0d78ac3..d4cb57b3b0ef 100644
>>> --- a/mm/shmem.c
>>> +++ b/mm/shmem.c
>>> @@ -2066,6 +2066,8 @@ static struct folio *shmem_swap_alloc_folio(struct inode *inode,
>>>          if (swapcache_prepare(entry, nr_pages)) {
>>>                  folio_put(new);
>>>                  new = ERR_PTR(-EEXIST);
>>> +               /* Relax a bit to prevent rapid repeated page faults */
>>> +               schedule_timeout_uninterruptible(1);
>>>                  /* Try smaller folio to avoid cache conflict */
>>>                  goto fallback;
>>>          }
>>> --
>>> 2.47.3
>>>
>>
>> Thanks! That's much more simpler than I expected. Do we need a wakeup
>> queue like the one in commit 01626a1823024? Perhaps the reporter can
>> help confirm and test? I personally prefer to keep it simple if shmem
>> users aren't as sensitive as anon users.
>
> I agree. I'd like to keep the bugfix as simple as possible, if the reporter's scenario isn't latency-sensitive.

Thanks for your analysis and the provided fix patch. I will test and verify it. By the way, when is the fix patch expected to be merged into the kernel?
#/******本邮件及其附件含有小米公司的保密信息,仅限于发送给上面地址中列出的个人或群组。禁止任何其他人以任何形式使用(包括但不限于全部或部分地泄露、复制、或散发)本邮件中的信息。如果您错收了本邮件,请您立即电话或邮件通知发件人并删除本邮件! This e-mail and its attachments contain confidential information from XIAOMI, which is intended only for the person or entity whose address is listed above. Any use of the information contained herein in any way (including, but not limited to, total or partial disclosure, reproduction, or dissemination) by persons other than the intended recipient(s) is prohibited. If you receive this e-mail in error, please notify the sender by phone or email immediately and delete it!******/#

      parent reply	other threads:[~2026-07-07  2:47 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-06  3:25 [PATCH 6.18.y] mm: shmem: fix potential livelock issue for shmem direct swapin Baolin Wang
2026-07-06  5:59 ` Kairui Song
2026-07-06 12:08   ` Baolin Wang
2026-07-06 13:04     ` Barry Song
2026-07-07  1:52       ` Baolin Wang
2026-07-07  2:47     ` 马超 [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1ac8eb5743e244f58efa28ea46aeb4d0@xiaomi.com \
    --to=machao26@xiaomi.com \
    --cc=akpm@linux-foundation.org \
    --cc=baohua@kernel.org \
    --cc=baolin.wang@linux.alibaba.com \
    --cc=hughd@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=ryncsn@gmail.com \
    --cc=stable@vger.kernel.org \
    --cc=tianxiaobin@xiaomi.com \
    --cc=xiaoyaoli@xiaomi.com \
    --cc=yudongbin@xiaomi.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.