History

bpf: Remove gfp_flags plumbing from bpf_local_storage_update()

Remove the check that rejects sleepable BPF programs from doing BPF_ANY/BPF_EXIST updates on local storage. This restriction was added in commit b00fa38a9c1c ("bpf: Enable non-atomic allocations in local storage") because kzalloc(GFP_KERNEL) could sleep inside local_storage->lock. This is no longer a concern: all local storage allocations now use kmalloc_nolock() which never sleeps. In addition, since kmalloc_nolock() only accepts __GFP_ACCOUNT, __GFP_ZERO and __GFP_NO_OBJ_EXT, the gfp_flags parameter plumbing from bpf_*_storage_get() to bpf_local_storage_update() becomes dead code. Remove gfp_flags from bpf_selem_alloc(), bpf_local_storage_alloc() and bpf_local_storage_update(). Drop the hidden 5th argument from bpf_*_storage_get helpers, and remove the verifier patching that injected GFP_KERNEL/GFP_ATOMIC into the fifth argument.

Amery Hung committed 7 months ago

Commit 136deea1 parent 5063e777 files · +30 −67Browse files

Unified · Split

7 files
  1. include/linux/bpf_local_storage.h +3 −4
  2. kernel/bpf/bpf_cgrp_storage.c +4 −5
  3. kernel/bpf/bpf_inode_storage.c +4 −5
  4. kernel/bpf/bpf_local_storage.c +6 −10
  5. kernel/bpf/bpf_task_storage.c +4 −5
  6. kernel/bpf/verifier.c +0 −26
  7. net/core/bpf_sk_storage.c +9 −12
