Skip to content

Commit 0e2ba9f

Browse files
Yonghong SongAlexei Starovoitov
authored andcommitted
bpf: Use smaller low/high marks for percpu allocation
Currently, refill low/high marks are set with the assumption of normal non-percpu memory allocation. For example, for an allocation size 256, for non-percpu memory allocation, low mark is 32 and high mark is 96, resulting in the batch allocation of 48 elements and the allocated memory will be 48 * 256 = 12KB for this particular cpu. Assuming an 128-cpu system, the total memory consumption across all cpus will be 12K * 128 = 1.5MB memory. This might be okay for non-percpu allocation, but may not be good for percpu allocation, which will consume 1.5MB * 128 = 192MB memory in the worst case if every cpu has a chance of memory allocation. In practice, percpu allocation is very rare compared to non-percpu allocation. So let us have smaller low/high marks which can avoid unnecessary memory consumption. Signed-off-by: Yonghong Song <[email protected]> Acked-by: Hou Tao <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 5b95e63 commit 0e2ba9f

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

kernel/bpf/memalloc.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,11 +464,17 @@ static void notrace irq_work_raise(struct bpf_mem_cache *c)
464464
* consume ~ 11 Kbyte per cpu.
465465
* Typical case will be between 11K and 116K closer to 11K.
466466
* bpf progs can and should share bpf_mem_cache when possible.
467+
*
468+
* Percpu allocation is typically rare. To avoid potential unnecessary large
469+
* memory consumption, set low_mark = 1 and high_mark = 3, resulting in c->batch = 1.
467470
*/
468471
static void init_refill_work(struct bpf_mem_cache *c)
469472
{
470473
init_irq_work(&c->refill_work, bpf_mem_refill);
471-
if (c->unit_size <= 256) {
474+
if (c->percpu_size) {
475+
c->low_watermark = 1;
476+
c->high_watermark = 3;
477+
} else if (c->unit_size <= 256) {
472478
c->low_watermark = 32;
473479
c->high_watermark = 96;
474480
} else {

0 commit comments

Comments
 (0)