From nobody Wed Dec 17 17:22:21 2025 Received: from out30-131.freemail.mail.aliyun.com (out30-131.freemail.mail.aliyun.com [115.124.30.131]) (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 993C91411CE for ; Mon, 6 May 2024 08:46:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.131 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714985210; cv=none; b=F+MVNNH/wLXNrIR5Fuy/i+9/mZPI0M1XnhFGmgf9lnUlqfBFE9j5Z5MdOt8oog41rcr1lDaZ0V1bEuL+/J+HD4rViTGCraIKkFiwGteePNkfkcNf9WrxDq4dUM3kSwzTYM+Dj+FV+JKVAyHZ2J+ec6UJX0dEyB24IWPN3tlTFRA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714985210; c=relaxed/simple; bh=rApUqYVsVt9Lmm4m+jmthQspAsl2swdyFeKQOkOm8b0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=kFdidOZugT8WRNKo0gC4KPnQVAcf0o24YLBf4ugguPWMpd3Af5ezzigrZsnbudLH+AhffdbNJhAWAXZwmF10PLPd0yCuUqVZymiQ4JHZY7gmdzjhUzLEfDEymNO2eQEWiaMSsocJQTTmnNYYNFI2sR2wdkGPcCMOMNrW1V1gF14= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=niS9UOjU; arc=none smtp.client-ip=115.124.30.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="niS9UOjU" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1714985206; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=p4+ojbN485lnDhEdHj7u8+ldhF5xWmY9Y6UtO0qnCFI=; b=niS9UOjU7LndqDhtzwDcEt+Nfw0URVg8yrCSFEALpmc3Vm5xcN/riz/NmlSyJqe7ND5+tiPqsKIzdFgV+vDpQ79KB5m6B03drAkW/amtl07eFjEmIzsvbwN3S0oWNkQzZDEC8NQG86j27UDgMD78jvNe1hRIBb1uP7LXmR7Wk3Q= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R571e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam033032014031;MF=baolin.wang@linux.alibaba.com;NM=1;PH=DS;RN=14;SR=0;TI=SMTPD_---0W5weiJg_1714985203; Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0W5weiJg_1714985203) by smtp.aliyun-inc.com; Mon, 06 May 2024 16:46:44 +0800 From: Baolin Wang To: akpm@linux-foundation.org, hughd@google.com Cc: willy@infradead.org, david@redhat.com, ioworker0@gmail.com, wangkefeng.wang@huawei.com, ying.huang@intel.com, 21cnbao@gmail.com, ryan.roberts@arm.com, shy828301@gmail.com, ziy@nvidia.com, baolin.wang@linux.alibaba.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 1/8] mm: move highest_order() and next_order() out of the THP config Date: Mon, 6 May 2024 16:46:25 +0800 Message-Id: X-Mailer: git-send-email 2.39.3 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Move highest_order() and next_order() out of the CONFIG_TRANSPARENT_HUGEPAGE macro, which can be common functions to be used. Signed-off-by: Baolin Wang Tested-by: Lance Yang --- include/linux/huge_mm.h | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h index 017cee864080..e49b56c40a11 100644 --- a/include/linux/huge_mm.h +++ b/include/linux/huge_mm.h @@ -106,6 +106,17 @@ extern struct kobj_attribute shmem_enabled_attr; #define HPAGE_PUD_MASK (~(HPAGE_PUD_SIZE - 1)) #define HPAGE_PUD_SIZE ((1UL) << HPAGE_PUD_SHIFT) =20 +static inline int highest_order(unsigned long orders) +{ + return fls_long(orders) - 1; +} + +static inline int next_order(unsigned long *orders, int prev) +{ + *orders &=3D ~BIT(prev); + return highest_order(*orders); +} + #ifdef CONFIG_TRANSPARENT_HUGEPAGE =20 extern unsigned long transparent_hugepage_flags; @@ -138,17 +149,6 @@ static inline bool hugepage_flags_enabled(void) huge_anon_orders_madvise; } =20 -static inline int highest_order(unsigned long orders) -{ - return fls_long(orders) - 1; -} - -static inline int next_order(unsigned long *orders, int prev) -{ - *orders &=3D ~BIT(prev); - return highest_order(*orders); -} - /* * Do the below checks: * - For file vma, check if the linear page offset of vma is --=20 2.39.3 From nobody Wed Dec 17 17:22:21 2025 Received: from out30-101.freemail.mail.aliyun.com (out30-101.freemail.mail.aliyun.com [115.124.30.101]) (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 CE1601420A6 for ; Mon, 6 May 2024 08:46:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.101 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714985212; cv=none; b=pKEkWJs4gdYXEA1NY0VBvi376OZtjzOVkpom3j98AzTBvTAgJkYEdrj617645G9mlmLHMlgB7EXYMwvfC+TBiJCzYT1GHLjT3dFNBHuO5xSacRnLyTg2ELByKdRzu5SDx5kH416IQf7rWWicaGgZLtsro8KFdOsW8+mhSvHDr+c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714985212; c=relaxed/simple; bh=TtEvthxzqcH6fibOTrZH8nZvm8Cacr0Qx1M3RVs4Sq8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=kiSTuLvQvKid/P8R/UjLKIUs7OC0NsQj09MQSCMMg6pGPFbuZigDeUpvVDdxTCB5nXSKE3t0unBp/GuWOHWLQyvzCXZr7++iie4LNrjXDKg7nHwnq4f66/bB1IjQiUKcjfmtL04UUIdQYWf9DyxxVvYC5umLajgjlKvCjg7POoY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=VaC/kRe1; arc=none smtp.client-ip=115.124.30.101 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="VaC/kRe1" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1714985207; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=zEr/Eu5mgqpEki8XSyTh3V+LovJcAoeI3KW1/F4hkgg=; b=VaC/kRe1uXXZZhETUNZOyPdATJLv9wdN9iawFkKUMls4e7lUzINH4uM0PxZENld2U6PYzjxI7QUSAKP0Az7SdKF6WA6ea1z1iMbU4a+R0Od02k2s4ZvGl6Pkke/GkaIFIoCl7ij1a0NoZcbMhyA1FELXiH5ihqdVzv3+OtK4feY= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R101e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam033022160150;MF=baolin.wang@linux.alibaba.com;NM=1;PH=DS;RN=14;SR=0;TI=SMTPD_---0W5weiKG_1714985205; Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0W5weiKG_1714985205) by smtp.aliyun-inc.com; Mon, 06 May 2024 16:46:46 +0800 From: Baolin Wang To: akpm@linux-foundation.org, hughd@google.com Cc: willy@infradead.org, david@redhat.com, ioworker0@gmail.com, wangkefeng.wang@huawei.com, ying.huang@intel.com, 21cnbao@gmail.com, ryan.roberts@arm.com, shy828301@gmail.com, ziy@nvidia.com, baolin.wang@linux.alibaba.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/8] mm: memory: extend finish_fault() to support large folio Date: Mon, 6 May 2024 16:46:26 +0800 Message-Id: X-Mailer: git-send-email 2.39.3 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add large folio mapping establishment support for finish_fault() as a prepa= ration, to support multi-size THP allocation of anonymous shmem pages in the follow= ing patches. Signed-off-by: Baolin Wang Tested-by: Lance Yang --- mm/memory.c | 43 +++++++++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index eea6e4984eae..936377220b77 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -4747,9 +4747,12 @@ vm_fault_t finish_fault(struct vm_fault *vmf) { struct vm_area_struct *vma =3D vmf->vma; struct page *page; + struct folio *folio; vm_fault_t ret; bool is_cow =3D (vmf->flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED); + int type, nr_pages, i; + unsigned long addr =3D vmf->address; =20 /* Did we COW the page? */ if (is_cow) @@ -4780,24 +4783,44 @@ vm_fault_t finish_fault(struct vm_fault *vmf) return VM_FAULT_OOM; } =20 + folio =3D page_folio(page); + nr_pages =3D folio_nr_pages(folio); + + if (unlikely(userfaultfd_armed(vma))) { + nr_pages =3D 1; + } else if (nr_pages > 1) { + unsigned long start =3D ALIGN_DOWN(vmf->address, nr_pages * PAGE_SIZE); + unsigned long end =3D start + nr_pages * PAGE_SIZE; + + /* In case the folio size in page cache beyond the VMA limits. */ + addr =3D max(start, vma->vm_start); + nr_pages =3D (min(end, vma->vm_end) - addr) >> PAGE_SHIFT; + + page =3D folio_page(folio, (addr - start) >> PAGE_SHIFT); + } vmf->pte =3D pte_offset_map_lock(vma->vm_mm, vmf->pmd, - vmf->address, &vmf->ptl); + addr, &vmf->ptl); if (!vmf->pte) return VM_FAULT_NOPAGE; =20 /* Re-check under ptl */ - if (likely(!vmf_pte_changed(vmf))) { - struct folio *folio =3D page_folio(page); - int type =3D is_cow ? MM_ANONPAGES : mm_counter_file(folio); - - set_pte_range(vmf, folio, page, 1, vmf->address); - add_mm_counter(vma->vm_mm, type, 1); - ret =3D 0; - } else { - update_mmu_tlb(vma, vmf->address, vmf->pte); + if (nr_pages =3D=3D 1 && unlikely(vmf_pte_changed(vmf))) { + update_mmu_tlb(vma, addr, vmf->pte); + ret =3D VM_FAULT_NOPAGE; + goto unlock; + } else if (nr_pages > 1 && !pte_range_none(vmf->pte, nr_pages)) { + for (i =3D 0; i < nr_pages; i++) + update_mmu_tlb(vma, addr + PAGE_SIZE * i, vmf->pte + i); ret =3D VM_FAULT_NOPAGE; + goto unlock; } =20 + set_pte_range(vmf, folio, page, nr_pages, addr); + type =3D is_cow ? MM_ANONPAGES : mm_counter_file(folio); + add_mm_counter(vma->vm_mm, type, nr_pages); + ret =3D 0; + +unlock: pte_unmap_unlock(vmf->pte, vmf->ptl); return ret; } --=20 2.39.3 From nobody Wed Dec 17 17:22:21 2025 Received: from out30-130.freemail.mail.aliyun.com (out30-130.freemail.mail.aliyun.com [115.124.30.130]) (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 B7B401422AE for ; Mon, 6 May 2024 08:46:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.130 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714985214; cv=none; b=flEACpr2X5qbLosewBCoFnVo4osto06PDDISIekXwDS8oBvTlemhqQQn3QhHNxM8LaELgD67deZ1nO5uSCydEuYYzCrId4u4TBTg1D17eo8jXt8soU3f2f0X+Bf07Ca8rG55Owg+2lCT3xUwKLYsQLz1fiusyWvb8jH1atGsFtg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714985214; c=relaxed/simple; bh=KSE8PzIjqSwH61R6iAt633ULjGLPnWmzNqBKlZgUjh8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=JRoUC1d4rjpoV0PHI61nt2B4rKmP/q1Nzq/AzbssyI4to3byWoyjI8icBhjnkv6YE9xRU5iG+Md6vGN97HckneTU8bzRmsxPwwih+P2cPB4lMRB017blPzA/vhWnkMciavHEFRPZTuNGrG/uh3KlJzMEpov5oTd1jblBDtYD3mM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=NBO3RhgZ; arc=none smtp.client-ip=115.124.30.130 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="NBO3RhgZ" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1714985209; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=e+vn44n5PSOyoo2A5G9/u00K/6MLWmZWHwZJ5wJKuyA=; b=NBO3RhgZtwwSxO/fhmCPakT/RPYawsIkyGEhX/KM/GFSgkZswZ3uv7xtXtsWEjhKjq1ki+1ki6grE2SeMeRsFfTW1EjUkG7ZGXFdA6g5LzJ8DHdqkDKVbmysXw2pT4vMWdTvre0GNyO0EeVMIyXjt0OUwxMMLI/9lNGtHngDTz8= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R721e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam033037067111;MF=baolin.wang@linux.alibaba.com;NM=1;PH=DS;RN=14;SR=0;TI=SMTPD_---0W5wQIAj_1714985206; Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0W5wQIAj_1714985206) by smtp.aliyun-inc.com; Mon, 06 May 2024 16:46:47 +0800 From: Baolin Wang To: akpm@linux-foundation.org, hughd@google.com Cc: willy@infradead.org, david@redhat.com, ioworker0@gmail.com, wangkefeng.wang@huawei.com, ying.huang@intel.com, 21cnbao@gmail.com, ryan.roberts@arm.com, shy828301@gmail.com, ziy@nvidia.com, baolin.wang@linux.alibaba.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 3/8] mm: shmem: add an 'order' parameter for shmem_alloc_hugefolio() Date: Mon, 6 May 2024 16:46:27 +0800 Message-Id: <1484509ad5a9751b449e01fb695c4e7634be14bf.1714978902.git.baolin.wang@linux.alibaba.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a new parameter to specify the huge page order for shmem_alloc_hugefoli= o(), as a preparation to supoort mTHP. Signed-off-by: Baolin Wang Tested-by: Lance Yang --- mm/shmem.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index fa2a0ed97507..e4483c4596a8 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1604,14 +1604,14 @@ static gfp_t limit_gfp_mask(gfp_t huge_gfp, gfp_t l= imit_gfp) } =20 static struct folio *shmem_alloc_hugefolio(gfp_t gfp, - struct shmem_inode_info *info, pgoff_t index) + struct shmem_inode_info *info, pgoff_t index, int order) { struct mempolicy *mpol; pgoff_t ilx; struct page *page; =20 - mpol =3D shmem_get_pgoff_policy(info, index, HPAGE_PMD_ORDER, &ilx); - page =3D alloc_pages_mpol(gfp, HPAGE_PMD_ORDER, mpol, ilx, numa_node_id()= ); + mpol =3D shmem_get_pgoff_policy(info, index, order, &ilx); + page =3D alloc_pages_mpol(gfp, order, mpol, ilx, numa_node_id()); mpol_cond_put(mpol); =20 return page_rmappable_folio(page); @@ -1660,7 +1660,7 @@ static struct folio *shmem_alloc_and_add_folio(gfp_t = gfp, index + HPAGE_PMD_NR - 1, XA_PRESENT)) return ERR_PTR(-E2BIG); =20 - folio =3D shmem_alloc_hugefolio(gfp, info, index); + folio =3D shmem_alloc_hugefolio(gfp, info, index, HPAGE_PMD_ORDER); if (!folio) count_vm_event(THP_FILE_FALLBACK); } else { --=20 2.39.3 From nobody Wed Dec 17 17:22:21 2025 Received: from out30-101.freemail.mail.aliyun.com (out30-101.freemail.mail.aliyun.com [115.124.30.101]) (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 60DB21422D9 for ; Mon, 6 May 2024 08:46:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.101 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714985215; cv=none; b=k1LQ6eDrzPJP2oGH0OmoEQ819aSTwapD4BJI+LKTW9E6o2nyopqyianbEJwCDvYt+Hi/SFZ7WkSENRZshfwtvDXl88YnNHL7Y1NKpJyhB4Kp05rmLHQyJkwDdph1ZhciNDhymJMIl++XONWHOWBJKo3Jd4nCLhoRK9vgCQlY+ig= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714985215; c=relaxed/simple; bh=aXZ3X5BsKkhtjbzkDsuZ2qmB5MEnQRxlo7XdIgbTI/w=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=d8o3Zk4cxCPLbCJLthfYjO0Rxnmo2G2JMnnAH9uIZ4nH37Yt3Bwl7pD4wv7LeoBshoc+8d3aM7TU13vART7woMDopjYP1MeoOT/R4GPIubzcXGmuhx1yjgXnp0SYRfdJ6nIeOkY5izqeUgyM3AMlgEP9KT3nNoT7wQu1HkUkJ+Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=ARCWruIb; arc=none smtp.client-ip=115.124.30.101 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="ARCWruIb" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1714985211; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=aUksnnssWSvRHZu6SVpR2z8swC3sgIQdPUjmp/ogrf0=; b=ARCWruIbNbLap1Zvf8QiIb609LrCzQXRyRWkpUh1wRWfe6eNKFHF7I4hUdge5LH0/NBbgnU9kBAD4jZD5wZIDY5kj4ILhW4W7Ulb57n2wbUgmEurGTeafF4yZijdSlIFQFiOJ7DWraO6pi1j/uUFZ4RET6HSbVY863X6a9AxtsE= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R101e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam033068173054;MF=baolin.wang@linux.alibaba.com;NM=1;PH=DS;RN=14;SR=0;TI=SMTPD_---0W5w3v7-_1714985207; Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0W5w3v7-_1714985207) by smtp.aliyun-inc.com; Mon, 06 May 2024 16:46:48 +0800 From: Baolin Wang To: akpm@linux-foundation.org, hughd@google.com Cc: willy@infradead.org, david@redhat.com, ioworker0@gmail.com, wangkefeng.wang@huawei.com, ying.huang@intel.com, 21cnbao@gmail.com, ryan.roberts@arm.com, shy828301@gmail.com, ziy@nvidia.com, baolin.wang@linux.alibaba.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 4/8] mm: shmem: add THP validation for PMD-mapped THP related statistics Date: Mon, 6 May 2024 16:46:28 +0800 Message-Id: X-Mailer: git-send-email 2.39.3 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In order to extend support for mTHP, add THP validation for PMD-mapped THP related statistics to avoid statistical confusion. Signed-off-by: Baolin Wang Reviewed-by: Barry Song Tested-by: Lance Yang --- mm/shmem.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index e4483c4596a8..a383ea9a89a5 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1661,7 +1661,7 @@ static struct folio *shmem_alloc_and_add_folio(gfp_t = gfp, return ERR_PTR(-E2BIG); =20 folio =3D shmem_alloc_hugefolio(gfp, info, index, HPAGE_PMD_ORDER); - if (!folio) + if (!folio && pages =3D=3D HPAGE_PMD_NR) count_vm_event(THP_FILE_FALLBACK); } else { pages =3D 1; @@ -1679,7 +1679,7 @@ static struct folio *shmem_alloc_and_add_folio(gfp_t = gfp, if (xa_find(&mapping->i_pages, &index, index + pages - 1, XA_PRESENT)) { error =3D -EEXIST; - } else if (huge) { + } else if (pages =3D=3D HPAGE_PMD_NR) { count_vm_event(THP_FILE_FALLBACK); count_vm_event(THP_FILE_FALLBACK_CHARGE); } @@ -2045,7 +2045,8 @@ static int shmem_get_folio_gfp(struct inode *inode, p= goff_t index, folio =3D shmem_alloc_and_add_folio(huge_gfp, inode, index, fault_mm, true); if (!IS_ERR(folio)) { - count_vm_event(THP_FILE_ALLOC); + if (folio_test_pmd_mappable(folio)) + count_vm_event(THP_FILE_ALLOC); goto alloced; } if (PTR_ERR(folio) =3D=3D -EEXIST) --=20 2.39.3 From nobody Wed Dec 17 17:22:21 2025 Received: from out30-124.freemail.mail.aliyun.com (out30-124.freemail.mail.aliyun.com [115.124.30.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 ABDA6142E76 for ; Mon, 6 May 2024 08:46:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714985222; cv=none; b=nykNTFPV1JeMHoReMEREgj825KupzVfrsRZ+sD85qmRk3iLrEDhtslgSizZDXM/V8AxnSmH/UUlEvd3CTR5SvdYWB61HnkGhbobFHCm5z6DKUiW6pt88BGg0i7HBvt1XXsRlJBSzkBKuRS6wVX12FFPLYKmX/+R6W0WqceZlvVs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714985222; c=relaxed/simple; bh=onYO+hDGg55XvRs/b0q2a0g+jYhwpA694tA8GodAcmk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=iKf/JYSQQxyGhY80F//6wCTFQEB+iCyWzTya5lYCE5WxC5cvazcQ+zfIncldTiJGGmKVVpEsY7tzKt35V8Fx4kVhJ4JvZHPRp/rgys/uXTBizlfyVASX4nhhNgBmzY+0Y7T9C8Rsjp6GP99f4uegGxrz8nDE53gwvEQLDvRlFDI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=U+fSD8Bx; arc=none smtp.client-ip=115.124.30.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="U+fSD8Bx" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1714985212; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=vbAZ9jfAMkqVV3Kg32TDOediCKK1trUe5zVBa6e0pWM=; b=U+fSD8Bx3p1i0NvaIXfxpMc20W6bOs3m8DHQ4Lt3uCGviA9zVsWrjrzY0ti5A4Gh5RfNCaLl5rMZ9R/FVrKUQQlH6ybd06wS6jvQzKcUJ9sFY8ysvPSSza40gYjJv3noUJ7U7hSMd8cVDEbBpSUrlLGzDfBhAEtjTBnO/xEOXBY= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R181e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam033045046011;MF=baolin.wang@linux.alibaba.com;NM=1;PH=DS;RN=14;SR=0;TI=SMTPD_---0W5w3v7S_1714985209; Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0W5w3v7S_1714985209) by smtp.aliyun-inc.com; Mon, 06 May 2024 16:46:50 +0800 From: Baolin Wang To: akpm@linux-foundation.org, hughd@google.com Cc: willy@infradead.org, david@redhat.com, ioworker0@gmail.com, wangkefeng.wang@huawei.com, ying.huang@intel.com, 21cnbao@gmail.com, ryan.roberts@arm.com, shy828301@gmail.com, ziy@nvidia.com, baolin.wang@linux.alibaba.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 5/8] mm: shmem: add multi-size THP sysfs interface for anonymous shmem Date: Mon, 6 May 2024 16:46:29 +0800 Message-Id: <6b4afed1ef26dbd08ae9ec58449b329564dcef3e.1714978902.git.baolin.wang@linux.alibaba.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" To support the use of mTHP with anonymous shmem, add a new sysfs interface 'shmem_enabled' in the '/sys/kernel/mm/transparent_hugepage/hugepages-kB/' directory for each mTHP to control whether shmem is enabled for that mTHP, with a value similar to the top level 'shmem_enabled', which can be set to: "always", "inherit (to inherit the top level setting)", "within_size", "adv= ise", "never", "deny", "force". These values follow the same semantics as the top level, except the 'deny' is equivalent to 'never', and 'force' is equivalent to 'always' to keep compatibility. By default, PMD-sized hugepages have enabled=3D"inherit" and all other huge= page sizes have enabled=3D"never" for '/sys/kernel/mm/transparent_hugepage/hugep= ages-xxkB/shmem_enabled'. In addition, if top level value is 'force', then only PMD-sized hugepages have enabled=3D"inherit", otherwise configuration will be failed and vice v= ersa. That means now we will avoid using non-PMD sized THP to override the global huge allocation. Signed-off-by: Baolin Wang Tested-by: Lance Yang --- Documentation/admin-guide/mm/transhuge.rst | 29 +++++++ include/linux/huge_mm.h | 10 +++ mm/huge_memory.c | 11 +-- mm/shmem.c | 96 ++++++++++++++++++++++ 4 files changed, 138 insertions(+), 8 deletions(-) diff --git a/Documentation/admin-guide/mm/transhuge.rst b/Documentation/adm= in-guide/mm/transhuge.rst index 076443cc10a6..a28496e15bdb 100644 --- a/Documentation/admin-guide/mm/transhuge.rst +++ b/Documentation/admin-guide/mm/transhuge.rst @@ -332,6 +332,35 @@ deny force Force the huge option on for all - very useful for testing; =20 +Anonymous shmem can also use "multi-size THP" (mTHP) by adding a new sysfs= knob +to control mTHP allocation: /sys/kernel/mm/transparent_hugepage/hugepages-= kB/shmem_enabled. +Its value for each mTHP is essentially consistent with the global setting,= except +for the addition of 'inherit' to ensure compatibility with the global sett= ings. +always + Attempt to allocate huge pages every time we need a new page; + +inherit + Inherit the top-level "shmem_enabled" value. By default, PMD-sized hug= epages + have enabled=3D"inherit" and all other hugepage sizes have enabled=3D"= never"; + +never + Do not allocate huge pages; + +within_size + Only allocate huge page if it will be fully within i_size. + Also respect fadvise()/madvise() hints; + +advise + Only allocate huge pages if requested with fadvise()/madvise(); + +deny + Has the same semantics as 'never', now mTHP allocation policy is only + used for anonymous shmem and no not override tmpfs. + +force + Has the same semantics as 'always', now mTHP allocation policy is only + used for anonymous shmem and no not override tmpfs. + Need of application restart =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D =20 diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h index e49b56c40a11..dbd6b3f56210 100644 --- a/include/linux/huge_mm.h +++ b/include/linux/huge_mm.h @@ -6,6 +6,7 @@ #include =20 #include /* only for vma_is_dax() */ +#include =20 vm_fault_t do_huge_pmd_anonymous_page(struct vm_fault *vmf); int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm, @@ -63,6 +64,7 @@ ssize_t single_hugepage_flag_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf, enum transparent_hugepage_flag flag); extern struct kobj_attribute shmem_enabled_attr; +extern struct kobj_attribute thpsize_shmem_enabled_attr; =20 /* * Mask of all large folio orders supported for anonymous THP; all orders = up to @@ -265,6 +267,14 @@ unsigned long thp_vma_allowable_orders(struct vm_area_= struct *vma, return __thp_vma_allowable_orders(vma, vm_flags, tva_flags, orders); } =20 +struct thpsize { + struct kobject kobj; + struct list_head node; + int order; +}; + +#define to_thpsize(kobj) container_of(kobj, struct thpsize, kobj) + enum mthp_stat_item { MTHP_STAT_ANON_FAULT_ALLOC, MTHP_STAT_ANON_FAULT_FALLBACK, diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 9efb6fefc391..d3080a8843f2 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -449,14 +449,6 @@ static void thpsize_release(struct kobject *kobj); static DEFINE_SPINLOCK(huge_anon_orders_lock); static LIST_HEAD(thpsize_list); =20 -struct thpsize { - struct kobject kobj; - struct list_head node; - int order; -}; - -#define to_thpsize(kobj) container_of(kobj, struct thpsize, kobj) - static ssize_t thpsize_enabled_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) { @@ -517,6 +509,9 @@ static struct kobj_attribute thpsize_enabled_attr =3D =20 static struct attribute *thpsize_attrs[] =3D { &thpsize_enabled_attr.attr, +#ifdef CONFIG_SHMEM + &thpsize_shmem_enabled_attr.attr, +#endif NULL, }; =20 diff --git a/mm/shmem.c b/mm/shmem.c index a383ea9a89a5..59cc26d44344 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -131,6 +131,14 @@ struct shmem_options { #define SHMEM_SEEN_QUOTA 32 }; =20 +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +static unsigned long huge_anon_shmem_orders_always __read_mostly; +static unsigned long huge_anon_shmem_orders_madvise __read_mostly; +static unsigned long huge_anon_shmem_orders_inherit __read_mostly; +static unsigned long huge_anon_shmem_orders_within_size __read_mostly; +static DEFINE_SPINLOCK(huge_anon_shmem_orders_lock); +#endif + #ifdef CONFIG_TMPFS static unsigned long shmem_default_max_blocks(void) { @@ -4687,6 +4695,12 @@ void __init shmem_init(void) SHMEM_SB(shm_mnt->mnt_sb)->huge =3D shmem_huge; else shmem_huge =3D SHMEM_HUGE_NEVER; /* just in case it was patched */ + + /* + * Default to setting PMD-sized THP to inherit the global setting and + * disable all other multi-size THPs, when anonymous shmem uses mTHP. + */ + huge_anon_shmem_orders_inherit =3D BIT(HPAGE_PMD_ORDER); #endif return; =20 @@ -4746,6 +4760,11 @@ static ssize_t shmem_enabled_store(struct kobject *k= obj, huge !=3D SHMEM_HUGE_NEVER && huge !=3D SHMEM_HUGE_DENY) return -EINVAL; =20 + /* Do not override huge allocation policy with non-PMD sized mTHP */ + if (huge =3D=3D SHMEM_HUGE_FORCE && + huge_anon_shmem_orders_inherit !=3D BIT(HPAGE_PMD_ORDER)) + return -EINVAL; + shmem_huge =3D huge; if (shmem_huge > SHMEM_HUGE_DENY) SHMEM_SB(shm_mnt->mnt_sb)->huge =3D shmem_huge; @@ -4753,6 +4772,83 @@ static ssize_t shmem_enabled_store(struct kobject *k= obj, } =20 struct kobj_attribute shmem_enabled_attr =3D __ATTR_RW(shmem_enabled); + +static ssize_t thpsize_shmem_enabled_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + int order =3D to_thpsize(kobj)->order; + const char *output; + + if (test_bit(order, &huge_anon_shmem_orders_always)) + output =3D "[always] inherit within_size advise never deny [force]"; + else if (test_bit(order, &huge_anon_shmem_orders_inherit)) + output =3D "always [inherit] within_size advise never deny force"; + else if (test_bit(order, &huge_anon_shmem_orders_within_size)) + output =3D "always inherit [within_size] advise never deny force"; + else if (test_bit(order, &huge_anon_shmem_orders_madvise)) + output =3D "always inherit within_size [advise] never deny force"; + else + output =3D "always inherit within_size advise [never] [deny] force"; + + return sysfs_emit(buf, "%s\n", output); +} + +static ssize_t thpsize_shmem_enabled_store(struct kobject *kobj, + struct kobj_attribute *attr, + const char *buf, size_t count) +{ + int order =3D to_thpsize(kobj)->order; + ssize_t ret =3D count; + + if (sysfs_streq(buf, "always") || sysfs_streq(buf, "force")) { + spin_lock(&huge_anon_shmem_orders_lock); + clear_bit(order, &huge_anon_shmem_orders_inherit); + clear_bit(order, &huge_anon_shmem_orders_madvise); + clear_bit(order, &huge_anon_shmem_orders_within_size); + set_bit(order, &huge_anon_shmem_orders_always); + spin_unlock(&huge_anon_shmem_orders_lock); + } else if (sysfs_streq(buf, "inherit")) { + /* Do not override huge allocation policy with non-PMD sized mTHP */ + if (shmem_huge =3D=3D SHMEM_HUGE_FORCE && + order !=3D HPAGE_PMD_ORDER) + return -EINVAL; + + spin_lock(&huge_anon_shmem_orders_lock); + clear_bit(order, &huge_anon_shmem_orders_always); + clear_bit(order, &huge_anon_shmem_orders_madvise); + clear_bit(order, &huge_anon_shmem_orders_within_size); + set_bit(order, &huge_anon_shmem_orders_inherit); + spin_unlock(&huge_anon_shmem_orders_lock); + } else if (sysfs_streq(buf, "within_size")) { + spin_lock(&huge_anon_shmem_orders_lock); + clear_bit(order, &huge_anon_shmem_orders_always); + clear_bit(order, &huge_anon_shmem_orders_inherit); + clear_bit(order, &huge_anon_shmem_orders_madvise); + set_bit(order, &huge_anon_shmem_orders_within_size); + spin_unlock(&huge_anon_shmem_orders_lock); + } else if (sysfs_streq(buf, "madvise")) { + spin_lock(&huge_anon_shmem_orders_lock); + clear_bit(order, &huge_anon_shmem_orders_always); + clear_bit(order, &huge_anon_shmem_orders_inherit); + clear_bit(order, &huge_anon_shmem_orders_within_size); + set_bit(order, &huge_anon_shmem_orders_madvise); + spin_unlock(&huge_anon_shmem_orders_lock); + } else if (sysfs_streq(buf, "never") || sysfs_streq(buf, "deny")) { + spin_lock(&huge_anon_shmem_orders_lock); + clear_bit(order, &huge_anon_shmem_orders_always); + clear_bit(order, &huge_anon_shmem_orders_inherit); + clear_bit(order, &huge_anon_shmem_orders_within_size); + clear_bit(order, &huge_anon_shmem_orders_madvise); + spin_unlock(&huge_anon_shmem_orders_lock); + } else { + ret =3D -EINVAL; + } + + return ret; +} + +struct kobj_attribute thpsize_shmem_enabled_attr =3D + __ATTR(shmem_enabled, 0644, thpsize_shmem_enabled_show, thpsize_shmem_ena= bled_store); #endif /* CONFIG_TRANSPARENT_HUGEPAGE && CONFIG_SYSFS */ =20 #else /* !CONFIG_SHMEM */ --=20 2.39.3 From nobody Wed Dec 17 17:22:21 2025 Received: from out30-99.freemail.mail.aliyun.com (out30-99.freemail.mail.aliyun.com [115.124.30.99]) (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 90B4A1428EE for ; Mon, 6 May 2024 08:46:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.99 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714985218; cv=none; b=Rymv1eDYmUWYJAXVRYbs7RQhN7c8lHzfud3o4shmo9sGyP1XsBMUQlj47QaMxJfGyn1FZCNHqXCSmcS2pD5qMyTPlnCIoWoxbJi12Rgn6GfmpHMbCuBX7impM56b78E7sbVp9yhJ/CwIhD5laLTmnpYbLCQGHrGoSVOMzDSJl0o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714985218; c=relaxed/simple; bh=zbAywCMVYCP3TkeDToSW6KPATMiu/KIVpMgId6NBSzs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=K66SEkh60Oad3dgFhu2+2ICnZR/RbQYUjpzO5b4HVZh0ZmZLVQZeeAf78y/T7bVhgNeLmvsBERaAtD6NJ6Z1DgBE0TAlKA4691x2Ug73ZDhASiMiNF2mW8otxXL6pPyl5d9FKhZhJp6roOOIrFLKJZIC6gk2Yq7X0J36O5ZdcYQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=nIi1ioel; arc=none smtp.client-ip=115.124.30.99 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="nIi1ioel" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1714985213; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=JkwuLgB40/tFCSMT5YJyWsVrrngk9w7x0Ah/Vo6lKcs=; b=nIi1ioelu5JoHLDNYOk6CvONM4ZsnGK/ByI0NKRsUgDurk29F4qptpQRIfUmufbEI6txS71B0wNpweUMCnqVigJ1Brjk0/+JwrkozdsCezf0RXUiUgTFeI0UUD1/SXpfQx5BanLme2qmpK4Khd93N1YSRuoQq7YOqT1/DF0cx8c= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R141e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam033068173054;MF=baolin.wang@linux.alibaba.com;NM=1;PH=DS;RN=14;SR=0;TI=SMTPD_---0W5wQICN_1714985210; Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0W5wQICN_1714985210) by smtp.aliyun-inc.com; Mon, 06 May 2024 16:46:51 +0800 From: Baolin Wang To: akpm@linux-foundation.org, hughd@google.com Cc: willy@infradead.org, david@redhat.com, ioworker0@gmail.com, wangkefeng.wang@huawei.com, ying.huang@intel.com, 21cnbao@gmail.com, ryan.roberts@arm.com, shy828301@gmail.com, ziy@nvidia.com, baolin.wang@linux.alibaba.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 6/8] mm: shmem: add mTHP support for anonymous shmem Date: Mon, 6 May 2024 16:46:30 +0800 Message-Id: X-Mailer: git-send-email 2.39.3 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Commit 19eaf44954df adds multi-size THP (mTHP) for anonymous pages, that can allow THP to be configured through the sysfs interface located at '/sys/kernel/mm/transparent_hugepage/hugepage-XXkb/enabled'. However, the anonymous share pages will ignore the anonymous mTHP rule configured through the sysfs interface, and can only use the PMD-mapped THP, that is not reasonable. Users expect to apply the mTHP rule for all anonymous pages, including the anonymous share pages, in order to enjoy the benefits of mTHP. For example, lower latency than PMD-mapped THP, smaller memory bloat than PMD-mapped THP, contiguous PTEs on ARM architectu= re to reduce TLB miss etc. The primary strategy is similar to supporting anonymous mTHP. Introduce a new interface '/mm/transparent_hugepage/hugepage-XXkb/shmem_enabled', which can have all the same values as the top-level '/sys/kernel/mm/transparent_hugepage/shmem_enabled', with adding a new additional "inherit" option. By default all sizes will be set to "never" except PMD size, which is set to "inherit". This ensures backward compatibi= lity with the shmem enabled of the top level, meanwhile also allows independent control of shmem enabled for each mTHP. Signed-off-by: Baolin Wang Tested-by: Lance Yang --- mm/shmem.c | 177 +++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 150 insertions(+), 27 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index 59cc26d44344..08ccea5170a1 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1611,6 +1611,106 @@ static gfp_t limit_gfp_mask(gfp_t huge_gfp, gfp_t l= imit_gfp) return result; } =20 +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +static unsigned long anon_shmem_allowable_huge_orders(struct inode *inode, + struct vm_area_struct *vma, pgoff_t index, + bool global_huge) +{ + unsigned long mask =3D READ_ONCE(huge_anon_shmem_orders_always); + unsigned long within_size_orders =3D READ_ONCE(huge_anon_shmem_orders_wit= hin_size); + unsigned long vm_flags =3D vma->vm_flags; + /* + * Check all the (large) orders below HPAGE_PMD_ORDER + 1 that + * are enabled for this vma. + */ + unsigned long orders =3D BIT(PMD_ORDER + 1) - 1; + loff_t i_size; + int order; + + if ((vm_flags & VM_NOHUGEPAGE) || + test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags)) + return 0; + + /* If the hardware/firmware marked hugepage support disabled. */ + if (transparent_hugepage_flags & (1 << TRANSPARENT_HUGEPAGE_UNSUPPORTED)) + return 0; + + /* + * Following the 'deny' semantics of the top level, force the huge + * option off from all mounts. + */ + if (shmem_huge =3D=3D SHMEM_HUGE_DENY) + return 0; + /* + * Only allow inherit orders if the top-level value is 'force', which + * means non-PMD sized THP can not override 'huge' mount option now. + */ + if (shmem_huge =3D=3D SHMEM_HUGE_FORCE) + return READ_ONCE(huge_anon_shmem_orders_inherit); + + /* Allow mTHP that will be fully within i_size. */ + order =3D highest_order(within_size_orders); + while (within_size_orders) { + index =3D round_up(index + 1, order); + i_size =3D round_up(i_size_read(inode), PAGE_SIZE); + if (i_size >> PAGE_SHIFT >=3D index) { + mask |=3D within_size_orders; + break; + } + + order =3D next_order(&within_size_orders, order); + } + + if (vm_flags & VM_HUGEPAGE) + mask |=3D READ_ONCE(huge_anon_shmem_orders_madvise); + + if (global_huge) + mask |=3D READ_ONCE(huge_anon_shmem_orders_inherit); + + return orders & mask; +} + +static unsigned long anon_shmem_suitable_orders(struct inode *inode, struc= t vm_fault *vmf, + struct address_space *mapping, pgoff_t index, + unsigned long orders) +{ + struct vm_area_struct *vma =3D vmf->vma; + unsigned long pages; + int order; + + orders =3D thp_vma_suitable_orders(vma, vmf->address, orders); + if (!orders) + return 0; + + /* Find the highest order that can add into the page cache */ + order =3D highest_order(orders); + while (orders) { + pages =3D 1UL << order; + index =3D round_down(index, pages); + if (!xa_find(&mapping->i_pages, &index, + index + pages - 1, XA_PRESENT)) + break; + order =3D next_order(&orders, order); + } + + return orders; +} +#else +static unsigned long anon_shmem_allowable_huge_orders(struct inode *inode, + struct vm_area_struct *vma, pgoff_t index, + bool global_huge) +{ + return 0; +} + +static unsigned long anon_shmem_suitable_orders(struct inode *inode, struc= t vm_fault *vmf, + struct address_space *mapping, pgoff_t index, + unsigned long orders) +{ + return 0; +} +#endif /* CONFIG_TRANSPARENT_HUGEPAGE */ + static struct folio *shmem_alloc_hugefolio(gfp_t gfp, struct shmem_inode_info *info, pgoff_t index, int order) { @@ -1639,38 +1739,55 @@ static struct folio *shmem_alloc_folio(gfp_t gfp, return (struct folio *)page; } =20 -static struct folio *shmem_alloc_and_add_folio(gfp_t gfp, - struct inode *inode, pgoff_t index, - struct mm_struct *fault_mm, bool huge) +static struct folio *shmem_alloc_and_add_folio(struct vm_fault *vmf, + gfp_t gfp, struct inode *inode, pgoff_t index, + struct mm_struct *fault_mm, bool huge, unsigned long orders) { struct address_space *mapping =3D inode->i_mapping; struct shmem_inode_info *info =3D SHMEM_I(inode); + struct vm_area_struct *vma =3D vmf ? vmf->vma : NULL; + unsigned long suitable_orders; struct folio *folio; long pages; - int error; + int error, order; =20 if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) huge =3D false; =20 - if (huge) { - pages =3D HPAGE_PMD_NR; - index =3D round_down(index, HPAGE_PMD_NR); + if (huge || orders > 0) { + if (vma && vma_is_anon_shmem(vma) && orders) { + suitable_orders =3D anon_shmem_suitable_orders(inode, vmf, + mapping, index, orders); + } else { + pages =3D HPAGE_PMD_NR; + suitable_orders =3D BIT(HPAGE_PMD_ORDER); + index =3D round_down(index, HPAGE_PMD_NR); =20 - /* - * Check for conflict before waiting on a huge allocation. - * Conflict might be that a huge page has just been allocated - * and added to page cache by a racing thread, or that there - * is already at least one small page in the huge extent. - * Be careful to retry when appropriate, but not forever! - * Elsewhere -EEXIST would be the right code, but not here. - */ - if (xa_find(&mapping->i_pages, &index, + /* + * Check for conflict before waiting on a huge allocation. + * Conflict might be that a huge page has just been allocated + * and added to page cache by a racing thread, or that there + * is already at least one small page in the huge extent. + * Be careful to retry when appropriate, but not forever! + * Elsewhere -EEXIST would be the right code, but not here. + */ + if (xa_find(&mapping->i_pages, &index, index + HPAGE_PMD_NR - 1, XA_PRESENT)) - return ERR_PTR(-E2BIG); + return ERR_PTR(-E2BIG); + } =20 - folio =3D shmem_alloc_hugefolio(gfp, info, index, HPAGE_PMD_ORDER); - if (!folio && pages =3D=3D HPAGE_PMD_NR) - count_vm_event(THP_FILE_FALLBACK); + order =3D highest_order(suitable_orders); + while (suitable_orders) { + pages =3D 1 << order; + index =3D round_down(index, pages); + folio =3D shmem_alloc_hugefolio(gfp, info, index, order); + if (folio) + goto allocated; + + if (pages =3D=3D HPAGE_PMD_NR) + count_vm_event(THP_FILE_FALLBACK); + order =3D next_order(&suitable_orders, order); + } } else { pages =3D 1; folio =3D shmem_alloc_folio(gfp, info, index); @@ -1678,6 +1795,7 @@ static struct folio *shmem_alloc_and_add_folio(gfp_t = gfp, if (!folio) return ERR_PTR(-ENOMEM); =20 +allocated: __folio_set_locked(folio); __folio_set_swapbacked(folio); =20 @@ -1972,7 +2090,8 @@ static int shmem_get_folio_gfp(struct inode *inode, p= goff_t index, struct mm_struct *fault_mm; struct folio *folio; int error; - bool alloced; + bool alloced, huge; + unsigned long orders =3D 0; =20 if (WARN_ON_ONCE(!shmem_mapping(inode->i_mapping))) return -EINVAL; @@ -2044,14 +2163,18 @@ static int shmem_get_folio_gfp(struct inode *inode,= pgoff_t index, return 0; } =20 - if (shmem_is_huge(inode, index, false, fault_mm, - vma ? vma->vm_flags : 0)) { + huge =3D shmem_is_huge(inode, index, false, fault_mm, + vma ? vma->vm_flags : 0); + /* Find hugepage orders that are allowed for anonymous shmem. */ + if (vma && vma_is_anon_shmem(vma)) + orders =3D anon_shmem_allowable_huge_orders(inode, vma, index, huge); + if (huge || orders > 0) { gfp_t huge_gfp; =20 huge_gfp =3D vma_thp_gfp_mask(vma); huge_gfp =3D limit_gfp_mask(huge_gfp, gfp); - folio =3D shmem_alloc_and_add_folio(huge_gfp, - inode, index, fault_mm, true); + folio =3D shmem_alloc_and_add_folio(vmf, huge_gfp, + inode, index, fault_mm, true, orders); if (!IS_ERR(folio)) { if (folio_test_pmd_mappable(folio)) count_vm_event(THP_FILE_ALLOC); @@ -2061,7 +2184,7 @@ static int shmem_get_folio_gfp(struct inode *inode, p= goff_t index, goto repeat; } =20 - folio =3D shmem_alloc_and_add_folio(gfp, inode, index, fault_mm, false); + folio =3D shmem_alloc_and_add_folio(vmf, gfp, inode, index, fault_mm, fal= se, 0); if (IS_ERR(folio)) { error =3D PTR_ERR(folio); if (error =3D=3D -EEXIST) @@ -2072,7 +2195,7 @@ static int shmem_get_folio_gfp(struct inode *inode, p= goff_t index, =20 alloced: alloced =3D true; - if (folio_test_pmd_mappable(folio) && + if (folio_test_large(folio) && DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE) < folio_next_index(folio) - 1) { struct shmem_sb_info *sbinfo =3D SHMEM_SB(inode->i_sb); --=20 2.39.3 From nobody Wed Dec 17 17:22:21 2025 Received: from out30-118.freemail.mail.aliyun.com (out30-118.freemail.mail.aliyun.com [115.124.30.118]) (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 7EE481420B3 for ; Mon, 6 May 2024 08:46:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.118 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714985218; cv=none; b=Un3dO/7QDakM0duKamt2b+MxKHbdUgsFfY4MRa1po96s0nlfQfpJU8mvbGY96MmPORbd9pFpqmGxBpJa6eh6s8zsA2T5GX9N3giW7OQjHCEOshJzR5F/TqhODNB9pW85AF/vtgXgI1hymWlKaRNsss1zC6gW1wOcwLBniCiUMRE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714985218; c=relaxed/simple; bh=mEf7qdKiwDYet1xOpUaPAS7unGOxruNdMBxPsPtjsEk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=qzK5Iwz4qgNVXFZYpCkUo4dDJDXEKsVO+io/ipQtNlU+p41tka4YGa/QKHMMeU42THK/EJe+jmpTWH50y+kUIGDbk0AI8Fzg54eR7KDoTUJfELKCw+izO4C7kepVGKjuu7rPJRyXlOYiKZJEyDsns9SvHLqZOcGpWAvNiP6xAGI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=hTI8TkZN; arc=none smtp.client-ip=115.124.30.118 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="hTI8TkZN" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1714985214; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=JwVqRNDg6ovq+W40+OWE62ViZ4KVyR46bNtfHFl1yVM=; b=hTI8TkZNM6lKGKawrV3DkYoFzOJc8c7v2EIZKKcMaJgQQg4Y1cgdQ68VHEVw0FFjLZ16/ZJ7v1iS63EOLnpgFzcBb3WVjEkoiGniXL4Pbm+M40L2xpKtj7DcJXZB025Z7rlvmTy63nmifCH/jgoKJldV/fyyc87p/SZzTKil4/E= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R111e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam033045046011;MF=baolin.wang@linux.alibaba.com;NM=1;PH=DS;RN=14;SR=0;TI=SMTPD_---0W5wQICo_1714985212; Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0W5wQICo_1714985212) by smtp.aliyun-inc.com; Mon, 06 May 2024 16:46:53 +0800 From: Baolin Wang To: akpm@linux-foundation.org, hughd@google.com Cc: willy@infradead.org, david@redhat.com, ioworker0@gmail.com, wangkefeng.wang@huawei.com, ying.huang@intel.com, 21cnbao@gmail.com, ryan.roberts@arm.com, shy828301@gmail.com, ziy@nvidia.com, baolin.wang@linux.alibaba.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 7/8] mm: shmem: add mTHP size alignment in shmem_get_unmapped_area Date: Mon, 6 May 2024 16:46:31 +0800 Message-Id: <6e7528bd80309e887427a6bade4b1233a92214d0.1714978902.git.baolin.wang@linux.alibaba.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Although the top-level hugepage allocation can be turned off, anonymous shm= em can still use mTHP by configuring the sysfs interface located at '/sys/kernel/mm/transparent_hugepage/hugepage-XXkb/shmem_enabled'. Therefor= e, add alignment for mTHP size to provide a suitable alignment address in shmem_get_unmapped_area(). Signed-off-by: Baolin Wang Tested-by: Lance Yang --- mm/shmem.c | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index 08ccea5170a1..27107afdff9e 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -2404,6 +2404,7 @@ unsigned long shmem_get_unmapped_area(struct file *fi= le, unsigned long inflated_len; unsigned long inflated_addr; unsigned long inflated_offset; + unsigned long hpage_size =3D HPAGE_PMD_SIZE; =20 if (len > TASK_SIZE) return -ENOMEM; @@ -2422,8 +2423,6 @@ unsigned long shmem_get_unmapped_area(struct file *fi= le, =20 if (shmem_huge =3D=3D SHMEM_HUGE_DENY) return addr; - if (len < HPAGE_PMD_SIZE) - return addr; if (flags & MAP_FIXED) return addr; /* @@ -2437,6 +2436,8 @@ unsigned long shmem_get_unmapped_area(struct file *fi= le, =20 if (shmem_huge !=3D SHMEM_HUGE_FORCE) { struct super_block *sb; + unsigned long __maybe_unused hpage_orders; + int order =3D 0; =20 if (file) { VM_BUG_ON(file->f_op !=3D &shmem_file_operations); @@ -2449,18 +2450,34 @@ unsigned long shmem_get_unmapped_area(struct file *= file, if (IS_ERR(shm_mnt)) return addr; sb =3D shm_mnt->mnt_sb; + + /* + * Find the highest mTHP order used for anonymous shmem to + * provide a suitable alignment address. + */ +#ifdef CONFIG_TRANSPARENT_HUGEPAGE + hpage_orders =3D READ_ONCE(huge_anon_shmem_orders_always); + hpage_orders |=3D READ_ONCE(huge_anon_shmem_orders_within_size); + hpage_orders |=3D READ_ONCE(huge_anon_shmem_orders_madvise); + hpage_orders |=3D READ_ONCE(huge_anon_shmem_orders_inherit); + order =3D highest_order(hpage_orders); + hpage_size =3D PAGE_SIZE << order; +#endif } - if (SHMEM_SB(sb)->huge =3D=3D SHMEM_HUGE_NEVER) + if (SHMEM_SB(sb)->huge =3D=3D SHMEM_HUGE_NEVER && !order) return addr; } =20 - offset =3D (pgoff << PAGE_SHIFT) & (HPAGE_PMD_SIZE-1); - if (offset && offset + len < 2 * HPAGE_PMD_SIZE) + if (len < hpage_size) + return addr; + + offset =3D (pgoff << PAGE_SHIFT) & (hpage_size - 1); + if (offset && offset + len < 2 * hpage_size) return addr; - if ((addr & (HPAGE_PMD_SIZE-1)) =3D=3D offset) + if ((addr & (hpage_size - 1)) =3D=3D offset) return addr; =20 - inflated_len =3D len + HPAGE_PMD_SIZE - PAGE_SIZE; + inflated_len =3D len + hpage_size - PAGE_SIZE; if (inflated_len > TASK_SIZE) return addr; if (inflated_len < len) @@ -2473,10 +2490,10 @@ unsigned long shmem_get_unmapped_area(struct file *= file, if (inflated_addr & ~PAGE_MASK) return addr; =20 - inflated_offset =3D inflated_addr & (HPAGE_PMD_SIZE-1); + inflated_offset =3D inflated_addr & (hpage_size - 1); inflated_addr +=3D offset - inflated_offset; if (inflated_offset > offset) - inflated_addr +=3D HPAGE_PMD_SIZE; + inflated_addr +=3D hpage_size; =20 if (inflated_addr > TASK_SIZE - len) return addr; --=20 2.39.3 From nobody Wed Dec 17 17:22:21 2025 Received: from out30-130.freemail.mail.aliyun.com (out30-130.freemail.mail.aliyun.com [115.124.30.130]) (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 45E501420C9 for ; Mon, 6 May 2024 08:46:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.130 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714985220; cv=none; b=d81f5/4vfuuWxWfaz8xaMkxIARpQ9ufATAxFurDEHSDZ2av3//d0dMWIfXJMdSp1HMP3ztqETMNfU36UrccsQU8cKR+di4YMk590qHxulZjSXpyFRgO5CANhmNjfYUQktNe84OljzmXkUClva6+GiirprSYmakmjugi+6jm3t1A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714985220; c=relaxed/simple; bh=NgEah7MUwkhHAMNvDN85ACnVMl/BukF5fiwdC+Ik/yU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=oSp90nxhi5WAGkfR364qf0xirZeaEA/W4zTkucw9r/B8kB6UfeZK7YJ7rupqjG6Bmuu5I+XeIs7IQm5uVflpe4wS185rs6pjGYC1twXVKWeCQNwaCqOtEMtcRei9q0+c6uzGS2wSpux0Vv6oRFJs7NOn7bWUx10Ce6asRx5Eg88= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=PePZSSIR; arc=none smtp.client-ip=115.124.30.130 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="PePZSSIR" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1714985216; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=NqOIe5Oq6MCM0KXK59PH5ZOetWOLxng8pqZWeky8+zw=; b=PePZSSIR7Zy3SdTVOpJG6XIksJNZ/2YXBqeelftayVhbeicfe26qlXpoHEpkYHDcDbE4wRscwgRPEy5RvL1127WzsCeJ7ax1P5kwPNQYcxTd6OuZOFGBR52+KxOk4CZKQhFjxeU+WiUiCTwUIkfGnjvKpycN1GfxCZxC1D16LLs= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R111e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam033022160150;MF=baolin.wang@linux.alibaba.com;NM=1;PH=DS;RN=14;SR=0;TI=SMTPD_---0W5weiNH_1714985213; Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0W5weiNH_1714985213) by smtp.aliyun-inc.com; Mon, 06 May 2024 16:46:54 +0800 From: Baolin Wang To: akpm@linux-foundation.org, hughd@google.com Cc: willy@infradead.org, david@redhat.com, ioworker0@gmail.com, wangkefeng.wang@huawei.com, ying.huang@intel.com, 21cnbao@gmail.com, ryan.roberts@arm.com, shy828301@gmail.com, ziy@nvidia.com, baolin.wang@linux.alibaba.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 8/8] mm: shmem: add mTHP counters for anonymous shmem Date: Mon, 6 May 2024 16:46:32 +0800 Message-Id: <34f0b24f7c061f796d91ade9766e20a9da0c7651.1714978902.git.baolin.wang@linux.alibaba.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add mTHP counters for anonymous shmem. Signed-off-by: Baolin Wang Tested-by: Lance Yang --- include/linux/huge_mm.h | 3 +++ mm/huge_memory.c | 6 ++++++ mm/shmem.c | 18 +++++++++++++++--- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h index dbd6b3f56210..c15bebb2cf53 100644 --- a/include/linux/huge_mm.h +++ b/include/linux/huge_mm.h @@ -281,6 +281,9 @@ enum mthp_stat_item { MTHP_STAT_ANON_FAULT_FALLBACK_CHARGE, MTHP_STAT_ANON_SWPOUT, MTHP_STAT_ANON_SWPOUT_FALLBACK, + MTHP_STAT_FILE_ALLOC, + MTHP_STAT_FILE_FALLBACK, + MTHP_STAT_FILE_FALLBACK_CHARGE, __MTHP_STAT_COUNT }; =20 diff --git a/mm/huge_memory.c b/mm/huge_memory.c index d3080a8843f2..fcda6ae604f6 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -555,6 +555,9 @@ DEFINE_MTHP_STAT_ATTR(anon_fault_fallback, MTHP_STAT_AN= ON_FAULT_FALLBACK); DEFINE_MTHP_STAT_ATTR(anon_fault_fallback_charge, MTHP_STAT_ANON_FAULT_FAL= LBACK_CHARGE); DEFINE_MTHP_STAT_ATTR(anon_swpout, MTHP_STAT_ANON_SWPOUT); DEFINE_MTHP_STAT_ATTR(anon_swpout_fallback, MTHP_STAT_ANON_SWPOUT_FALLBACK= ); +DEFINE_MTHP_STAT_ATTR(file_alloc, MTHP_STAT_FILE_ALLOC); +DEFINE_MTHP_STAT_ATTR(file_fallback, MTHP_STAT_FILE_FALLBACK); +DEFINE_MTHP_STAT_ATTR(file_fallback_charge, MTHP_STAT_FILE_FALLBACK_CHARGE= ); =20 static struct attribute *stats_attrs[] =3D { &anon_fault_alloc_attr.attr, @@ -562,6 +565,9 @@ static struct attribute *stats_attrs[] =3D { &anon_fault_fallback_charge_attr.attr, &anon_swpout_attr.attr, &anon_swpout_fallback_attr.attr, + &file_alloc_attr.attr, + &file_fallback_attr.attr, + &file_fallback_charge_attr.attr, NULL, }; =20 diff --git a/mm/shmem.c b/mm/shmem.c index 27107afdff9e..1af2f0aa384d 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1786,6 +1786,9 @@ static struct folio *shmem_alloc_and_add_folio(struct= vm_fault *vmf, =20 if (pages =3D=3D HPAGE_PMD_NR) count_vm_event(THP_FILE_FALLBACK); +#ifdef CONFIG_TRANSPARENT_HUGEPAGE + count_mthp_stat(order, MTHP_STAT_FILE_FALLBACK); +#endif order =3D next_order(&suitable_orders, order); } } else { @@ -1805,9 +1808,15 @@ static struct folio *shmem_alloc_and_add_folio(struc= t vm_fault *vmf, if (xa_find(&mapping->i_pages, &index, index + pages - 1, XA_PRESENT)) { error =3D -EEXIST; - } else if (pages =3D=3D HPAGE_PMD_NR) { - count_vm_event(THP_FILE_FALLBACK); - count_vm_event(THP_FILE_FALLBACK_CHARGE); + } else if (pages > 1) { + if (pages =3D=3D HPAGE_PMD_NR) { + count_vm_event(THP_FILE_FALLBACK); + count_vm_event(THP_FILE_FALLBACK_CHARGE); + } +#ifdef CONFIG_TRANSPARENT_HUGEPAGE + count_mthp_stat(folio_order(folio), MTHP_STAT_FILE_FALLBACK); + count_mthp_stat(folio_order(folio), MTHP_STAT_FILE_FALLBACK_CHARGE); +#endif } goto unlock; } @@ -2178,6 +2187,9 @@ static int shmem_get_folio_gfp(struct inode *inode, p= goff_t index, if (!IS_ERR(folio)) { if (folio_test_pmd_mappable(folio)) count_vm_event(THP_FILE_ALLOC); +#ifdef CONFIG_TRANSPARENT_HUGEPAGE + count_mthp_stat(folio_order(folio), MTHP_STAT_FILE_ALLOC); +#endif goto alloced; } if (PTR_ERR(folio) =3D=3D -EEXIST) --=20 2.39.3