Skip to content

Commit dbbee9d

Browse files
gormanmtorvalds
authored andcommitted
mm/page_alloc: convert per-cpu list protection to local_lock
There is a lack of clarity of what exactly local_irq_save/local_irq_restore protects in page_alloc.c . It conflates the protection of per-cpu page allocation structures with per-cpu vmstat deltas. This patch protects the PCP structure using local_lock which for most configurations is identical to IRQ enabling/disabling. The scope of the lock is still wider than it should be but this is decreased later. It is possible for the local_lock to be embedded safely within struct per_cpu_pages but it adds complexity to free_unref_page_list. [[email protected]: coding style fixes] [[email protected]: work around a pahole limitation with zero-sized struct pagesets] Link: https://lkml.kernel.org/r/[email protected] [[email protected]: Make pagesets static] Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Mel Gorman <[email protected]> Acked-by: Vlastimil Babka <[email protected]> Acked-by: Peter Zijlstra (Intel) <[email protected]> Cc: Chuck Lever <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Jesper Dangaard Brouer <[email protected]> Cc: Michal Hocko <[email protected]> Cc: Sebastian Andrzej Siewior <[email protected]> Cc: Thomas Gleixner <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 28f836b commit dbbee9d

File tree

3 files changed

+51
-15
lines changed

3 files changed

+51
-15
lines changed

include/linux/mmzone.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <linux/atomic.h>
2121
#include <linux/mm_types.h>
2222
#include <linux/page-flags.h>
23+
#include <linux/local_lock.h>
2324
#include <asm/page.h>
2425

