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 1D4BDC021B1 for ; Wed, 19 Feb 2025 23:20:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A2A3F280279; Wed, 19 Feb 2025 18:19:59 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9B2F5280276; Wed, 19 Feb 2025 18:19:59 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 80594280279; Wed, 19 Feb 2025 18:19:59 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 5F43E280276 for ; Wed, 19 Feb 2025 18:19:59 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 178741A0376 for ; Wed, 19 Feb 2025 23:19:59 +0000 (UTC) X-FDA: 83138264118.29.6BA0239 Received: from mail-qt1-f176.google.com (mail-qt1-f176.google.com [209.85.160.176]) by imf30.hostedemail.com (Postfix) with ESMTP id 2BD7A80013 for ; Wed, 19 Feb 2025 23:19:57 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="k/0GCXRf"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf30.hostedemail.com: domain of lokeshgidra@google.com designates 209.85.160.176 as permitted sender) smtp.mailfrom=lokeshgidra@google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1740007197; a=rsa-sha256; cv=none; b=46mjRLz2yfjA+BlS8bFNW25OmESJzc8pdW0sOvyspZTBq/JlzSLxbynXR+0zMDqnr4ASth RVwm5kI9WURbElBktJ+Zd+oYBW18lrefZB4NFr8wVEXAQhk/ZbdPjlYfX+QgfOIPqLu1hA UlM1iPjQnPd8oTruIs8bIrivNV07RRY= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="k/0GCXRf"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf30.hostedemail.com: domain of lokeshgidra@google.com designates 209.85.160.176 as permitted sender) smtp.mailfrom=lokeshgidra@google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1740007197; 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=ZTn7IdANd5+OwuUDOviJNPx8fYljpy+W7Qh0TY3l+ak=; b=hAKVYp3r+L0opXwBzfLkRzuwpkTck5xBaYUtzsezGKpo6NGTtZ7wedlMixWWHE5BbqrJ9d GkyANcRryzvjqXQUMJ2oAXNdG6ppN8QyEDkb1oxjRQ8c6Sl6bbpqIgKhNpfmiNanLHf5Ky W9JUX36UA89+hZAt1Bi/aNGBIES+brI= Received: by mail-qt1-f176.google.com with SMTP id d75a77b69052e-471fa3b19bcso101241cf.0 for ; Wed, 19 Feb 2025 15:19:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1740007196; x=1740611996; 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=ZTn7IdANd5+OwuUDOviJNPx8fYljpy+W7Qh0TY3l+ak=; b=k/0GCXRfHe3GYhsNs7/0mkMW0ADd+ErcviJFXW9m1siNbGMlZNuB+/pF3P5EOegydU Dw+4KcpqN3bdqsKQIKh23x9Py0+ZUBIZ0jveAeh7SDev94rzQ9Bw+gQuKkPQ9HjnAVOW SycFMRDinCb0lhInx9/SoEPS4upEU6meYMpc3x+Wbacgr6ZCxpvNk3/mEHNwPnLB+rW1 X2FDnuNWxQI7lC+f8k0EEjYmMJ2jejnhPD3hcqfh+YHAzwuon+YUNkvDG+U0pCDHjBAN Ojzf+G6xiExIWqq4APJ+t038sbazMdC2BgaReu3bPlRuHrZo4KVfmqTgAR04Y+EqgZ6r QfEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740007196; x=1740611996; 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=ZTn7IdANd5+OwuUDOviJNPx8fYljpy+W7Qh0TY3l+ak=; b=fqSGnb8dq6w9g1/xC/dw2HNNwpF4KZOPcjMdfAxmH7MM6qx3Ma7J3Up1x+QbxLe5A3 cZ/7twhCik/ISoUjo7oEqZcFLAYlpZR41MNuK7XL/Y+J4+MN43D1711bKSjbn1bGGpzr F7iuT0eUrQBa56dvaiJrW9XvoGCrb+2OP0Pvr6/8s9gLJ41LKQqkps8BQnkPQpAGbj2z BQ1S3L9gGqUmwqfCjBB4Tnenot7OY2408gWfl8Cd8N+gQD8muxGPwnjM7Mrge29Fnnms Uxl8Qnp8taH7sMpYP91Qy9Yzvuybp2wCDi2FyPfIIbjpefx0Lsq9WB2EcHwZHKceP4eM 31Mw== X-Forwarded-Encrypted: i=1; AJvYcCWZSGEH4jmMgjgtKm5m5bMSJ9TCFDRn7LT+cQFhSyRo9eb8vEYh746WoB8ViG9TnapoULoyd2R8rg==@kvack.org X-Gm-Message-State: AOJu0YzVEwwlJvkDRQPlZ+npRU6k3Q1FaOp0qA4ZpvVPepA+FiMjly2z DPqSd/G3oEQ6d6U7m86mYXbbx266YF7YgnbcyXGFoj5fPHq/XTXn8ahwqX7w27k0G3Lel/v3w7p JNlrQZIsmTJvJGuOjbYcn7qcFFakekqZYauv+ X-Gm-Gg: ASbGnctvr++IJQXFS5wWpcgxEfU3h9PbfgDFSqEa5dEf4xnhqF2yA4N/WTuyFnb8tfE J8q3jWVJTnhV8ixHiHbfOjJYzcIX/SOTUKr8tMFBiyV2AklIU0uxbukLMzlxzVQSQ80XZp/eYT9 6fg82v8e5HMwwV7sOCy6AZ3VPVN00= X-Google-Smtp-Source: AGHT+IFBWCOfY9p2hyvzuE8mPmDo3MHHI8DXClAaf7zZEESZemzzeXvyk5i9ZgTxZc+jzdXP6eg5vppYtADxCCT9Bok= X-Received: by 2002:a05:622a:1a88:b0:471:9ece:b13b with SMTP id d75a77b69052e-4721708fbf4mr524581cf.1.1740007195890; Wed, 19 Feb 2025 15:19:55 -0800 (PST) MIME-Version: 1.0 References: <20250219112519.92853-1-21cnbao@gmail.com> In-Reply-To: From: Lokesh Gidra Date: Wed, 19 Feb 2025 15:19:44 -0800 X-Gm-Features: AWEUYZkT8naKVFNVw2UCSAtbNHnmFbAusmnE6kcuNKIOLd5HOCSM2iGaSKUXSAA Message-ID: Subject: Re: [PATCH RFC] mm: Fix kernel BUG when userfaultfd_move encounters swapcache To: Barry Song <21cnbao@gmail.com> Cc: Peter Xu , Suren Baghdasaryan , linux-mm@kvack.org, akpm@linux-foundation.org, linux-kernel@vger.kernel.org, zhengtangquan@oppo.com, Barry Song , Andrea Arcangeli , Al Viro , Axel Rasmussen , Brian Geffon , Christian Brauner , David Hildenbrand , Hugh Dickins , Jann Horn , Kalesh Singh , "Liam R . Howlett" , Matthew Wilcox , Michal Hocko , Mike Rapoport , Nicolas Geoffray , Ryan Roberts , Shuah Khan , ZhangPeng , Yu Zhao Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 2BD7A80013 X-Stat-Signature: 4n5dqe7esp6ebi5mo5p1pgieifmtmx53 X-Rspam-User: X-HE-Tag: 1740007197-132070 X-HE-Meta: U2FsdGVkX18OX5dlLzeBjBIAH5NoFvl2omiY4oSUTD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AjLNzy6/umS2Y4rexwPDZEavEa3YcDVs0HHb+n2lOVMaC3wGn+2e5xqdEn0tcJC5Yvx10kyw9q2IWkrcPB7d7RfTa/NCzHFacH+knLy8g9GY5m9O3NMx1gwz0XtBRgtMOnMyTUO8wSV1aDBliPZ5bvHUFcTeM7bQtrkmvHiP47VnTaFHUfY53S9rtW/QhfNVEbjBwuc= 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 Wed, Feb 19, 2025 at 3:04=E2=80=AFPM Barry Song <21cnbao@gmail.com> wrot= e: > > On Thu, Feb 20, 2025 at 11:15=E2=80=AFAM Peter Xu wro= te: > > > > On Thu, Feb 20, 2025 at 09:37:50AM +1300, Barry Song wrote: > > > On Thu, Feb 20, 2025 at 7:27=E2=80=AFAM Suren Baghdasaryan wrote: > > > > > > > > On Wed, Feb 19, 2025 at 3:25=E2=80=AFAM Barry Song <21cnbao@gmail.c= om> wrote: > > > > > > > > > > From: Barry Song > > > > > > > > > > userfaultfd_move() checks whether the PTE entry is present or a > > > > > swap entry. > > > > > > > > > > - If the PTE entry is present, move_present_pte() handles folio > > > > > migration by setting: > > > > > > > > > > src_folio->index =3D linear_page_index(dst_vma, dst_addr); > > > > > > > > > > - If the PTE entry is a swap entry, move_swap_pte() simply copies > > > > > the PTE to the new dst_addr. > > > > > > > > > > This approach is incorrect because even if the PTE is a swap > > > > > entry, it can still reference a folio that remains in the swap > > > > > cache. > > > > > > > > > > If do_swap_page() is triggered, it may locate the folio in the > > > > > swap cache. However, during add_rmap operations, a kernel panic > > > > > can occur due to: > > > > > page_pgoff(folio, page) !=3D linear_page_index(vma, address) > > > > > > > > Thanks for the report and reproducer! > > > > > > > > > > > > > > $./a.out > /dev/null > > > > > [ 13.336953] page: refcount:6 mapcount:1 mapping:00000000f43db1= 9c index:0xffffaf150 pfn:0x4667c > > > > > [ 13.337520] head: order:2 mapcount:1 entire_mapcount:0 nr_page= s_mapped:1 pincount:0 > > > > > [ 13.337716] memcg:ffff00000405f000 > > > > > [ 13.337849] anon flags: 0x3fffc0000020459(locked|uptodate|dirt= y|owner_priv_1|head|swapbacked|node=3D0|zone=3D0|lastcpupid=3D0xffff) > > > > > [ 13.338630] raw: 03fffc0000020459 ffff80008507b538 ffff8000850= 7b538 ffff000006260361 > > > > > [ 13.338831] raw: 0000000ffffaf150 0000000000004000 00000006000= 00000 ffff00000405f000 > > > > > [ 13.339031] head: 03fffc0000020459 ffff80008507b538 ffff800085= 07b538 ffff000006260361 > > > > > [ 13.339204] head: 0000000ffffaf150 0000000000004000 0000000600= 000000 ffff00000405f000 > > > > > [ 13.339375] head: 03fffc0000000202 fffffdffc0199f01 ffffffff00= 000000 0000000000000001 > > > > > [ 13.339546] head: 0000000000000004 0000000000000000 00000000ff= ffffff 0000000000000000 > > > > > [ 13.339736] page dumped because: VM_BUG_ON_PAGE(page_pgoff(fol= io, page) !=3D linear_page_index(vma, address)) > > > > > [ 13.340190] ------------[ cut here ]------------ > > > > > [ 13.340316] kernel BUG at mm/rmap.c:1380! > > > > > [ 13.340683] Internal error: Oops - BUG: 00000000f2000800 [#1] = PREEMPT SMP > > > > > [ 13.340969] Modules linked in: > > > > > [ 13.341257] CPU: 1 UID: 0 PID: 107 Comm: a.out Not tainted 6.1= 4.0-rc3-gcf42737e247a-dirty #299 > > > > > [ 13.341470] Hardware name: linux,dummy-virt (DT) > > > > > [ 13.341671] pstate: 60000005 (nZCv daif -PAN -UAO -TCO -DIT -S= SBS BTYPE=3D--) > > > > > [ 13.341815] pc : __page_check_anon_rmap+0xa0/0xb0 > > > > > [ 13.341920] lr : __page_check_anon_rmap+0xa0/0xb0 > > > > > [ 13.342018] sp : ffff80008752bb20 > > > > > [ 13.342093] x29: ffff80008752bb20 x28: fffffdffc0199f00 x27: 0= 000000000000001 > > > > > [ 13.342404] x26: 0000000000000000 x25: 0000000000000001 x24: 0= 000000000000001 > > > > > [ 13.342575] x23: 0000ffffaf0d0000 x22: 0000ffffaf0d0000 x21: f= ffffdffc0199f00 > > > > > [ 13.342731] x20: fffffdffc0199f00 x19: ffff000006210700 x18: 0= 0000000ffffffff > > > > > [ 13.342881] x17: 6c203d2120296567 x16: 6170202c6f696c6f x15: 6= 62866666f67705f > > > > > [ 13.343033] x14: 6567617028454741 x13: 2929737365726464 x12: f= fff800083728ab0 > > > > > [ 13.343183] x11: ffff800082996bf8 x10: 0000000000000fd7 x9 : f= fff80008011bc40 > > > > > [ 13.343351] x8 : 0000000000017fe8 x7 : 00000000fffff000 x6 : f= fff8000829eebf8 > > > > > [ 13.343498] x5 : c0000000fffff000 x4 : 0000000000000000 x3 : 0= 000000000000000 > > > > > [ 13.343645] x2 : 0000000000000000 x1 : ffff0000062db980 x0 : 0= 00000000000005f > > > > > [ 13.343876] Call trace: > > > > > [ 13.344045] __page_check_anon_rmap+0xa0/0xb0 (P) > > > > > [ 13.344234] folio_add_anon_rmap_ptes+0x22c/0x320 > > > > > [ 13.344333] do_swap_page+0x1060/0x1400 > > > > > [ 13.344417] __handle_mm_fault+0x61c/0xbc8 > > > > > [ 13.344504] handle_mm_fault+0xd8/0x2e8 > > > > > [ 13.344586] do_page_fault+0x20c/0x770 > > > > > [ 13.344673] do_translation_fault+0xb4/0xf0 > > > > > [ 13.344759] do_mem_abort+0x48/0xa0 > > > > > [ 13.344842] el0_da+0x58/0x130 > > > > > [ 13.344914] el0t_64_sync_handler+0xc4/0x138 > > > > > [ 13.345002] el0t_64_sync+0x1ac/0x1b0 > > > > > [ 13.345208] Code: aa1503e0 f000f801 910f6021 97ff5779 (d421000= 0) > > > > > [ 13.345504] ---[ end trace 0000000000000000 ]--- > > > > > [ 13.345715] note: a.out[107] exited with irqs disabled > > > > > [ 13.345954] note: a.out[107] exited with preempt_count 2 > > > > > > > > > > Fully fixing it would be quite complex, requiring similar handlin= g > > > > > of folios as done in move_present_pte. > > > > > > > > How complex would that be? Is it a matter of adding > > > > folio_maybe_dma_pinned() checks, doing folio_move_anon_rmap() and > > > > folio->index =3D linear_page_index like in move_present_pte() or > > > > something more? > > > > > > My main concern is still with large folios that require a split_folio= () > > > during move_pages(), as the entire folio shares the same index and > > > anon_vma. However, userfaultfd_move() moves pages individually, > > > making a split necessary. > > > > > > However, in split_huge_page_to_list_to_order(), there is a: > > > > > > if (folio_test_writeback(folio)) > > > return -EBUSY; > > > > > > This is likely true for swapcache, right? However, even for move_pres= ent_pte(), > > > it simply returns -EBUSY: > > > > > > move_pages_pte() > > > { > > > /* at this point we have src_folio locked */ > > > if (folio_test_large(src_folio)) { > > > /* split_folio() can block */ > > > pte_unmap(&orig_src_pte); > > > pte_unmap(&orig_dst_pte); > > > src_pte =3D dst_pte =3D NULL; > > > err =3D split_folio(src_folio); > > > if (err) > > > goto out; > > > > > > /* have to reacquire the folio after it got s= plit */ > > > folio_unlock(src_folio); > > > folio_put(src_folio); > > > src_folio =3D NULL; > > > goto retry; > > > } > > > } > > > > > > Do we need a folio_wait_writeback() before calling split_folio()? > > > > Maybe no need in the first version to fix the immediate bug? > > > > It's also not always the case to hit writeback here. IIUC, writeback on= ly > > happens for a short window when the folio was just added into swapcache= . > > MOVE can happen much later after that anytime before a swapin. My > > understanding is that's also what Matthew wanted to point out. It may = be > > better justified of that in a separate change with some performance > > measurements. > > The bug we=E2=80=99re discussing occurs precisely within the short window= you > mentioned. > > 1. add_to_swap: The folio is added to swapcache. > 2. try_to_unmap: PTEs are converted to swap entries. > 3. pageout > 4. Swapcache is cleared. > > The issue happens between steps 2 and 4, where the PTE is not present, bu= t > the folio is still in swapcache - the current code does move_swap_pte() b= ut does > not fixup folio->index within swapcache. > > My point is that if we want a proper fix for mTHP, we'd better handle wri= teback. > Otherwise, this isn=E2=80=99t much different from directly returning -EBU= SY as proposed > in this RFC. > > For small folios, there=E2=80=99s no split_folio issue, making it relativ= ely > simpler. Lokesh > mentioned plans to madvise NOHUGEPAGE in ART, so fixing small folios is l= ikely > the first priority. Fixing for the non-mTHP case first sounds good to me. For a large folio in swap cache maybe you can return EBUSY for now? But for that, I believe the cleanest and simplest would be to restructure move_pages_pte() such that folio is retrieved from vm_normal_foliio() if src_pte is present, otherwise, from filemap_get_folio() and then incorporate the check you have in this FRC in there. This way the entire locking dance logic in there can be reused for swap-cache case as well. > > > > > Thanks, > > > > -- > > Peter Xu > > Thanks > Barry