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 D5F24C4345F for ; Mon, 15 Apr 2024 17:54:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3B7856B008A; Mon, 15 Apr 2024 13:54:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 367A96B008C; Mon, 15 Apr 2024 13:54:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 208356B0092; Mon, 15 Apr 2024 13:54:55 -0400 (EDT) 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 F35DD6B008A for ; Mon, 15 Apr 2024 13:54:54 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id A756A80799 for ; Mon, 15 Apr 2024 17:54:54 +0000 (UTC) X-FDA: 82012516908.14.208E71D Received: from mail-lf1-f46.google.com (mail-lf1-f46.google.com [209.85.167.46]) by imf22.hostedemail.com (Postfix) with ESMTP id 8AD52C000B for ; Mon, 15 Apr 2024 17:54:52 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=htNEoN7K; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf22.hostedemail.com: domain of shy828301@gmail.com designates 209.85.167.46 as permitted sender) smtp.mailfrom=shy828301@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1713203692; 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=D5yI4YGbKBikU8AcE9UGGcfp9pwh4H8/pFo+VhmnALw=; b=TlxdGclGW7wX3vwIXixv4fJrg8WV735NXUAN0jPuFsk0I8nHhhl8J2v5nd3ik7Pmja5xRz 4/WQYQ8UhlgL8j9fgKXm3rkTG6KkojaFswDPElUJzgrlUBNmUfmTwyGQdVWbTlGRy67Bhw P2wBGgFhuKzj4E8/L0nT8Oc2hi20QMg= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=htNEoN7K; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf22.hostedemail.com: domain of shy828301@gmail.com designates 209.85.167.46 as permitted sender) smtp.mailfrom=shy828301@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1713203692; a=rsa-sha256; cv=none; b=V5TUvxI1CuuEb0k86yNXGK/I940qqp7lgUFdc/ply5XpUIohEFhRnI/F1LSioflhbyy4Yb aKqLRe/okMwUTxwoY+ApST7EAWGw49G2vUzGiYMsvMyFb7yWo6SwRKfToLR1TlmRaYB0p3 ktC4+uQC7E+wBNbp6rHC+O1u3WntISM= Received: by mail-lf1-f46.google.com with SMTP id 2adb3069b0e04-518931f8d23so2504038e87.3 for ; Mon, 15 Apr 2024 10:54:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1713203690; x=1713808490; 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=D5yI4YGbKBikU8AcE9UGGcfp9pwh4H8/pFo+VhmnALw=; b=htNEoN7Kfdm1GFFx53FL1pc3j4QlKKSru7qzOAWkH2z7myx/Fwcko0uNShnSwut5aL iIbnhKYtD6C5pZlOfQE1rsfJVif29JUMGUerM1aebUcIlTYwX7GxiiilY6dX3sytWJhE oPdJ3SRqYxzcmpDk76Rk82nAb6eW3kSgQkzGXTPZkZmiFY9bqo/EU8uJB+cvxttfZT3v nAXjwdJpLHc9+TfV9cTmZYYvvEDSN9XNY/MkHFMnkEKV2SkEK1uJuGpqmE869aERrHQh nF2n/BVjxIaeZpJjAipZtUZg+cnsYeDuHp9k/YxFVxHc2ZGb4yNd14J7DHkvb86MVsbv Uovw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713203691; x=1713808491; 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=D5yI4YGbKBikU8AcE9UGGcfp9pwh4H8/pFo+VhmnALw=; b=NdJqGzt3WI9oOe03q50gNtaHMJol+471mKisgrwxueRuitkiYMM11jJQLhpUHs2uza PJyrVZN1qlvH9myXwY78Zcq6zD3He7HVav7YB7YGpXNmZBFpzU3mfPfTj/aTdeuMJ9Mr 9LOFmPk8L+DeevhubwfY3U8LfhsJlIkkvNnfMzuRAd2lA9kCBSVP9KVo6KInNAMPM+0y zjKixGfUZs7VeSRjmwL8VL9mf1SXWSw9N8PIqQli+JLzAs+8ndxGd0fJKnmlrRARMHCN iL4px90oz4HUn4snlR0cCfoomsv75DdgHQzanj0QOZNcEfOYTslfRHVk0ffxTA+1dkW5 WTOg== X-Forwarded-Encrypted: i=1; AJvYcCXNVFx0K1OsA3Dj7sFrrFlH6c6hZGDdFczBhgRSEydn2QNiT9ncGe2NY8d9JmuCA/Z4Ag/fUWl15Oq2Nw9FhqvYPQU= X-Gm-Message-State: AOJu0YymNxMgzOl4+z/OQMcNd3hsApzlXNSg1G8j4KJtbx2l1jt4YYnV +W2xl+kS+JsY7m+zJ2Cw939ISQ2qW9DDA4Vu7sGOoaVHjVb8/KZ1nTGidG4FEFb9VB3aObtLzW1 4OcxOO/bqnKEXGBeMFfcDzTLqErA= X-Google-Smtp-Source: AGHT+IFnySQ7OQBwXRANZblJ5BSLCbR+oIRObEDkGHfVRnB9X/PiogR7rMPhTtVWXYXLGE39Qj4dY8it4GcT/uw7PdA= X-Received: by 2002:a05:6512:401b:b0:516:9e97:d3ee with SMTP id br27-20020a056512401b00b005169e97d3eemr8377040lfb.53.1713203690360; Mon, 15 Apr 2024 10:54:50 -0700 (PDT) MIME-Version: 1.0 References: <20240411153232.169560-1-zi.yan@sent.com> <2C698A64-268C-4E43-9EDE-6238B656A391@nvidia.com> <60049ec1-df14-4c3f-b3dd-5d771c2ceac4@redhat.com> In-Reply-To: <60049ec1-df14-4c3f-b3dd-5d771c2ceac4@redhat.com> From: Yang Shi Date: Mon, 15 Apr 2024 10:54:38 -0700 Message-ID: Subject: Re: [PATCH] mm/rmap: do not add fully unmapped large folio to deferred split list To: David Hildenbrand Cc: Zi Yan , linux-mm@kvack.org, Andrew Morton , Matthew Wilcox , Ryan Roberts , Barry Song <21cnbao@gmail.com>, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspam-User: X-Stat-Signature: by4e1916aikpnrrhqnkh9wo6eadyeyip X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 8AD52C000B X-HE-Tag: 1713203692-453578 X-HE-Meta: U2FsdGVkX19JorsPAP+m55fMfyJmMkyREz+xecvmfXR7sICkIkBFwj3oYwd4CC0kELRuF21/Zl5b/zqS/fIk7KfU+gSqrsoIoPaMJ1cjvBpP56FJbFa2+reY7BNNCjokD2TaRO5wREIqogCR7ob1KHfW/lNZ9s9KCgg21ZJVBqWJNmYQ0LfevVK2hwa2vWmWnV3DjEQlf5Xq/ZpxQC9SGoh7uurY/AxBiwvSIfCWzA+mIGjp96977KfNqDzpxANRWFiLSWq2u9tzyzPBFPg/GxbzHEyqx3oFVXp4NLP7NfErL+lc8s4nD8I3MT/ClEvfjVEN+RQHmxi+XxCjpTC9CqTmEN8e3cjXgdkTRoLFH6l8f56TQoMOHDEn8Bu04OYk/J9vEhsL49eI6QRHu9qdS8hNQhlNYTsc0Bnub52B00bAobVxFNEUgLLDM8ZVNsdi6N6q79zF/hXGVzqXEA37iJQxZQ0gdzwt0M3zYY8Umk06xHpgcwpvx1twkvMbH7wcn+OS1KbFdourLs3r/k78DC7Vu7rWzPg2zyTD7vEL+/R7hzb6YHqStGPu8FNOnyidRtuWiGkBSZbMLElMCCdH3Som2vieE/toRMmyw/oNUyz+CpiFVXihzyZNaNo3f+2o/riHoZKbMT1GdHsc2JyhZufi9LWGC3yhCzxkZn0TNi5d47WV3+/eEddW8KDitQ1BpTVqm5XU2eXqiVGJlfnYB1zhQ3FJiahWXve/plpxWHBs7aNdXnvr2PiDxozMDyVXiu4HupK2wG4035FzJvwEhA6XZcO8J7p0Icbwik9XPuPjzqPeVl3224nOcUQH7B4VqJ4ST935VWk0bnjeh5SKRvOUAdYna9Jqb4G6ATWLqrBPjNENTWykYxpTtvCbedFpfG5qNLutDqNQ5+Sn+VfgDkp0L11frOwyn2+Naz4YZnEXAviqNye2gthr5IyVRN368p3lCQ23RxiH0FfSJCo 4YXPPuzt 5lVopCfi7KmSM4K0kZLusU4rM66PAejxAMLfSjJyf9aU65yUA+Y3AfSsyJJtF71eoNf8YHc/XxREotWPmcMm0YTX1P8f1FEDUf+kW9iTzlb7mR2mfQVyCR4VvhY00mjB6BFH+FCFbAkhig0ugUFEnJmlCJhyv35Y8xSg09SmcZzou1UYNTI5GjWlJ0OdLzRFApVuAJ7yrCH9HOyDYTwyuNFskxLS2DhlNTFRLYcm/XG3sSyAIoH3jpiE7/9gJw9Mh/SCPcA16rN8abhkzQzW97TUT9ggSOH+XEnxst+V6bCN6z+YEer54bR1s2Ou30L81dltW2sgMcXG8NMOwkZpiO6oAfriDmE5OPbiSyZHZL3aaHVM= 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 Mon, Apr 15, 2024 at 8:40=E2=80=AFAM David Hildenbrand wrote: > > On 13.04.24 00:29, Yang Shi wrote: > > On Fri, Apr 12, 2024 at 2:06=E2=80=AFPM Zi Yan wrote: > >> > >> On 12 Apr 2024, at 15:32, David Hildenbrand wrote: > >> > >>> On 12.04.24 16:35, Zi Yan wrote: > >>>> On 11 Apr 2024, at 11:46, David Hildenbrand wrote: > >>>> > >>>>> On 11.04.24 17:32, Zi Yan wrote: > >>>>>> From: Zi Yan > >>>>>> > >>>>>> In __folio_remove_rmap(), a large folio is added to deferred split= list > >>>>>> if any page in a folio loses its final mapping. It is possible tha= t > >>>>>> the folio is unmapped fully, but it is unnecessary to add the foli= o > >>>>>> to deferred split list at all. Fix it by checking folio mapcount b= efore > >>>>>> adding a folio to deferred split list. > >>>>>> > >>>>>> Signed-off-by: Zi Yan > >>>>>> --- > >>>>>> mm/rmap.c | 9 ++++++--- > >>>>>> 1 file changed, 6 insertions(+), 3 deletions(-) > >>>>>> > >>>>>> diff --git a/mm/rmap.c b/mm/rmap.c > >>>>>> index 2608c40dffad..d599a772e282 100644 > >>>>>> --- a/mm/rmap.c > >>>>>> +++ b/mm/rmap.c > >>>>>> @@ -1494,7 +1494,7 @@ static __always_inline void __folio_remove_r= map(struct folio *folio, > >>>>>> enum rmap_level level) > >>>>>> { > >>>>>> atomic_t *mapped =3D &folio->_nr_pages_mapped; > >>>>>> - int last, nr =3D 0, nr_pmdmapped =3D 0; > >>>>>> + int last, nr =3D 0, nr_pmdmapped =3D 0, mapcount =3D 0; > >>>>>> enum node_stat_item idx; > >>>>>> __folio_rmap_sanity_checks(folio, page, nr_pages, leve= l); > >>>>>> @@ -1506,7 +1506,8 @@ static __always_inline void __folio_remove_r= map(struct folio *folio, > >>>>>> break; > >>>>>> } > >>>>>> - atomic_sub(nr_pages, &folio->_large_mapcount); > >>>>>> + mapcount =3D atomic_sub_return(nr_pages, > >>>>>> + &folio->_large_mapcount) += 1; > >>>>> > >>>>> That becomes a new memory barrier on some archs. Rather just re-rea= d it below. Re-reading should be fine here. > >>>> > >>>> Would atomic_sub_return_relaxed() work? Originally I was using atomi= c_read(mapped) > >>>> below, but to save an atomic op, I chose to read mapcount here. > >>> > >>> Some points: > >>> > >>> (1) I suggest reading about atomic get/set vs. atomic RMW vs. atomic > >>> RMW that return a value -- and how they interact with memory barriers= . > >>> Further, how relaxed variants are only optimized on some architecture= s. > >>> > >>> atomic_read() is usually READ_ONCE(), which is just an "ordinary" mem= ory > >>> access that should not be refetched. Usually cheaper than most other = stuff > >>> that involves atomics. > >> > >> I should have checked the actual implementation instead of being foole= d > >> by the name. Will read about it. Thanks. > >> > >>> > >>> (2) We can either use folio_large_mapcount() =3D=3D 0 or !atomic_read= (mapped) > >>> to figure out if the folio is now completely unmapped. > >>> > >>> (3) There is one fundamental issue: if we are not batch-unmapping the= whole > >>> thing, we will still add the folios to the deferred split queue. Migr= ation > >>> would still do that, or if there are multiple VMAs covering a folio. > >>> > >>> (4) We should really avoid making common operations slower only to ma= ke > >>> some unreliable stats less unreliable. > >>> > >>> > >>> We should likely do something like the following, which might even be= a bit > >>> faster in some cases because we avoid a function call in case we unma= p > >>> individual PTEs by checking _deferred_list ahead of time > >>> > >>> diff --git a/mm/rmap.c b/mm/rmap.c > >>> index 2608c40dffad..356598b3dc3c 100644 > >>> --- a/mm/rmap.c > >>> +++ b/mm/rmap.c > >>> @@ -1553,9 +1553,11 @@ static __always_inline void __folio_remove_rma= p(struct folio *folio, > >>> * page of the folio is unmapped and at least one pa= ge > >>> * is still mapped. > >>> */ > >>> - if (folio_test_large(folio) && folio_test_anon(folio)= ) > >>> - if (level =3D=3D RMAP_LEVEL_PTE || nr < nr_pm= dmapped) > >>> - deferred_split_folio(folio); > >>> + if (folio_test_large(folio) && folio_test_anon(folio)= && > >>> + (level =3D=3D RMAP_LEVEL_PTE || nr < nr_pmdmapped= ) && > >>> + atomic_read(mapped) && > >>> + data_race(list_empty(&folio->_deferred_list))) > >> > >> data_race() might not be needed, as Ryan pointed out[1] > >> > >>> + deferred_split_folio(folio); > >>> } > >>> > >>> I also thought about handling the scenario where we unmap the whole > >>> think in smaller chunks. We could detect "!atomic_read(mapped)" and > >>> detect that it is on the deferred split list, and simply remove it > >>> from that list incrementing an THP_UNDO_DEFERRED_SPLIT_PAGE event. > >>> > >>> But it would be racy with concurrent remapping of the folio (might ha= ppen with > >>> anon folios in corner cases I guess). > >>> > >>> What we can do is the following, though: > >>> > >>> diff --git a/mm/huge_memory.c b/mm/huge_memory.c > >>> index dc30139590e6..f05cba1807f2 100644 > >>> --- a/mm/huge_memory.c > >>> +++ b/mm/huge_memory.c > >>> @@ -3133,6 +3133,8 @@ void folio_undo_large_rmappable(struct folio *f= olio) > >>> ds_queue =3D get_deferred_split_queue(folio); > >>> spin_lock_irqsave(&ds_queue->split_queue_lock, flags); > >>> if (!list_empty(&folio->_deferred_list)) { > >>> + if (folio_test_pmd_mappable(folio)) > >>> + count_vm_event(THP_UNDO_DEFERRED_SPLIT_PAGE); > >>> ds_queue->split_queue_len--; > >>> list_del_init(&folio->_deferred_list); > >>> } > >>> > >>> Adding the right event of course. > >>> > >>> > >>> Then it's easy to filter out these "temporarily added to the list, bu= t never split > >>> before the folio was freed" cases. > >> > >> So instead of making THP_DEFERRED_SPLIT_PAGE precise, use > >> THP_DEFERRED_SPLIT_PAGE - THP_UNDO_DEFERRED_SPLIT_PAGE instead? That s= hould work. > > > > It is definitely possible that the THP on the deferred split queue are > > freed instead of split. For example, 1M is unmapped for a 2M THP, then > > later the remaining 1M is unmapped, or the process exits before memory > > pressure happens. So how come we can tell it is "temporarily added to > > list"? Then THP_DEFERRED_SPLIT_PAGE - THP_UNDO_DEFERRED_SPLIT_PAGE > > actually just counts how many pages are still on deferred split queue. > > Not quite I think. I don't think we have a counter that counts how many > large folios on the deferred list were split. I think we only have > THP_SPLIT_PAGE. Yes, we just count how many THP were split regardless of why they got split. They may be split from a deferred split queue due to memory pressure, migration, etc. > > We could have > * THP_DEFERRED_SPLIT_PAGE > * THP_UNDO_DEFERRED_SPLIT_PAGE > * THP_PERFORM_DEFERRED_SPLIT_PAGE > > Maybe that would catch more cases (not sure if all, though). Then, you > could tell how many are still on that list. THP_DEFERRED_SPLIT_PAGE - > THP_UNDO_DEFERRED_SPLIT_PAGE - THP_PERFORM_DEFERRED_SPLIT_PAGE. > > That could give one a clearer picture how deferred split interacts with > actual splitting (possibly under memory pressure), the whole reason why > deferred splitting was added after all. I'm not quite sure whether there is a solid usecase or not. If we have, we could consider this. But a simpler counter may be more preferred. > > > It may be useful. However the counter is typically used to estimate > > how many THP are partially unmapped during a period of time. > > I'd say it's a bit of an abuse of that counter; well, or interpreting > something into the counter that that counter never reliably represented. It was way more reliable than now. > > I can easily write a program that keeps sending your counter to infinity > simply by triggering that behavior in a loop, so it's all a bit shaky. I don't doubt that. But let's get back to reality. The counter used to stay reasonable and reliable with most real life workloads before mTHP. There may be over-counting, for example, when unmapping a PTE-mapped THP which was not on a deferred split queue before. But such a case is not common for real life workloads because the huge PMD has to be split by partial unmap for most cases. And the partial unmap will add the THP to deferred split queue. But now a common workload, for example, just process exit, may probably send the counter to infinity. > > Something like Ryans script makes more sense, where you get a clearer > picture of what's mapped where and how. Because that information can be > much more valuable than just knowing if it's mapped fully or partially > (again, relevant for handling with memory waste). Ryan's script is very helpful. But the counter has been existing and used for years, and it is a quick indicator and much easier to monitor in a large-scale fleet. If we think the reliability of the counter is not worth fixing, why don't we just remove it. No counter is better than a broken counter. > > -- > Cheers, > > David / dhildenb >