Skip to content

Commit a93ace4

Browse files
committed
percpu: move region iterations out of pcpu_[de]populate_chunk()
Previously, pcpu_[de]populate_chunk() were called with the range which may contain multiple target regions in it and pcpu_[de]populate_chunk() iterated over the regions. This has the benefit of batching up cache flushes for all the regions; however, we're planning to add more bookkeeping logic around [de]population to support atomic allocations and this delegation of iterations gets in the way. This patch moves the region iterations out of pcpu_[de]populate_chunk() into its callers - pcpu_alloc() and pcpu_reclaim() - so that we can later add logic to track more states around them. This change may make cache and tlb flushes more frequent but multi-region [de]populations are rare anyway and if this actually becomes a problem, it's not difficult to factor out cache flushes as separate callbacks which are directly invoked from percpu.c. Signed-off-by: Tejun Heo <tj@kernel.org>
1 parent dca4964 commit a93ace4

3 files changed

Lines changed: 28 additions & 54 deletions

File tree

‎mm/percpu-km.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,14 @@
3333

3434
#include <linux/log2.h>
3535

36-
static int pcpu_populate_chunk(struct pcpu_chunk *chunk, int off, int size)
36+
static int pcpu_populate_chunk(struct pcpu_chunk *chunk,
37+
int page_start, int page_end)
3738
{
3839
return 0;
3940
}
4041

41-
static void pcpu_depopulate_chunk(struct pcpu_chunk *chunk, int off, int size)
42+
static void pcpu_depopulate_chunk(struct pcpu_chunk *chunk,
43+
int page_start, int page_end)
4244
{
4345
/* nada */
4446
}

‎mm/percpu-vm.c‎

