From patchwork Fri Sep 6 05:28:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Baolin Wang X-Patchwork-Id: 13793286 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 AFDD0C54FC6 for ; Fri, 6 Sep 2024 05:28:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E3D5E6B007B; Fri, 6 Sep 2024 01:28:45 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DEE176B0082; Fri, 6 Sep 2024 01:28:45 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CDC486B0085; Fri, 6 Sep 2024 01:28:45 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id AF6D06B007B for ; Fri, 6 Sep 2024 01:28:45 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 0FB5B120F54 for ; Fri, 6 Sep 2024 05:28:45 +0000 (UTC) X-FDA: 82533183810.24.AB81058 Received: from out30-110.freemail.mail.aliyun.com (out30-110.freemail.mail.aliyun.com [115.124.30.110]) by imf11.hostedemail.com (Postfix) with ESMTP id 759D04000A for ; Fri, 6 Sep 2024 05:28:41 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=rxB+BsJb; dmarc=pass (policy=none) header.from=linux.alibaba.com; spf=pass (imf11.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.110 as permitted sender) smtp.mailfrom=baolin.wang@linux.alibaba.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725600443; a=rsa-sha256; cv=none; b=zgIN1WRXmKC0PrbBKiYljO0IMm1yhfFpo+NS/mgkGF2xF6VbY0bnBuy5DeBWF6kNdRUhc8 sDsKWlTYjDVz3OYrxbOePoss6NDdzIofmlkAna8YsWJ6Xqe+7ofB3H0AQC8awPBnSXRXJ6 UMbUj/ngL4mYYoQI8ywQwG6uPfY0Ox8= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=rxB+BsJb; dmarc=pass (policy=none) header.from=linux.alibaba.com; spf=pass (imf11.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.110 as permitted sender) smtp.mailfrom=baolin.wang@linux.alibaba.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1725600443; 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: references:dkim-signature; bh=B59qOTQ5B/DlCcvhL+utjjELqTBP0dtzxxRmP6Grzk4=; b=XJU+WM6bJ7nHjZUF0BDiJls6dZpJgTn1Nj9thsUgHB0LUps8DwtseKkoP9kACNyCa9ik/N +5mvEI2gxcMyWgSLaMfaqcrs/KMdAJHPm7tU3xJty+OPI59JXaRwZysjjr9WFelJ8YLooq TY55Jnz9kXktcKgCNsHnUgezjvxrCV0= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1725600518; h=From:To:Subject:Date:Message-Id:MIME-Version:Content-Type; bh=B59qOTQ5B/DlCcvhL+utjjELqTBP0dtzxxRmP6Grzk4=; b=rxB+BsJbvF8oCPWHpz1KnArCLWSZwdMBO9ytx13b7IRW+JqTIf3oM4XXQEVq3JZPvL64yt3Qdz2cc+YFseNuhZ1yNN9TsifTfBc20+jd5SMDVRXE6Pav3C1VSsWvI9qYylit8ueGMZjG0VgKgA2zocRrr3MUhTITHaqVg4O9j/Y= Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0WEOJP-i_1725600516) by smtp.aliyun-inc.com; Fri, 06 Sep 2024 13:28:37 +0800 From: Baolin Wang To: akpm@linux-foundation.org, hughd@google.com Cc: willy@infradead.org, david@redhat.com, 21cnbao@gmail.com, ryan.roberts@arm.com, baolin.wang@linux.alibaba.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH] mm: shmem: fix khugepaged activation policy for shmem Date: Fri, 6 Sep 2024 13:28:23 +0800 Message-Id: <7c796904528e21152ba5aa639e963e0ae45b7040.1725600217.git.baolin.wang@linux.alibaba.com> X-Mailer: git-send-email 2.39.3 MIME-Version: 1.0 X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 759D04000A X-Stat-Signature: i9i9ofqanjdaaxzdbzzx8bq8ixfdmhty X-Rspam-User: X-HE-Tag: 1725600521-412085 X-HE-Meta: U2FsdGVkX1/4lDOBDnDY81NVee+XCcQuJM4fqPNngpMNg8YXRhfXXR6B9+6N7/jZUMSycitAHBSMSoTsz/u4n+XKDFGEWnEpVTUagSGJiyolO3YbMTkr3tbECoXLTEnMaHjR06DVTeMSqi6EdWbv7Cv00VPdei7VB4jIVocIK2WAXaEotV/feoig+79oB/V/zKsFECQoX2S91aJQX5Gx3lJkk9IHblZOnh+Cqkem8YtjqgP0Q630QKC74DAxdSKdf+lpdH63fOKsowhYcTxJzBHWuzxVZ5swdKjiAjJMhh9AH4oyVR9lFa/EwqihFIr9Dbj0rFj7o21TYjAhQ3S4Uq1gxq7fev1cgUC63+D92IYx+h1a5obpN8v/TRGbXbb8b00zEVl/BJBYpNikwMAvL3Z/Usw12YNbvm9OjpAoEmhn2t6KW4mRHBvA8a5iV7XrwDTNTHIxJOFgpDWnh+gIzdqZpXTJT+/YzLIMTRs1SGgGEmIZBhq6/YfsxbxI0I6CN6yXHPnif2URyimWKL3wW8YJQqEuknWHOQYTIelfyyGF6W+Er6+VNo5aIFhOowfaKblUQPbcooloPsOl/LbqsNThbV65y8QGLoO8zOlDx+z1fOGuiv+5nVQ/QEOm1hI7EAkfYZ+4RbEdppzSDDnp56IBe9Ba5d29Xdh7N9fsp5UHhPz/zFxYRQwYlIpYkN8qhvnBZmgFfIxLjBq+4NOUZvfYpWQ2xyLahlGY3EDHizJy/TGs/iczLjBgGl0lenxoIVXBoMODcDWDw3NZpGZObisjuslfWIgBkxUVGixlPwlWA2qFBtuyH9SvlHc0bhB+NsYc4S03m7nklUqCQMO9CtLlKqlWuqvPf4rzwaH1Y7BNyWjHcaxSNcHwSdULsUMjkHfrjeuMUl5xS1eYMSO2vHIugMYO0TUUJ7rHJDdZCIKB14aSihqLL5KiaRxAfDzpB0OEPpFCxI2ZUOuEQqZ s65ZmMoS YY54FwiHh3dYgsl6zUeZBVB2GE8xWOn5oi81GiY2GAIS8vIUXgbKjHWptGOEEnjMw5xPoPdVeedo8y4jkthz2F/0Tlf/Qbv3A+i0Z1/TgELiwcPGZgtxQYvy7+d0ylkyHAidaWizigenYl25inMC1JshaLzrddaVbJ33T+FaAjJzLLurCQdhaO9ExKWY8I46i0ug0QV8IpitQ4cfoSg7Zd3hBCWIy6nIzrGwSdhRYMxfIxu9Cs5Fbxx7SVehDaVZX6K2y5rG94qCPErofChc5zA2cJGyfpbzZghGg9Xqfg3eouLB45vSfXS3h81b6eo3up0L6ZeQJyIH292siUMCF1zI8Ag== 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: Shmem has a separate interface (different from anonymous pages) to control huge page allocation, that means shmem THP can be enabled while anonymous THP is disabled. However, in this case, khugepaged will not start to collapse shmem THP, which is unreasonable. To fix this issue, we should call start_stop_khugepaged() to activate or deactivate the khugepaged thread when setting shmem mTHP interfaces. Moreover, add a new helper shmem_hpage_pmd_enabled() to help to check whether shmem THP is enabled, which will determine if khugepaged should be activated. Reported-by: Ryan Roberts Signed-off-by: Baolin Wang Reviewed-by: Ryan Roberts --- Hi Ryan, I remember we discussed the shmem khugepaged activation issue before, but I haven’t seen any follow-up patches to fix it. Recently, I am trying to fix the shmem mTHP collapse issue in the series [1], and I also addressed this activation issue. Please correct me if you have a better idea. Thanks. [1] https://lore.kernel.org/all/cover.1724140601.git.baolin.wang@linux.alibaba.com/T/#u --- include/linux/shmem_fs.h | 6 ++++++ mm/khugepaged.c | 2 ++ mm/shmem.c | 29 +++++++++++++++++++++++++++-- 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/include/linux/shmem_fs.h b/include/linux/shmem_fs.h index 515a9a6a3c6f..ee6635052383 100644 --- a/include/linux/shmem_fs.h +++ b/include/linux/shmem_fs.h @@ -114,6 +114,7 @@ int shmem_unuse(unsigned int type); unsigned long shmem_allowable_huge_orders(struct inode *inode, struct vm_area_struct *vma, pgoff_t index, loff_t write_end, bool shmem_huge_force); +bool shmem_hpage_pmd_enabled(void); #else static inline unsigned long shmem_allowable_huge_orders(struct inode *inode, struct vm_area_struct *vma, pgoff_t index, @@ -121,6 +122,11 @@ static inline unsigned long shmem_allowable_huge_orders(struct inode *inode, { return 0; } + +static inline bool shmem_hpage_pmd_enabled(void) +{ + return false; +} #endif #ifdef CONFIG_SHMEM diff --git a/mm/khugepaged.c b/mm/khugepaged.c index f9c39898eaff..caf10096d4d1 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -430,6 +430,8 @@ static bool hugepage_pmd_enabled(void) if (test_bit(PMD_ORDER, &huge_anon_orders_inherit) && hugepage_global_enabled()) return true; + if (shmem_hpage_pmd_enabled()) + return true; return false; } diff --git a/mm/shmem.c b/mm/shmem.c index 74f093d88c78..d7c342ae2b37 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1653,6 +1653,23 @@ static gfp_t limit_gfp_mask(gfp_t huge_gfp, gfp_t limit_gfp) } #ifdef CONFIG_TRANSPARENT_HUGEPAGE +bool shmem_hpage_pmd_enabled(void) +{ + if (shmem_huge == SHMEM_HUGE_DENY) + return false; + if (test_bit(HPAGE_PMD_ORDER, &huge_shmem_orders_always)) + return true; + if (test_bit(HPAGE_PMD_ORDER, &huge_shmem_orders_madvise)) + return true; + if (test_bit(HPAGE_PMD_ORDER, &huge_shmem_orders_within_size)) + return true; + if (test_bit(HPAGE_PMD_ORDER, &huge_shmem_orders_inherit) && + shmem_huge != SHMEM_HUGE_NEVER) + return true; + + return false; +} + unsigned long shmem_allowable_huge_orders(struct inode *inode, struct vm_area_struct *vma, pgoff_t index, loff_t write_end, bool shmem_huge_force) @@ -5036,7 +5053,7 @@ static ssize_t shmem_enabled_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count) { char tmp[16]; - int huge; + int huge, err; if (count + 1 > sizeof(tmp)) return -EINVAL; @@ -5060,7 +5077,9 @@ static ssize_t shmem_enabled_store(struct kobject *kobj, shmem_huge = huge; if (shmem_huge > SHMEM_HUGE_DENY) SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge; - return count; + + err = start_stop_khugepaged(); + return err ? err : count; } struct kobj_attribute shmem_enabled_attr = __ATTR_RW(shmem_enabled); @@ -5137,6 +5156,12 @@ static ssize_t thpsize_shmem_enabled_store(struct kobject *kobj, ret = -EINVAL; } + if (ret > 0) { + int err = start_stop_khugepaged(); + + if (err) + ret = err; + } return ret; }