From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BFE8E3ED5A2 for ; Wed, 6 May 2026 18:34:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778092493; cv=none; b=HdpYFYr/5Z0fBYkcs2eVS/lO+X2Y7AdMq3ICoEDFjeLIrK1UWR0kaKbpmLoJk0zebid4n3qTJoYFwzxGX9Q3cx7J/95tXP4oH6Y9ReWWXIK7z2VgK7p6DWpUZfKO7CT5xV6sA+184XViznOGrqLSDyJqp+fCBB9rhReVlkhdeO0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778092493; c=relaxed/simple; bh=n7xyr8vRvJDDDj9QTMN4Zh0VnXwovZ9cWbdkk0d1eWI=; h=Message-ID:Date:MIME-Version:Subject:From:To:Cc:References: In-Reply-To:Content-Type; b=XyEogxRaXe9bqtu4/X29Mm9haSDYpvtKhAAtepOecNPVMG06qQulmfXCQ+ZGsWR1cV4PW7g67pB0PjqTQj2c5iNX8V9eNv7tkzQy25vL385KGKEREwmjfnkLzB6U1f6yu5RTIN8/g9imbDwbyBPm4rOgW7q5KJr08OdL0YMmIEw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=FMVoTlaT; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="FMVoTlaT" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1778092490; h=from:from: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; bh=Q7QTr1jCxOp/gsoLdmeTkxZFcNRoA5to1Y5n8ppLv4s=; b=FMVoTlaTyPnprxFbgpBHowREJLOlEn4MOOffn1Rj4Wr8xRgch5qabKQy+rMpPoF9RkDwK8 FfIxELGHKC2++JjIQJDnfHOQUXP1wHEvz9sX5gLw98JLjWwZ6zwxeJ7fo/9CwUEQTp4lIA qh3Igx74gRSCYfFSj6hF9fvrug4edTU= Received: from mail-qv1-f71.google.com (mail-qv1-f71.google.com [209.85.219.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-486-xGqtYnSXMoqM9cNm5HAALA-1; Wed, 06 May 2026 14:34:49 -0400 X-MC-Unique: xGqtYnSXMoqM9cNm5HAALA-1 X-Mimecast-MFC-AGG-ID: xGqtYnSXMoqM9cNm5HAALA_1778092489 Received: by mail-qv1-f71.google.com with SMTP id 6a1803df08f44-8b5ceedb5c1so117075476d6.0 for ; Wed, 06 May 2026 11:34:49 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778092489; x=1778697289; h=content-transfer-encoding:in-reply-to:content-language:references :cc:to:from:subject:user-agent:mime-version:date:message-id:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Q7QTr1jCxOp/gsoLdmeTkxZFcNRoA5to1Y5n8ppLv4s=; b=dumg/USCuspTF/HAk3nnP6oQYBH+kZEC3IRs5d9bRJtBnzFQmt7TnfllbWLjj3M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afa3 with SMTP id 6a1803df08f44-8bd75d1afc5mr12613626d6.7.1778092488588; Wed, 06 May 2026 11:34:48 -0700 (PDT) X-Received: by 2002:a05:6214:f2f:b0:8bd:75d1:afa3 with SMTP id 6a1803df08f44-8bd75d1afc5mr12612666d6.7.1778092487925; Wed, 06 May 2026 11:34:47 -0700 (PDT) Received: from [192.168.2.110] (bras-base-aylmpq0104w-grc-22-70-53-202-134.dsl.bell.ca. [70.53.202.134]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-8b53c1dc606sm184831366d6.31.2026.05.06.11.34.46 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 06 May 2026 11:34:47 -0700 (PDT) Message-ID: Date: Wed, 6 May 2026 14:34:46 -0400 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: (sashiko review) Re: [PATCH v4 9/9] mm: thp: always enable mTHP support From: Luiz Capitulino To: linux-kernel@vger.kernel.org, linux-mm@kvack.org, david@kernel.org, baolin.wang@linux.alibaba.com, ziy@nvidia.com, lance.yang@linux.dev Cc: corbet@lwn.net, tsbogend@alpha.franken.de, maddy@linux.ibm.com, mpe@ellerman.id.au, agordeev@linux.ibm.com, gerald.schaefer@linux.ibm.com, hca@linux.ibm.com, gor@linux.ibm.com, x86@kernel.org, dave.hansen@linux.intel.com, djbw@kernel.org, vishal.l.verma@intel.com, dave.jiang@intel.com, akpm@linux-foundation.org, lorenzo.stoakes@oracle.com References: Content-Language: en-US, en-CA In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 2026-05-01 15:18, Luiz Capitulino wrote: > If PMD-sized pages are not supported on an architecture (ie. the > arch implements arch_has_pmd_leaves() and it returns false) then the > current code disables all THP, including mTHP. > > This commit fixes this by allowing mTHP to be always enabled for all > archs. When PMD-sized pages are not supported, its sysfs entry won't be > created and their mapping will be disallowed at page-fault time. > > Similarly, this commit implements the following changes for shmem in > shmem_allowable_huge_orders(): > > - Drop the pgtable_has_pmd_leaves() check so that mTHP sizes are > considered > - Filter out PMD and PUD orders from allowable orders when > PMD-sized pages are not supported by the CPU > > Signed-off-by: Luiz Capitulino > --- > mm/huge_memory.c | 23 ++++++++++++++++++----- > mm/shmem.c | 14 +++++++++----- > 2 files changed, 27 insertions(+), 10 deletions(-) > > diff --git a/mm/huge_memory.c b/mm/huge_memory.c > index 32254febe097..c1765c8e3dc6 100644 > --- a/mm/huge_memory.c > +++ b/mm/huge_memory.c > @@ -126,6 +126,14 @@ unsigned long __thp_vma_allowable_orders(struct vm_area_struct *vma, > else > supported_orders = THP_ORDERS_ALL_FILE_DEFAULT; > > + if (!pgtable_has_pmd_leaves()) { > + /* > + * The CPU doesn't support PMD-sized pages, assume it > + * doesn't support PUD-sized pages either. > + */ > + supported_orders &= ~(BIT(PMD_ORDER) | BIT(PUD_ORDER)); > + } > + > orders &= supported_orders; > if (!orders) > return 0; > @@ -133,7 +141,7 @@ unsigned long __thp_vma_allowable_orders(struct vm_area_struct *vma, > if (!vma->vm_mm) /* vdso */ > return 0; > > - if (!pgtable_has_pmd_leaves() || vma_thp_disabled(vma, vm_flags, forced_collapse)) > + if (vma_thp_disabled(vma, vm_flags, forced_collapse)) > return 0; > > /* khugepaged doesn't collapse DAX vma, but page fault is fine. */ > @@ -848,7 +856,7 @@ static int __init hugepage_init_sysfs(struct kobject **hugepage_kobj) > * disable all other sizes. powerpc's PMD_ORDER isn't a compile-time > * constant so we have to do this here. > */ > - if (!anon_orders_configured) > + if (!anon_orders_configured && pgtable_has_pmd_leaves()) > huge_anon_orders_inherit = BIT(PMD_ORDER); > > *hugepage_kobj = kobject_create_and_add("transparent_hugepage", mm_kobj); > @@ -870,6 +878,14 @@ static int __init hugepage_init_sysfs(struct kobject **hugepage_kobj) > } > > orders = THP_ORDERS_ALL_ANON | THP_ORDERS_ALL_FILE_DEFAULT; > + if (!pgtable_has_pmd_leaves()) { > + /* > + * The CPU doesn't support PMD-sized pages, assume it > + * doesn't support PUD-sized pages either. > + */ > + orders &= ~(BIT(PMD_ORDER) | BIT(PUD_ORDER)); > + } > + > order = highest_order(orders); > while (orders) { > thpsize = thpsize_create(order, *hugepage_kobj); > @@ -969,9 +985,6 @@ static int __init hugepage_init(void) > int err; > struct kobject *hugepage_kobj; > > - if (!pgtable_has_pmd_leaves()) > - return -EINVAL; > - > /* > * hugepages can't be allocated by the buddy allocator > */ > diff --git a/mm/shmem.c b/mm/shmem.c > index a48f034830cd..23893c2bc2dd 100644 > --- a/mm/shmem.c > +++ b/mm/shmem.c > @@ -1840,16 +1840,19 @@ unsigned long shmem_allowable_huge_orders(struct inode *inode, > unsigned long mask = READ_ONCE(huge_shmem_orders_always); > unsigned long within_size_orders = READ_ONCE(huge_shmem_orders_within_size); > vm_flags_t vm_flags = vma ? vma->vm_flags : 0; > - unsigned int global_orders; > + unsigned int global_orders, filter_orders = 0; > > - if (!pgtable_has_pmd_leaves() || (vma && vma_thp_disabled(vma, vm_flags, shmem_huge_force))) > + if (vma && vma_thp_disabled(vma, vm_flags, shmem_huge_force)) > return 0; > > + if (!pgtable_has_pmd_leaves()) > + filter_orders = BIT(PMD_ORDER) | BIT(PUD_ORDER); > + > global_orders = shmem_huge_global_enabled(inode, index, write_end, > shmem_huge_force, vma, vm_flags); > /* Tmpfs huge pages allocation */ > if (!vma || !vma_is_anon_shmem(vma)) > - return global_orders; > + return global_orders & ~filter_orders; """ Could this lead to unintended truncation of the order masks? Because filter_orders is declared as an unsigned int, assigning the result of BIT() causes an implicit downcast from unsigned long. """ If I'm checking this right, the maximum bit number we can get to is 22 on arm64. So, we're safe. That being said we should be consistent with the return type for both filter_order and global_orders. I'll change it. NOTE: I'm skipping the other comment from Sashiko which is about the same issue. > > /* > * Following the 'deny' semantics of the top level, force the huge > @@ -1863,7 +1866,7 @@ unsigned long shmem_allowable_huge_orders(struct inode *inode, > * means non-PMD sized THP can not override 'huge' mount option now. > */ > if (shmem_huge == SHMEM_HUGE_FORCE) > - return READ_ONCE(huge_shmem_orders_inherit); > + return READ_ONCE(huge_shmem_orders_inherit) & ~filter_orders; > > /* Allow mTHP that will be fully within i_size. */ > mask |= shmem_get_orders_within_size(inode, within_size_orders, index, 0); > @@ -1874,6 +1877,7 @@ unsigned long shmem_allowable_huge_orders(struct inode *inode, > if (global_orders > 0) > mask |= READ_ONCE(huge_shmem_orders_inherit); > > + mask &= ~filter_orders; > return THP_ORDERS_ALL_FILE_DEFAULT & mask; > } > > @@ -5457,7 +5461,7 @@ void __init shmem_init(void) > * Default to setting PMD-sized THP to inherit the global setting and > * disable all other multi-size THPs. > */ > - if (!shmem_orders_configured) > + if (!shmem_orders_configured && pgtable_has_pmd_leaves()) > huge_shmem_orders_inherit = BIT(HPAGE_PMD_ORDER); > #endif > return;