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 4982BC4345F for ; Thu, 11 Apr 2024 13:51:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D1B536B0095; Thu, 11 Apr 2024 09:51:13 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CCBA46B0098; Thu, 11 Apr 2024 09:51:13 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B93066B0099; Thu, 11 Apr 2024 09:51:13 -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 9B6B26B0095 for ; Thu, 11 Apr 2024 09:51:13 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 5F5191C1038 for ; Thu, 11 Apr 2024 13:51:13 +0000 (UTC) X-FDA: 81997387626.21.E6E726C Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf24.hostedemail.com (Postfix) with ESMTP id 9ABC4180020 for ; Thu, 11 Apr 2024 13:51:11 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf24.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=1712843471; 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=L82hQffH1VXKz2ODwgGbIJU5+sLIA89OaTFNzV8moOA=; b=mCmf8ioIUMJQVb8gm9orQE/Qde6MLsfK5QfKKf5CWCJk7EbQCLSssr4ZYU+sFnO+Jc6OqZ 07paSGI1kDnkarGyGNW+HWAHPqqcx3S0ACSa4AY/rbHsKyQMMMruX0Ql2289hFUjZdaLcc cdwhe2zXK50+SC4wKUaq9xyYPscnv/I= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf24.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=1712843471; a=rsa-sha256; cv=none; b=pKX8CYSuJdqOjg7J4ELT+H3NO5MdDW8T6ThKPSw9QBpLR2uQwdKfgNm9Cb0ehK4bL8TzsV hAwCOFgow3JIFx5+O+zVe/4eKWWXh1rBLzOUzx4p77Gus8o1cTSbwbPyAU/KUh0Ao9J+Ea 6wI6beOc1uTl70l8SUdXcFaSb2zLrs0= 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 339D8339; Thu, 11 Apr 2024 06:51:40 -0700 (PDT) Received: from [10.1.38.151] (XHFQ2J9959.cambridge.arm.com [10.1.38.151]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id D57AF3F64C; Thu, 11 Apr 2024 06:51:08 -0700 (PDT) Message-ID: Date: Thu, 11 Apr 2024 14:51:07 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v5 1/2] mm/madvise: optimize lazyfreeing with mTHP in madvise_free Content-Language: en-GB To: Lance Yang Cc: David Hildenbrand , akpm@linux-foundation.org, 21cnbao@gmail.com, mhocko@suse.com, fengwei.yin@intel.com, zokeefe@google.com, shy828301@gmail.com, xiehuan09@gmail.com, wangkefeng.wang@huawei.com, songmuchun@bytedance.com, peterx@redhat.com, minchan@kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org References: <20240408042437.10951-1-ioworker0@gmail.com> <20240408042437.10951-2-ioworker0@gmail.com> <38c4add8-53a2-49ca-9f1b-f62c2ee3e764@arm.com> <013334d5-62d2-4256-8045-168893a0a0cf@redhat.com> From: Ryan Roberts In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: 9ABC4180020 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: 4ryhkueci88shdp4xmx5fco835bjyaro X-HE-Tag: 1712843471-180952 X-HE-Meta: U2FsdGVkX19bEFiqm+yUCSiRX5jXfLgTF4oOe5A4e4YEA3Mq22oxkgtx+rCgfDugE2hpMOUiuIPe/YMBWJvZoNVV3kRxVIDloZa793MRmjGF3uoLEPc5tISAUIDybrRYK35Q79bVjSbTy2MdCIwm/NdXVQtsuHLx1o94tLzGN51ONsr9oNTtkqyuf8v1Wy/3x5NzNjxM70De8WXsEJHONxdiBJsxg0YaYBBxpv3ElcK48eGxnO2lkuVcGH3DhJHG7E/Gql6lZ8yfwObBX2aRNwQo9Tro/irXdjhS5TRHv2JcVXxAnEUUyXeX0cKdjvbv3kUVg3nX7sVJXSJB+ncCDWRHds4hKYqPNdqgGnaXe6XDn4oU+N/iZEMAinG5gRlCpP2VK5JJz/WF60GIrstUSip5HxmIh+JS7NXl8g6DaO03Mt668YJVwC3SXyh5jSDRdaDJbwfVClTcR3AM7jV65V8mcW42zL0JOz5JgaSWhFwH/rkgb7GZ7eNajL06LGm3CuxrvLMjPy9MwBa7GvVRwCu/T7fwsknhs9k8mNSSIIybGvQxPiQPfPWPZsQpSVvrGryFQEp5wQ6NNdsvha1M7AtiMECoC15FvABJil2R/B8S+KG8f3YfgxvulCDqTdV5oEFa44cXgGgaBgQrf37nh8tjkCFNsGI185BL6xanUVzsB0EdBZ6TIUEzIw3Mj1m26Phi6Td6OZwUhlGw8N1EHLDlQr9s3Md1CEtSPKqcX38nPfPgjBLkMa9XAjNahDuGhjl/Clkh0r+iJSH6IyVo+dB/kHxy0LJuGFhJViCQ/1v95OeghTSbLwtCWGMkQZrQh/NW0Ff3zWdJCbo8O2hdO0pegzjczXR7msFjEHS5qaaT2OCTCQiXqM9NOaOWlJK5VwoTouWxwujhsYPx5JrUMn+cTx0Gp8tSRqI7LYLkV3gDuaadg/Nw9DiAJHhq+Fjki1/6tbkIf5J1TCbPxaY PgkMsn6Q qUxj/CjNdH/xaBHUrjS52Tkl7Hn1sSemJwKvBPEuuIktcmlSDp6ViT2pYbFE4u7+JmuQr1MJKQSByUGuUsp75mKcs5VFfB81xjlwoSfZH1nrNwJhTpQcGBhIXWkL3PzDSaC3sZqYxEnkhyMx3iClGplmYh79q373AkEoE1J7mc8EunEwO77QRgs4fGm73ukNr8jMH9qH0FGmsy72abwguIhhmzPC3WehSk0jPDYfjzfDaH0SPOlFziVv1Oj7X1KG9IKDKjY7MMGMvdb1ceNu4ETo7Zdn1xb5ZoBExHb4JtQDP2nR33jqUSrb1xaCQsqONZjK1M3RF2OJWu3fGb8V2V89kpemOM0W9ZHkBLdUlz/QwLZB7sqJhnd23FyJjuK7nrAf1 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 11/04/2024 13:23, Lance Yang wrote: > On Thu, Apr 11, 2024 at 7:27 PM Ryan Roberts wrote: >> >> On 11/04/2024 12:20, David Hildenbrand wrote: >>> On 11.04.24 13:11, Ryan Roberts wrote: >>>> On 08/04/2024 05:24, Lance Yang wrote: >>>>> This patch optimizes lazyfreeing with PTE-mapped mTHP[1] >>>>> (Inspired by David Hildenbrand[2]). We aim to avoid unnecessary folio >>>>> splitting if the large folio is fully mapped within the target range. >>>>> >>>>> If a large folio is locked or shared, or if we fail to split it, we just >>>>> leave it in place and advance to the next PTE in the range. But note that >>>>> the behavior is changed; previously, any failure of this sort would cause >>>>> the entire operation to give up. As large folios become more common, >>>>> sticking to the old way could result in wasted opportunities. >>>>> >>>>> On an Intel I5 CPU, lazyfreeing a 1GiB VMA backed by PTE-mapped folios of >>>>> the same size results in the following runtimes for madvise(MADV_FREE) in >>>>> seconds (shorter is better): >>>>> >>>>> Folio Size | Old | New | Change >>>>> ------------------------------------------ >>>>> 4KiB | 0.590251 | 0.590259 | 0% >>>>> 16KiB | 2.990447 | 0.185655 | -94% >>>>> 32KiB | 2.547831 | 0.104870 | -95% >>>>> 64KiB | 2.457796 | 0.052812 | -97% >>>>> 128KiB | 2.281034 | 0.032777 | -99% >>>>> 256KiB | 2.230387 | 0.017496 | -99% >>>>> 512KiB | 2.189106 | 0.010781 | -99% >>>>> 1024KiB | 2.183949 | 0.007753 | -99% >>>>> 2048KiB | 0.002799 | 0.002804 | 0% >>>>> >>>>> [1] https://lkml.kernel.org/r/20231207161211.2374093-5-ryan.roberts@arm.com >>>>> [2] https://lore.kernel.org/linux-mm/20240214204435.167852-1-david@redhat.com >>>>> >>>>> Signed-off-by: Lance Yang >>>>> --- >>>>> include/linux/pgtable.h | 34 +++++++++ >>>>> mm/internal.h | 12 +++- >>>>> mm/madvise.c | 149 ++++++++++++++++++++++------------------ >>>>> mm/memory.c | 4 +- >>>>> 4 files changed, 129 insertions(+), 70 deletions(-) >>>>> >>>>> diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h >>>>> index 0f4b2faa1d71..4dd442787420 100644 >>>>> --- a/include/linux/pgtable.h >>>>> +++ b/include/linux/pgtable.h >>>>> @@ -489,6 +489,40 @@ static inline pte_t ptep_get_and_clear(struct mm_struct >>>>> *mm, >>>>> } >>>>> #endif >>>>> +#ifndef mkold_clean_ptes >>>>> +/** >>>>> + * mkold_clean_ptes - Mark PTEs that map consecutive pages of the same folio >>>>> + * as old and clean. >>>>> + * @mm: Address space the pages are mapped into. >>>>> + * @addr: Address the first page is mapped at. >>>>> + * @ptep: Page table pointer for the first entry. >>>>> + * @nr: Number of entries to mark old and clean. >>>>> + * >>>>> + * May be overridden by the architecture; otherwise, implemented by >>>>> + * get_and_clear/modify/set for each pte in the range. >>>>> + * >>>>> + * Note that PTE bits in the PTE range besides the PFN can differ. For example, >>>>> + * some PTEs might be write-protected. >>>>> + * >>>>> + * Context: The caller holds the page table lock. The PTEs map consecutive >>>>> + * pages that belong to the same folio. The PTEs are all in the same PMD. >>>>> + */ >>>>> +static inline void mkold_clean_ptes(struct mm_struct *mm, unsigned long addr, >>>>> + pte_t *ptep, unsigned int nr) >>>> > > Thanks for the suggestions, Ryan, David! > >>>> Just thinking out loud, I wonder if it would be cleaner to convert mkold_ptes() >>>> (which I added as part of swap-out) to something like: > > Yeah, this is definitely cleaner than before. > >>>> >>>> clear_young_dirty_ptes(struct mm_struct *mm, unsigned long addr, >>>> pte_t *ptep, unsigned int nr, >>>> bool clear_young, bool clear_dirty); >>>> >>>> Then we can use the same function for both use cases and also have the ability >>>> to only clear dirty in future if we ever need it. The other advantage is that we >>>> only need to plumb a single function down the arm64 arch code. As it currently >>>> stands, those 2 functions would be duplicating most of their code. > > Agreed. It's indeed a good idea to use a single function for both use cases. > >>> >>> Yes. Maybe better use proper __bitwise flags, the compiler should be smart >>> enough to optimize either way. > > Nice. I'll use the __bitwise flags as the input. > >> >> Agreed. I was also thinking perhaps it makes sense to start using output bitwise >> flags for folio_pte_batch() since this patch set takes us up to 3 optional bool >> pointers for different things. Might be cleaner to have input flags to tell it >> what we care about and output flags to highlight those things. I guess the >> compiler should be able to optimize in the same way. >> > > Should I start using output bitwise flags for folio_pte_batch() in > this patch set? I don't think its crucial (yet). I'd leave it as you have done it for now, unless David shouts. > > Thanks, > Lance