History

bpf: rework memlock-based memory accounting for maps

In order to unify the existing memlock charging code with the memcg-based memory accounting, which will be added later, let's rework the current scheme. Currently the following design is used: 1) .alloc() callback optionally checks if the allocation will likely succeed using bpf_map_precharge_memlock() 2) .alloc() performs actual allocations 3) .alloc() callback calculates map cost and sets map.memory.pages 4) map_create() calls bpf_map_init_memlock() which sets map.memory.user and performs actual charging; in case of failure the map is destroyed <map is in use> 1) bpf_map_free_deferred() calls bpf_map_release_memlock(), which performs uncharge and releases the user 2) .map_free() callback releases the memory The scheme can be simplified and made more robust: 1) .alloc() calculates map cost and calls bpf_map_charge_init() 2) bpf_map_charge_init() sets map.memory.user and performs actual charge 3) .alloc() performs actual allocations <map is in use> 1) .map_free() callback releases the memory 2) bpf_map_charge_finish() performs uncharge and releases the user The new scheme also allows to reuse bpf_map_charge_init()/finish() functions for memcg-based accounting. Because charges are performed before actual allocations and uncharges after freeing the memory, no bogus memory pressure can be created. In cases when the map structure is not available (e.g. it's not created yet, or is already destroyed), on-stack bpf_map_memory structure is used. The charge can be transferred with the bpf_map_charge_move() function.

Roman Gushchin committed 7 years ago

Commit b936ca61 parent 3539b9614 files · +112 −88Browse files

Showing the changes to include/linux (1 of 14 files). Show all

Unified · Split

include/linux/bpf.h+4−1
@@ -650,9 +650,12 @@ struct bpf_map *__bpf_map_get(struct fd f); · 649 unmodified lines
650650 struct bpf_map * __must_check bpf_map_inc(struct bpf_map *map, bool uref);
651651 void bpf_map_put_with_uref(struct bpf_map *map);
652652 void bpf_map_put(struct bpf_map *map);
653−int bpf_map_precharge_memlock(u32 pages);
654653 int bpf_map_charge_memlock(struct bpf_map *map, u32 pages);
655654 void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages);
655+int bpf_map_charge_init(struct bpf_map_memory *mem, u32 pages);
656+void bpf_map_charge_finish(struct bpf_map_memory *mem);
657+void bpf_map_charge_move(struct bpf_map_memory *dst,
658+ struct bpf_map_memory *src);
656659 void *bpf_map_area_alloc(size_t size, int numa_node);
657660 void bpf_map_area_free(void *base);
658661 void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t