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 B3B68C6FD1F for ; Tue, 26 Mar 2024 16:17:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 25DC56B0092; Tue, 26 Mar 2024 12:17:23 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 20DB76B0095; Tue, 26 Mar 2024 12:17:23 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0D6136B0096; Tue, 26 Mar 2024 12:17:23 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id EE30A6B0092 for ; Tue, 26 Mar 2024 12:17:22 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 9C0F31608FA for ; Tue, 26 Mar 2024 16:17:22 +0000 (UTC) X-FDA: 81939695124.09.EE51428 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf27.hostedemail.com (Postfix) with ESMTP id CD29240016 for ; Tue, 26 Mar 2024 16:17:19 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf27.hostedemail.com: domain of ryan.roberts@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=ryan.roberts@arm.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1711469840; 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=ikxlqLFrhwL3EOrncByoqKBD1HwIxZdjUWHsNw9ELgc=; b=o0LvSx5crJXcTHj8WQLvgzA1KyqKOQK5Yt6cgjLGw6ewuEucKSovzQ2MX7/7q+N+2KeV23 9uGNxVx32fzHHX+FQts1demTfN2w+DdE6C+gk1/9rK+iUKodgO1ER4KukszhJWHeqn2EP8 xD9Hs6QMrYd4N/pW/RLz4hGg172xgSI= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf27.hostedemail.com: domain of ryan.roberts@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=ryan.roberts@arm.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711469840; a=rsa-sha256; cv=none; b=wpyJUEQBbYS7zA1Kflf11o1/nJ2uRr0DYRX50rAPXBCVfsOQcvCqkSwBEmLnqAx6Kw9X6i YiZpmEANO1GJSuuAXKhGmhPw9dLEg5uggFLGGYiJHAfTpLSPL1hQJoofVFrWRcHLqy3k/Y qIWz4k/AAIy9HK9TMi13spBn+tPPy5M= 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 6F8792F4; Tue, 26 Mar 2024 09:17:52 -0700 (PDT) Received: from [10.1.29.179] (XHFQ2J9959.cambridge.arm.com [10.1.29.179]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 1BF9A3F694; Tue, 26 Mar 2024 09:17:15 -0700 (PDT) Message-ID: Date: Tue, 26 Mar 2024 16:17:14 +0000 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2] mm: hold PTL from the first PTE while reclaiming a large folio Content-Language: en-GB To: David Hildenbrand , Barry Song <21cnbao@gmail.com>, akpm@linux-foundation.org, linux-mm@kvack.org Cc: chrisl@kernel.org, hanchuanhua@oppo.com, hughd@google.com, linux-kernel@vger.kernel.org, mhocko@suse.com, shy828301@gmail.com, v-songbaohua@oppo.com, wangkefeng.wang@huawei.com, willy@infradead.org, xiang@kernel.org, ying.huang@intel.com, yuzhao@google.com References: <20240306095219.71086-1-21cnbao@gmail.com> From: Ryan Roberts In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: CD29240016 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: znwx3rdnrchm3xiubqjw4u1dapsutfqq X-HE-Tag: 1711469839-155809 X-HE-Meta: U2FsdGVkX1/A2m8mWl1c7XSKCnce6l9Gqub+XieinRrcea5+0Nxq4RtDF+MrWKKGPHIPn63j8m1HmUM2mUMVtTQLajuDICemw4U0z+VXfSXzYKz0n3FSUpbFCPdJRVKdgt/YAcFf0zH8tW4zCusz03VzaXhyN5NOSomRVwbXWXrGtjLiA2bunINAGVBvRS4Eo6NEbB+vCKcbMn+R16/6w2NHamMc5kQ6DgKvuFwBLDQuZyANh8pl4wq1E58iuWvaK2WBucx8EsPvEwuMaT8fsis9aH7Cm1GYCMdRRYohOeyZqSMeAn90MRCbqYP+ZC+TcFqVxMb1xs6GyYjukcQu3WqP170GNDv94YNGMVPoCSHAy8OxFDtsiiaWszyha4LVblMSrHwdy7qa75QhLrJ+n9GppDxCmgJMOL0ix7mZrL5ZTzMvlHclw/qzvA4I+SD3leJFJjNPO7hlaX33Lf2S1YCcBiDAelIbJYRTkeD5oooMN2T2FOiR/DsJn09hJuB420KIrIqJJDRTeE2+vukF2wrixD75OZkSh31pYZ3abhebJeIm9yRlM5MWxra2gBYXupuuVBDaajDcXB3xtlCmkfAB+WAxsRgqyww8tthXprS+etEF2ftg8TnHa95Hycj2x7PFTvRUdH1wfy9pjCIuq7WZkucbUwvRPKu0cslJ7g+BXdArBvvujWJ61m4VLXFFlXQ3aJrRj2ckjWcX7f4O0Htnizvp5IWFm66obxyd8S5WXRG80rKACaxSYS63vdTlQrt47Cigj5V33Px/DR2nfPtz8XD4yam0CBzhXlJJmSshYOCVpJSktbCD37AvkFecn91QxKujCzMG2VohQ/vagh8SRKAGR7wi4BKY2erXvgAnPU4PovLhXj06BcR8RjBvbnah9GiSkG82KCg54s8C1nZ/q1Hb5BStAha9DHkHiQ3OgBHIT3QHQqDiDo+FGHezY7KRCv9Mr1srt9A98O7 ODhZne1V 89yplL/e6hdUdutAW4IIAjk7Hb55te8OXjXLBZ6xSks9bdNkR+7PF3fIb/qQe3OgXlldEnHHU+BdIV+nkopF1Kbi2VpkewQ56u+cN/7RvdQgjqSTfCfTRGNHF8U6tLHY5KFfZ8FckbSFb78GnxAdnNHMZuczjYp61uUO7qzkCK1Wb+sa/dQ11pKksYX+n2fB+P+XCa7/zzVUioTx6hF+IYq6VgVvtcrRVGgJWnoKENI0A5/e7qiuwfqYlSVhtP1Ktxz99 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 26/03/2024 16:10, David Hildenbrand wrote: > On 06.03.24 10:52, Barry Song wrote: >> From: Barry Song >> >> Within try_to_unmap_one(), page_vma_mapped_walk() races with other >> PTE modifications preceded by pte clear. While iterating over PTEs >> of a large folio, it only starts acquiring PTL from the first valid >> (present) PTE. PTE modifications can temporarily set PTEs to >> pte_none. Consequently, the initial PTEs of a large folio might >> be skipped in try_to_unmap_one(). >> For example, for an anon folio, if we skip PTE0, we may have PTE0 >> which is still present, while PTE1 ~ PTE(nr_pages - 1) are swap >> entries after try_to_unmap_one(). >> So folio will be still mapped, the folio fails to be reclaimed >> and is put back to LRU in this round. >> This also breaks up PTEs optimization such as CONT-PTE on this >> large folio and may lead to accident folio_split() afterwards. >> And since a part of PTEs are now swap entries, accessing those >> parts will introduce overhead - do_swap_page. >> Although the kernel can withstand all of the above issues, the >> situation still seems quite awkward and warrants making it more >> ideal. >> The same race also occurs with small folios, but they have only >> one PTE, thus, it won't be possible for them to be partially >> unmapped. >> This patch holds PTL from PTE0, allowing us to avoid reading PTE >> values that are in the process of being transformed. With stable >> PTE values, we can ensure that this large folio is either >> completely reclaimed or that all PTEs remain untouched in this >> round. >> A corner case is that if we hold PTL from PTE0 and most initial >> PTEs have been really unmapped before that, we may increase the >> duration of holding PTL. Thus we only apply this optimization to >> folios which are still entirely mapped (not in deferred_split >> list). >> >> Cc: Hugh Dickins >> Signed-off-by: Barry Song >> --- >>   v2: >>    * Refine commit message and code comment after reading all comments >>      from Ryan and David, thanks! >>    * Avoid increasing the duration of PTL by applying optimization >>      on folios not in deferred_split_list with respect to Ying's >>      comment, thanks! >> >>   mm/vmscan.c | 12 ++++++++++++ >>   1 file changed, 12 insertions(+) >> >> diff --git a/mm/vmscan.c b/mm/vmscan.c >> index 0b888a2afa58..7106741387e8 100644 >> --- a/mm/vmscan.c >> +++ b/mm/vmscan.c >> @@ -1270,6 +1270,18 @@ static unsigned int shrink_folio_list(struct list_head >> *folio_list, >>                 if (folio_test_pmd_mappable(folio)) >>                   flags |= TTU_SPLIT_HUGE_PMD; >> +            /* >> +             * Without TTU_SYNC, try_to_unmap will only begin to hold PTL >> +             * from the first present PTE within a large folio. Some initial >> +             * PTEs might be skipped due to races with parallel PTE writes >> +             * in which PTEs can be cleared temporarily before being written >> +             * new present values. This will lead to a large folio is still >> +             * mapped while some subpages have been partially unmapped after >> +             * try_to_unmap; TTU_SYNC helps try_to_unmap acquire PTL from the >> +             * first PTE, eliminating the influence of temporary PTE values. >> +             */ >> +            if (folio_test_large(folio) && list_empty(&folio->_deferred_list)) >> +                flags |= TTU_SYNC; >>                 try_to_unmap(folio, flags); >>               if (folio_mapped(folio)) { > > Hopefully this won't have unexpected performance "surprises". > > I do wonder if we should really care about the "_deferred_list" optimization > here, though, I'd just drop it. I also concluded that we do need the data_race() annotation around list_empty() if you do wind up keeping it. But I agree with David about dropping it. > > In any case > > Acked-by: David Hildenbrand >