Lines changed: 16 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -261,75 +261,52 @@ static void pcpu_post_map_flush(struct pcpu_chunk *chunk,
261261
/**
262262
* pcpu_populate_chunk - populate and map an area of a pcpu_chunk
263263
* @chunk: chunk of interest
264-
* @off: offset to the area to populate
265-
* @size: size of the area to populate in bytes
264+
* @page_start: the start page
265+
* @page_end: the end page
266266
*
267267
* For each cpu, populate and map pages [@page_start,@page_end) into
268268
* @chunk.
269269
*
270270
* CONTEXT:
271271
* pcpu_alloc_mutex, does GFP_KERNEL allocation.
272272
*/
273-
static int pcpu_populate_chunk(struct pcpu_chunk *chunk, int off, int size)
273+
static int pcpu_populate_chunk(struct pcpu_chunk *chunk,
274+
int page_start, int page_end)
274275
{
275-
int page_start = PFN_DOWN(off);
276-
int page_end = PFN_UP(off + size);
277-
int free_end = page_start, unmap_end = page_start;
278276
struct page **pages;
279-
int rs, re, rc;
280277

281278
pages = pcpu_get_pages(chunk);
282279
if (!pages)
283280
return -ENOMEM;
284281

285-
/* alloc and map */
286-
pcpu_for_each_unpop_region(chunk, rs, re, page_start, page_end) {
287-
rc = pcpu_alloc_pages(chunk, pages, rs, re);
288-
if (rc)
289-
goto err_free;
290-
free_end = re;
291-
}
282+
if (pcpu_alloc_pages(chunk, pages, page_start, page_end))
283+
return -ENOMEM;
292284

293-
pcpu_for_each_unpop_region(chunk, rs, re, page_start, page_end) {
294-
rc = pcpu_map_pages(chunk, pages, rs, re);
295-
if (rc)
296-
goto err_unmap;
297-
unmap_end = re;
285+
if (pcpu_map_pages(chunk, pages, page_start, page_end)) {
286+
pcpu_free_pages(chunk, pages, page_start, page_end);
287+
return -ENOMEM;
298288
}
299289
pcpu_post_map_flush(chunk, page_start, page_end);
300290

301291
return 0;
302-
303-
err_unmap:
304-
pcpu_pre_unmap_flush(chunk, page_start, unmap_end);
305-
pcpu_for_each_unpop_region(chunk, rs, re, page_start, unmap_end)
306-
pcpu_unmap_pages(chunk, pages, rs, re);
307-
pcpu_post_unmap_tlb_flush(chunk, page_start, unmap_end);
308-
err_free:
309-
pcpu_for_each_unpop_region(chunk, rs, re, page_start, free_end)
310-
pcpu_free_pages(chunk, pages, rs, re);
311-
return rc;
312292
}
313293

314294
/**
315295
* pcpu_depopulate_chunk - depopulate and unmap an area of a pcpu_chunk
316296
* @chunk: chunk to depopulate
317-
* @off: offset to the area to depopulate
318-
* @size: size of the area to depopulate in bytes
297+
* @page_start: the start page
298+
* @page_end: the end page
319299
*
320300
* For each cpu, depopulate and unmap pages [@page_start,@page_end)
321-
* from @chunk. If @flush is true, vcache is flushed before unmapping
322-
* and tlb after.
301+
* from @chunk.
323302
*
324303
* CONTEXT:
325304
* pcpu_alloc_mutex.
326305
*/
327-
static void pcpu_depopulate_chunk(struct pcpu_chunk *chunk, int off, int size)
306+
static void pcpu_depopulate_chunk(struct pcpu_chunk *chunk,
307+
int page_start, int page_end)
328308
{
329-
int page_start = PFN_DOWN(off);
330-
int page_end = PFN_UP(off + size);
331309
struct page **pages;
332-
int rs, re;
333310

334311
/*
335312
* If control reaches here, there must have been at least one
@@ -342,13 +319,11 @@ static void pcpu_depopulate_chunk(struct pcpu_chunk *chunk, int off, int size)
342319
/* unmap and free */
343320
pcpu_pre_unmap_flush(chunk, page_start, page_end);
344321

345-
pcpu_for_each_pop_region(chunk, rs, re, page_start, page_end)
346-
pcpu_unmap_pages(chunk, pages, rs, re);
322+
pcpu_unmap_pages(chunk, pages, page_start, page_end);
347323

348324
/* no need to flush tlb, vmalloc will handle it lazily */
349325

350-
pcpu_for_each_pop_region(chunk, rs, re, page_start, page_end)
351-
pcpu_free_pages(chunk, pages, rs, re);
326+
pcpu_free_pages(chunk, pages, page_start, page_end);
352327
}
353328

354329
static struct pcpu_chunk *pcpu_create_chunk(void)

‎mm/percpu.c‎

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -807,20 +807,17 @@ static void __percpu *pcpu_alloc(size_t size, size_t align, bool reserved)
807807
page_start = PFN_DOWN(off);
808808
page_end = PFN_UP(off + size);
809809

810-
rs = page_start;
811-
pcpu_next_pop(chunk, &rs, &re, page_end);
812-
813-
if (rs != page_start || re != page_end) {
810+
pcpu_for_each_unpop_region(chunk, rs, re, page_start, page_end) {
814811
WARN_ON(chunk->immutable);
815812

816-
if (pcpu_populate_chunk(chunk, off, size)) {
813+
if (pcpu_populate_chunk(chunk, rs, re)) {
817814
spin_lock_irqsave(&pcpu_lock, flags);
818815
pcpu_free_area(chunk, off);
819816
err = "failed to populate";
820817
goto fail_unlock;
821818
}
822819

823-
bitmap_set(chunk->populated, page_start, page_end - page_start);
820+
bitmap_set(chunk->populated, rs, re - rs);
824821
}
825822

826823
mutex_unlock(&pcpu_alloc_mutex);
@@ -919,12 +916,12 @@ static void pcpu_reclaim(struct work_struct *work)
919916
spin_unlock_irq(&pcpu_lock);
920917

921918
list_for_each_entry_safe(chunk, next, &todo, list) {
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);
919+
int rs, re;
927920

921+
pcpu_for_each_pop_region(chunk, rs, re, 0, pcpu_unit_pages) {
922+
pcpu_depopulate_chunk(chunk, rs, re);
923+
bitmap_clear(chunk->populated, rs, re - rs);
924+
}
928925
pcpu_destroy_chunk(chunk);
929926
}
930927

0 commit comments

Comments
 (0)