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 A9E0DC48BF6 for ; Mon, 4 Mar 2024 12:21:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 240B16B0071; Mon, 4 Mar 2024 07:21:07 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1F0AE6B0072; Mon, 4 Mar 2024 07:21:07 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0B8746B0074; Mon, 4 Mar 2024 07:21:07 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id E7DEA6B0071 for ; Mon, 4 Mar 2024 07:21:06 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 945CA160558 for ; Mon, 4 Mar 2024 12:21:06 +0000 (UTC) X-FDA: 81859266132.28.DDE0EDC Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf29.hostedemail.com (Postfix) with ESMTP id 71512120013 for ; Mon, 4 Mar 2024 12:21:04 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=none; spf=pass (imf29.hostedemail.com: domain of ryan.roberts@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=ryan.roberts@arm.com; dmarc=pass (policy=none) header.from=arm.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1709554864; 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; bh=ntUPsd1ROgL96QRGVG8beDJwvESFOMCWrLAHUB/AzNk=; b=PmmXtqPbMWsy5w1kFaK1xXB0YlMcrBEX1IDOZ5zlXndzRgfpd6DBwwQKZVNWwgtOky9lAC DuCAUakvGvDQUXUpxhj59QSeArvoYgwW6TcUo3hPmE95r6uM/7DZhYbqsx9PmNIaomE++y d8cMPCKg0rMjuTOrZWrQVwUdYzgK2Ec= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=none; spf=pass (imf29.hostedemail.com: domain of ryan.roberts@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=ryan.roberts@arm.com; dmarc=pass (policy=none) header.from=arm.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1709554865; a=rsa-sha256; cv=none; b=DflqzJj4gqIK6EY6obN89r79RRSSeKlqUPJUzvoeLHQPAj8Mj6GYzqSg93DnGkuhoPuJ+q Ia0hkUGRng9U53voU2Huv08Hiy5YId86vQF9xng99qLpSj540y68GgKwCSRcQgY5VsbJn1 Rjpyb1x6NMS1ofgrw3xWXe7Ytiq6ujc= Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id D500F1FB; Mon, 4 Mar 2024 04:21:39 -0800 (PST) Received: from [10.57.68.92] (unknown [10.57.68.92]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 5B0943F738; Mon, 4 Mar 2024 04:21:00 -0800 (PST) Message-ID: <706b7129-85f6-4470-9fd9-f955a8e6bd7c@arm.com> Date: Mon, 4 Mar 2024 12:20:58 +0000 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [RFC PATCH] mm: hold PTL from the first PTE while reclaiming a large folio Content-Language: en-GB To: Barry Song <21cnbao@gmail.com>, akpm@linux-foundation.org, linux-mm@kvack.org Cc: david@redhat.com, chrisl@kernel.org, yuzhao@google.com, hanchuanhua@oppo.com, linux-kernel@vger.kernel.org, willy@infradead.org, ying.huang@intel.com, xiang@kernel.org, mhocko@suse.com, shy828301@gmail.com, wangkefeng.wang@huawei.com, Barry Song , Hugh Dickins References: <20240304103757.235352-1-21cnbao@gmail.com> From: Ryan Roberts In-Reply-To: <20240304103757.235352-1-21cnbao@gmail.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: 71512120013 X-Rspam-User: X-Stat-Signature: ek3q5phx794i55od964dgzk1igi43xdb X-Rspamd-Server: rspam01 X-HE-Tag: 1709554864-691255 X-HE-Meta: U2FsdGVkX1/oxGsgyiGTL4gewUWupe4fLThKXWQD17whmabWv9Hefsz2EdQHra995L2PF1dvz4SGjJwvWb4FDgzTOVoJ7uz8JXIxpm24srlqVSz0Y6lpTND1f55IKDC4MFpWmcMeJUDpa4vg3oQyzbwzpuf0X3eOyCcCtIAMJYNeyhMKygcKgSvtTBf4D5lnJVtJu5IDBINuVk/ILSDKi0uZFjAiRg1TRpZfOYetZ7p1OQc/cDxOnqYiIQbsP0hiNHfPsIkFLRN8ZLXzlHvKR/6t5mMzud904fqNpQ+29jIjTCso6QIk+hGqX8mpswswB1BkYRSP4kj/yG8KIHcYiYYp34DB8Zy6nAQAYW3cA5UU1pFXuNI762Kpp4rql5JT4gzgLILd6EW43hrQNhc57lP/Gxxi0Jq6niyNYyB/ly6lFAkc2TvcRVJfudEHnkkutCNZUWU/ejAHc1yVeoQPBZGOw8s8ZLu/pm8yx7q2JmqfFdWRHCcFOylpbqI+Mdpso1PoQ+4Q63tdhNS64tWa7g2M8r2E+UUazMLfpjZdz2URBxZ2FXotBdjl+MK4ohpSPcu718RJSuUA1joZZqSGPNTf/b04mU7Ncp4DZPMxGu/qbAphQsZsSQu8xUD/iZKOvqtbW02zagmH2LKNFz3zz8V2y9v6ldTp8oN+YEJ66EoIQ05XxoTxeB3E/Mj2RYdVP8ZCM4u996aFhkTcUZH2FPzpn3OjZw6vBfifznu57W2XSV61zTWHtlyNzbFZpppgMNEdkqO2lSoscahgCC1BqftaWiqBCZ/9ENDVJmtVVby498d61rd+wB9npMCdOShpZZ1CY+Ixo7SgmAfF6WIa2kWS6nHrJLZuM+kaXIe5AFtNRaphY0Jy0CTyVBzGsLR+cEM2wuE+mCO4QV+Kthw/wgBrhvAeznQhMj3lnzobEhW4iNXg5mlt5GBRNUJ6lhLQIupJ+BS5aUcC88MEqyk 9FyK6Y97 eYvq7RCfZgA/g19HOXvNj2kU/cMdqu0PbxRUbrWa4dbsiUi1u1rSwOhpwcHllrBvWFlw2cXmIscubF+hGVSCwjDyXggpSYSWWT9319/9TNxdk/Pi0nc/lsxxgWcjm+fJOTNnQPWMEYjzW7nlv5/l23VpwkkyClqlC9U61K2Ze7IGrWyYxe9bcrHlExUisxJdg8N0vGXTJCFYBWAUSW4gXxQoMHbNyUOojjvr7 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: Hi Barry, On 04/03/2024 10:37, Barry Song wrote: > 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 > 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(). I just want to check my understanding here - I think the problem occurs for PTE-mapped, PMD-sized folios as well as smaller-than-PMD-size large folios? Now that I've had a look at the code and have a better understanding, I think that must be the case? And therefore this problem exists independently of my work to support swap-out of mTHP? (From your previous report I was under the impression that it only affected mTHP). Its just that the problem is becoming more pronounced because with mTHP, PTE-mapped large folios are much more common? > 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’s even more worrying is, its PTEs are no longer in a unified > 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 Do we need a Fixes tag? > --- > 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 list_head *folio_list, > > if (folio_test_pmd_mappable(folio)) > flags |= TTU_SPLIT_HUGE_PMD; > + /* > + * if page table lock is not held from the first PTE of > + * a large folio, some PTEs might be skipped because of > + * races with break-before-make, for example, PTEs can > + * be pte_none intermediately, thus one or more PTEs > + * might be skipped in try_to_unmap_one, we might result > + * in a large folio is partially mapped and partially > + * unmapped after try_to_unmap > + */ > + if (folio_test_large(folio)) > + flags |= TTU_SYNC; This looks sensible to me after thinking about it for a while. But I also have a gut feeling that there might be some more subtleties that are going over my head, since I'm not expert in this area. So will leave others to provide R-b :) Thanks, Ryan > > try_to_unmap(folio, flags); > if (folio_mapped(folio)) {