From patchwork Thu Apr 18 13:44:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lance Yang X-Patchwork-Id: 13634760 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 5C9F3C4345F for ; Thu, 18 Apr 2024 13:44:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AE5A76B0089; Thu, 18 Apr 2024 09:44:46 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A955F6B008C; Thu, 18 Apr 2024 09:44:46 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9366B6B0092; Thu, 18 Apr 2024 09:44:46 -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 74FBC6B0089 for ; Thu, 18 Apr 2024 09:44:46 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id D723FA1C3F for ; Thu, 18 Apr 2024 13:44:45 +0000 (UTC) X-FDA: 82022772930.25.5C34F3D Received: from mail-pf1-f172.google.com (mail-pf1-f172.google.com [209.85.210.172]) by imf30.hostedemail.com (Postfix) with ESMTP id 1F5DE8001B for ; Thu, 18 Apr 2024 13:44:43 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=jcN1jA1o; spf=pass (imf30.hostedemail.com: domain of ioworker0@gmail.com designates 209.85.210.172 as permitted sender) smtp.mailfrom=ioworker0@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1713447884; a=rsa-sha256; cv=none; b=G4DR4NHTrFOzOyudmpZ3W7/LGjVLOMyFRochrcabeTF9kY+3ba33Zv9RMHrKfDlx0Dh7QN //6eRcIeRhvb8Gd9LH/pTl03ifZFZOp8fEC+lVF6kez/m415cCyojIDx285Mxh2wRj2EVz xJONuyU2jtam+S3pNBpMrgov+D+SElo= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=jcN1jA1o; spf=pass (imf30.hostedemail.com: domain of ioworker0@gmail.com designates 209.85.210.172 as permitted sender) smtp.mailfrom=ioworker0@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1713447884; 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-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=Fqqbw3IjyKnn1m1Xe9QqS4J7Q9A7Cu8Dc3YLSqhOLnE=; b=L4HIXfPtHJQ5e7f0TB1aHvIUxJZehT2GEQYSi04HfLtKSIDP6ZZk0OFJmGJacXQzEIfwMa MQuFm/psz93n3DPnppVyAP2NIo+Qnja7Kd1IJ96Ytt03zUyNfdPS4TkSNzphS0Pu4Dwokl R5oo/DBGs5S+SljTpGZzeNs/pVTDnso= Received: by mail-pf1-f172.google.com with SMTP id d2e1a72fcca58-6ecff9df447so852618b3a.1 for ; Thu, 18 Apr 2024 06:44:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1713447883; x=1714052683; darn=kvack.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=Fqqbw3IjyKnn1m1Xe9QqS4J7Q9A7Cu8Dc3YLSqhOLnE=; b=jcN1jA1oZWnHgtq2QNN80KiPZwdxUvHZUPkK9BFR8Lx1Qy4huehzPgXoi4k2I2ajFO YmfbzCdUunu6g4p3PYWeoT+FqV6TEAVkMPOh/ydIxswLWSjZlkcXKmRzkEAQFsCpZu99 vQ/Q5qP5IBEV1kPeE4g2tTQ4gw05LPPezOmm06qrh8T4O3vtKGPDKrS+r5QZ34ZUXEdJ NM/opKr0LXCl5R+fmPGxy+xW/lWrQbzDth9oMHfzvOCG5csTYqyJDPxWeh6axOzxnzIf mFNZig63hxjLgJ/B0sLCbOsJxQyzBbM0pBZ29yQ3GB5d6iUjFbo69emy6lp+myXn9I0M hu/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713447883; x=1714052683; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Fqqbw3IjyKnn1m1Xe9QqS4J7Q9A7Cu8Dc3YLSqhOLnE=; b=scwJV1DKDAQ2Szt4Y7rYyXKuOERnI2xXlp4vh2g+zbwEDABZzoK0f+6mdNWBoDzhlW MIGamH7KnaDIWmgN3YkMSIBH3EgHCCkfZGvDtybPUWtMxZQX9fZ+wu0IbFMrSKSDxRc6 U1C9+YPqSqFHi6o9p/3B8VWBTHKz+3vy4tDkz3eIP8/5QoPtjvGG9pecR8Afw85S5a1h kx8ZkRe9HwyW8sbT0V6J4CHSJzC+pbhDgRpW1b7tc01guw8bWPGhTIwbRf2IfYB4MW4/ 3O2TprDmp7bgXZHmZ9EXu9ktzRV93fic5uqPlTi7CfXaIRJmnrvYAZaohH3IStXGmzc3 WSBQ== X-Forwarded-Encrypted: i=1; AJvYcCWgJGQq0sHUUChwOnlp15jOqYwDsb0Saf0gjpz5GYRNnN9Bm21x/sdUQz7yNEdo1xaWkUnIUZqM4D32cc8LV9HbQyc= X-Gm-Message-State: AOJu0YwWtdzQPiOiZQJXPY+A7AGuh1Yj9fo/TM2dM3svUaRsrzAxvFvl mRRhhs+PBT9P9bYcCYWqTSxrjytpMFNInovHDvqeePC55JijQ9ml X-Google-Smtp-Source: AGHT+IG6+LasurbPxhaqNQ0AYUMX60c9471MkKIjRI6pRxYPA5DllfNCaYBL9yaDm8iPUDeFg62vQQ== X-Received: by 2002:a05:6a21:3e0d:b0:1a9:c4ca:dc74 with SMTP id bk13-20020a056a213e0d00b001a9c4cadc74mr3589823pzc.5.1713447882856; Thu, 18 Apr 2024 06:44:42 -0700 (PDT) Received: from LancedeMBP.lan ([112.10.225.217]) by smtp.gmail.com with ESMTPSA id d8-20020a170902b70800b001e4fdcf67desm1504837pls.299.2024.04.18.06.44.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Apr 2024 06:44:42 -0700 (PDT) From: Lance Yang To: akpm@linux-foundation.org Cc: ryan.roberts@arm.com, david@redhat.com, 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, Lance Yang Subject: [PATCH v10 0/4] mm/madvise: enhance lazyfreeing with mTHP in madvise_free Date: Thu, 18 Apr 2024 21:44:31 +0800 Message-Id: <20240418134435.6092-1-ioworker0@gmail.com> X-Mailer: git-send-email 2.33.1 MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 1F5DE8001B X-Stat-Signature: k5iyq6e9nipjjbz5cqbdcdpuco4pyc8s X-Rspam-User: X-HE-Tag: 1713447883-522953 X-HE-Meta: U2FsdGVkX1/VgiBDv0vSGvYnUlMuLQNzbwfp5tTA8sAD2LJUEsS7+urEBSotc/LYf05lsVVVVlT1nST2wY7UZHr3RMcOcuCMRlfbKgGSOnFqZx2nwll13W82bglf/tM+G6k50G63GrktY29AS53vx/a7LNQkL8YnWrb5+YB9tSOl/96t6E4nb2LEF9i8LIu4mBn0VqEfob2A2Iy5T39GcqKfP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SEu0tMD0UVayN+Nes2khSiXi7sq8rXRrHcI23KOxAc9P5a00afun99DFpluC+idS9sS/+lGZa648opwYOlB5T9e9Kv305tqCY9nret1aQj2cogHdVYt+ROXBOg3QVYTgtyvTv4UbztYgzG5AH5mF/58o7Co8GBN3aG6uZQa+kPlAZVI4UNHdmd7W3YMXhmRSIkYxllEvddAEeeQYvReW1TvgC5XWfg3IbGypdHqJJ+L9UN+H/FNy2T6/HHDtklwDebvZ8= 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 All, This patchset adds support for lazyfreeing multi-size THP (mTHP) without needing to first split the large folio via split_folio(). However, we still need to split a large folio that is not 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. Performance Testing =================== 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% --- This patchset applies against mm-unstable (f52bcd4a9f60). The performance numbers are from v2. I did a quick benchmark run of v10 and nothing significantly changed. Changes since v9 [9] ==================== - mm/madvise: optimize lazyfreeing with mTHP in madvise_free - Pass any_dirty to madvise_folio_pte_batch() (Thanks to David Hildenbrand) - Pick up AB's, Thanks to David! Changes since v8 [8] ==================== - mm/madvise: optimize lazyfreeing with mTHP in madvise_free - Leave the split folio code as is in the caller (per David Hildenbrand) - Use cydp_flags here that will make this easier to read (per David Hildenbrand) - Pick up RB's, Thanks to Ryan! Changes since v7 [7] ==================== - mm/madvise: optimize lazyfreeing with mTHP in madvise_free - Remove the duplicated check for the mapcount (per Ryan Roberts, David Hildenbrand) - Pick up AB's and RB's. Thanks to Ryan and David! Changes since v6 [6] ==================== - Fix a bug with incorrect bitwise operations (Thanks to Ryan Roberts) - Use a cmpxchg loop to only clear one of the flags to prevent race with the HW (per Ryan Roberts) Changes since v5 [5] ==================== - Convert mkold_ptes() to clear_young_dirty_ptes() (per Ryan Roberts) - Use the __bitwise flags as the input for clear_young_dirty_ptes() (per David Hildenbrand) - Follow the pattern already established by the original code (per Ryan Roberts) Changes since v4 [4] ==================== - The first patch implements the MADV_FREE change and introduces mkold_clean_ptes() with a generic implementation. The second patch specializes mkold_clean_ptes() for arm64, providing a performance boost specific to arm64 (per Ryan Roberts) - Drop the full parameter and call ptep_get_and_clear() in mkold_clean_ptes() (per Ryan Roberts) - Keep the previous behavior that avoids locking the folio if it wasn't in the swapcache or if it wasn't dirty (per Ryan Roberts) Changes since v3 [3] ==================== - Rename refresh_full_ptes -> mkold_clean_ptes (per Ryan Roberts) - Override mkold_clean_ptes() for arm64 to make it faster (per Ryan Roberts) - Update the changelog Changes since v2 [2] ==================== - Only skip all the PTEs for nr_pages when the number of batched PTEs matches nr_pages (per Barry Song) - Change folio_pte_batch() to consume an optional *any_dirty and *any_young function (per David Hildenbrand) - Move the ptep_get_and_clear_full() loop into refresh_full_ptes() (per David Hildenbrand) - Follow a similar pattern for madvise_free_pte_range() (per Ryan Roberts) Changes since v1 [1] ==================== - Update the performance numbers - Update the changelog (per Ryan Roberts) - Check the COW folio (per Yin Fengwei) - Check if we are mapping all subpages (per Barry Song, David Hildenbrand, Ryan Roberts) [1] https://lore.kernel.org/linux-mm/20240225123215.86503-1-ioworker0@gmail.com [2] https://lore.kernel.org/linux-mm/20240307061425.21013-1-ioworker0@gmail.com [3] https://lore.kernel.org/linux-mm/20240316102952.39233-1-ioworker0@gmail.com [4] https://lore.kernel.org/linux-mm/20240402124029.47846-1-ioworker0@gmail.com [5] https://lore.kernel.org/linux-mm/20240408042437.10951-1-ioworker0@gmail.com [6] https://lore.kernel.org/linux-mm/20240413002219.71246-1-ioworker0@gmail.com [7] https://lore.kernel.org/linux-mm/20240416033457.32154-1-ioworker0@gmail.com [8] https://lore.kernel.org/linux-mm/20240417141436.77963-1-ioworker0@gmail.com [9] https://lore.kernel.org/linux-mm/20240418105750.98866-1-ioworker0@gmail.com Thanks, Lance Lance Yang (4): mm/madvise: introduce clear_young_dirty_ptes() batch helper mm/arm64: override clear_young_dirty_ptes() batch helper mm/memory: add any_dirty optional pointer to folio_pte_batch() mm/madvise: optimize lazyfreeing with mTHP in madvise_free arch/arm64/include/asm/pgtable.h | 55 +++++++++++++++++++++++++++++++++++++++ arch/arm64/mm/contpte.c | 29 +++++++++++++++++++++++ include/linux/mm_types.h | 9 ++++++++ include/linux/pgtable.h | 74 +++++++++++++++++++++------------------ mm/internal.h | 12 ++++++++-- mm/madvise.c | 107 +++++++++++++++++++-------------------- mm/memory.c | 4 ++-- 7 files changed, 209 insertions(+), 81 deletions(-)