History

bpf: Make non-preallocated allocation low priority

GFP_ATOMIC doesn't cooperate well with memcg pressure so far, especially if we allocate too much GFP_ATOMIC memory. For example, when we set the memcg limit to limit a non-preallocated bpf memory, the GFP_ATOMIC can easily break the memcg limit by force charge. So it is very dangerous to use GFP_ATOMIC in non-preallocated case. One way to make it safe is to remove __GFP_HIGH from GFP_ATOMIC, IOW, use (__GFP_ATOMIC | __GFP_KSWAPD_RECLAIM) instead, then it will be limited if we allocate too much memory. There's a plan to completely remove __GFP_ATOMIC in the mm side[1], so let's use GFP_NOWAIT instead. We introduced BPF_F_NO_PREALLOC is because full map pre-allocation is too memory expensive for some cases. That means removing __GFP_HIGH doesn't break the rule of BPF_F_NO_PREALLOC, but has the same goal with it-avoiding issues caused by too much memory. So let's remove it. This fix can also apply to other run-time allocations, for example, the allocation in lpm trie, local storage and devmap. So let fix it consistently over the bpf code It also fixes a typo in the comment. [1]. https://lore.kernel.org/linux-mm/163712397076.13692.4727608274002939094@noble.neil.brown.name/

Yafang Shao committed 4 years ago

Commit ace2bee1 parent 1d5f82d4 files · +6 −6Browse files

Unified · Split

4 files
  1. kernel/bpf/devmap.c +1 −1
  2. kernel/bpf/hashtab.c +3 −3
  3. kernel/bpf/local_storage.c +1 −1
  4. kernel/bpf/lpm_trie.c +1 −1
kernel/bpf/devmap.c+1−1
@@ -845,7 +845,7 @@ static struct bpf_dtab_netdev *__dev_map_alloc_node(struct net *net, · 844 unmodified lines
845845 struct bpf_dtab_netdev *dev;
846846
847847 dev = bpf_map_kmalloc_node(&dtab->map, sizeof(*dev),
848− GFP_ATOMIC | __GFP_NOWARN,
848+ GFP_NOWAIT | __GFP_NOWARN,
849849 dtab->map.numa_node);
850850 if (!dev)
851851 return ERR_PTR(-ENOMEM);
kernel/bpf/hashtab.c+3−3
@@ -61,7 +61,7 @@ · 60 unmodified lines
6161 *
6262 * As regular device interrupt handlers and soft interrupts are forced into
6363 * thread context, the existing code which does
64− * spin_lock*(); alloc(GPF_ATOMIC); spin_unlock*();
64+ * spin_lock*(); alloc(GFP_ATOMIC); spin_unlock*();
6565 * just works.
6666 *
6767 * In theory the BPF locks could be converted to regular spinlocks as well,
@@ -978,7 +978,7 @@ static struct htab_elem *alloc_htab_elem(struct bpf_htab *htab, void *key, · 910 unmodified lines
978978 goto dec_count;
979979 }
980980 l_new = bpf_map_kmalloc_node(&htab->map, htab->elem_size,
981− GFP_ATOMIC | __GFP_NOWARN,
981+ GFP_NOWAIT | __GFP_NOWARN,
982982 htab->map.numa_node);
983983 if (!l_new) {
984984 l_new = ERR_PTR(-ENOMEM);
@@ -996,7 +996,7 @@ static struct htab_elem *alloc_htab_elem(struct bpf_htab *htab, void *key, · 11 unmodified lines
996996 } else {
997997 /* alloc_percpu zero-fills */
998998 pptr = bpf_map_alloc_percpu(&htab->map, size, 8,
999− GFP_ATOMIC | __GFP_NOWARN);
999+ GFP_NOWAIT | __GFP_NOWARN);
10001000 if (!pptr) {
10011001 kfree(l_new);
10021002 l_new = ERR_PTR(-ENOMEM);
kernel/bpf/local_storage.c+1−1
@@ -165,7 +165,7 @@ static int cgroup_storage_update_elem(struct bpf_map *map, void *key, · 164 unmodified lines
165165 }
166166
167167 new = bpf_map_kmalloc_node(map, struct_size(new, data, map->value_size),
168− __GFP_ZERO | GFP_ATOMIC | __GFP_NOWARN,
168+ __GFP_ZERO | GFP_NOWAIT | __GFP_NOWARN,
169169 map->numa_node);
170170 if (!new)
171171 return -ENOMEM;
kernel/bpf/lpm_trie.c+1−1
@@ -285,7 +285,7 @@ static struct lpm_trie_node *lpm_trie_node_alloc(const struct lpm_trie *trie, · 284 unmodified lines
285285 if (value)
286286 size += trie->map.value_size;
287287
288− node = bpf_map_kmalloc_node(&trie->map, size, GFP_ATOMIC | __GFP_NOWARN,
288+ node = bpf_map_kmalloc_node(&trie->map, size, GFP_NOWAIT | __GFP_NOWARN,
289289 trie->map.numa_node);
290290 if (!node)
291291 return NULL;

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