Skip to content

Commit 037c7c6

Browse files
Panky-codeskawasaki
authored andcommitted
mm: add static huge zero folio
There are many places in the kernel where we need to zeroout larger chunks but the maximum segment we can zeroout at a time by ZERO_PAGE is limited by PAGE_SIZE. This is especially annoying in block devices and filesystems where we attach multiple ZERO_PAGEs to the bio in different bvecs. With multipage bvec support in block layer, it is much more efficient to send out larger zero pages as a part of single bvec. This concern was raised during the review of adding LBS support to XFS[1][2]. Usually huge_zero_folio is allocated on demand, and it will be deallocated by the shrinker if there are no users of it left. At moment, huge_zero_folio infrastructure refcount is tied to the process lifetime that created it. This might not work for bio layer as the completions can be async and the process that created the huge_zero_folio might no longer be alive. And, one of the main point that came during discussion is to have something bigger than zero page as a drop-in replacement. Add a config option STATIC_HUGE_ZERO_FOLIO that will always allocate the huge_zero_folio, and it will never drop the reference. This makes using the huge_zero_folio without having to pass any mm struct and does not tie the lifetime of the zero folio to anything, making it a drop-in replacement for ZERO_PAGE. If STATIC_PMD_ZERO_PAGE config option is enabled, then mm_get_huge_zero_folio() will simply return this page instead of dynamically allocating a new PMD page. This option can waste memory in small systems or systems with 64k base page size. So make it an opt-in and also add an option from individual architecture so that we don't enable this feature for larger base page size systems. [1] https://lore.kernel.org/linux-xfs/[email protected]/ [2] https://lore.kernel.org/linux-xfs/[email protected]/ Co-Developed-by: David Hildenbrand <[email protected]> Signed-off-by: Pankaj Raghav <[email protected]>
1 parent 3db91bb commit 037c7c6

4 files changed

Lines changed: 57 additions & 0 deletions

File tree

arch/x86/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ config X86
153153
select ARCH_WANT_OPTIMIZE_HUGETLB_VMEMMAP if X86_64
154154
select ARCH_WANT_HUGETLB_VMEMMAP_PREINIT if X86_64
155155
select ARCH_WANTS_THP_SWAP if X86_64
156+
select ARCH_WANTS_STATIC_HUGE_ZERO_FOLIO if X86_64
156157
select ARCH_HAS_PARANOID_L1D_FLUSH
157158
select ARCH_WANT_IRQS_OFF_ACTIVATE_MM
158159
select BUILDTIME_TABLE_SORT

include/linux/huge_mm.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,7 @@ vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf);
480480

481481
extern struct folio *huge_zero_folio;
482482
extern unsigned long huge_zero_pfn;
483+
extern atomic_t huge_zero_folio_is_static;
483484

484485
static inline bool is_huge_zero_folio(const struct folio *folio)
485486
{
@@ -493,6 +494,16 @@ static inline bool is_huge_zero_pmd(pmd_t pmd)
493494

494495
struct folio *mm_get_huge_zero_folio(struct mm_struct *mm);
495496
void mm_put_huge_zero_folio(struct mm_struct *mm);
497+
struct folio *__get_static_huge_zero_folio(void);
498+
499+
static inline struct folio *get_static_huge_zero_folio(void)
500+
{
501+
if (!IS_ENABLED(CONFIG_STATIC_HUGE_ZERO_FOLIO))
502+
return NULL;
503+
if (likely(atomic_read(&huge_zero_folio_is_static)))
504+
return huge_zero_folio;
505+
return __get_static_huge_zero_folio();
506+
}
496507

497508
static inline bool thp_migration_supported(void)
498509
{
@@ -679,6 +690,11 @@ static inline int change_huge_pud(struct mmu_gather *tlb,
679690
{
680691
return 0;
681692
}
693+
694+
static inline struct folio *get_static_huge_zero_folio(void)
695+
{
696+
return NULL;
697+
}
682698
#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
683699

684700
static inline int split_folio_to_list_to_order(struct folio *folio,

mm/Kconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,18 @@ config ARCH_WANT_GENERAL_HUGETLB
823823
config ARCH_WANTS_THP_SWAP
824824
def_bool n
825825

826+
config ARCH_WANTS_STATIC_HUGE_ZERO_FOLIO
827+
def_bool n
828+
829+
config STATIC_HUGE_ZERO_FOLIO
830+
bool "Allocate a PMD sized folio for zeroing"
831+
depends on ARCH_WANTS_STATIC_HUGE_ZERO_FOLIO
832+
help
833+
Typically huge_zero_folio, which is a PMD page of zeroes, is allocated
834+
on demand and deallocated when not in use. This option will
835+
allocate huge_zero_folio but it will never free it.
836+
Not suitable for memory constrained systems.
837+
826838
config MM_ID
827839
def_bool n
828840

mm/huge_memory.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ static unsigned long deferred_split_scan(struct shrinker *shrink,
7676
static bool split_underused_thp = true;
7777

7878
static atomic_t huge_zero_refcount;
79+
static atomic_t huge_zero_static_fail_count __read_mostly;
80+
atomic_t huge_zero_folio_is_static __read_mostly;
7981
struct folio *huge_zero_folio __read_mostly;
8082
unsigned long huge_zero_pfn __read_mostly = ~0UL;
8183
unsigned long huge_anon_orders_always __read_mostly;
@@ -267,6 +269,32 @@ void mm_put_huge_zero_folio(struct mm_struct *mm)
267269
put_huge_zero_page();
268270
}
269271

272+
#ifdef CONFIG_STATIC_HUGE_ZERO_FOLIO
273+
struct folio *__get_static_huge_zero_folio(void)
274+
{
275+
/*
276+
* If we failed to allocate a huge zero folio multiple times,
277+
* just refrain from trying.
278+
*/
279+
if (atomic_read(&huge_zero_static_fail_count) > 2)
280+
return NULL;
281+
282+
/*
283+
* Our raised reference will prevent the shrinker from ever having
284+
* success.
285+
*/
286+
if (!get_huge_zero_page()) {
287+
atomic_inc(&huge_zero_static_fail_count);
288+
return NULL;
289+
}
290+
291+
if (atomic_cmpxchg(&huge_zero_folio_is_static, 0, 1) != 0)
292+
put_huge_zero_page();
293+
294+
return huge_zero_folio;
295+
}
296+
#endif /* CONFIG_STATIC_HUGE_ZERO_FOLIO */
297+
270298
static unsigned long shrink_huge_zero_folio_count(struct shrinker *shrink,
271299
struct shrink_control *sc)
272300
{

0 commit comments

Comments
 (0)