include/linux/bpf_local_storage.h+3−4
@@ -188,7 +188,7 @@ int bpf_selem_link_map(struct bpf_local_storage_map *smap, · 187 unmodified lines
188188
189189 struct bpf_local_storage_elem *
190190 bpf_selem_alloc(struct bpf_local_storage_map *smap, void *owner, void *value,
191− bool swap_uptrs, gfp_t gfp_flags);
191+ bool swap_uptrs);
192192
193193 void bpf_selem_free(struct bpf_local_storage_elem *selem,
194194 bool reuse_now);
@@ -196,12 +196,11 @@ void bpf_selem_free(struct bpf_local_storage_elem *selem, · 1 unmodified line
196196 int
197197 bpf_local_storage_alloc(void *owner,
198198 struct bpf_local_storage_map *smap,
199− struct bpf_local_storage_elem *first_selem,
200− gfp_t gfp_flags);
199+ struct bpf_local_storage_elem *first_selem);
201200
202201 struct bpf_local_storage_data *
203202 bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap,
204− void *value, u64 map_flags, bool swap_uptrs, gfp_t gfp_flags);
203+ void *value, u64 map_flags, bool swap_uptrs);
205204
206205 u64 bpf_local_storage_map_mem_usage(const struct bpf_map *map);
207206
kernel/bpf/bpf_cgrp_storage.c+4−5
@@ -76,7 +76,7 @@ static long bpf_cgrp_storage_update_elem(struct bpf_map *map, void *key, · 75 unmodified lines
7676 return PTR_ERR(cgroup);
7777
7878 sdata = bpf_local_storage_update(cgroup, (struct bpf_local_storage_map *)map,
79− value, map_flags, false, GFP_ATOMIC);
79+ value, map_flags, false);
8080 cgroup_put(cgroup);
8181 return PTR_ERR_OR_ZERO(sdata);
8282 }
@@ -122,9 +122,8 @@ static void cgroup_storage_map_free(struct bpf_map *map) · 39 unmodified lines
122122 bpf_local_storage_map_free(map, &cgroup_cache);
123123 }
124124
125−/* *gfp_flags* is a hidden argument provided by the verifier */
126−BPF_CALL_5(bpf_cgrp_storage_get, struct bpf_map *, map, struct cgroup *, cgroup,
127− void *, value, u64, flags, gfp_t, gfp_flags)
125+BPF_CALL_4(bpf_cgrp_storage_get, struct bpf_map *, map, struct cgroup *, cgroup,
126+ void *, value, u64, flags)
128127 {
129128 struct bpf_local_storage_data *sdata;
130129
@@ -143,7 +142,7 @@ BPF_CALL_5(bpf_cgrp_storage_get, struct bpf_map *, map, struct cgroup *, cgroup, · 12 unmodified lines
143142 if (!percpu_ref_is_dying(&cgroup->self.refcnt) &&
144143 (flags & BPF_LOCAL_STORAGE_GET_F_CREATE))
145144 sdata = bpf_local_storage_update(cgroup, (struct bpf_local_storage_map *)map,
146− value, BPF_NOEXIST, false, gfp_flags);
145+ value, BPF_NOEXIST, false);
147146
148147 out:
149148 return IS_ERR_OR_NULL(sdata) ? (unsigned long)NULL : (unsigned long)sdata->data;
kernel/bpf/bpf_inode_storage.c+4−5
@@ -98,7 +98,7 @@ static long bpf_fd_inode_storage_update_elem(struct bpf_map *map, void *key, · 97 unmodified lines
9898
9999 sdata = bpf_local_storage_update(file_inode(fd_file(f)),
100100 (struct bpf_local_storage_map *)map,
101− value, map_flags, false, GFP_ATOMIC);
101+ value, map_flags, false);
102102 return PTR_ERR_OR_ZERO(sdata);
103103 }
104104
@@ -122,9 +122,8 @@ static long bpf_fd_inode_storage_delete_elem(struct bpf_map *map, void *key) · 17 unmodified lines
122122 return inode_storage_delete(file_inode(fd_file(f)), map);
123123 }
124124
125−/* *gfp_flags* is a hidden argument provided by the verifier */
126−BPF_CALL_5(bpf_inode_storage_get, struct bpf_map *, map, struct inode *, inode,
127− void *, value, u64, flags, gfp_t, gfp_flags)
125+BPF_CALL_4(bpf_inode_storage_get, struct bpf_map *, map, struct inode *, inode,
126+ void *, value, u64, flags)
128127 {
129128 struct bpf_local_storage_data *sdata;
130129
@@ -150,7 +149,7 @@ BPF_CALL_5(bpf_inode_storage_get, struct bpf_map *, map, struct inode *, inode, · 19 unmodified lines
150149 if (flags & BPF_LOCAL_STORAGE_GET_F_CREATE) {
151150 sdata = bpf_local_storage_update(
152151 inode, (struct bpf_local_storage_map *)map, value,
153− BPF_NOEXIST, false, gfp_flags);
152+ BPF_NOEXIST, false);
154153 return IS_ERR(sdata) ? (unsigned long)NULL :
155154 (unsigned long)sdata->data;
156155 }
kernel/bpf/bpf_local_storage.c+6−10
@@ -68,7 +68,7 @@ static bool selem_linked_to_map(const struct bpf_local_storage_elem *selem) · 67 unmodified lines
6868
6969 struct bpf_local_storage_elem *
7070 bpf_selem_alloc(struct bpf_local_storage_map *smap, void *owner,
71− void *value, bool swap_uptrs, gfp_t gfp_flags)
71+ void *value, bool swap_uptrs)
7272 {
7373 struct bpf_local_storage_elem *selem;
7474
@@ -475,8 +475,7 @@ static int check_flags(const struct bpf_local_storage_data *old_sdata, · 400 unmodified lines
475475
476476 int bpf_local_storage_alloc(void *owner,
477477 struct bpf_local_storage_map *smap,
478− struct bpf_local_storage_elem *first_selem,
479− gfp_t gfp_flags)
478+ struct bpf_local_storage_elem *first_selem)
480479 {
481480 struct bpf_local_storage *prev_storage, *storage;
482481 struct bpf_local_storage **owner_storage_ptr;
@@ -546,7 +545,7 @@ uncharge: · 63 unmodified lines
546545 */
547546 struct bpf_local_storage_data *
548547 bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap,
549− void *value, u64 map_flags, bool swap_uptrs, gfp_t gfp_flags)
548+ void *value, u64 map_flags, bool swap_uptrs)
550549 {
551550 struct bpf_local_storage_data *old_sdata = NULL;
552551 struct bpf_local_storage_elem *alloc_selem, *selem = NULL;
@@ -563,9 +562,6 @@ bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap, · 10 unmodified lines
563562 !btf_record_has_field(smap->map.record, BPF_SPIN_LOCK)))
564563 return ERR_PTR(-EINVAL);
565564
566− if (gfp_flags == GFP_KERNEL && (map_flags & ~BPF_F_LOCK) != BPF_NOEXIST)
567− return ERR_PTR(-EINVAL);
568−
569565 local_storage = rcu_dereference_check(*owner_storage(smap, owner),
570566 bpf_rcu_lock_held());
571567 if (!local_storage || hlist_empty(&local_storage->list)) {
@@ -574,11 +570,11 @@ bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap, · 2 unmodified lines
574570 if (err)
575571 return ERR_PTR(err);
576572
577− selem = bpf_selem_alloc(smap, owner, value, swap_uptrs, gfp_flags);
573+ selem = bpf_selem_alloc(smap, owner, value, swap_uptrs);
578574 if (!selem)
579575 return ERR_PTR(-ENOMEM);
580576
581− err = bpf_local_storage_alloc(owner, smap, selem, gfp_flags);
577+ err = bpf_local_storage_alloc(owner, smap, selem);
582578 if (err) {
583579 bpf_selem_free(selem, true);
584580 mem_uncharge(smap, owner, smap->elem_size);
@@ -608,7 +604,7 @@ bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap, · 23 unmodified lines
608604 /* A lookup has just been done before and concluded a new selem is
609605 * needed. The chance of an unnecessary alloc is unlikely.
610606 */
611− alloc_selem = selem = bpf_selem_alloc(smap, owner, value, swap_uptrs, gfp_flags);
607+ alloc_selem = selem = bpf_selem_alloc(smap, owner, value, swap_uptrs);
612608 if (!alloc_selem)
613609 return ERR_PTR(-ENOMEM);
614610
kernel/bpf/bpf_task_storage.c+4−5
@@ -118,7 +118,7 @@ static long bpf_pid_task_storage_update_elem(struct bpf_map *map, void *key, · 117 unmodified lines
118118
119119 sdata = bpf_local_storage_update(
120120 task, (struct bpf_local_storage_map *)map, value, map_flags,
121− true, GFP_ATOMIC);
121+ true);
122122
123123 err = PTR_ERR_OR_ZERO(sdata);
124124 out:
@@ -165,9 +165,8 @@ out: · 40 unmodified lines
165165 return err;
166166 }
167167
168−/* *gfp_flags* is a hidden argument provided by the verifier */
169−BPF_CALL_5(bpf_task_storage_get, struct bpf_map *, map, struct task_struct *,
170− task, void *, value, u64, flags, gfp_t, gfp_flags)
168+BPF_CALL_4(bpf_task_storage_get, struct bpf_map *, map, struct task_struct *,
169+ task, void *, value, u64, flags)
171170 {
172171 struct bpf_local_storage_data *sdata;
173172
@@ -184,7 +183,7 @@ BPF_CALL_5(bpf_task_storage_get, struct bpf_map *, map, struct task_struct *, · 10 unmodified lines
184183 (flags & BPF_LOCAL_STORAGE_GET_F_CREATE)) {
185184 sdata = bpf_local_storage_update(
186185 task, (struct bpf_local_storage_map *)map, value,
187− BPF_NOEXIST, false, gfp_flags);
186+ BPF_NOEXIST, false);
188187 return IS_ERR(sdata) ? (unsigned long)NULL : (unsigned long)sdata->data;
189188 }
190189
kernel/bpf/verifier.c+0−26
@@ -589,14 +589,6 @@ static bool is_may_goto_insn_at(struct bpf_verifier_env *env, int insn_idx) · 588 unmodified lines
589589 return bpf_is_may_goto_insn(&env->prog->insnsi[insn_idx]);
590590 }
591591
592−static bool is_storage_get_function(enum bpf_func_id func_id)
593−{
594− return func_id == BPF_FUNC_sk_storage_get ||
595− func_id == BPF_FUNC_inode_storage_get ||
596− func_id == BPF_FUNC_task_storage_get ||
597− func_id == BPF_FUNC_cgrp_storage_get;
598−}
599−
600592 static bool helper_multiple_ref_obj_use(enum bpf_func_id func_id,
601593 const struct bpf_map *map)
602594 {
@@ -24429,24 +24421,6 @@ static int do_misc_fixups(struct bpf_verifier_env *env) · 23826 unmodified lines
2442924421 goto patch_call_imm;
2443024422 }
2443124423
24432− if (is_storage_get_function(insn->imm)) {
24433− if (env->insn_aux_data[i + delta].non_sleepable)
24434− insn_buf[0] = BPF_MOV64_IMM(BPF_REG_5, (__force __s32)GFP_ATOMIC);
24435− else
24436− insn_buf[0] = BPF_MOV64_IMM(BPF_REG_5, (__force __s32)GFP_KERNEL);
24437− insn_buf[1] = *insn;
24438− cnt = 2;
24439−
24440− new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
24441− if (!new_prog)
24442− return -ENOMEM;
24443−
24444− delta += cnt - 1;
24445− env->prog = prog = new_prog;
24446− insn = new_prog->insnsi + i + delta;
24447− goto patch_call_imm;
24448− }
24449−
2445024424 /* bpf_per_cpu_ptr() and bpf_this_cpu_ptr() */
2445124425 if (env->insn_aux_data[i + delta].call_with_percpu_alloc_ptr) {
2445224426 /* patch with 'r1 = *(u64 *)(r1 + 0)' since for percpu data,
net/core/bpf_sk_storage.c+9−12
@@ -106,7 +106,7 @@ static long bpf_fd_sk_storage_update_elem(struct bpf_map *map, void *key, · 105 unmodified lines
106106 if (sock) {
107107 sdata = bpf_local_storage_update(
108108 sock->sk, (struct bpf_local_storage_map *)map, value,
109− map_flags, false, GFP_ATOMIC);
109+ map_flags, false);
110110 sockfd_put(sock);
111111 return PTR_ERR_OR_ZERO(sdata);
112112 }
@@ -137,7 +137,7 @@ bpf_sk_storage_clone_elem(struct sock *newsk, · 24 unmodified lines
137137 {
138138 struct bpf_local_storage_elem *copy_selem;
139139
140− copy_selem = bpf_selem_alloc(smap, newsk, NULL, false, GFP_ATOMIC);
140+ copy_selem = bpf_selem_alloc(smap, newsk, NULL, false);
141141 if (!copy_selem)
142142 return NULL;
143143
@@ -202,7 +202,7 @@ int bpf_sk_storage_clone(const struct sock *sk, struct sock *newsk) · 58 unmodified lines
202202 }
203203 bpf_selem_link_storage_nolock(new_sk_storage, copy_selem);
204204 } else {
205− ret = bpf_local_storage_alloc(newsk, smap, copy_selem, GFP_ATOMIC);
205+ ret = bpf_local_storage_alloc(newsk, smap, copy_selem);
206206 if (ret) {
207207 bpf_selem_free(copy_selem, true);
208208 atomic_sub(smap->elem_size,
@@ -227,9 +227,8 @@ out: · 18 unmodified lines
227227 return ret;
228228 }
229229
230−/* *gfp_flags* is a hidden argument provided by the verifier */
231−BPF_CALL_5(bpf_sk_storage_get, struct bpf_map *, map, struct sock *, sk,
232− void *, value, u64, flags, gfp_t, gfp_flags)
230+BPF_CALL_4(bpf_sk_storage_get, struct bpf_map *, map, struct sock *, sk,
231+ void *, value, u64, flags)
233232 {
234233 struct bpf_local_storage_data *sdata;
235234
@@ -250,7 +249,7 @@ BPF_CALL_5(bpf_sk_storage_get, struct bpf_map *, map, struct sock *, sk, · 14 unmodified lines
250249 refcount_inc_not_zero(&sk->sk_refcnt)) {
251250 sdata = bpf_local_storage_update(
252251 sk, (struct bpf_local_storage_map *)map, value,
253− BPF_NOEXIST, false, gfp_flags);
252+ BPF_NOEXIST, false);
254253 /* sk must be a fullsock (guaranteed by verifier),
255254 * so sock_gen_put() is unnecessary.
256255 */
@@ -383,16 +382,14 @@ static bool bpf_sk_storage_tracing_allowed(const struct bpf_prog *prog) · 126 unmodified lines
383382 return false;
384383 }
385384
386−/* *gfp_flags* is a hidden argument provided by the verifier */
387−BPF_CALL_5(bpf_sk_storage_get_tracing, struct bpf_map *, map, struct sock *, sk,
388− void *, value, u64, flags, gfp_t, gfp_flags)
385+BPF_CALL_4(bpf_sk_storage_get_tracing, struct bpf_map *, map, struct sock *, sk,
386+ void *, value, u64, flags)
389387 {
390388 WARN_ON_ONCE(!bpf_rcu_lock_held());
391389 if (in_hardirq() || in_nmi())
392390 return (unsigned long)NULL;
393391
394− return (unsigned long)____bpf_sk_storage_get(map, sk, value, flags,
395− gfp_flags);
392+ return (unsigned long)____bpf_sk_storage_get(map, sk, value, flags);
396393 }
397394
398395 BPF_CALL_2(bpf_sk_storage_delete_tracing, struct bpf_map *, map,