From patchwork Thu Jul 4 09:10:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ryan Roberts X-Patchwork-Id: 13723503 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 03299C30653 for ; Thu, 4 Jul 2024 09:11:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5F3546B00B4; Thu, 4 Jul 2024 05:11:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5A3246B00B6; Thu, 4 Jul 2024 05:11:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 46AA86B00B7; Thu, 4 Jul 2024 05:11:03 -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 2793E6B00B4 for ; Thu, 4 Jul 2024 05:11:03 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id A5AB01210F3 for ; Thu, 4 Jul 2024 09:11:02 +0000 (UTC) X-FDA: 82301500764.27.486AC88 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf15.hostedemail.com (Postfix) with ESMTP id E3546A001C for ; Thu, 4 Jul 2024 09:11:00 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=none; spf=pass (imf15.hostedemail.com: domain of ryan.roberts@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=ryan.roberts@arm.com; dmarc=pass (policy=none) header.from=arm.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1720084236; 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; bh=dMwhbgmiEoWQzARBm/DsPldzQIHU3lPphTqN1zBONlw=; b=n/ka3yQFQOvtim0xodXkFMJxlZXhkOhUCHepCgTmWK72L7yDXd8vkyR2Pm/qf86cpEHElT kp2jSwNJOt1wrmAIAzgw8f3R9N0NbNAg6TG6tk6nzBlvRscBnyI/BnQnZ8hL4bhqUjgoU2 Wjod8LUHCHjZzcuMfIn9iyFnC8gJDfA= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1720084236; a=rsa-sha256; cv=none; b=AGf2SCjCe2j8RtIPRYhV22L5ibZgX7ZBAUcJmC+zIubGW6A8oYEwZLpmmu8nHkCBbg5mpS qb5YGbRJrJfA6IOyQ/3Befn/xgFBAxzV1StH6w9mPTUo6+T71viPBSYH8Ue/hluQi3pWPC pfSILBctxOHYRl5L/4UzIvEiLtj2XK0= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=none; spf=pass (imf15.hostedemail.com: domain of ryan.roberts@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=ryan.roberts@arm.com; dmarc=pass (policy=none) header.from=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 E3570367; Thu, 4 Jul 2024 02:11:24 -0700 (PDT) Received: from e125769.cambridge.arm.com (e125769.cambridge.arm.com [10.1.196.27]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 7C89E3F766; Thu, 4 Jul 2024 02:10:58 -0700 (PDT) From: Ryan Roberts To: Andrew Morton , Jonathan Corbet , David Hildenbrand , Barry Song , Baolin Wang , Lance Yang , Yang Shi Cc: Ryan Roberts , linux-mm@kvack.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: [PATCH v2] mm: Fix khugepaged activation policy Date: Thu, 4 Jul 2024 10:10:50 +0100 Message-ID: <20240704091051.2411934-1-ryan.roberts@arm.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: E3546A001C X-Stat-Signature: nwaj5opniuirqpxp6wtwsedyb15c78bc X-HE-Tag: 1720084260-932762 X-HE-Meta: U2FsdGVkX19mJM7ERp9nlnVamxlI2O3vGDd6mjNYsNOw8yrZ8lXcCAF1DE35xDJSjDQgiFX+1NVDZRGxtVAsU96C84yyiyqpX0n7Tlp0Ddlvwf1LvraZ5LIt3idV0DlU9Xz3a7oztlpy7tXDbeazou390yrLEVHZ/P7C1XJ6g6xj0MBCXCemIRek1+xsxMGDoLjpJaZm/XS10hRphJ3aE2GhSH88V9mPG8NTYVrYkEBa6eqaDNNJT0L51Yw4Nu/xC7+MD+/G/Lef6QyVUhpuzKcJ7BV+xQADxiKP6OcaLVmNqHEq66s7lJPs/KnfHBHSeIWJwyYL4CSTXaeWYmum4gfUt2Sv5g9j/EKamLlB+4W2/QmZQxa9A7tNFK7GVxHariRjo7UsTfz3Mc19UjJtqSAIEnPI/ho4Z3OPTXz7xGbSipxLIzinCvREo94avpC0nTLsjO5eW30jjwDv43rTX5TlFz3oYJXH0XsDpsKrF56gbZ/Rfc6N1sQZTOW3gdKeYrjd+DhfFDbs1ULlEVj6vvaUpCmFp4+Tvw8qqtCEMnSf8lU54d9QMeApXPvDUbv9QLNkBIjgXkld1eWaC52RxrmohCiFhrgYQ4HOu9IzXYmWymN1a7OT1nrPFe1VvoIHSb1fuWMWdd/5vzb0lkSfnj/99Z6zqvjZb9e2tz/rm8KjNBsOcCD2kYQ80Sus2bT1a1kCE54LchTR1Rmnv550tNOygy8si0mjSXhJRUgF5adk34gbB9/BpSEPvdkhmWdQvahyTR07jHz94jHz8UZJ2CK75Nb1WJX+dvc9TqX8NlDR6FSxX4rAbOYk5HdVH5de2LxfGDD89+4f9qg1oI33GNlgJrW4yhNzCuvh9buEAtyj2/aQhqpgDzlUPP0CUnEFSDaQ2mugSPfkgrcYsFxuKDA+GxwjmqQvBHRfltg1Ehfvd8CPPri6cQ38UuLdSGrWhlE49ZJJbYNoOsMU3sv 5E0J7FEa UwkfredNStVcq5kI7IhYXERLGMkdpkFFsxJyBmnFgel7TA54PgreqF8DB5PrDA+HJoNBrb8CP7XuVkrI0j0bPmJLDoGaDW7rTFfeKThcFShVY9Z8bGur8gt+9j1TtoS8rYsx2Mba1gBQdWc8EAkbRMzJj5Omq6qyPsq+tI1r2lLPaYXCnrY6AFlyoE5ysL+0ZMqAWCW2WwEwG45oTPbJC4MO0h5Xu1yqLpmKuBi5BkScLNrc= 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: Since the introduction of mTHP, the docuementation has stated that khugepaged would be enabled when any mTHP size is enabled, and disabled when all mTHP sizes are disabled. There are 2 problems with this; 1. this is not what was implemented by the code and 2. this is not the desirable behavior. Desirable behavior is for khugepaged to be enabled when any PMD-sized THP is enabled, anon or file. (Note that file THP is still controlled by the top-level control so we must always consider that, as well as the PMD-size mTHP control for anon). khugepaged only supports collapsing to PMD-sized THP so there is no value in enabling it when PMD-sized THP is disabled. So let's change the code and documentation to reflect this policy. Further, per-size enabled control modification events were not previously forwarded to khugepaged to give it an opportunity to start or stop. Consequently the following was resulting in khugepaged eroneously not being activated: echo never > /sys/kernel/mm/transparent_hugepage/enabled echo always > /sys/kernel/mm/transparent_hugepage/hugepages-2048kB/enabled Signed-off-by: Ryan Roberts Fixes: 3485b88390b0 ("mm: thp: introduce multi-size THP sysfs interface") Closes: https://lore.kernel.org/linux-mm/7a0bbe69-1e3d-4263-b206-da007791a5c4@redhat.com/ Cc: stable@vger.kernel.org --- Hi All, Applies on top of mm-unstable from a couple of days ago (9bb8753acdd8). No regressions observed in mm selftests. When fixing this I also noticed that khugepaged doesn't get (and never has been) activated/deactivated by `shmem_enabled=`. I've concluded that this is definitely a (separate) bug. But I'm waiting for the conclusion of the conversation at [2] before fixing, so will send separately. Changes since v1 [1] ==================== - hugepage_pmd_enabled() now considers CONFIG_READ_ONLY_THP_FOR_FS as part of decision; means that for kernels without this config, khugepaged will not be started when only the top-level control is enabled. [1] https://lore.kernel.org/linux-mm/20240702144617.2291480-1-ryan.roberts@arm.com/ [2] https://lore.kernel.org/linux-mm/65c37315-2741-481f-b433-cec35ef1af35@arm.com/ Thanks, Ryan Documentation/admin-guide/mm/transhuge.rst | 11 +++++------ include/linux/huge_mm.h | 15 ++++++++------- mm/huge_memory.c | 7 +++++++ mm/khugepaged.c | 13 ++++++------- 4 files changed, 26 insertions(+), 20 deletions(-) -- 2.43.0 diff --git a/Documentation/admin-guide/mm/transhuge.rst b/Documentation/admin-guide/mm/transhuge.rst index 709fe10b60f4..fc321d40b8ac 100644 --- a/Documentation/admin-guide/mm/transhuge.rst +++ b/Documentation/admin-guide/mm/transhuge.rst @@ -202,12 +202,11 @@ PMD-mappable transparent hugepage:: cat /sys/kernel/mm/transparent_hugepage/hpage_pmd_size -khugepaged will be automatically started when one or more hugepage -sizes are enabled (either by directly setting "always" or "madvise", -or by setting "inherit" while the top-level enabled is set to "always" -or "madvise"), and it'll be automatically shutdown when the last -hugepage size is disabled (either by directly setting "never", or by -setting "inherit" while the top-level enabled is set to "never"). +khugepaged will be automatically started when PMD-sized THP is enabled +(either of the per-size anon control or the top-level control are set +to "always" or "madvise"), and it'll be automatically shutdown when +PMD-sized THP is disabled (when both the per-size anon control and the +top-level control are "never") Khugepaged controls ------------------- diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h index 4d155c7a4792..6debd8b6fd7d 100644 --- a/include/linux/huge_mm.h +++ b/include/linux/huge_mm.h @@ -128,16 +128,17 @@ static inline bool hugepage_global_always(void) (1< 0) { + int err; + + err = start_stop_khugepaged(); + if (err) + ret = err; + } return ret; } diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 409f67a817f1..708d0e74b61f 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -449,7 +449,7 @@ void khugepaged_enter_vma(struct vm_area_struct *vma, unsigned long vm_flags) { if (!test_bit(MMF_VM_HUGEPAGE, &vma->vm_mm->flags) && - hugepage_flags_enabled()) { + hugepage_pmd_enabled()) { if (thp_vma_allowable_order(vma, vm_flags, TVA_ENFORCE_SYSFS, PMD_ORDER)) __khugepaged_enter(vma->vm_mm); @@ -2462,8 +2462,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result, static int khugepaged_has_work(void) { - return !list_empty(&khugepaged_scan.mm_head) && - hugepage_flags_enabled(); + return !list_empty(&khugepaged_scan.mm_head) && hugepage_pmd_enabled(); } static int khugepaged_wait_event(void) @@ -2536,7 +2535,7 @@ static void khugepaged_wait_work(void) return; } - if (hugepage_flags_enabled()) + if (hugepage_pmd_enabled()) wait_event_freezable(khugepaged_wait, khugepaged_wait_event()); } @@ -2567,7 +2566,7 @@ static void set_recommended_min_free_kbytes(void) int nr_zones = 0; unsigned long recommended_min; - if (!hugepage_flags_enabled()) { + if (!hugepage_pmd_enabled()) { calculate_min_free_kbytes(); goto update_wmarks; } @@ -2617,7 +2616,7 @@ int start_stop_khugepaged(void) int err = 0; mutex_lock(&khugepaged_mutex); - if (hugepage_flags_enabled()) { + if (hugepage_pmd_enabled()) { if (!khugepaged_thread) khugepaged_thread = kthread_run(khugepaged, NULL, "khugepaged"); @@ -2643,7 +2642,7 @@ int start_stop_khugepaged(void) void khugepaged_min_free_kbytes_update(void) { mutex_lock(&khugepaged_mutex); - if (hugepage_flags_enabled() && khugepaged_thread) + if (hugepage_pmd_enabled() && khugepaged_thread) set_recommended_min_free_kbytes(); mutex_unlock(&khugepaged_mutex); }