Skip to content

Commit dca4964

Browse files
committed
percpu: move common parts out of pcpu_[de]populate_chunk()
percpu-vm and percpu-km implement separate versions of pcpu_[de]populate_chunk() and some part which is or should be common are currently in the specific implementations. Make the following changes. * Allocate area clearing is moved from the pcpu_populate_chunk() implementations to pcpu_alloc(). This makes percpu-km's version noop. * Quick exit tests in pcpu_[de]populate_chunk() of percpu-vm are moved to their respective callers so that they are applied to percpu-km too. This doesn't make any meaningful difference as both functions are noop for percpu-km; however, this is more consistent and will help implementing atomic allocation support. Signed-off-by: Tejun Heo <tj@kernel.org>
1 parent cdb4cba commit dca4964

3 files changed

Lines changed: 31 additions & 40 deletions

File tree

‎mm/percpu-km.c‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,6 @@
3535

3636
static int pcpu_populate_chunk(struct pcpu_chunk *chunk, int off, int size)
3737
{
38-
unsigned int cpu;
39-
40-
for_each_possible_cpu(cpu)
41-
memset((void *)pcpu_chunk_addr(chunk, cpu, 0) + off, 0, size);
42-
4338
return 0;
4439
}
4540

‎mm/percpu-vm.c‎

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ static void pcpu_post_map_flush(struct pcpu_chunk *chunk,
265265
* @size: size of the area to populate in bytes
266266
*
267267
* For each cpu, populate and map pages [@page_start,@page_end) into
268-
* @chunk. The area is cleared on return.
268+
* @chunk.
269269
*
270270
* CONTEXT:
271271
* pcpu_alloc_mutex, does GFP_KERNEL allocation.
@@ -276,18 +276,8 @@ static int pcpu_populate_chunk(struct pcpu_chunk *chunk, int off, int size)
276276
int page_end = PFN_UP(off + size);
277277
int free_end = page_start, unmap_end = page_start;
278278
struct page **pages;
279-
unsigned int cpu;
280279
int rs, re, rc;
281280

282-
/* quick path, check whether all pages are already there */
283-
rs = page_start;
284-
pcpu_next_pop(chunk, &rs, &re, page_end);
285-
if (rs == page_start && re == page_end)
286-
goto clear;
287-
288-
/* need to allocate and map pages, this chunk can't be immutable */
289-
WARN_ON(chunk->immutable);
290-
291281
pages = pcpu_get_pages(chunk);
292282
if (!pages)
293283
return -ENOMEM;
@@ -308,10 +298,6 @@ static int pcpu_populate_chunk(struct pcpu_chunk *chunk, int off, int size)
308298
}
309299
pcpu_post_map_flush(chunk, page_start, page_end);
310300

311-
bitmap_set(chunk->populated, page_start, page_end - page_start);
312-
clear:
313-
for_each_possible_cpu(cpu)
314-
memset((void *)pcpu_chunk_addr(chunk, cpu, 0) + off, 0, size);
315301
return 0;
316302

317303
err_unmap:
@@ -345,15 +331,6 @@ static void pcpu_depopulate_chunk(struct pcpu_chunk *chunk, int off, int size)
345331
struct page **pages;
346332
int rs, re;
347333

348-
/* quick path, check whether it's empty already */
349-
rs = page_start;
350-
pcpu_next_unpop(chunk, &rs, &re, page_end);
351-
if (rs == page_start && re == page_end)
352-
return;
353-
354-
/* immutable chunks can't be depopulated */
355-
WARN_ON(chunk->immutable);
356-
357334
/*
358335
* If control reaches here, there must have been at least one
359336
* successful population attempt so the temp pages array must
@@ -372,8 +349,6 @@ static void pcpu_depopulate_chunk(struct pcpu_chunk *chunk, int off, int size)
372349

373350
pcpu_for_each_pop_region(chunk, rs, re, page_start, page_end)
374351
pcpu_free_pages(chunk, pages, rs, re);
375-
376-
bitmap_clear(chunk->populated, page_start, page_end - page_start);
377352
}
378353

379354
static struct pcpu_chunk *pcpu_create_chunk(void)

‎mm/percpu.c‎

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,8 @@ static void __percpu *pcpu_alloc(size_t size, size_t align, bool reserved)
709709
static int warn_limit = 10;
710710
struct pcpu_chunk *chunk;
711711
const char *err;
712-
int slot, off, new_alloc;
712+
int slot, off, new_alloc, cpu;
713+
int page_start, page_end, rs, re;
713714
unsigned long flags;
714715
void __percpu *ptr;
715716

@@ -802,17 +803,32 @@ static void __percpu *pcpu_alloc(size_t size, size_t align, bool reserved)
802803
area_found:
803804
spin_unlock_irqrestore(&pcpu_lock, flags);
804805

805-
/* populate, map and clear the area */
806-
if (pcpu_populate_chunk(chunk, off, size)) {
807-
spin_lock_irqsave(&pcpu_lock, flags);
808-
pcpu_free_area(chunk, off);
809-
err = "failed to populate";
810-
goto fail_unlock;
806+
/* populate if not all pages are already there */
807+
page_start = PFN_DOWN(off);
808+
page_end = PFN_UP(off + size);
809+
810+
rs = page_start;
811+
pcpu_next_pop(chunk, &rs, &re, page_end);
812+
813+
if (rs != page_start || re != page_end) {
814+
WARN_ON(chunk->immutable);
815+
816+
if (pcpu_populate_chunk(chunk, off, size)) {
817+
spin_lock_irqsave(&pcpu_lock, flags);
818+
pcpu_free_area(chunk, off);
819+
err = "failed to populate";
820+
goto fail_unlock;
821+
}
822+
823+
bitmap_set(chunk->populated, page_start, page_end - page_start);
811824
}
812825

813826
mutex_unlock(&pcpu_alloc_mutex);
814827

815-
/* return address relative to base address */
828+
/* clear the areas and return address relative to base address */
829+
for_each_possible_cpu(cpu)
830+
memset((void *)pcpu_chunk_addr(chunk, cpu, 0) + off, 0, size);
831+
816832
ptr = __addr_to_pcpu_ptr(chunk->base_addr + off);
817833
kmemleak_alloc_percpu(ptr, size);
818834
return ptr;
@@ -903,7 +919,12 @@ static void pcpu_reclaim(struct work_struct *work)
903919
spin_unlock_irq(&pcpu_lock);
904920

905921
list_for_each_entry_safe(chunk, next, &todo, list) {
906-
pcpu_depopulate_chunk(chunk, 0, pcpu_unit_size);
922+
int rs = 0, re;
923+
924+
pcpu_next_unpop(chunk, &rs, &re, PFN_UP(pcpu_unit_size));
925+
if (rs || re != PFN_UP(pcpu_unit_size))
926+
pcpu_depopulate_chunk(chunk, 0, pcpu_unit_size);
927+
907928
pcpu_destroy_chunk(chunk);
908929
}
909930

0 commit comments

Comments
 (0)