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 DEBF7C54756 for ; Wed, 21 May 2025 02:45:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6C3976B0085; Tue, 20 May 2025 22:45:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 69B176B0088; Tue, 20 May 2025 22:45:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 58B016B0089; Tue, 20 May 2025 22:45:39 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 3BBB36B0085 for ; Tue, 20 May 2025 22:45:39 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id B64C0E7F85 for ; Wed, 21 May 2025 02:45:38 +0000 (UTC) X-FDA: 83465374356.03.7BBF204 Received: from mail-lj1-f177.google.com (mail-lj1-f177.google.com [209.85.208.177]) by imf05.hostedemail.com (Postfix) with ESMTP id B945A100002 for ; Wed, 21 May 2025 02:45:36 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=gULzwN+7; spf=pass (imf05.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.208.177 as permitted sender) smtp.mailfrom=ryncsn@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1747795536; 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=ekWVNyS5Ehqk/LS3e/3t4Uu2hE0jQa8FW1yvGkAqang=; b=jbUazM1aEvVtg0j04Lydv9i3Pj7rE0hUUeJHgyg08yuNT7KtbsMrBJ51rZFSDfdZOJ93mW chVBTPRoeXanZ9yB46SaP0x9Thc8a7EXgPPVne7ziFAMSs4oy69nrHTIRdC2IMQ3jGG94T /Ka544RP2TpP9HtmuGpqSFayFsLVyhQ= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=gULzwN+7; spf=pass (imf05.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.208.177 as permitted sender) smtp.mailfrom=ryncsn@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1747795536; a=rsa-sha256; cv=none; b=nbtAfTwSjPz38efehIlmdmU4t17LG/XLkQY5bijPEabTpWT8GydT4jI0qJyTW8tkXOKLKT 4HvtIjOtfXCjTcYq1QhGzcozLKCD/i3aWtQGoiwkEzsEZOF0iKgShzjOf+jGGaiMzyzj6R 3OIr9c4c7epavdRNdndPpURxqyDxGuQ= Received: by mail-lj1-f177.google.com with SMTP id 38308e7fff4ca-328114b26e1so27319901fa.1 for ; Tue, 20 May 2025 19:45:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1747795535; x=1748400335; darn=kvack.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=ekWVNyS5Ehqk/LS3e/3t4Uu2hE0jQa8FW1yvGkAqang=; b=gULzwN+7CdTcVAIZe7Z/cccTO/o6fei3rFWDEjIhB38CyCCJpVKL34I0uhdU6ifCg9 MJaQbd9ihTZLBZ/WX//Nrv0A3TWunarGs6TnSz5KzTJ+k6GhwhwbKTxlkCpn720LfKc2 Aoe4KxAwJ/3iXV+N3JqcAdZqnZlG3/Xkea/4mp6CXzlIl/Q5Q4tZROIisv4lDB9qKlVR dso3+mWXs9GSvPvjnqf5OciWdwiMpy7CsmVzrAUtbVBr70vQRMwCEj2DCfLcclRfoxF2 OPUmfYVD7iscCoW0SEYk6/t7ZgpqiT70ncpIDtoif4+uyGCfSYs+WmY46fE56ec/exN7 I6Ow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747795535; x=1748400335; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ekWVNyS5Ehqk/LS3e/3t4Uu2hE0jQa8FW1yvGkAqang=; b=PEYFf9BI9DoSQkGBWIecI88hOT5/iA5IF2geM1fz/+VHUjZEvrzyEAWo7z9MjqMui7 eMtkUq0ZYeZn7iILu6CGtu9FC8V7Ju7IhH6cQhfg28m754RS9QohMjtplcaNkvCZiiac stzzEiJDgmu9yDmJUk4AHzAuQr75XVMNfH9SK0wruWVEWwjfZfRtFaKs1SQf7363Fvh2 53lCLEFa/jD+8Ol+r16oiZOKVjc5IFh0GZ5iVwct4hSWqp4M29f9kycm3wX2hDaeYQMN 0ZSZUXk4NQW9p43pDdhX1RgkvB5Pjy0QvSYoTPa+fkkYS1ZYhdv3PmLxE6orZJk1gX3B QXIQ== X-Forwarded-Encrypted: i=1; AJvYcCX9m1+BpaFiooNIMjyw+VnnLfVcs7zuYTjpfcemmG/5uuItninSg/WF1fiB6SjThNCsRiPU1jAzHQ==@kvack.org X-Gm-Message-State: AOJu0YyJ477AkTf719eZ6cCP5sOWOsYJE7gR9LWDziO/u2xPko2RZwCv gBVhz+Lhs35WVi7DswZyaNdglJJWvSpEeNV95l48DGtMU5acc6Qrs0WMNuwSzn29G1OC3WJ+z03 m0swOF2cZrR4z1GYIbYxMRxegA76/sJc= X-Gm-Gg: ASbGncs6+Pt0kxWXre61TW4mDmblKQvdu134dQsxqkIi8WTSOctNjFn9MQbG4a7HSK/ OL+MMAif3RZqRrJqJidQlnIsUIlK4KdHMScbK/6MCe8Mo/1zLN01OecE49AXcX9+ho7iE5ec/Qs aXy0CEyWQp8Xk1F8dEzawt3GyC4R1+zE8= X-Google-Smtp-Source: AGHT+IGUyYziX3ezJyY+S/9pxHTLIxdL2yhsMx14GQw0tXGQTTnZA4BthMBrSAQicvr8OaEBhYiF3Pz805LHFm7SjA8= X-Received: by 2002:a2e:a995:0:b0:30c:aae:6d61 with SMTP id 38308e7fff4ca-3280974eef6mr65341931fa.30.1747795534455; Tue, 20 May 2025 19:45:34 -0700 (PDT) MIME-Version: 1.0 References: <20250514201729.48420-6-ryncsn@gmail.com> <20250519043847.1806-1-21cnbao@gmail.com> In-Reply-To: From: Kairui Song Date: Wed, 21 May 2025 10:45:16 +0800 X-Gm-Features: AX0GCFspAzlf_oLb-7X0sbp31_S7uHzktacxEMv4ktnM4eJqURlBG9mDxUMf1Ac Message-ID: Subject: Re: [PATCH 05/28] mm, swap: sanitize swap cache lookup convention To: Barry Song <21cnbao@gmail.com> Cc: akpm@linux-foundation.org, Baolin Wang , Baoquan He , Chris Li , David Hildenbrand , Johannes Weiner , Hugh Dickins , Kalesh Singh , LKML , linux-mm , Nhat Pham , Ryan Roberts , Kemeng Shi , Tim Chen , Matthew Wilcox , "Huang, Ying" , Yosry Ahmed Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Stat-Signature: 54ysuw1nmgjpu8p946chgh8uk4spen3m X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: B945A100002 X-HE-Tag: 1747795536-340590 X-HE-Meta: U2FsdGVkX18ZldRl91RbeDIXaGkmIqsWE/U6Fn50iUY3XW8ZEBl19ThVkTX5Ki/B0iVT/mIEGSFjtYZz86r+TM2OycAl9Kr54yn8PoPMAe4tuME1bBcoGBXAha+Vc+49GYcKAoWy3fKr+EsCENwkohLFxX5O8nUI9rP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+3GwNgbKbwGHhG3FtKmErHW5FQpflA6YhF8oGxZXiEaNqBy8h8gzp3wgTHIo8cXryTVfuyUXmL12pTCkueeJf1czZJAa8tujbzFS5Pskc/qaGn7F5By19w3wL+FEa32ENzs58m9ejh6BGE5A= 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: Barry Song <21cnbao@gmail.com> =E4=BA=8E 2025=E5=B9=B45=E6=9C=8821=E6=97=A5= =E5=91=A8=E4=B8=89 06:33=E5=86=99=E9=81=93=EF=BC=9A > > On Wed, May 21, 2025 at 7:10=E2=80=AFAM Kairui Song wr= ote: > > > > On Tue, May 20, 2025 at 12:41=E2=80=AFPM Barry Song <21cnbao@gmail.com>= wrote: > > > > > > On Tue, May 20, 2025 at 3:31=E2=80=AFPM Kairui Song wrote: > > > > > > > > On Mon, May 19, 2025 at 12:38=E2=80=AFPM Barry Song <21cnbao@gmail.= com> wrote: > > > > > > > > > > > From: Kairui Song > > > > > > > > > > > diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c > > > > > > index e5a0db7f3331..5b4f01aecf35 100644 > > > > > > --- a/mm/userfaultfd.c > > > > > > +++ b/mm/userfaultfd.c > > > > > > @@ -1409,6 +1409,10 @@ static int move_pages_pte(struct mm_stru= ct *mm, pmd_t *dst_pmd, pmd_t *src_pmd, > > > > > > goto retry; > > > > > > } > > > > > > } > > > > > > + if (!folio_swap_contains(src_folio, entry)) { > > > > > > + err =3D -EBUSY; > > > > > > + goto out; > > > > > > + } > > > > > > > > > > It seems we don't need this. In move_swap_pte(), we have been che= cking pte pages > > > > > are stable: > > > > > > > > > > if (!is_pte_pages_stable(dst_pte, src_pte, orig_dst_pte, = orig_src_pte, > > > > > dst_pmd, dst_pmdval)) { > > > > > double_pt_unlock(dst_ptl, src_ptl); > > > > > return -EAGAIN; > > > > > } > > > > > > > > The tricky part is when swap_cache_get_folio returns the folio, bot= h > > > > folio and ptes are unlocked. So is it possible that someone else > > > > swapped in the entries, then swapped them out again using the same > > > > entries? > > > > > > > > The folio will be different here but PTEs are still the same value = to > > > > they will pass the is_pte_pages_stable check, we previously saw > > > > similar races with anon fault or shmem. I think more strict checkin= g > > > > won't hurt here. > > > > > > This doesn't seem to be the same case as the one you fixed in > > > do_swap_page(). Here, we're hitting the swap cache, whereas in that > > > case, there was no one hitting the swap cache, and you used > > > swap_prepare() to set up the cache to fix the issue. > > > > > > By the way, if we're not hitting the swap cache, src_folio will be > > > NULL. Also, it seems that folio_swap_contains(src_folio, entry) does > > > not guard against that case either. > > > > Ah, that's true, it should be moved inside the if (folio) {...} block > > above. Thanks for catching this! > > > > > But I suspect we won't have a problem, since we're not swapping in = =E2=80=94 > > > we didn't read any stale data, right? Swap-in will only occur after w= e > > > move the PTEs. > > > > My concern is that a parallel swapin / swapout could result in the > > folio to be a completely irrelevant or invalid folio. > > > > It's not about the dst, but in the move src side, something like: > > > > CPU1 CPU2 > > move_pages_pte > > folio =3D swap_cache_get_folio(...) > > | Got folio A here > > move_swap_pte > > > > > > | Now folio A is no longer valid. > > | It's very unlikely but here SWAP > > | could reuse the same entry as abov= e. > > > swap_cache_get_folio() does increment the folio's refcount, but it seems = this > doesn't prevent do_swap_page() from freeing the swap entry after swapping > in src_pte with folio A, if it's a read fault. > for write fault, folio_ref_count(folio) =3D=3D (1 + folio_nr_pages(folio)= ) > will be false: > > static inline bool should_try_to_free_swap(struct folio *folio, > struct vm_area_struct *vma, > unsigned int fault_flags) > { > ... > > /* > * If we want to map a page that's in the swapcache writable, we > * have to detect via the refcount if we're really the exclusive > * user. Try freeing the swapcache to get rid of the swapcache > * reference only in case it's likely that we'll be the exlusive = user. > */ > return (fault_flags & FAULT_FLAG_WRITE) && !folio_test_ksm(folio)= && > folio_ref_count(folio) =3D=3D (1 + folio_nr_pages(folio))= ; > } > > and for swapout, __removing_mapping does check refcount as well: > > static int __remove_mapping(struct address_space *mapping, struct folio *= folio, > bool reclaimed, struct mem_cgroup *target_mem= cg) > { > refcount =3D 1 + folio_nr_pages(folio); > if (!folio_ref_freeze(folio, refcount)) > goto cannot_free; > > } > > However, since __remove_mapping() occurs after pageout(), it seems > this also doesn't prevent swapout from allocating a new swap entry to > fill src_pte. > > It seems your concern is valid=E2=80=94unless I'm missing something. > Do you have a reproducer? If so, this will likely need a separate fix > patch rather than being hidden in this patchset. Thanks for the analysis. I don't have a reproducer yet, I did some local experiments and that seems possible, but the race window is so tiny and it's very difficult to make the swap entry reuse to collide with that, I'll try more but in theory this seems possible, or at least looks very fragile. And yeah, let's patch the kernel first if that's a real issue. > > > double_pt_lock > > is_pte_pages_stable > > | Passed because of entry reuse. > > folio_move_anon_rmap(...) > > | Moved invalid folio A. > > > > And could it be possible that the swap_cache_get_folio returns NULL > > here, but later right before the double_pt_lock, a folio is added to > > swap cache? Maybe we better check the swap cache after clear and > > releasing dst lock, but before releasing src lock? > > It seems you're suggesting that a parallel swap-in allocates and adds > a folio to the swap cache, but the PTE has not yet been updated from > a swap entry to a present mapping? > > As long as do_swap_page() adds the folio to the swap cache > before updating the PTE to present, this scenario seems possible. Yes, that's two kinds of problems here. I suspected there could be an ABA problem while working on the series, but wasn't certain. And just realised there could be another missed cache read here thanks to your review and discussion :) > > It seems we need to double-check: > > diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c > index bc473ad21202..976053bd2bf1 100644 > --- a/mm/userfaultfd.c > +++ b/mm/userfaultfd.c > @@ -1102,8 +1102,14 @@ static int move_swap_pte(struct mm_struct *mm, > struct vm_area_struct *dst_vma, > if (src_folio) { > folio_move_anon_rmap(src_folio, dst_vma); > src_folio->index =3D linear_page_index(dst_vma, dst_addr)= ; > + } else { > + struct folio *folio =3D > filemap_get_folio(swap_address_space(entry), > + swap_cache_index(entry)); > + if (!IS_ERR_OR_NULL(folio)) { > + double_pt_unlock(dst_ptl, src_ptl); > + return -EAGAIN; > + } > } > - > orig_src_pte =3D ptep_get_and_clear(mm, src_addr, src_pte); > #ifdef CONFIG_MEM_SOFT_DIRTY > orig_src_pte =3D pte_swp_mksoft_dirty(orig_src_pte); Maybe it has to get even dirtier here to call swapcache_prepare too to cover the SYNC_IO case? > > Let me run test case [1] to check whether this ever happens. I guess I ne= ed to > hack kernel a bit to always add folio to swapcache even for SYNC IO. That will cause quite a performance regression I think. Good thing is, that's exactly the problem this series is solving by dropping the SYNC IO swapin path and never bypassing the swap cache, while improving the performance, eliminating things like this. One more reason to justify the approach :) > > [1] https://lore.kernel.org/linux-mm/20250219112519.92853-1-21cnbao@gmail= .com/ I'll try this too. > > > > > > > > > > > > > > > > > > > > > > Also, -EBUSY is somehow incorrect error code. > > > > > > > > Yes, thanks, I'll use EAGAIN here just like move_swap_pte. > > > > > > > > > > > > > > > > > > > err =3D move_swap_pte(mm, dst_vma, dst_addr, src_= addr, dst_pte, src_pte, > > > > > > orig_dst_pte, orig_src_pte, dst_p= md, dst_pmdval, > > > > > > dst_ptl, src_ptl, src_folio); > > > > > > > > > > > > > > > > Thanks > Barry