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 1142FC54798 for ; Tue, 5 Mar 2024 08:56:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 69E9A6B0080; Tue, 5 Mar 2024 03:56:17 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 64ECF6B0081; Tue, 5 Mar 2024 03:56:17 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 516C06B0082; Tue, 5 Mar 2024 03:56:17 -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 408B56B0080 for ; Tue, 5 Mar 2024 03:56:17 -0500 (EST) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id E356F1C0E16 for ; Tue, 5 Mar 2024 08:56:16 +0000 (UTC) X-FDA: 81862378752.02.89AC1DE Received: from mail-vk1-f181.google.com (mail-vk1-f181.google.com [209.85.221.181]) by imf25.hostedemail.com (Postfix) with ESMTP id 4C2C2A000E for ; Tue, 5 Mar 2024 08:56:15 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=c2oXXfBp; spf=pass (imf25.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.221.181 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=1709628975; 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=xf7eeKiUo+Z6E3AtxwZ6h2eHT+M9RMwHIVhw9daL31I=; b=WTDhYmR7MVErg9qBHtMFwjk51OWYEg/3uIgkekuTWdjw4A9LCOdb+Xqs0SyRmTcXIf5r/r lyt4Q4xSabeCN5etJR9qb13qX9IQqHYxzESCWn1eOLp22N+hX0IYhCsrWrXKTT08CkDy1s 9rVSqPHCkooXInrnFLOJW+PVEPOJbvo= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1709628975; a=rsa-sha256; cv=none; b=1S51AAIO5k8Nb0rvnsOOPgOSz2SJOedWQrqZd74Zwp+8174KFzhWD6L/wQptpAzk8PklAN lYwJaIt69XYkZgg+MOAEF4naVsC5zDbENTUy6eXFjujZf7xvNK19SibfPBVvZJNKnY3OkU qQFpsuWKCU0EieI6n+mWtqHo2dVThmo= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=c2oXXfBp; spf=pass (imf25.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.221.181 as permitted sender) smtp.mailfrom=21cnbao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-vk1-f181.google.com with SMTP id 71dfb90a1353d-4ce2cd0cee2so1383500e0c.2 for ; Tue, 05 Mar 2024 00:56:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709628974; x=1710233774; 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=xf7eeKiUo+Z6E3AtxwZ6h2eHT+M9RMwHIVhw9daL31I=; b=c2oXXfBpOw/9g3jaGzjty99rzG6epow+vYyD96GB9J8jbJ7JvZgkVLA6sFJN2cH1xY 77zqAb++lbo7e6vC0D+qNWoAOTsChSlbhnChPyxrWasvu+T6CyDWGyT38Oft+UBxU21e ECl+oI4VIY952UbSYnxSslEWYJYDnW4iEE1CHnHMygvUGgVOLN+5Nvd7pCidvOogmVGO rFGjLh2QVscymEWBbIW1eDjrRpaBkxN+6R1fNjIDsGRXkylKffopNkidZpv80c2U+HM5 5drQgbcPn3O8ZUFB99lxk6jaj2oe2PescKp2qWjFQBCDYYmaM0+Aw2uJCOsbJnYe6QRe 8TdQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709628974; x=1710233774; 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=xf7eeKiUo+Z6E3AtxwZ6h2eHT+M9RMwHIVhw9daL31I=; b=bX3djmvWO5kTi5r2eWU+8yeQn5T5ZfojCYpba6LHfUux/T0Z80ajcz9ON83f6gdaHw oQ85bWf0SJWG85kH/mL1Ee0jdMSqZKB+NF1BepAdGnXOopEWazWnrgDDp2GvBIFzQfkl o9nJsSUO3ITL62LDW8hajAiHGFz/ZscJKoj9PM8kYK39uVa50DHqglPjMgFQ5MMBAgNS vsza4JTswVHMGYgR0aLVSMKHurLtuPuqLbRF0cP12BnlievEAV+GSaLW78B6u24rHdaI ghiKq5+eMDoGHYQZnOLR+kvvNyM+86QEgfAGRBuHDi5Yo5RPk2KpkWeS1tt3dlAbfV6H gG7Q== X-Forwarded-Encrypted: i=1; AJvYcCWDYLRHlD+VLncMz+YR4qTK2/GZwKM8VVS8VUB7SrdaPNchBvvkAMXUfTGP3iKWkvsIFChuBsb5gVq6iRVXG6z9sSg= X-Gm-Message-State: AOJu0Yz7jDWGh5xCz6w8XcP9yBsbmV41JDm5QbWlyuGjmMzyUOpTL9yu tw+qlhcftJ86G2PyxouwbcTFU/xEaP7gq9oyv9WYqu8vLSpBtyqYP+7QY6Z9B6i8ULyfR3vLqFk z300/R9vqwo+0SEjujMTyIcu8ZFU= X-Google-Smtp-Source: AGHT+IH27VErZkiLtO2IdHog333rbZI3ycr1XH26aXwrNf1p+sK4n3gGZBqRc/BxEFNaQi/YkEnS/mfqo1Q98C8D+GE= X-Received: by 2002:ac5:ce02:0:b0:4cd:20ea:35b1 with SMTP id j2-20020ac5ce02000000b004cd20ea35b1mr874707vki.2.1709628974288; Tue, 05 Mar 2024 00:56:14 -0800 (PST) MIME-Version: 1.0 References: <20240304103757.235352-1-21cnbao@gmail.com> <878r2x9ly3.fsf@yhuang6-desk2.ccr.corp.intel.com> In-Reply-To: <878r2x9ly3.fsf@yhuang6-desk2.ccr.corp.intel.com> From: Barry Song <21cnbao@gmail.com> Date: Tue, 5 Mar 2024 21:56:02 +1300 Message-ID: Subject: Re: [RFC PATCH] mm: hold PTL from the first PTE while reclaiming a large folio To: "Huang, Ying" Cc: akpm@linux-foundation.org, linux-mm@kvack.org, david@redhat.com, ryan.roberts@arm.com, chrisl@kernel.org, yuzhao@google.com, hanchuanhua@oppo.com, linux-kernel@vger.kernel.org, willy@infradead.org, xiang@kernel.org, mhocko@suse.com, shy828301@gmail.com, wangkefeng.wang@huawei.com, Barry Song , Hugh Dickins Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Stat-Signature: j9zgy3bby3nkfz3yz5mboupnbkezxwqy X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 4C2C2A000E X-Rspam-User: X-HE-Tag: 1709628975-914937 X-HE-Meta: U2FsdGVkX1+Q/ye273ZcgQ6ftvm8DC5ztdG2Y8iNAEqFlR5M5IYHJfQxKSvAJRMrnyJBQl82E7hdAvaJvpCtKAPiGAiyCHLyHP2+2SDAbNVa9malSi7MZ+PQzLFlKzVf53aFzKH+k43gUIwz/rYLoHfupEy+cXyp2Sf8nS9JPUtmeidbz10qubGYeMpZ5o2ZNiTs21vKm8FgAMGT+/bvQu+prF/GM7wGs5ULfg60wAjD0Po8uu6vr9rrS6/G6jgMJngvcXuHKNfQ7kanJomeMO/1e3hy2AGFflo1UCphMJMNRANA8zeCcge9q7l4gyE6gkW4zLdGCWfPqDTZpE50M11mA252+yhMBqLC8uQeObgk6w+e0IuDsjGosz1hr3SsVLsd/qWnVKjXdk09C9xmW8+ASkz2jwR3KoyQSoLbKtTMY/p8zWADt8iWhkKR3ZJ/BSISWUDxAJ+dpt9+yg1EpKAkjnrpLeO45JDXvnKoIP0Ir1KAlB+Q18YxaUWvSunhOtJOiaLXHNdv1FGCGrfvsoIPebnVQesHGTlEAilHz+PlE7+aifq+M43LnDsTYT085gv7rOE94MY0492bytra307IZHvXjFdRuEBpLSCPpcvMRVJZ8ZkqsAaqoyvtbVUN+JsQOPp5AmHnWvSAU3E6eEZBiVsFCo0DZlsjzJLiVyjbE9A4l1wtQAlFMXnPaz/wp/9AEPUXHb2Yv+9EALcJacAI/AfbGwimLT6Yt9EaZW2BwHuGQL8K9jpPoGhJoZxky+DeIxicK+k7cykyAE+ZDCgTrxBgUQo5NCicI7wHlKF4lo9PbJaPskpeDIjdsMnhsbI0/O4Ou43k0X0dR/sx3+lVxAbdNy1olR4jrS+KhuRRR4Ims110ThTvfU9aa8X/JUfIwgBYTMpCP6ZJcEfPc8tSRKMoy2IXhHCqiU7TifOJQ885lkjJC9i4Q4S1LuB0thOVaCd0fXE1WNbrRe9 AEWEgTm4 daX1fHSP/EnVQmqvMO9LcjdF/rcj6rP1jpKma X-Bogosity: Ham, tests=bogofilter, spamicity=0.000002, 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 Tue, Mar 5, 2024 at 8:30=E2=80=AFPM Huang, Ying w= rote: > > Barry Song <21cnbao@gmail.com> writes: > > > From: Barry Song > > > > page_vma_mapped_walk() within try_to_unmap_one() races with other > > PTEs modification such as break-before-make, while iterating PTEs > > Sorry, I don't know what is "break-before-make", can you elaborate? > IIUC, ptep_modify_prot_start()/ptep_modify_prot_commit() can clear PTE > temporarily, which may cause race with page_vma_mapped_walk(). Is that > the issue that you try to fix? we are writing pte to zero(break) before writing a new value(make). while this behavior is within PTL in another thread, page_vma_mapped_walk() of try_to_unmap_one thread won't take PTL till it meets a present PTE. for example, if another threads are modifying nr_pages PTEs under PTL, but we don't hold PTL, we might skip one or two PTEs at the beginning of a large folio. For a large folio, after try_to_unmap_one(), we may result in PTE0 and PTE1 untouched but PTE2~nr_pages-1 are set to swap entries. by holding PTL from PTE0 for large folios, we won't get these intermediate values. At the moment we get PTL, other threads have done. > > -- > Best Regards, > Huang, Ying > > > of a large folio, it will only begin to acquire PTL after it gets > > a valid(present) PTE. break-before-make intermediately sets PTEs > > to pte_none. Thus, a large folio's PTEs might be partially skipped > > in try_to_unmap_one(). > > For example, for an anon folio, after try_to_unmap_one(), we may > > have PTE0 present, while PTE1 ~ PTE(nr_pages - 1) are swap entries. > > So folio will be still mapped, the folio fails to be reclaimed. > > What=E2=80=99s even more worrying is, its PTEs are no longer in a unifi= ed > > state. This might lead to accident folio_split() afterwards. And > > since a part of PTEs are now swap entries, accessing them will > > incur page fault - do_swap_page. > > It creates both anxiety and more expense. While we can't avoid > > userspace's unmap to break up unified PTEs such as CONT-PTE for > > a large folio, we can indeed keep away from kernel's breaking up > > them due to its code design. > > This patch is holding PTL from PTE0, thus, the folio will either > > be entirely reclaimed or entirely kept. On the other hand, this > > approach doesn't increase PTL contention. Even w/o the patch, > > page_vma_mapped_walk() will always get PTL after it sometimes > > skips one or two PTEs because intermediate break-before-makes > > are short, according to test. Of course, even w/o this patch, > > the vast majority of try_to_unmap_one still can get PTL from > > PTE0. This patch makes the number 100%. > > The other option is that we can give up in try_to_unmap_one > > once we find PTE0 is not the first entry we get PTL, we call > > page_vma_mapped_walk_done() to end the iteration at this case. > > This will keep the unified PTEs while the folio isn't reclaimed. > > The result is quite similar with small folios with one PTE - > > either entirely reclaimed or entirely kept. > > Reclaiming large folios by holding PTL from PTE0 seems a better > > option comparing to giving up after detecting PTL begins from > > non-PTE0. > > > > Cc: Hugh Dickins > > Signed-off-by: Barry Song > > --- > > mm/vmscan.c | 11 +++++++++++ > > 1 file changed, 11 insertions(+) > > > > diff --git a/mm/vmscan.c b/mm/vmscan.c > > index 0b888a2afa58..e4722fbbcd0c 100644 > > --- a/mm/vmscan.c > > +++ b/mm/vmscan.c > > @@ -1270,6 +1270,17 @@ static unsigned int shrink_folio_list(struct lis= t_head *folio_list, > > > > if (folio_test_pmd_mappable(folio)) > > flags |=3D TTU_SPLIT_HUGE_PMD; > > + /* > > + * if page table lock is not held from the first = PTE of > > + * a large folio, some PTEs might be skipped beca= use of > > + * races with break-before-make, for example, PTE= s can > > + * be pte_none intermediately, thus one or more P= TEs > > + * might be skipped in try_to_unmap_one, we might= result > > + * in a large folio is partially mapped and parti= ally > > + * unmapped after try_to_unmap > > + */ > > + if (folio_test_large(folio)) > > + flags |=3D TTU_SYNC; > > > > try_to_unmap(folio, flags); > > if (folio_mapped(folio)) { Thanks Barry