Skip to content

Commit ffd386a

Browse files
committed
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tj/percpu
* 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tj/percpu: percpu: allow limited allocation before slab is online percpu: make @dyn_size always mean min dyn_size in first chunk init functions
2 parents 03da309 + 099a19d commit ffd386a

3 files changed

Lines changed: 64 additions & 42 deletions

File tree

‎include/linux/percpu.h‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@
4444
/* minimum unit size, also is the maximum supported allocation size */
4545
#define PCPU_MIN_UNIT_SIZE PFN_ALIGN(64 << 10)
4646

47+
/*
48+
* Percpu allocator can serve percpu allocations before slab is
49+
* initialized which allows slab to depend on the percpu allocator.
50+
* The following two parameters decide how much resource to
51+
* preallocate for this. Keep PERCPU_DYNAMIC_RESERVE equal to or
52+
* larger than PERCPU_DYNAMIC_EARLY_SIZE.
53+
*/
54+
#define PERCPU_DYNAMIC_EARLY_SLOTS 128
55+
#define PERCPU_DYNAMIC_EARLY_SIZE (12 << 10)
56+
4757
/*
4858
* PERCPU_DYNAMIC_RESERVE indicates the amount of free area to piggy
4959
* back on the first chunk for dynamic percpu allocation if arch is
@@ -104,16 +114,11 @@ extern struct pcpu_alloc_info * __init pcpu_alloc_alloc_info(int nr_groups,
104114
int nr_units);
105115
extern void __init pcpu_free_alloc_info(struct pcpu_alloc_info *ai);
106116

107-
extern struct pcpu_alloc_info * __init pcpu_build_alloc_info(
108-
size_t reserved_size, ssize_t dyn_size,
109-
size_t atom_size,
110-
pcpu_fc_cpu_distance_fn_t cpu_distance_fn);
111-
112117
extern int __init pcpu_setup_first_chunk(const struct pcpu_alloc_info *ai,
113118
void *base_addr);
114119

115120
#ifdef CONFIG_NEED_PER_CPU_EMBED_FIRST_CHUNK
116-
extern int __init pcpu_embed_first_chunk(size_t reserved_size, ssize_t dyn_size,
121+
extern int __init pcpu_embed_first_chunk(size_t reserved_size, size_t dyn_size,
117122
size_t atom_size,
118123
pcpu_fc_cpu_distance_fn_t cpu_distance_fn,
119124
pcpu_fc_alloc_fn_t alloc_fn,
@@ -140,6 +145,7 @@ extern bool is_kernel_percpu_address(unsigned long addr);
140145
#ifndef CONFIG_HAVE_SETUP_PER_CPU_AREA
141146
extern void __init setup_per_cpu_areas(void);
142147
#endif
148+
extern void __init percpu_init_late(void);
143149

144150
#else /* CONFIG_SMP */
145151

@@ -153,6 +159,8 @@ static inline bool is_kernel_percpu_address(unsigned long addr)
153159

154160
static inline void __init setup_per_cpu_areas(void) { }
155161

162+
static inline void __init percpu_init_late(void) { }
163+
156164
static inline void *pcpu_lpage_remapped(void *kaddr)
157165
{
158166
return NULL;

‎init/main.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,7 @@ static void __init mm_init(void)
532532
page_cgroup_init_flatmem();
533533
mem_init();
534534
kmem_cache_init();
535+
percpu_init_late();
535536
pgtable_cache_init();
536537
vmalloc_init();
537538
}

‎mm/percpu.c‎

