From mboxrd@z Thu Jan 1 00:00:00 1970 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 19B66C77B7F for ; Wed, 25 Jun 2025 01:40:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EE9B46B00AC; Tue, 24 Jun 2025 21:40:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D2B176B00AB; Tue, 24 Jun 2025 21:40:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BA3886B00AF; Tue, 24 Jun 2025 21:40:24 -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 885B76B00AB for ; Tue, 24 Jun 2025 21:40:24 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 489A1814BE for ; Wed, 25 Jun 2025 01:40:24 +0000 (UTC) X-FDA: 83592217968.23.48F2156 Received: from out30-131.freemail.mail.aliyun.com (out30-131.freemail.mail.aliyun.com [115.124.30.131]) by imf25.hostedemail.com (Postfix) with ESMTP id 676C7A0006 for ; Wed, 25 Jun 2025 01:40:22 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=Dxi26+wi; dmarc=pass (policy=none) header.from=linux.alibaba.com; spf=pass (imf25.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=1750815622; a=rsa-sha256; cv=none; b=cNsGPGb5cAGLoKvPq+p8uuhhMLYWsI8+VUN3Xj93Jm0AyAD8w/VUPQOrnmpdnw3o3bkrD4 g+az2udyZ6PhFg1hnUwH1M7GaioS19vvldx0WY3msFpi2lCtMAhaq4TyCmFJtyUk8pXP7J +2IgSyRbf9pjaWyCyagzT5fz4czq5yc= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=Dxi26+wi; dmarc=pass (policy=none) header.from=linux.alibaba.com; spf=pass (imf25.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=1750815622; 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:in-reply-to:references:references:dkim-signature; bh=GL9/OiTkXksGZXz7UtfDaxwwQxp+Wh0rcjedimt1h+Y=; b=Sq6scnPaRs+5jVqa7GUEzQR7iwEbW/aXwo7iJDDY5HNs2IYRFWqwSoUkZROg7rfxZatPaL GolUYGhlKgmeJBHqDWUmh1+hTaSnnXq9sesmwKj3A1yx2xd9UhclGbMRRI+HUbqQZEjCOA XiRCeA6Jd/JHs7VWhZ8tw2wdo7TlNzU= DKIM-Signature:v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1750815619; h=From:To:Subject:Date:Message-ID:MIME-Version:Content-Type; bh=GL9/OiTkXksGZXz7UtfDaxwwQxp+Wh0rcjedimt1h+Y=; b=Dxi26+wiEg0rCBlO4WCZmvGJIuVrM1Ipxi/m+k8f523S/UAuWEEYd0nfcRkN19pdX9tMw+O4bYu6fdMSNN5FNMqhN5F1ez5pYlKC9wtD5r6I1prk0YJ8bDv8v2G9PugseSMYY62Nuji39uZiBAgzRQ61IND+tgiCeJkxs7we3Mg= Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0WeqBlk4_1750815618 cluster:ay36) by smtp.aliyun-inc.com; Wed, 25 Jun 2025 09:40:19 +0800 From: Baolin Wang To: akpm@linux-foundation.org, hughd@google.com, david@redhat.com Cc: ziy@nvidia.com, lorenzo.stoakes@oracle.com, Liam.Howlett@oracle.com, npache@redhat.com, ryan.roberts@arm.com, dev.jain@arm.com, baohua@kernel.org, baolin.wang@linux.alibaba.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 2/2] mm: shmem: disallow hugepages if the system-wide shmem THP sysfs settings are disabled Date: Wed, 25 Jun 2025 09:40:10 +0800 Message-ID: X-Mailer: git-send-email 2.43.5 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Rspam-User: X-Rspamd-Queue-Id: 676C7A0006 X-Rspamd-Server: rspam10 X-Stat-Signature: 7qpoqjhsm1qbj14fwb5yw4do7urd5xwt X-HE-Tag: 1750815622-719791 X-HE-Meta: U2FsdGVkX18TlsmTrTJ9ZN9UyI5P+3HqG8vQQ398yPEyOFxxHj5z5lZDsAy8ThRWmFcrizpe0DUlgsdB5SzXuZ0uOLQoCh9G6bRAHhTvJOih85QLsKeb3BYGpb1Hft2dCgvHPsFFGloVHws8op6NlOraEh6yEx5NbbKGeiYrk6sI6NYX9fSSI4ZQFQOhQzeSkDu1rOHNJeNcMYobW/k/gHK7Nw0v+kNe3uxmhxBzUIBrtMv4hNZkkuGG5NuPN76e8OJZIzXpCyigOJF+4E8HlwkXS2/+VyhF5NIJCTfoUujZdSUQehQzBTWLFDsR55RD5DkVwfiFhtizJVKc54hBI5vINcxGfylxm69DMGydr+oZqmtUhTK2nN6N1crfo+1GB+bLyHEi9BgiZa8k0JMykON7mdWFkBEInOTGRT4pfyYsJ+Ntw2pVM4jbTDa/A3331LGKun5Y7BCe8U5Nl9qiwbSDHgIWj7dMojnOQrcvtM0G9eJVzbXi8pySBnaSQhqmpoqJExsHcLVDehNW6RM6E4UjaOt/OE0GIeD/z4gm1/MgiLjw/ro5hAhemhWMcWqewCQYlX/BbtzWBeOi9v6xs1/qt3tiRVMlked6W0BM5CFBMGTT6L5FYE1PKSKJT+ChPHkqckXaxrwppynGZS2wWmCzGLUWeehFonWF3dbfwRUIzSZktWtoNnFQ/d4Aiu9Lrcdlt6bZooGuUQLgqip9GqyUR18mQO3/0qdEShm9mcNI9NBWA49L3AhY9DLEsRCkrvJGyqx7/zxF2KOUZPM83nrQlWLNB08HrVss3mZ/EBPdyY8qOe1kV9IVKA22tn268IN8D5MitofVI5aMi7gOsUzG+nSIE75R7yjn8cy4fYDYKmGxuWXS5g== 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: When invoking thp_vma_allowable_orders(), if the TVA_ENFORCE_SYSFS flag is not specified, we will ignore the THP sysfs settings. And the MADV_COLLAPSE is an example of such a case. The MADV_COLLAPSE will ignore the system-wide shmem THP sysfs settings, which means that even though we have disabled the shmem THP configuration, MADV_COLLAPSE will still attempt to collapse into a shmem THP. This violates the rule we have agreed upon: never means never. Another rule for madvise, referring to David's suggestion: “allowing for collapsing in a VM without VM_HUGEPAGE in the "madvise" mode would be fine". To fix the MADV_COLLAPSE issue for shmem, then the current strategy should be: For shmem, if none of ‘always’, ‘madvise’, ‘within_size’, and ‘inherit’ have enabled PMD-sized THP, then MADV_COLLAPSE will be prohibited from collapsing PMD-sized THP. Similarly, if 'deny' is set, it will also prohibit MADV_COLLAPSE. For tmpfs, if the mount option is set with the 'huge=never' parameter, then MADV_COLLAPSE will be prohibited from collapsing PMD-sized THP. Meanwhile, we should fix the khugepaged selftest for shmem MADV_COLLAPSE by enabling shmem THP like anonymous pages collapse. Acked-by: Zi Yan Reviewed-by: Lorenzo Stoakes Signed-off-by: Baolin Wang --- mm/shmem.c | 6 +++--- tools/testing/selftests/mm/khugepaged.c | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index 2b19965d27df..e3f51fab2b7d 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -637,7 +637,7 @@ static unsigned int shmem_huge_global_enabled(struct inode *inode, pgoff_t index return 0; if (shmem_huge == SHMEM_HUGE_DENY) return 0; - if (shmem_huge_force || shmem_huge == SHMEM_HUGE_FORCE) + if (shmem_huge == SHMEM_HUGE_FORCE) return maybe_pmd_order; /* @@ -672,7 +672,7 @@ static unsigned int shmem_huge_global_enabled(struct inode *inode, pgoff_t index fallthrough; case SHMEM_HUGE_ADVISE: - if (vm_flags & VM_HUGEPAGE) + if (shmem_huge_force || (vm_flags & VM_HUGEPAGE)) return maybe_pmd_order; fallthrough; default: @@ -1806,7 +1806,7 @@ unsigned long shmem_allowable_huge_orders(struct inode *inode, /* Allow mTHP that will be fully within i_size. */ mask |= shmem_get_orders_within_size(inode, within_size_orders, index, 0); - if (vm_flags & VM_HUGEPAGE) + if (shmem_huge_force || (vm_flags & VM_HUGEPAGE)) mask |= READ_ONCE(huge_shmem_orders_madvise); if (global_orders > 0) diff --git a/tools/testing/selftests/mm/khugepaged.c b/tools/testing/selftests/mm/khugepaged.c index 85bfff53dba6..9517ed99c382 100644 --- a/tools/testing/selftests/mm/khugepaged.c +++ b/tools/testing/selftests/mm/khugepaged.c @@ -502,7 +502,7 @@ static void __madvise_collapse(const char *msg, char *p, int nr_hpages, printf("%s...", msg); settings.thp_enabled = THP_ALWAYS; - settings.shmem_enabled = SHMEM_NEVER; + settings.shmem_enabled = SHMEM_ALWAYS; thp_push_settings(&settings); /* Clear VM_NOHUGEPAGE */ -- 2.43.5