From patchwork Wed Sep 11 01:42:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolin Wang X-Patchwork-Id: 13799599 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 1F5E4EE01FA for ; Wed, 11 Sep 2024 01:42:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7F700900006; Tue, 10 Sep 2024 21:42:49 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7A6AA8D00E2; Tue, 10 Sep 2024 21:42:49 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 66F29900006; Tue, 10 Sep 2024 21:42:49 -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 7B1918D00E2 for ; Tue, 10 Sep 2024 21:42:48 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 13BD380E84 for ; Wed, 11 Sep 2024 01:42:48 +0000 (UTC) X-FDA: 82550758416.09.823EC05 Received: from out30-131.freemail.mail.aliyun.com (out30-131.freemail.mail.aliyun.com [115.124.30.131]) by imf30.hostedemail.com (Postfix) with ESMTP id DAF4A80018 for ; Wed, 11 Sep 2024 01:42:43 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=je+qgkQK; dmarc=pass (policy=none) header.from=linux.alibaba.com; spf=pass (imf30.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.131 as permitted sender) smtp.mailfrom=baolin.wang@linux.alibaba.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726018889; a=rsa-sha256; cv=none; b=qhdjFQ9Qo+uRiKH8lkNB8rrWvBZJdhlIKtzoT/l1Co82GmGzEX/52TTLHiUHdaTS70uZAx purTs0V2Zf3f2u2pfeT3PKoOq8/hoNOmEarzEnz/enkXO8aV/Ww9O/ZE16x6P3IeV04VoD oD3pDhyOabXbUlI8nlDdKR3fEXRfEsM= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=je+qgkQK; dmarc=pass (policy=none) header.from=linux.alibaba.com; spf=pass (imf30.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.131 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=1726018889; 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=DfCL37UysHfNkqniIfqYNCGqdfKJNSoWCe1sTKj/AZ0=; b=bFqFf+AbIM8KjPjrFnCAsxJ/L9It2INvsXSt5J4ltgD1rLdagIuU92lnbhjWHw8d0X4cNH 5HeWqyBpyPqqWxECMjgUJBGJvo4GjGkrGpLku9v6Q05rRNgudLpCwG2RBAaHOEr0hgJGJl K3QFz4Or6L5fmlNYAhAP8nrmMJO/voc= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1726018959; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=DfCL37UysHfNkqniIfqYNCGqdfKJNSoWCe1sTKj/AZ0=; b=je+qgkQKXLSFW/2f72n2yxTZceh+jO1K1ka5xcQEhPOQ9ys8l28OAJCCF+sZyP05ROOXkV2EXmTw2GUwvIyocnRytizgdHSM3DLjivSTFtAIbNv6pBT4dSpDP0dnzZ9QUKSIFSAVGcEg36njNUfS64Ta2xmBNqhA5Y/w7Mr24lA= Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0WEm09Ww_1726018957) by smtp.aliyun-inc.com; Wed, 11 Sep 2024 09:42:38 +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 v2] mm: shmem: fix khugepaged activation policy for shmem Date: Wed, 11 Sep 2024 09:42:30 +0800 Message-Id: X-Mailer: git-send-email 2.39.3 MIME-Version: 1.0 X-Rspamd-Queue-Id: DAF4A80018 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 3am4jdbqdu7jrm5xcgrc7nw6o84cta7e X-HE-Tag: 1726018963-297762 X-HE-Meta: U2FsdGVkX18R8IXpjUCNoHHoYe3xyxL+qZZJl/KZW60pkZqW71Fc+Z6GDsmErKGx6tSF3T1C4xYrviU7DLPMUV9+E/OMEnsMTFwllgbABuDHCg2HdVE4ceYj0XsrgxtV2mQpucqCrgHkczM7pZTo4m2+7trTY12kXkCRA+SvToRYV/+K/BXEXTsTtZe+By2ZKdZgs5/paZWPuUAWtBmxQ7uegaZxWvtWKjp1Su2U3urjA1ESlINjr+BvrsBdaFd23nY05xlLuE/dQODWLGB+HyJVn67PJg7UcmKUqALC94S2FVMX6slWWl5eO8nXbXjgWf4hFiy12toaoDjN8WeppJ9iMW0GV3IzzIjclF8nIQeEN0gwJfbrlBn44/q3RcRpbf0OT+2Z3AXFHPF4IZc6nPm7KhwIPBd2aflQsykqXqlsqqKb7VIZH1nbhDHksO1Cx9nJq5SggvkpitL3C+HQiB3LUVQW/dhrx3nK43i64rSsl/YwsvtGRnjG0JNhGeVBvb9pxST4SN7GY8FOv0ft45PX9HoAtizd+yMO9oHk5WJOcamPAMa5w5Z8I2u/JYVOmtZJysrNBBte8ZDM7xS/elav7xzl/wvGfMnnEvS9Xu19XqVbeAt1RZoLbUlExYniArqBA7Doh6TQ9WDizyrJrNfiLuJb/Ahph2zL9U4rwbIjF+98/ho8OdsRj/ZFizB32YUw8/lsUtHfEZ8zA+o5E/phhiMwgfyzAgkRsRKxvEDaYdgA9snM2XxrV0YQkq/CZS6Ff5yeKo6mu6yAJNqHZrfHuOM2cwt4V5hFLnduJ++3bequSWDvx1xYLIKkUIbvj1HsEr5+qjwAdaRY17WFPnGb5aQn2XAXn6U3i9TqZcM0uxO8ehGEdrAZKf0w+pkbs8EGwdeCdp11CkPQQc9unawAeio5vc20J/58qtDpjJlhYgc36ueO3wvYUc7nOJNKuMuJm6zEEfDJQH+f8cC 96HozIeb pOQ1O+KoVfzgV33hg8MH+tg+8NjOxgwHGgtLck7k3RjN17ECMPw1hn1JU+YTWMQtop56eQ/uG2lcbWb2TVFVhY7yMITeL3kH/kHAfdK4YSGZi57rrMsPnZlamCsKYmjb5/2Ky3Lcxtg/4K5liwrLkzRxeWoTfyH4TKhCt1Op68x4xyVir0RAl8e+OOK3Aw6sxn/hpzSKU3OeLa5UNxTXpkmo5wXhPT4r2DPlVjo1zUT8PkG0+sx1cml6jeRrApKa1SV/PqZjvRhXH9cmQR9PdYC6d7IvprLwYRvw+ 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 --- Changes from v1: - Add reviewed tag from Ryan. Thanks. - Add some shmem comments per Ryan. --- include/linux/shmem_fs.h | 6 ++++++ mm/khugepaged.c | 6 +++++- mm/shmem.c | 29 +++++++++++++++++++++++++++-- 3 files changed, 38 insertions(+), 3 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..ee4dd03bf7d4 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -416,9 +416,11 @@ static inline int hpage_collapse_test_exit_or_disable(struct mm_struct *mm) static bool hugepage_pmd_enabled(void) { /* - * We cover both the anon and the file-backed case here; file-backed + * We cover the anon, shmem and the file-backed case here; file-backed * hugepages, when configured in, are determined by the global control. * Anon pmd-sized hugepages are determined by the pmd-size control. + * Shmem pmd-sized hugepages are also determined by its pmd-size control, + * except when the global shmem_huge is set to SHMEM_HUGE_DENY. */ if (IS_ENABLED(CONFIG_READ_ONLY_THP_FOR_FS) && hugepage_global_enabled()) @@ -430,6 +432,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 361affdf3990..181b1b051070 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; }