2526
/* Free memory management - zoned buddy allocator. */
@@ -337,6 +338,7 @@ enum zone_watermarks {
337338
#define high_wmark_pages(z) (z->_watermark[WMARK_HIGH] + z->watermark_boost)
338339
#define wmark_pages(z, i) (z->_watermark[i] + z->watermark_boost)
339340

341+
/* Fields and list protected by pagesets local_lock in page_alloc.c */
340342
struct per_cpu_pages {
341343
int count; /* number of pages in the list */
342344
int high; /* high watermark, emptying needed */

lib/Kconfig.debug

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,9 @@ config DEBUG_INFO_BTF
313313
config PAHOLE_HAS_SPLIT_BTF
314314
def_bool $(success, test `$(PAHOLE) --version | sed -E 's/v([0-9]+)\.([0-9]+)/\1\2/'` -ge "119")
315315

316+
config PAHOLE_HAS_ZEROSIZE_PERCPU_SUPPORT
317+
def_bool $(success, test `$(PAHOLE) --version | sed -E 's/v([0-9]+)\.([0-9]+)/\1\2/'` -ge "122")
318+
316319
config DEBUG_INFO_BTF_MODULES
317320
def_bool y
318321
depends on DEBUG_INFO_BTF && MODULES && PAHOLE_HAS_SPLIT_BTF

mm/page_alloc.c

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,24 @@ typedef int __bitwise fpi_t;
122122
static DEFINE_MUTEX(pcp_batch_high_lock);
123123
#define MIN_PERCPU_PAGELIST_FRACTION (8)
124124

125+
struct pagesets {
126+
local_lock_t lock;
127+
#if defined(CONFIG_DEBUG_INFO_BTF) && \
128+
!defined(CONFIG_DEBUG_LOCK_ALLOC) && \
129+
!defined(CONFIG_PAHOLE_HAS_ZEROSIZE_PERCPU_SUPPORT)
130+
/*
131+
* pahole 1.21 and earlier gets confused by zero-sized per-CPU
132+
* variables and produces invalid BTF. Ensure that
133+
* sizeof(struct pagesets) != 0 for older versions of pahole.
134+
*/
135+
char __pahole_hack;
136+
#warning "pahole too old to support zero-sized struct pagesets"
137+
#endif
138+
};
139+
static DEFINE_PER_CPU(struct pagesets, pagesets) = {
140+
.lock = INIT_LOCAL_LOCK(lock),
141+
};
142+
125143
#ifdef CONFIG_USE_PERCPU_NUMA_NODE_ID
126144
DEFINE_PER_CPU(int, numa_node);
127145
EXPORT_PER_CPU_SYMBOL(numa_node);
@@ -1453,6 +1471,10 @@ static void free_pcppages_bulk(struct zone *zone, int count,
14531471
} while (--count && --batch_free && !list_empty(list));
14541472
}
14551473

1474+
/*
1475+
* local_lock_irq held so equivalent to spin_lock_irqsave for
1476+
* both PREEMPT_RT and non-PREEMPT_RT configurations.
1477+
*/
14561478
spin_lock(&zone->lock);
14571479
isolated_pageblocks = has_isolate_pageblock(zone);
14581480

@@ -1573,6 +1595,11 @@ static void __free_pages_ok(struct page *page, unsigned int order,
15731595
return;
15741596

15751597
migratetype = get_pfnblock_migratetype(page, pfn);
1598+
1599+
/*
1600+
* TODO FIX: Disable IRQs before acquiring IRQ-safe zone->lock
1601+
* and protect vmstat updates.
1602+
*/
15761603
local_irq_save(flags);
15771604
__count_vm_events(PGFREE, 1 << order);
15781605
free_one_page(page_zone(page), page, pfn, order, migratetype,
@@ -2955,6 +2982,10 @@ static int rmqueue_bulk(struct zone *zone, unsigned int order,
29552982
{
29562983
int i, allocated = 0;
29572984

2985+
/*
2986+
* local_lock_irq held so equivalent to spin_lock_irqsave for
2987+
* both PREEMPT_RT and non-PREEMPT_RT configurations.
2988+
*/
29582989
spin_lock(&zone->lock);
29592990
for (i = 0; i < count; ++i) {
29602991
struct page *page = __rmqueue(zone, order, migratetype,
@@ -3007,12 +3038,12 @@ void drain_zone_pages(struct zone *zone, struct per_cpu_pages *pcp)
30073038
unsigned long flags;
30083039
int to_drain, batch;
30093040

3010-
local_irq_save(flags);
3041+
local_lock_irqsave(&pagesets.lock, flags);
30113042
batch = READ_ONCE(pcp->batch);
30123043
to_drain = min(pcp->count, batch);
30133044
if (to_drain > 0)
30143045
free_pcppages_bulk(zone, to_drain, pcp);
3015-
local_irq_restore(flags);
3046+
local_unlock_irqrestore(&pagesets.lock, flags);
30163047
}
30173048
#endif
30183049

@@ -3028,13 +3059,13 @@ static void drain_pages_zone(unsigned int cpu, struct zone *zone)
30283059
unsigned long flags;
30293060
struct per_cpu_pages *pcp;
30303061

3031-
local_irq_save(flags);
3062+
local_lock_irqsave(&pagesets.lock, flags);
30323063

30333064
pcp = per_cpu_ptr(zone->per_cpu_pageset, cpu);
30343065
if (pcp->count)
30353066
free_pcppages_bulk(zone, pcp->count, pcp);
30363067

3037-
local_irq_restore(flags);
3068+
local_unlock_irqrestore(&pagesets.lock, flags);
30383069
}
30393070

30403071
/*
@@ -3297,9 +3328,9 @@ void free_unref_page(struct page *page)
32973328
if (!free_unref_page_prepare(page, pfn))
32983329
return;
32993330

3300-
local_irq_save(flags);
3331+
local_lock_irqsave(&pagesets.lock, flags);
33013332
free_unref_page_commit(page, pfn);
3302-
local_irq_restore(flags);
3333+
local_unlock_irqrestore(&pagesets.lock, flags);
33033334
}
33043335

33053336
/*
@@ -3319,7 +3350,7 @@ void free_unref_page_list(struct list_head *list)
33193350
set_page_private(page, pfn);
33203351
}
33213352

3322-
local_irq_save(flags);
3353+
local_lock_irqsave(&pagesets.lock, flags);
33233354
list_for_each_entry_safe(page, next, list, lru) {
33243355
unsigned long pfn = page_private(page);
33253356

@@ -3332,12 +3363,12 @@ void free_unref_page_list(struct list_head *list)
33323363
* a large list of pages to free.
33333364
*/
33343365
if (++batch_count == SWAP_CLUSTER_MAX) {
3335-
local_irq_restore(flags);
3366+
local_unlock_irqrestore(&pagesets.lock, flags);
33363367
batch_count = 0;
3337-
local_irq_save(flags);
3368+
local_lock_irqsave(&pagesets.lock, flags);
33383369
}
33393370
}
3340-
local_irq_restore(flags);
3371+
local_unlock_irqrestore(&pagesets.lock, flags);
33413372
}
33423373

33433374
/*
@@ -3494,15 +3525,15 @@ static struct page *rmqueue_pcplist(struct zone *preferred_zone,
34943525
struct page *page;
34953526
unsigned long flags;
34963527

3497-
local_irq_save(flags);
3528+
local_lock_irqsave(&pagesets.lock, flags);
34983529
pcp = this_cpu_ptr(zone->per_cpu_pageset);
34993530
list = &pcp->lists[migratetype];
35003531
page = __rmqueue_pcplist(zone, migratetype, alloc_flags, pcp, list);
35013532
if (page) {
35023533
__count_zid_vm_events(PGALLOC, page_zonenum(page), 1);
35033534
zone_statistics(preferred_zone, zone);
35043535
}
3505-
local_irq_restore(flags);
3536+
local_unlock_irqrestore(&pagesets.lock, flags);
35063537
return page;
35073538
}
35083539

@@ -5103,7 +5134,7 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid,
51035134
goto failed;
51045135

51055136
/* Attempt the batch allocation */
5106-
local_irq_save(flags);
5137+
local_lock_irqsave(&pagesets.lock, flags);
51075138
pcp = this_cpu_ptr(zone->per_cpu_pageset);
51085139
pcp_list = &pcp->lists[ac.migratetype];
51095140

@@ -5141,12 +5172,12 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid,
51415172
nr_populated++;
51425173
}
51435174

5144-
local_irq_restore(flags);
5175+
local_unlock_irqrestore(&pagesets.lock, flags);
51455176

51465177
return nr_populated;
51475178

51485179
failed_irq:
5149-
local_irq_restore(flags);
5180+
local_unlock_irqrestore(&pagesets.lock, flags);
51505181

51515182
failed:
51525183
page = __alloc_pages(gfp, 0, preferred_nid, nodemask);

0 commit comments

Comments
 (0)