Lines changed: 49 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,9 @@ static void __maybe_unused pcpu_next_pop(struct pcpu_chunk *chunk,
282282
*/
283283
static void *pcpu_mem_alloc(size_t size)
284284
{
285+
if (WARN_ON_ONCE(!slab_is_available()))
286+
return NULL;
287+
285288
if (size <= PAGE_SIZE)
286289
return kzalloc(size, GFP_KERNEL);
287290
else {
@@ -392,13 +395,6 @@ static int pcpu_extend_area_map(struct pcpu_chunk *chunk, int new_alloc)
392395
old_size = chunk->map_alloc * sizeof(chunk->map[0]);
393396
memcpy(new, chunk->map, old_size);
394397

395-
/*
396-
* map_alloc < PCPU_DFL_MAP_ALLOC indicates that the chunk is
397-
* one of the first chunks and still using static map.
398-
*/
399-
if (chunk->map_alloc >= PCPU_DFL_MAP_ALLOC)
400-
old = chunk->map;
401-
402398
chunk->map_alloc = new_alloc;
403399
chunk->map = new;
404400
new = NULL;
@@ -604,7 +600,7 @@ static struct pcpu_chunk *pcpu_alloc_chunk(void)
604600
{
605601
struct pcpu_chunk *chunk;
606602

607-
chunk = kzalloc(pcpu_chunk_struct_size, GFP_KERNEL);
603+
chunk = pcpu_mem_alloc(pcpu_chunk_struct_size);
608604
if (!chunk)
609605
return NULL;
610606

@@ -1013,20 +1009,6 @@ phys_addr_t per_cpu_ptr_to_phys(void *addr)
10131009
return page_to_phys(pcpu_addr_to_page(addr));
10141010
}
10151011

1016-
static inline size_t pcpu_calc_fc_sizes(size_t static_size,
1017-
size_t reserved_size,
1018-
ssize_t *dyn_sizep)
1019-
{
1020-
size_t size_sum;
1021-
1022-
size_sum = PFN_ALIGN(static_size + reserved_size +
1023-
(*dyn_sizep >= 0 ? *dyn_sizep : 0));
1024-
if (*dyn_sizep != 0)
1025-
*dyn_sizep = size_sum - static_size - reserved_size;
1026-
1027-
return size_sum;
1028-
}
1029-
10301012
/**
10311013
* pcpu_alloc_alloc_info - allocate percpu allocation info
10321014
* @nr_groups: the number of groups
@@ -1085,7 +1067,7 @@ void __init pcpu_free_alloc_info(struct pcpu_alloc_info *ai)
10851067
/**
10861068
* pcpu_build_alloc_info - build alloc_info considering distances between CPUs
10871069
* @reserved_size: the size of reserved percpu area in bytes
1088-
* @dyn_size: free size for dynamic allocation in bytes, -1 for auto
1070+
* @dyn_size: minimum free size for dynamic allocation in bytes
10891071
* @atom_size: allocation atom size
10901072
* @cpu_distance_fn: callback to determine distance between cpus, optional
10911073
*
@@ -1103,8 +1085,8 @@ void __init pcpu_free_alloc_info(struct pcpu_alloc_info *ai)
11031085
* On success, pointer to the new allocation_info is returned. On
11041086
* failure, ERR_PTR value is returned.
11051087
*/
1106-
struct pcpu_alloc_info * __init pcpu_build_alloc_info(
1107-
size_t reserved_size, ssize_t dyn_size,
1088+
static struct pcpu_alloc_info * __init pcpu_build_alloc_info(
1089+
size_t reserved_size, size_t dyn_size,
11081090
size_t atom_size,
11091091
pcpu_fc_cpu_distance_fn_t cpu_distance_fn)
11101092
{
@@ -1123,13 +1105,17 @@ struct pcpu_alloc_info * __init pcpu_build_alloc_info(
11231105
memset(group_map, 0, sizeof(group_map));
11241106
memset(group_cnt, 0, sizeof(group_cnt));
11251107

1108+
/* calculate size_sum and ensure dyn_size is enough for early alloc */
1109+
size_sum = PFN_ALIGN(static_size + reserved_size +
1110+
max_t(size_t, dyn_size, PERCPU_DYNAMIC_EARLY_SIZE));
1111+
dyn_size = size_sum - static_size - reserved_size;
1112+
11261113
/*
11271114
* Determine min_unit_size, alloc_size and max_upa such that
11281115
* alloc_size is multiple of atom_size and is the smallest
11291116
* which can accomodate 4k aligned segments which are equal to
11301117
* or larger than min_unit_size.
11311118
*/
1132-
size_sum = pcpu_calc_fc_sizes(static_size, reserved_size, &dyn_size);
11331119
min_unit_size = max_t(size_t, size_sum, PCPU_MIN_UNIT_SIZE);
11341120

11351121
alloc_size = roundup(min_unit_size, atom_size);
@@ -1350,7 +1336,8 @@ int __init pcpu_setup_first_chunk(const struct pcpu_alloc_info *ai,
13501336
void *base_addr)
13511337
{
13521338
static char cpus_buf[4096] __initdata;
1353-
static int smap[2], dmap[2];
1339+
static int smap[PERCPU_DYNAMIC_EARLY_SLOTS] __initdata;
1340+
static int dmap[PERCPU_DYNAMIC_EARLY_SLOTS] __initdata;
13541341
size_t dyn_size = ai->dyn_size;
13551342
size_t size_sum = ai->static_size + ai->reserved_size + dyn_size;
13561343
struct pcpu_chunk *schunk, *dchunk = NULL;
@@ -1373,14 +1360,13 @@ int __init pcpu_setup_first_chunk(const struct pcpu_alloc_info *ai,
13731360
} while (0)
13741361

13751362
/* sanity checks */
1376-
BUILD_BUG_ON(ARRAY_SIZE(smap) >= PCPU_DFL_MAP_ALLOC ||
1377-
ARRAY_SIZE(dmap) >= PCPU_DFL_MAP_ALLOC);
13781363
PCPU_SETUP_BUG_ON(ai->nr_groups <= 0);
13791364
PCPU_SETUP_BUG_ON(!ai->static_size);
13801365
PCPU_SETUP_BUG_ON(!base_addr);
13811366
PCPU_SETUP_BUG_ON(ai->unit_size < size_sum);
13821367
PCPU_SETUP_BUG_ON(ai->unit_size & ~PAGE_MASK);
13831368
PCPU_SETUP_BUG_ON(ai->unit_size < PCPU_MIN_UNIT_SIZE);
1369+
PCPU_SETUP_BUG_ON(ai->dyn_size < PERCPU_DYNAMIC_EARLY_SIZE);
13841370
PCPU_SETUP_BUG_ON(pcpu_verify_alloc_info(ai) < 0);
13851371

13861372
/* process group information and build config tables accordingly */
@@ -1532,7 +1518,7 @@ early_param("percpu_alloc", percpu_alloc_setup);
15321518
/**
15331519
* pcpu_embed_first_chunk - embed the first percpu chunk into bootmem
15341520
* @reserved_size: the size of reserved percpu area in bytes
1535-
* @dyn_size: free size for dynamic allocation in bytes, -1 for auto
1521+
* @dyn_size: minimum free size for dynamic allocation in bytes
15361522
* @atom_size: allocation atom size
15371523
* @cpu_distance_fn: callback to determine distance between cpus, optional
15381524
* @alloc_fn: function to allocate percpu page
@@ -1553,18 +1539,15 @@ early_param("percpu_alloc", percpu_alloc_setup);
15531539
* vmalloc space is not orders of magnitude larger than distances
15541540
* between node memory addresses (ie. 32bit NUMA machines).
15551541
*
1556-
* When @dyn_size is positive, dynamic area might be larger than
1557-
* specified to fill page alignment. When @dyn_size is auto,
1558-
* @dyn_size is just big enough to fill page alignment after static
1559-
* and reserved areas.
1542+
* @dyn_size specifies the minimum dynamic area size.
15601543
*
15611544
* If the needed size is smaller than the minimum or specified unit
15621545
* size, the leftover is returned using @free_fn.
15631546
*
15641547
* RETURNS:
15651548
* 0 on success, -errno on failure.
15661549
*/
1567-
int __init pcpu_embed_first_chunk(size_t reserved_size, ssize_t dyn_size,
1550+
int __init pcpu_embed_first_chunk(size_t reserved_size, size_t dyn_size,
15681551
size_t atom_size,
15691552
pcpu_fc_cpu_distance_fn_t cpu_distance_fn,
15701553
pcpu_fc_alloc_fn_t alloc_fn,
@@ -1695,7 +1678,7 @@ int __init pcpu_page_first_chunk(size_t reserved_size,
16951678

16961679
snprintf(psize_str, sizeof(psize_str), "%luK", PAGE_SIZE >> 10);
16971680

1698-
ai = pcpu_build_alloc_info(reserved_size, -1, PAGE_SIZE, NULL);
1681+
ai = pcpu_build_alloc_info(reserved_size, 0, PAGE_SIZE, NULL);
16991682
if (IS_ERR(ai))
17001683
return PTR_ERR(ai);
17011684
BUG_ON(ai->nr_groups != 1);
@@ -1821,3 +1804,33 @@ void __init setup_per_cpu_areas(void)
18211804
__per_cpu_offset[cpu] = delta + pcpu_unit_offsets[cpu];
18221805
}
18231806
#endif /* CONFIG_HAVE_SETUP_PER_CPU_AREA */
1807+
1808+
/*
1809+
* First and reserved chunks are initialized with temporary allocation
1810+
* map in initdata so that they can be used before slab is online.
1811+
* This function is called after slab is brought up and replaces those
1812+
* with properly allocated maps.
1813+
*/
1814+
void __init percpu_init_late(void)
1815+
{
1816+
struct pcpu_chunk *target_chunks[] =
1817+
{ pcpu_first_chunk, pcpu_reserved_chunk, NULL };
1818+
struct pcpu_chunk *chunk;
1819+
unsigned long flags;
1820+
int i;
1821+
1822+
for (i = 0; (chunk = target_chunks[i]); i++) {
1823+
int *map;
1824+
const size_t size = PERCPU_DYNAMIC_EARLY_SLOTS * sizeof(map[0]);
1825+
1826+
BUILD_BUG_ON(size > PAGE_SIZE);
1827+
1828+
map = pcpu_mem_alloc(size);
1829+
BUG_ON(!map);
1830+
1831+
spin_lock_irqsave(&pcpu_lock, flags);
1832+
memcpy(map, chunk->map, size);
1833+
chunk->map = map;
1834+
spin_unlock_irqrestore(&pcpu_lock, flags);
1835+
}
1836+
}

0 commit comments

Comments
 (0)