Received: by 2002:ab2:1347:0:b0:1f4:ac9d:b246 with SMTP id g7csp326713lqg; Thu, 11 Apr 2024 04:28:37 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUeoVxPq3t32LXVhJcB2He6LOXm5h00awZRLvb26IOUvMibHFa+53xEGDjFHdvf8r4TGbRvvG+VnbZcY9zonwWv3MarHSFzZ6/NCTdTGQ== X-Google-Smtp-Source: AGHT+IEJ8USKEP25b9fkA3WpaXdcfeRwsNKWG+lwfyVZ82ahV2dESxdoyE59uqPp7vjp4RFrg5co X-Received: by 2002:a50:d498:0:b0:56e:2148:f517 with SMTP id s24-20020a50d498000000b0056e2148f517mr3072053edi.33.1712834916978; Thu, 11 Apr 2024 04:28:36 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712834916; cv=pass; d=google.com; s=arc-20160816; b=Jti22LH8wSK3ZmBgg2ox+D4eo43r5+M+CjZRft/DSUM5UT6GsdHjEjeNbLAUx0Pvu4 KL/3W2Op+shXMscheLws/vShPos4TqQ9Q+74rOyUVEX7SKpAvNVeNbvc4fSgDEmN8vPB z8IaN2dP5LCqTeIM6JwZizUxkbhlID4PYeOieRISAEZChl5FU3T+W3GkSzuZK6RUvlh6 4s2nSvG110TlNZAlLQ4hEWRMBkwHwPQiTTNJxYfmObsguq9P+iJR9bCxS7QYWmUxN7rP zk41lpvfYJPlVGyZ7L/aGnv/lxiH2aPgLARBAEPPj0OL+SzqC92/zKml2JwI6ilff8PE y4OQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id; bh=9rGkwGx7wbFra7UW06q/mrgqnXK02p1SocMykaC3hwo=; fh=qMg+g8EH2Is0JY12gzOWigxPAczJApQu2mvmuL1SpCw=; b=PgibpJVEW4zySMREa2OAJbIpG4vNmOG0ZtHY/hC+vzlB7FpLqbK93olkRliCsx1XJF sif2u2Bj1pfEYxskVMqGSfw9tA9YkKqR3FRExGiyQHN3WJlRPxOnS9YiQJl1TCgdoqqj iJkGYj7fwF30NUVXy6r+PeVVAZHXKF+oT8TUZjPKFOjP0LqB2DlNAN/SxhTX41gponTD /tlqm5oDKaqslspU6Si+R6HWHp7IyT1q6ovN4ERXowV999kYXuD/Iq5ObpFFnwvsbmnL yZWL/aaYW/RRF5C7rlrYKzEeBXy4AHim+rqPgB0LZpg+QvWIGSh8gLyFb6aPKAUuX1+L oAsg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-140337-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-140337-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id es18-20020a056402381200b0056fe1805953si683524edb.451.2024.04.11.04.28.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Apr 2024 04:28:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-140337-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-140337-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-140337-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 875FB1F22371 for ; Thu, 11 Apr 2024 11:28:36 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C4CA6149C7C; Thu, 11 Apr 2024 11:27:54 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6918213D258 for ; Thu, 11 Apr 2024 11:27:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712834874; cv=none; b=FKAjjRU8khb1kB5ru2Vh1ZD+rs92i7TMTVYEPIKMcYIPbIYy92NkvZGjWmIibX/O/qaUjk6ZkmWCG9z/q8kBKkuvhtTcXqr09IcjN2HYGAgFY+fY2KOM+cEEgGQFdxhOr+tcbfUI2cyICew9M3sxLVDS0sOf0U7YNkCmHn74l/k= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712834874; c=relaxed/simple; bh=9zkVJL8zRVuR9kRv+VB0FpDhOZ3XwqRKYTV5gfmfVMc=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=Qs6LiQGzYMIuFIlMHKNR0AkmEehwy9ODgmtyFo2Nd6A3/hPBs+WF7cHcf4k6hjcsKTPA6VSXZdYZRTAb2VmKS3u1/zh1ZW2uLcqamhDizet9B8wVtjGsqJDcmUjzo1X0gmNkhjCEFmY94wiTWxwfpyRP5H1YUslPg/zgJ9kQ8k8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com 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 56423113E; Thu, 11 Apr 2024 04:28:21 -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 229323F64C; Thu, 11 Apr 2024 04:27:49 -0700 (PDT) Message-ID: Date: Thu, 11 Apr 2024 12:27:47 +0100 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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: David Hildenbrand , Lance Yang , akpm@linux-foundation.org Cc: 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: <013334d5-62d2-4256-8045-168893a0a0cf@redhat.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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) >> >> 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: >> >> 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. > > Yes. Maybe better use proper __bitwise flags, the compiler should be smart > enough to optimize either way. 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.