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 A5860C021B1 for ; Wed, 19 Feb 2025 23:04:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 27BB3280278; Wed, 19 Feb 2025 18:04:56 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 22BB5280276; Wed, 19 Feb 2025 18:04:56 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0CC92280278; Wed, 19 Feb 2025 18:04:56 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id E712B280276 for ; Wed, 19 Feb 2025 18:04:55 -0500 (EST) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 682691203DC for ; Wed, 19 Feb 2025 23:04:55 +0000 (UTC) X-FDA: 83138226150.16.0A4E97C Received: from mail-vs1-f49.google.com (mail-vs1-f49.google.com [209.85.217.49]) by imf28.hostedemail.com (Postfix) with ESMTP id 8AE05C000C for ; Wed, 19 Feb 2025 23:04:53 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=aV0m7ocS; spf=pass (imf28.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.217.49 as permitted sender) smtp.mailfrom=21cnbao@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=1740006293; 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=uidLPg20wl1w97l7EABQcqabC1BxJ6CM2osXQnhShac=; b=eRuQyn1/eYEFuND0aKNbJXiTVZD4hvifk3IN8m5NQ2mVxvEGjtUYykSmxt48KFjCXnwPJx 0kspnUXobgkzX1Hqu88/99qE1LYKI8e5ITlnNH3gQDZzL3WwLQd67+IuNQBQfnWNx9MAgA i9MzatNj7DsX11P1JdpfqorSzAWZv7Q= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=aV0m7ocS; spf=pass (imf28.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.217.49 as permitted sender) smtp.mailfrom=21cnbao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1740006293; a=rsa-sha256; cv=none; b=CV7XdKrbi8YLKDEuDIwtkaNWTXujdiC+ZnLsJjfeiizk/8ztpfA+ZyU8rNmMdZ2caVf3fq dw2LZh692Pe8Ou51m1HxVvj2XQqguK/ZKtrQNcCvr9bLZM4smLGBbPYus9O6x+3Ev3fh4a hGkBF32sSQj8lxH4+XuC6EM9986SpZ0= Received: by mail-vs1-f49.google.com with SMTP id ada2fe7eead31-4be5033a2cbso169651137.1 for ; Wed, 19 Feb 2025 15:04:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1740006292; x=1740611092; 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=uidLPg20wl1w97l7EABQcqabC1BxJ6CM2osXQnhShac=; b=aV0m7ocSiVbPkXVVRI3k3KEC5zkzJI4McoVzNFZe6u+QiNe+BrrBlkrFVpZgIzDMqk W8sR/otWIW6J7H2g6yeG79Zqtcb0DX+Pv4gwad2pwgPF6jKjZw4yH74DNlQsJmciXFeH YtapXodVnlc3pDn0FGo7ZPxPqUVUX0wVqya2/9YYUFytKC7e2b1SfEm/QlxqFfxPGqW4 0jodlXKy35thsjMp3q3hf7kG4pNog/JRC0SfK1+WS91owxBp5fS5J+sfzhnSYYs/K08P h2qAh9am9/iKYHmb+dAQFoX3q/wSmcuAhX3oqD+tl0HtCsvZLRmhVYwTV+pymk08UT5B oNRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740006292; x=1740611092; 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=uidLPg20wl1w97l7EABQcqabC1BxJ6CM2osXQnhShac=; b=KKBq6Q4SqF3AbSgrdvCEbpx5N7vpQHrGO3jF+kxzHiBQJPXql3yZlbrC0uD3TcZJJV C45YEH171jFg5d4KlT1p6o+f5tNie7NfQ/u9m8DTRSLr0w+0cpKacLkvTFU7fY9mdnxM 3G25GhKsEQqx/+IL3fXYDSF/By67aNGMrzf1UAttAEEKL/neEVU4q/a8XiCtChCd1oMK sHjMCseQsGgH8kzhXpeekGETtZ4OrtU0DtjmltHR7BOP7K+X+nD+uTysVepiYUlxBkq9 r2liacFCZcKBBgAyDXTCMBZN2aiCblhhPBEOxn2y8b5lgWdWsLEq9ybx1WI+A7laDn6X gssw== X-Forwarded-Encrypted: i=1; AJvYcCX9e+yqAyGojeUSwtglsXggteDX3Kz5Hrf2YYP+BiOHduXLVTco3/7aHzvd485Snh4upwR+bLqBIg==@kvack.org X-Gm-Message-State: AOJu0YylwLMQ0V1sNeoB98NlYn+M3aNhrEaWwlldVBI+x4GvLawB6H51 gkNYAku7+Q9Ixsn3e8stFicZrdQOs5Za8TXv8be2Kc4bXwcunxVjYoAcuJ6QDKoY55wOiuRzYot QlIsbQ/GvCvJzY0YRQrJrWQJK93A= X-Gm-Gg: ASbGncuNBrIQaT7eTo2lsEIs9s4/EdXJQWSNR6k3EwG/oGeOb446fHWUERTvIJlVUWg REMaRTqQOK8N1AKXIWRQK6Fu2SDv9UVCsuU7XHdA9n/4bewyEQLM4jS7H5JQRM2GyO75HilLw X-Google-Smtp-Source: AGHT+IGrdkIY4lXGB+MUujv7Pnn0efdTUe09/ZnLHPuC85PpHDq5XcGi+qtNHxQsqy3GaZmVv/GgJadJnSWliOuj0L8= X-Received: by 2002:a05:6122:1d14:b0:520:3e1c:500f with SMTP id 71dfb90a1353d-521df8abda3mr140838e0c.8.1740006291798; Wed, 19 Feb 2025 15:04:51 -0800 (PST) MIME-Version: 1.0 References: <20250219112519.92853-1-21cnbao@gmail.com> In-Reply-To: From: Barry Song <21cnbao@gmail.com> Date: Thu, 20 Feb 2025 12:04:40 +1300 X-Gm-Features: AWEUYZleJHGLzpoDFFv-z8ffjhdATbzXIfb4efNN34LhW9ZfDLayMqbFjcrZKMg Message-ID: Subject: Re: [PATCH RFC] mm: Fix kernel BUG when userfaultfd_move encounters swapcache To: Peter Xu Cc: Suren Baghdasaryan , Lokesh Gidra , 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-Rspam-User: X-Rspamd-Server: rspam11 X-Rspamd-Queue-Id: 8AE05C000C X-Stat-Signature: u5tss4iuiit5ku7j51hngs85wsezuxra X-HE-Tag: 1740006293-8262 X-HE-Meta: U2FsdGVkX18c/EAOvKFWIXqMTUDxrFWphdJEL4vtDjm1POJ0SHEtXjXgntwy4Q1z8PT9zNHlLVnIU5sJQM3Tt6OpFbp3VOO6PfwzW2hQQVjJpSig55+qVwvMhlRMFOuM7w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Q7EbBCbeo2wJ00rb9Vv095M0dWKcynyDdhQneO115B9YcCTCGK0rEwF9IdsS9Jhesj2QRaKwo39vvbWntE8bkzYJxKL3plCHjEKHKb/wJ2UzB/3sQEgIEHFZ/ZYfiDor+7HfdPjLWa9gc3JS9ufgqJBCKYBxrIMVs0JPjET6cjfnLeVIMUNmqmYZi6tnIT4JmvGDwUjIxZp2HZmX4U0+p75pL1CiFcYfYsd67oCUoAZyNu0= 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 Thu, Feb 20, 2025 at 11:15=E2=80=AFAM Peter Xu wrote= : > > 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.com= > 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:00000000f43db19c= index:0xffffaf150 pfn:0x4667c > > > > [ 13.337520] head: order:2 mapcount:1 entire_mapcount:0 nr_pages_= mapped:1 pincount:0 > > > > [ 13.337716] memcg:ffff00000405f000 > > > > [ 13.337849] anon flags: 0x3fffc0000020459(locked|uptodate|dirty|= owner_priv_1|head|swapbacked|node=3D0|zone=3D0|lastcpupid=3D0xffff) > > > > [ 13.338630] raw: 03fffc0000020459 ffff80008507b538 ffff80008507b= 538 ffff000006260361 > > > > [ 13.338831] raw: 0000000ffffaf150 0000000000004000 0000000600000= 000 ffff00000405f000 > > > > [ 13.339031] head: 03fffc0000020459 ffff80008507b538 ffff80008507= b538 ffff000006260361 > > > > [ 13.339204] head: 0000000ffffaf150 0000000000004000 000000060000= 0000 ffff00000405f000 > > > > [ 13.339375] head: 03fffc0000000202 fffffdffc0199f01 ffffffff0000= 0000 0000000000000001 > > > > [ 13.339546] head: 0000000000000004 0000000000000000 00000000ffff= ffff 0000000000000000 > > > > [ 13.339736] page dumped because: VM_BUG_ON_PAGE(page_pgoff(folio= , 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] PR= EEMPT SMP > > > > [ 13.340969] Modules linked in: > > > > [ 13.341257] CPU: 1 UID: 0 PID: 107 Comm: a.out Not tainted 6.14.= 0-rc3-gcf42737e247a-dirty #299 > > > > [ 13.341470] Hardware name: linux,dummy-virt (DT) > > > > [ 13.341671] pstate: 60000005 (nZCv daif -PAN -UAO -TCO -DIT -SSB= S 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: 000= 0000000000001 > > > > [ 13.342404] x26: 0000000000000000 x25: 0000000000000001 x24: 000= 0000000000001 > > > > [ 13.342575] x23: 0000ffffaf0d0000 x22: 0000ffffaf0d0000 x21: fff= ffdffc0199f00 > > > > [ 13.342731] x20: fffffdffc0199f00 x19: ffff000006210700 x18: 000= 00000ffffffff > > > > [ 13.342881] x17: 6c203d2120296567 x16: 6170202c6f696c6f x15: 662= 866666f67705f > > > > [ 13.343033] x14: 6567617028454741 x13: 2929737365726464 x12: fff= f800083728ab0 > > > > [ 13.343183] x11: ffff800082996bf8 x10: 0000000000000fd7 x9 : fff= f80008011bc40 > > > > [ 13.343351] x8 : 0000000000017fe8 x7 : 00000000fffff000 x6 : fff= f8000829eebf8 > > > > [ 13.343498] x5 : c0000000fffff000 x4 : 0000000000000000 x3 : 000= 0000000000000 > > > > [ 13.343645] x2 : 0000000000000000 x1 : ffff0000062db980 x0 : 000= 000000000005f > > > > [ 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 (d4210000) > > > > [ 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 handling > > > > 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_presen= t_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 spl= it */ > > 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 only > 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 y= ou 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, but the folio is still in swapcache - the current code does move_swap_pte() but= does not fixup folio->index within swapcache. My point is that if we want a proper fix for mTHP, we'd better handle write= back. Otherwise, this isn=E2=80=99t much different from directly returning -EBUSY= as proposed in this RFC. For small folios, there=E2=80=99s no split_folio issue, making it relativel= y simpler. Lokesh mentioned plans to madvise NOHUGEPAGE in ART, so fixing small folios is lik= ely the first priority. > > Thanks, > > -- > Peter Xu Thanks Barry