~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/kernel/bpf/offload.c

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /kernel/bpf/offload.c (Version linux-6.12-rc7) and /kernel/bpf/offload.c (Version linux-5.9.16)


  1 /*                                                  1 /*
  2  * Copyright (C) 2017-2018 Netronome Systems,       2  * Copyright (C) 2017-2018 Netronome Systems, Inc.
  3  *                                                  3  *
  4  * This software is licensed under the GNU Gen      4  * This software is licensed under the GNU General License Version 2,
  5  * June 1991 as shown in the file COPYING in t      5  * June 1991 as shown in the file COPYING in the top-level directory of this
  6  * source tree.                                     6  * source tree.
  7  *                                                  7  *
  8  * THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES       8  * THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS"
  9  * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRES      9  * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 10  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES      10  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 11  * FOR A PARTICULAR PURPOSE. THE ENTIRE RISK A     11  * FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE
 12  * OF THE PROGRAM IS WITH YOU. SHOULD THE PROG     12  * OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME
 13  * THE COST OF ALL NECESSARY SERVICING, REPAIR     13  * THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
 14  */                                                14  */
 15                                                    15 
 16 #include <linux/bpf.h>                             16 #include <linux/bpf.h>
 17 #include <linux/bpf_verifier.h>                    17 #include <linux/bpf_verifier.h>
 18 #include <linux/bug.h>                             18 #include <linux/bug.h>
 19 #include <linux/kdev_t.h>                          19 #include <linux/kdev_t.h>
 20 #include <linux/list.h>                            20 #include <linux/list.h>
 21 #include <linux/lockdep.h>                         21 #include <linux/lockdep.h>
 22 #include <linux/netdevice.h>                       22 #include <linux/netdevice.h>
 23 #include <linux/printk.h>                          23 #include <linux/printk.h>
 24 #include <linux/proc_ns.h>                         24 #include <linux/proc_ns.h>
 25 #include <linux/rhashtable.h>                      25 #include <linux/rhashtable.h>
 26 #include <linux/rtnetlink.h>                       26 #include <linux/rtnetlink.h>
 27 #include <linux/rwsem.h>                           27 #include <linux/rwsem.h>
 28 #include <net/xdp.h>                           << 
 29                                                    28 
 30 /* Protects offdevs, members of bpf_offload_ne     29 /* Protects offdevs, members of bpf_offload_netdev and offload members
 31  * of all progs.                                   30  * of all progs.
 32  * RTNL lock cannot be taken when holding this     31  * RTNL lock cannot be taken when holding this lock.
 33  */                                                32  */
 34 static DECLARE_RWSEM(bpf_devs_lock);               33 static DECLARE_RWSEM(bpf_devs_lock);
 35                                                    34 
 36 struct bpf_offload_dev {                           35 struct bpf_offload_dev {
 37         const struct bpf_prog_offload_ops *ops     36         const struct bpf_prog_offload_ops *ops;
 38         struct list_head netdevs;                  37         struct list_head netdevs;
 39         void *priv;                                38         void *priv;
 40 };                                                 39 };
 41                                                    40 
 42 struct bpf_offload_netdev {                        41 struct bpf_offload_netdev {
 43         struct rhash_head l;                       42         struct rhash_head l;
 44         struct net_device *netdev;                 43         struct net_device *netdev;
 45         struct bpf_offload_dev *offdev; /* NUL !!  44         struct bpf_offload_dev *offdev;
 46         struct list_head progs;                    45         struct list_head progs;
 47         struct list_head maps;                     46         struct list_head maps;
 48         struct list_head offdev_netdevs;           47         struct list_head offdev_netdevs;
 49 };                                                 48 };
 50                                                    49 
 51 static const struct rhashtable_params offdevs_     50 static const struct rhashtable_params offdevs_params = {
 52         .nelem_hint             = 4,               51         .nelem_hint             = 4,
 53         .key_len                = sizeof(struc     52         .key_len                = sizeof(struct net_device *),
 54         .key_offset             = offsetof(str     53         .key_offset             = offsetof(struct bpf_offload_netdev, netdev),
 55         .head_offset            = offsetof(str     54         .head_offset            = offsetof(struct bpf_offload_netdev, l),
 56         .automatic_shrinking    = true,            55         .automatic_shrinking    = true,
 57 };                                                 56 };
 58                                                    57 
 59 static struct rhashtable offdevs;                  58 static struct rhashtable offdevs;
                                                   >>  59 static bool offdevs_inited;
 60                                                    60 
 61 static int bpf_dev_offload_check(struct net_de     61 static int bpf_dev_offload_check(struct net_device *netdev)
 62 {                                                  62 {
 63         if (!netdev)                               63         if (!netdev)
 64                 return -EINVAL;                    64                 return -EINVAL;
 65         if (!netdev->netdev_ops->ndo_bpf)          65         if (!netdev->netdev_ops->ndo_bpf)
 66                 return -EOPNOTSUPP;                66                 return -EOPNOTSUPP;
 67         return 0;                                  67         return 0;
 68 }                                                  68 }
 69                                                    69 
 70 static struct bpf_offload_netdev *                 70 static struct bpf_offload_netdev *
 71 bpf_offload_find_netdev(struct net_device *net     71 bpf_offload_find_netdev(struct net_device *netdev)
 72 {                                                  72 {
 73         lockdep_assert_held(&bpf_devs_lock);       73         lockdep_assert_held(&bpf_devs_lock);
 74                                                    74 
                                                   >>  75         if (!offdevs_inited)
                                                   >>  76                 return NULL;
 75         return rhashtable_lookup_fast(&offdevs     77         return rhashtable_lookup_fast(&offdevs, &netdev, offdevs_params);
 76 }                                                  78 }
 77                                                    79 
 78 static int __bpf_offload_dev_netdev_register(s !!  80 int bpf_prog_offload_init(struct bpf_prog *prog, union bpf_attr *attr)
 79                                              s << 
 80 {                                                  81 {
 81         struct bpf_offload_netdev *ondev;          82         struct bpf_offload_netdev *ondev;
                                                   >>  83         struct bpf_prog_offload *offload;
 82         int err;                                   84         int err;
 83                                                    85 
 84         ondev = kzalloc(sizeof(*ondev), GFP_KE !!  86         if (attr->prog_type != BPF_PROG_TYPE_SCHED_CLS &&
 85         if (!ondev)                            !!  87             attr->prog_type != BPF_PROG_TYPE_XDP)
 86                 return -ENOMEM;                !!  88                 return -EINVAL;
 87                                                << 
 88         ondev->netdev = netdev;                << 
 89         ondev->offdev = offdev;                << 
 90         INIT_LIST_HEAD(&ondev->progs);         << 
 91         INIT_LIST_HEAD(&ondev->maps);          << 
 92                                                << 
 93         err = rhashtable_insert_fast(&offdevs, << 
 94         if (err) {                             << 
 95                 netdev_warn(netdev, "failed to << 
 96                 goto err_free;                 << 
 97         }                                      << 
 98                                                << 
 99         if (offdev)                            << 
100                 list_add(&ondev->offdev_netdev << 
101         return 0;                              << 
102                                                << 
103 err_free:                                      << 
104         kfree(ondev);                          << 
105         return err;                            << 
106 }                                              << 
107                                                << 
108 static void __bpf_prog_offload_destroy(struct  << 
109 {                                              << 
110         struct bpf_prog_offload *offload = pro << 
111                                                << 
112         if (offload->dev_state)                << 
113                 offload->offdev->ops->destroy( << 
114                                                << 
115         list_del_init(&offload->offloads);     << 
116         kfree(offload);                        << 
117         prog->aux->offload = NULL;             << 
118 }                                              << 
119                                                << 
120 static int bpf_map_offload_ndo(struct bpf_offl << 
121                                enum bpf_netdev << 
122 {                                              << 
123         struct netdev_bpf data = {};           << 
124         struct net_device *netdev;             << 
125                                                << 
126         ASSERT_RTNL();                         << 
127                                                << 
128         data.command = cmd;                    << 
129         data.offmap = offmap;                  << 
130         /* Caller must make sure netdev is val << 
131         netdev = offmap->netdev;               << 
132                                                << 
133         return netdev->netdev_ops->ndo_bpf(net << 
134 }                                              << 
135                                                << 
136 static void __bpf_map_offload_destroy(struct b << 
137 {                                              << 
138         WARN_ON(bpf_map_offload_ndo(offmap, BP << 
139         /* Make sure BPF_MAP_GET_NEXT_ID can't << 
140         bpf_map_free_id(&offmap->map);         << 
141         list_del_init(&offmap->offloads);      << 
142         offmap->netdev = NULL;                 << 
143 }                                              << 
144                                                << 
145 static void __bpf_offload_dev_netdev_unregiste << 
146                                                << 
147 {                                              << 
148         struct bpf_offload_netdev *ondev, *alt << 
149         struct bpf_offloaded_map *offmap, *mtm << 
150         struct bpf_prog_offload *offload, *ptm << 
151                                                << 
152         ASSERT_RTNL();                         << 
153                                                << 
154         ondev = rhashtable_lookup_fast(&offdev << 
155         if (WARN_ON(!ondev))                   << 
156                 return;                        << 
157                                                << 
158         WARN_ON(rhashtable_remove_fast(&offdev << 
159                                                << 
160         /* Try to move the objects to another  << 
161         if (offdev) {                          << 
162                 list_del(&ondev->offdev_netdev << 
163                 altdev = list_first_entry_or_n << 
164                                                << 
165                                                << 
166         }                                      << 
167                                                << 
168         if (altdev) {                          << 
169                 list_for_each_entry(offload, & << 
170                         offload->netdev = altd << 
171                 list_splice_init(&ondev->progs << 
172                                                << 
173                 list_for_each_entry(offmap, &o << 
174                         offmap->netdev = altde << 
175                 list_splice_init(&ondev->maps, << 
176         } else {                               << 
177                 list_for_each_entry_safe(offlo << 
178                         __bpf_prog_offload_des << 
179                 list_for_each_entry_safe(offma << 
180                         __bpf_map_offload_dest << 
181         }                                      << 
182                                                << 
183         WARN_ON(!list_empty(&ondev->progs));   << 
184         WARN_ON(!list_empty(&ondev->maps));    << 
185         kfree(ondev);                          << 
186 }                                              << 
187                                                    89 
188 static int __bpf_prog_dev_bound_init(struct bp !!  90         if (attr->prog_flags)
189 {                                              !!  91                 return -EINVAL;
190         struct bpf_offload_netdev *ondev;      << 
191         struct bpf_prog_offload *offload;      << 
192         int err;                               << 
193                                                    92 
194         offload = kzalloc(sizeof(*offload), GF     93         offload = kzalloc(sizeof(*offload), GFP_USER);
195         if (!offload)                              94         if (!offload)
196                 return -ENOMEM;                    95                 return -ENOMEM;
197                                                    96 
198         offload->prog = prog;                      97         offload->prog = prog;
199         offload->netdev = netdev;              << 
200                                                    98 
                                                   >>  99         offload->netdev = dev_get_by_index(current->nsproxy->net_ns,
                                                   >> 100                                            attr->prog_ifindex);
                                                   >> 101         err = bpf_dev_offload_check(offload->netdev);
                                                   >> 102         if (err)
                                                   >> 103                 goto err_maybe_put;
                                                   >> 104 
                                                   >> 105         down_write(&bpf_devs_lock);
201         ondev = bpf_offload_find_netdev(offloa    106         ondev = bpf_offload_find_netdev(offload->netdev);
202         /* When program is offloaded require p << 
203          * bpf_offload_netdev, avoid the one c << 
204          */                                    << 
205         if (bpf_prog_is_offloaded(prog->aux) & << 
206                 err = -EINVAL;                 << 
207                 goto err_free;                 << 
208         }                                      << 
209         if (!ondev) {                             107         if (!ondev) {
210                 /* When only binding to the de !! 108                 err = -EINVAL;
211                  * create an entry in the hash !! 109                 goto err_unlock;
212                  */                            << 
213                 err = __bpf_offload_dev_netdev << 
214                 if (err)                       << 
215                         goto err_free;         << 
216                 ondev = bpf_offload_find_netde << 
217         }                                         110         }
218         offload->offdev = ondev->offdev;          111         offload->offdev = ondev->offdev;
219         prog->aux->offload = offload;             112         prog->aux->offload = offload;
220         list_add_tail(&offload->offloads, &ond    113         list_add_tail(&offload->offloads, &ondev->progs);
221                                                !! 114         dev_put(offload->netdev);
222         return 0;                              << 
223 err_free:                                      << 
224         kfree(offload);                        << 
225         return err;                            << 
226 }                                              << 
227                                                << 
228 int bpf_prog_dev_bound_init(struct bpf_prog *p << 
229 {                                              << 
230         struct net_device *netdev;             << 
231         int err;                               << 
232                                                << 
233         if (attr->prog_type != BPF_PROG_TYPE_S << 
234             attr->prog_type != BPF_PROG_TYPE_X << 
235                 return -EINVAL;                << 
236                                                << 
237         if (attr->prog_flags & ~(BPF_F_XDP_DEV << 
238                 return -EINVAL;                << 
239                                                << 
240         /* Frags are allowed only if program i << 
241          * if it is requesting bpf offload.    << 
242          */                                    << 
243         if (attr->prog_flags & BPF_F_XDP_HAS_F << 
244             !(attr->prog_flags & BPF_F_XDP_DEV << 
245                 return -EINVAL;                << 
246                                                << 
247         if (attr->prog_type == BPF_PROG_TYPE_S << 
248             attr->prog_flags & BPF_F_XDP_DEV_B << 
249                 return -EINVAL;                << 
250                                                << 
251         netdev = dev_get_by_index(current->nsp << 
252         if (!netdev)                           << 
253                 return -EINVAL;                << 
254                                                << 
255         err = bpf_dev_offload_check(netdev);   << 
256         if (err)                               << 
257                 goto out;                      << 
258                                                << 
259         prog->aux->offload_requested = !(attr- << 
260                                                << 
261         down_write(&bpf_devs_lock);            << 
262         err = __bpf_prog_dev_bound_init(prog,  << 
263         up_write(&bpf_devs_lock);                 115         up_write(&bpf_devs_lock);
264                                                   116 
265 out:                                           !! 117         return 0;
266         dev_put(netdev);                       !! 118 err_unlock:
267         return err;                            << 
268 }                                              << 
269                                                << 
270 int bpf_prog_dev_bound_inherit(struct bpf_prog << 
271 {                                              << 
272         int err;                               << 
273                                                << 
274         if (!bpf_prog_is_dev_bound(old_prog->a << 
275                 return 0;                      << 
276                                                << 
277         if (bpf_prog_is_offloaded(old_prog->au << 
278                 return -EINVAL;                << 
279                                                << 
280         new_prog->aux->dev_bound = old_prog->a << 
281         new_prog->aux->offload_requested = old << 
282                                                << 
283         down_write(&bpf_devs_lock);            << 
284         if (!old_prog->aux->offload) {         << 
285                 err = -EINVAL;                 << 
286                 goto out;                      << 
287         }                                      << 
288                                                << 
289         err = __bpf_prog_dev_bound_init(new_pr << 
290                                                << 
291 out:                                           << 
292         up_write(&bpf_devs_lock);                 119         up_write(&bpf_devs_lock);
                                                   >> 120 err_maybe_put:
                                                   >> 121         if (offload->netdev)
                                                   >> 122                 dev_put(offload->netdev);
                                                   >> 123         kfree(offload);
293         return err;                               124         return err;
294 }                                                 125 }
295                                                   126 
296 int bpf_prog_offload_verifier_prep(struct bpf_    127 int bpf_prog_offload_verifier_prep(struct bpf_prog *prog)
297 {                                                 128 {
298         struct bpf_prog_offload *offload;         129         struct bpf_prog_offload *offload;
299         int ret = -ENODEV;                        130         int ret = -ENODEV;
300                                                   131 
301         down_read(&bpf_devs_lock);                132         down_read(&bpf_devs_lock);
302         offload = prog->aux->offload;             133         offload = prog->aux->offload;
303         if (offload) {                            134         if (offload) {
304                 ret = offload->offdev->ops->pr    135                 ret = offload->offdev->ops->prepare(prog);
305                 offload->dev_state = !ret;        136                 offload->dev_state = !ret;
306         }                                         137         }
307         up_read(&bpf_devs_lock);                  138         up_read(&bpf_devs_lock);
308                                                   139 
309         return ret;                               140         return ret;
310 }                                                 141 }
311                                                   142 
312 int bpf_prog_offload_verify_insn(struct bpf_ve    143 int bpf_prog_offload_verify_insn(struct bpf_verifier_env *env,
313                                  int insn_idx,    144                                  int insn_idx, int prev_insn_idx)
314 {                                                 145 {
315         struct bpf_prog_offload *offload;         146         struct bpf_prog_offload *offload;
316         int ret = -ENODEV;                        147         int ret = -ENODEV;
317                                                   148 
318         down_read(&bpf_devs_lock);                149         down_read(&bpf_devs_lock);
319         offload = env->prog->aux->offload;        150         offload = env->prog->aux->offload;
320         if (offload)                              151         if (offload)
321                 ret = offload->offdev->ops->in    152                 ret = offload->offdev->ops->insn_hook(env, insn_idx,
322                                                   153                                                       prev_insn_idx);
323         up_read(&bpf_devs_lock);                  154         up_read(&bpf_devs_lock);
324                                                   155 
325         return ret;                               156         return ret;
326 }                                                 157 }
327                                                   158 
328 int bpf_prog_offload_finalize(struct bpf_verif    159 int bpf_prog_offload_finalize(struct bpf_verifier_env *env)
329 {                                                 160 {
330         struct bpf_prog_offload *offload;         161         struct bpf_prog_offload *offload;
331         int ret = -ENODEV;                        162         int ret = -ENODEV;
332                                                   163 
333         down_read(&bpf_devs_lock);                164         down_read(&bpf_devs_lock);
334         offload = env->prog->aux->offload;        165         offload = env->prog->aux->offload;
335         if (offload) {                            166         if (offload) {
336                 if (offload->offdev->ops->fina    167                 if (offload->offdev->ops->finalize)
337                         ret = offload->offdev-    168                         ret = offload->offdev->ops->finalize(env);
338                 else                              169                 else
339                         ret = 0;                  170                         ret = 0;
340         }                                         171         }
341         up_read(&bpf_devs_lock);                  172         up_read(&bpf_devs_lock);
342                                                   173 
343         return ret;                               174         return ret;
344 }                                                 175 }
345                                                   176 
346 void                                              177 void
347 bpf_prog_offload_replace_insn(struct bpf_verif    178 bpf_prog_offload_replace_insn(struct bpf_verifier_env *env, u32 off,
348                               struct bpf_insn     179                               struct bpf_insn *insn)
349 {                                                 180 {
350         const struct bpf_prog_offload_ops *ops    181         const struct bpf_prog_offload_ops *ops;
351         struct bpf_prog_offload *offload;         182         struct bpf_prog_offload *offload;
352         int ret = -EOPNOTSUPP;                    183         int ret = -EOPNOTSUPP;
353                                                   184 
354         down_read(&bpf_devs_lock);                185         down_read(&bpf_devs_lock);
355         offload = env->prog->aux->offload;        186         offload = env->prog->aux->offload;
356         if (offload) {                            187         if (offload) {
357                 ops = offload->offdev->ops;       188                 ops = offload->offdev->ops;
358                 if (!offload->opt_failed && op    189                 if (!offload->opt_failed && ops->replace_insn)
359                         ret = ops->replace_ins    190                         ret = ops->replace_insn(env, off, insn);
360                 offload->opt_failed |= ret;       191                 offload->opt_failed |= ret;
361         }                                         192         }
362         up_read(&bpf_devs_lock);                  193         up_read(&bpf_devs_lock);
363 }                                                 194 }
364                                                   195 
365 void                                              196 void
366 bpf_prog_offload_remove_insns(struct bpf_verif    197 bpf_prog_offload_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt)
367 {                                                 198 {
368         struct bpf_prog_offload *offload;         199         struct bpf_prog_offload *offload;
369         int ret = -EOPNOTSUPP;                    200         int ret = -EOPNOTSUPP;
370                                                   201 
371         down_read(&bpf_devs_lock);                202         down_read(&bpf_devs_lock);
372         offload = env->prog->aux->offload;        203         offload = env->prog->aux->offload;
373         if (offload) {                            204         if (offload) {
374                 if (!offload->opt_failed && of    205                 if (!offload->opt_failed && offload->offdev->ops->remove_insns)
375                         ret = offload->offdev-    206                         ret = offload->offdev->ops->remove_insns(env, off, cnt);
376                 offload->opt_failed |= ret;       207                 offload->opt_failed |= ret;
377         }                                         208         }
378         up_read(&bpf_devs_lock);                  209         up_read(&bpf_devs_lock);
379 }                                                 210 }
380                                                   211 
381 void bpf_prog_dev_bound_destroy(struct bpf_pro !! 212 static void __bpf_prog_offload_destroy(struct bpf_prog *prog)
382 {                                                 213 {
383         struct bpf_offload_netdev *ondev;      !! 214         struct bpf_prog_offload *offload = prog->aux->offload;
384         struct net_device *netdev;             << 
385                                                   215 
386         rtnl_lock();                           !! 216         if (offload->dev_state)
387         down_write(&bpf_devs_lock);            !! 217                 offload->offdev->ops->destroy(prog);
388         if (prog->aux->offload) {              << 
389                 list_del_init(&prog->aux->offl << 
390                                                   218 
391                 netdev = prog->aux->offload->n !! 219         /* Make sure BPF_PROG_GET_NEXT_ID can't find this dead program */
392                 __bpf_prog_offload_destroy(pro !! 220         bpf_prog_free_id(prog, true);
393                                                   221 
394                 ondev = bpf_offload_find_netde !! 222         list_del_init(&offload->offloads);
395                 if (!ondev->offdev && list_emp !! 223         kfree(offload);
396                         __bpf_offload_dev_netd !! 224         prog->aux->offload = NULL;
397         }                                      !! 225 }
                                                   >> 226 
                                                   >> 227 void bpf_prog_offload_destroy(struct bpf_prog *prog)
                                                   >> 228 {
                                                   >> 229         down_write(&bpf_devs_lock);
                                                   >> 230         if (prog->aux->offload)
                                                   >> 231                 __bpf_prog_offload_destroy(prog);
398         up_write(&bpf_devs_lock);                 232         up_write(&bpf_devs_lock);
399         rtnl_unlock();                         << 
400 }                                                 233 }
401                                                   234 
402 static int bpf_prog_offload_translate(struct b    235 static int bpf_prog_offload_translate(struct bpf_prog *prog)
403 {                                                 236 {
404         struct bpf_prog_offload *offload;         237         struct bpf_prog_offload *offload;
405         int ret = -ENODEV;                        238         int ret = -ENODEV;
406                                                   239 
407         down_read(&bpf_devs_lock);                240         down_read(&bpf_devs_lock);
408         offload = prog->aux->offload;             241         offload = prog->aux->offload;
409         if (offload)                              242         if (offload)
410                 ret = offload->offdev->ops->tr    243                 ret = offload->offdev->ops->translate(prog);
411         up_read(&bpf_devs_lock);                  244         up_read(&bpf_devs_lock);
412                                                   245 
413         return ret;                               246         return ret;
414 }                                                 247 }
415                                                   248 
416 static unsigned int bpf_prog_warn_on_exec(cons    249 static unsigned int bpf_prog_warn_on_exec(const void *ctx,
417                                           cons    250                                           const struct bpf_insn *insn)
418 {                                                 251 {
419         WARN(1, "attempt to execute device eBP    252         WARN(1, "attempt to execute device eBPF program on the host!");
420         return 0;                                 253         return 0;
421 }                                                 254 }
422                                                   255 
423 int bpf_prog_offload_compile(struct bpf_prog *    256 int bpf_prog_offload_compile(struct bpf_prog *prog)
424 {                                                 257 {
425         prog->bpf_func = bpf_prog_warn_on_exec    258         prog->bpf_func = bpf_prog_warn_on_exec;
426                                                   259 
427         return bpf_prog_offload_translate(prog    260         return bpf_prog_offload_translate(prog);
428 }                                                 261 }
429                                                   262 
430 struct ns_get_path_bpf_prog_args {                263 struct ns_get_path_bpf_prog_args {
431         struct bpf_prog *prog;                    264         struct bpf_prog *prog;
432         struct bpf_prog_info *info;               265         struct bpf_prog_info *info;
433 };                                                266 };
434                                                   267 
435 static struct ns_common *bpf_prog_offload_info    268 static struct ns_common *bpf_prog_offload_info_fill_ns(void *private_data)
436 {                                                 269 {
437         struct ns_get_path_bpf_prog_args *args    270         struct ns_get_path_bpf_prog_args *args = private_data;
438         struct bpf_prog_aux *aux = args->prog-    271         struct bpf_prog_aux *aux = args->prog->aux;
439         struct ns_common *ns;                     272         struct ns_common *ns;
440         struct net *net;                          273         struct net *net;
441                                                   274 
442         rtnl_lock();                              275         rtnl_lock();
443         down_read(&bpf_devs_lock);                276         down_read(&bpf_devs_lock);
444                                                   277 
445         if (aux->offload) {                       278         if (aux->offload) {
446                 args->info->ifindex = aux->off    279                 args->info->ifindex = aux->offload->netdev->ifindex;
447                 net = dev_net(aux->offload->ne    280                 net = dev_net(aux->offload->netdev);
448                 get_net(net);                     281                 get_net(net);
449                 ns = &net->ns;                    282                 ns = &net->ns;
450         } else {                                  283         } else {
451                 args->info->ifindex = 0;          284                 args->info->ifindex = 0;
452                 ns = NULL;                        285                 ns = NULL;
453         }                                         286         }
454                                                   287 
455         up_read(&bpf_devs_lock);                  288         up_read(&bpf_devs_lock);
456         rtnl_unlock();                            289         rtnl_unlock();
457                                                   290 
458         return ns;                                291         return ns;
459 }                                                 292 }
460                                                   293 
461 int bpf_prog_offload_info_fill(struct bpf_prog    294 int bpf_prog_offload_info_fill(struct bpf_prog_info *info,
462                                struct bpf_prog    295                                struct bpf_prog *prog)
463 {                                                 296 {
464         struct ns_get_path_bpf_prog_args args     297         struct ns_get_path_bpf_prog_args args = {
465                 .prog   = prog,                   298                 .prog   = prog,
466                 .info   = info,                   299                 .info   = info,
467         };                                        300         };
468         struct bpf_prog_aux *aux = prog->aux;     301         struct bpf_prog_aux *aux = prog->aux;
469         struct inode *ns_inode;                   302         struct inode *ns_inode;
470         struct path ns_path;                      303         struct path ns_path;
471         char __user *uinsns;                      304         char __user *uinsns;
472         int res;                                  305         int res;
473         u32 ulen;                                 306         u32 ulen;
474                                                   307 
475         res = ns_get_path_cb(&ns_path, bpf_pro    308         res = ns_get_path_cb(&ns_path, bpf_prog_offload_info_fill_ns, &args);
476         if (res) {                                309         if (res) {
477                 if (!info->ifindex)               310                 if (!info->ifindex)
478                         return -ENODEV;           311                         return -ENODEV;
479                 return res;                       312                 return res;
480         }                                         313         }
481                                                   314 
482         down_read(&bpf_devs_lock);                315         down_read(&bpf_devs_lock);
483                                                   316 
484         if (!aux->offload) {                      317         if (!aux->offload) {
485                 up_read(&bpf_devs_lock);          318                 up_read(&bpf_devs_lock);
486                 return -ENODEV;                   319                 return -ENODEV;
487         }                                         320         }
488                                                   321 
489         ulen = info->jited_prog_len;              322         ulen = info->jited_prog_len;
490         info->jited_prog_len = aux->offload->j    323         info->jited_prog_len = aux->offload->jited_len;
491         if (info->jited_prog_len && ulen) {       324         if (info->jited_prog_len && ulen) {
492                 uinsns = u64_to_user_ptr(info-    325                 uinsns = u64_to_user_ptr(info->jited_prog_insns);
493                 ulen = min_t(u32, info->jited_    326                 ulen = min_t(u32, info->jited_prog_len, ulen);
494                 if (copy_to_user(uinsns, aux->    327                 if (copy_to_user(uinsns, aux->offload->jited_image, ulen)) {
495                         up_read(&bpf_devs_lock    328                         up_read(&bpf_devs_lock);
496                         return -EFAULT;           329                         return -EFAULT;
497                 }                                 330                 }
498         }                                         331         }
499                                                   332 
500         up_read(&bpf_devs_lock);                  333         up_read(&bpf_devs_lock);
501                                                   334 
502         ns_inode = ns_path.dentry->d_inode;       335         ns_inode = ns_path.dentry->d_inode;
503         info->netns_dev = new_encode_dev(ns_in    336         info->netns_dev = new_encode_dev(ns_inode->i_sb->s_dev);
504         info->netns_ino = ns_inode->i_ino;        337         info->netns_ino = ns_inode->i_ino;
505         path_put(&ns_path);                       338         path_put(&ns_path);
506                                                   339 
507         return 0;                                 340         return 0;
508 }                                                 341 }
509                                                   342 
510 const struct bpf_prog_ops bpf_offload_prog_ops    343 const struct bpf_prog_ops bpf_offload_prog_ops = {
511 };                                                344 };
512                                                   345 
                                                   >> 346 static int bpf_map_offload_ndo(struct bpf_offloaded_map *offmap,
                                                   >> 347                                enum bpf_netdev_command cmd)
                                                   >> 348 {
                                                   >> 349         struct netdev_bpf data = {};
                                                   >> 350         struct net_device *netdev;
                                                   >> 351 
                                                   >> 352         ASSERT_RTNL();
                                                   >> 353 
                                                   >> 354         data.command = cmd;
                                                   >> 355         data.offmap = offmap;
                                                   >> 356         /* Caller must make sure netdev is valid */
                                                   >> 357         netdev = offmap->netdev;
                                                   >> 358 
                                                   >> 359         return netdev->netdev_ops->ndo_bpf(netdev, &data);
                                                   >> 360 }
                                                   >> 361 
513 struct bpf_map *bpf_map_offload_map_alloc(unio    362 struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr)
514 {                                                 363 {
515         struct net *net = current->nsproxy->ne    364         struct net *net = current->nsproxy->net_ns;
516         struct bpf_offload_netdev *ondev;         365         struct bpf_offload_netdev *ondev;
517         struct bpf_offloaded_map *offmap;         366         struct bpf_offloaded_map *offmap;
518         int err;                                  367         int err;
519                                                   368 
520         if (!capable(CAP_SYS_ADMIN))              369         if (!capable(CAP_SYS_ADMIN))
521                 return ERR_PTR(-EPERM);           370                 return ERR_PTR(-EPERM);
522         if (attr->map_type != BPF_MAP_TYPE_ARR    371         if (attr->map_type != BPF_MAP_TYPE_ARRAY &&
523             attr->map_type != BPF_MAP_TYPE_HAS    372             attr->map_type != BPF_MAP_TYPE_HASH)
524                 return ERR_PTR(-EINVAL);          373                 return ERR_PTR(-EINVAL);
525                                                   374 
526         offmap = bpf_map_area_alloc(sizeof(*of !! 375         offmap = kzalloc(sizeof(*offmap), GFP_USER);
527         if (!offmap)                              376         if (!offmap)
528                 return ERR_PTR(-ENOMEM);          377                 return ERR_PTR(-ENOMEM);
529                                                   378 
530         bpf_map_init_from_attr(&offmap->map, a    379         bpf_map_init_from_attr(&offmap->map, attr);
531                                                   380 
532         rtnl_lock();                              381         rtnl_lock();
533         down_write(&bpf_devs_lock);               382         down_write(&bpf_devs_lock);
534         offmap->netdev = __dev_get_by_index(ne    383         offmap->netdev = __dev_get_by_index(net, attr->map_ifindex);
535         err = bpf_dev_offload_check(offmap->ne    384         err = bpf_dev_offload_check(offmap->netdev);
536         if (err)                                  385         if (err)
537                 goto err_unlock;                  386                 goto err_unlock;
538                                                   387 
539         ondev = bpf_offload_find_netdev(offmap    388         ondev = bpf_offload_find_netdev(offmap->netdev);
540         if (!ondev) {                             389         if (!ondev) {
541                 err = -EINVAL;                    390                 err = -EINVAL;
542                 goto err_unlock;                  391                 goto err_unlock;
543         }                                         392         }
544                                                   393 
545         err = bpf_map_offload_ndo(offmap, BPF_    394         err = bpf_map_offload_ndo(offmap, BPF_OFFLOAD_MAP_ALLOC);
546         if (err)                                  395         if (err)
547                 goto err_unlock;                  396                 goto err_unlock;
548                                                   397 
549         list_add_tail(&offmap->offloads, &onde    398         list_add_tail(&offmap->offloads, &ondev->maps);
550         up_write(&bpf_devs_lock);                 399         up_write(&bpf_devs_lock);
551         rtnl_unlock();                            400         rtnl_unlock();
552                                                   401 
553         return &offmap->map;                      402         return &offmap->map;
554                                                   403 
555 err_unlock:                                       404 err_unlock:
556         up_write(&bpf_devs_lock);                 405         up_write(&bpf_devs_lock);
557         rtnl_unlock();                            406         rtnl_unlock();
558         bpf_map_area_free(offmap);             !! 407         kfree(offmap);
559         return ERR_PTR(err);                      408         return ERR_PTR(err);
560 }                                                 409 }
561                                                   410 
                                                   >> 411 static void __bpf_map_offload_destroy(struct bpf_offloaded_map *offmap)
                                                   >> 412 {
                                                   >> 413         WARN_ON(bpf_map_offload_ndo(offmap, BPF_OFFLOAD_MAP_FREE));
                                                   >> 414         /* Make sure BPF_MAP_GET_NEXT_ID can't find this dead map */
                                                   >> 415         bpf_map_free_id(&offmap->map, true);
                                                   >> 416         list_del_init(&offmap->offloads);
                                                   >> 417         offmap->netdev = NULL;
                                                   >> 418 }
                                                   >> 419 
562 void bpf_map_offload_map_free(struct bpf_map *    420 void bpf_map_offload_map_free(struct bpf_map *map)
563 {                                                 421 {
564         struct bpf_offloaded_map *offmap = map    422         struct bpf_offloaded_map *offmap = map_to_offmap(map);
565                                                   423 
566         rtnl_lock();                              424         rtnl_lock();
567         down_write(&bpf_devs_lock);               425         down_write(&bpf_devs_lock);
568         if (offmap->netdev)                       426         if (offmap->netdev)
569                 __bpf_map_offload_destroy(offm    427                 __bpf_map_offload_destroy(offmap);
570         up_write(&bpf_devs_lock);                 428         up_write(&bpf_devs_lock);
571         rtnl_unlock();                            429         rtnl_unlock();
572                                                   430 
573         bpf_map_area_free(offmap);             !! 431         kfree(offmap);
574 }                                              << 
575                                                << 
576 u64 bpf_map_offload_map_mem_usage(const struct << 
577 {                                              << 
578         /* The memory dynamically allocated in << 
579         return sizeof(struct bpf_offloaded_map << 
580 }                                                 432 }
581                                                   433 
582 int bpf_map_offload_lookup_elem(struct bpf_map    434 int bpf_map_offload_lookup_elem(struct bpf_map *map, void *key, void *value)
583 {                                                 435 {
584         struct bpf_offloaded_map *offmap = map    436         struct bpf_offloaded_map *offmap = map_to_offmap(map);
585         int ret = -ENODEV;                        437         int ret = -ENODEV;
586                                                   438 
587         down_read(&bpf_devs_lock);                439         down_read(&bpf_devs_lock);
588         if (offmap->netdev)                       440         if (offmap->netdev)
589                 ret = offmap->dev_ops->map_loo    441                 ret = offmap->dev_ops->map_lookup_elem(offmap, key, value);
590         up_read(&bpf_devs_lock);                  442         up_read(&bpf_devs_lock);
591                                                   443 
592         return ret;                               444         return ret;
593 }                                                 445 }
594                                                   446 
595 int bpf_map_offload_update_elem(struct bpf_map    447 int bpf_map_offload_update_elem(struct bpf_map *map,
596                                 void *key, voi    448                                 void *key, void *value, u64 flags)
597 {                                                 449 {
598         struct bpf_offloaded_map *offmap = map    450         struct bpf_offloaded_map *offmap = map_to_offmap(map);
599         int ret = -ENODEV;                        451         int ret = -ENODEV;
600                                                   452 
601         if (unlikely(flags > BPF_EXIST))          453         if (unlikely(flags > BPF_EXIST))
602                 return -EINVAL;                   454                 return -EINVAL;
603                                                   455 
604         down_read(&bpf_devs_lock);                456         down_read(&bpf_devs_lock);
605         if (offmap->netdev)                       457         if (offmap->netdev)
606                 ret = offmap->dev_ops->map_upd    458                 ret = offmap->dev_ops->map_update_elem(offmap, key, value,
607                                                   459                                                        flags);
608         up_read(&bpf_devs_lock);                  460         up_read(&bpf_devs_lock);
609                                                   461 
610         return ret;                               462         return ret;
611 }                                                 463 }
612                                                   464 
613 int bpf_map_offload_delete_elem(struct bpf_map    465 int bpf_map_offload_delete_elem(struct bpf_map *map, void *key)
614 {                                                 466 {
615         struct bpf_offloaded_map *offmap = map    467         struct bpf_offloaded_map *offmap = map_to_offmap(map);
616         int ret = -ENODEV;                        468         int ret = -ENODEV;
617                                                   469 
618         down_read(&bpf_devs_lock);                470         down_read(&bpf_devs_lock);
619         if (offmap->netdev)                       471         if (offmap->netdev)
620                 ret = offmap->dev_ops->map_del    472                 ret = offmap->dev_ops->map_delete_elem(offmap, key);
621         up_read(&bpf_devs_lock);                  473         up_read(&bpf_devs_lock);
622                                                   474 
623         return ret;                               475         return ret;
624 }                                                 476 }
625                                                   477 
626 int bpf_map_offload_get_next_key(struct bpf_ma    478 int bpf_map_offload_get_next_key(struct bpf_map *map, void *key, void *next_key)
627 {                                                 479 {
628         struct bpf_offloaded_map *offmap = map    480         struct bpf_offloaded_map *offmap = map_to_offmap(map);
629         int ret = -ENODEV;                        481         int ret = -ENODEV;
630                                                   482 
631         down_read(&bpf_devs_lock);                483         down_read(&bpf_devs_lock);
632         if (offmap->netdev)                       484         if (offmap->netdev)
633                 ret = offmap->dev_ops->map_get    485                 ret = offmap->dev_ops->map_get_next_key(offmap, key, next_key);
634         up_read(&bpf_devs_lock);                  486         up_read(&bpf_devs_lock);
635                                                   487 
636         return ret;                               488         return ret;
637 }                                                 489 }
638                                                   490 
639 struct ns_get_path_bpf_map_args {                 491 struct ns_get_path_bpf_map_args {
640         struct bpf_offloaded_map *offmap;         492         struct bpf_offloaded_map *offmap;
641         struct bpf_map_info *info;                493         struct bpf_map_info *info;
642 };                                                494 };
643                                                   495 
644 static struct ns_common *bpf_map_offload_info_    496 static struct ns_common *bpf_map_offload_info_fill_ns(void *private_data)
645 {                                                 497 {
646         struct ns_get_path_bpf_map_args *args     498         struct ns_get_path_bpf_map_args *args = private_data;
647         struct ns_common *ns;                     499         struct ns_common *ns;
648         struct net *net;                          500         struct net *net;
649                                                   501 
650         rtnl_lock();                              502         rtnl_lock();
651         down_read(&bpf_devs_lock);                503         down_read(&bpf_devs_lock);
652                                                   504 
653         if (args->offmap->netdev) {               505         if (args->offmap->netdev) {
654                 args->info->ifindex = args->of    506                 args->info->ifindex = args->offmap->netdev->ifindex;
655                 net = dev_net(args->offmap->ne    507                 net = dev_net(args->offmap->netdev);
656                 get_net(net);                     508                 get_net(net);
657                 ns = &net->ns;                    509                 ns = &net->ns;
658         } else {                                  510         } else {
659                 args->info->ifindex = 0;          511                 args->info->ifindex = 0;
660                 ns = NULL;                        512                 ns = NULL;
661         }                                         513         }
662                                                   514 
663         up_read(&bpf_devs_lock);                  515         up_read(&bpf_devs_lock);
664         rtnl_unlock();                            516         rtnl_unlock();
665                                                   517 
666         return ns;                                518         return ns;
667 }                                                 519 }
668                                                   520 
669 int bpf_map_offload_info_fill(struct bpf_map_i    521 int bpf_map_offload_info_fill(struct bpf_map_info *info, struct bpf_map *map)
670 {                                                 522 {
671         struct ns_get_path_bpf_map_args args =    523         struct ns_get_path_bpf_map_args args = {
672                 .offmap = map_to_offmap(map),     524                 .offmap = map_to_offmap(map),
673                 .info   = info,                   525                 .info   = info,
674         };                                        526         };
675         struct inode *ns_inode;                   527         struct inode *ns_inode;
676         struct path ns_path;                      528         struct path ns_path;
677         int res;                                  529         int res;
678                                                   530 
679         res = ns_get_path_cb(&ns_path, bpf_map    531         res = ns_get_path_cb(&ns_path, bpf_map_offload_info_fill_ns, &args);
680         if (res) {                                532         if (res) {
681                 if (!info->ifindex)               533                 if (!info->ifindex)
682                         return -ENODEV;           534                         return -ENODEV;
683                 return res;                       535                 return res;
684         }                                         536         }
685                                                   537 
686         ns_inode = ns_path.dentry->d_inode;       538         ns_inode = ns_path.dentry->d_inode;
687         info->netns_dev = new_encode_dev(ns_in    539         info->netns_dev = new_encode_dev(ns_inode->i_sb->s_dev);
688         info->netns_ino = ns_inode->i_ino;        540         info->netns_ino = ns_inode->i_ino;
689         path_put(&ns_path);                       541         path_put(&ns_path);
690                                                   542 
691         return 0;                                 543         return 0;
692 }                                                 544 }
693                                                   545 
694 static bool __bpf_offload_dev_match(struct bpf    546 static bool __bpf_offload_dev_match(struct bpf_prog *prog,
695                                     struct net    547                                     struct net_device *netdev)
696 {                                                 548 {
697         struct bpf_offload_netdev *ondev1, *on    549         struct bpf_offload_netdev *ondev1, *ondev2;
698         struct bpf_prog_offload *offload;         550         struct bpf_prog_offload *offload;
699                                                   551 
700         if (!bpf_prog_is_dev_bound(prog->aux))    552         if (!bpf_prog_is_dev_bound(prog->aux))
701                 return false;                     553                 return false;
702                                                   554 
703         offload = prog->aux->offload;             555         offload = prog->aux->offload;
704         if (!offload)                             556         if (!offload)
705                 return false;                     557                 return false;
706         if (offload->netdev == netdev)            558         if (offload->netdev == netdev)
707                 return true;                      559                 return true;
708                                                   560 
709         ondev1 = bpf_offload_find_netdev(offlo    561         ondev1 = bpf_offload_find_netdev(offload->netdev);
710         ondev2 = bpf_offload_find_netdev(netde    562         ondev2 = bpf_offload_find_netdev(netdev);
711                                                   563 
712         return ondev1 && ondev2 && ondev1->off    564         return ondev1 && ondev2 && ondev1->offdev == ondev2->offdev;
713 }                                                 565 }
714                                                   566 
715 bool bpf_offload_dev_match(struct bpf_prog *pr    567 bool bpf_offload_dev_match(struct bpf_prog *prog, struct net_device *netdev)
716 {                                                 568 {
717         bool ret;                                 569         bool ret;
718                                                   570 
719         down_read(&bpf_devs_lock);                571         down_read(&bpf_devs_lock);
720         ret = __bpf_offload_dev_match(prog, ne    572         ret = __bpf_offload_dev_match(prog, netdev);
721         up_read(&bpf_devs_lock);                  573         up_read(&bpf_devs_lock);
722                                                   574 
723         return ret;                               575         return ret;
724 }                                                 576 }
725 EXPORT_SYMBOL_GPL(bpf_offload_dev_match);         577 EXPORT_SYMBOL_GPL(bpf_offload_dev_match);
726                                                   578 
727 bool bpf_prog_dev_bound_match(const struct bpf << 
728 {                                              << 
729         bool ret;                              << 
730                                                << 
731         if (bpf_prog_is_offloaded(lhs->aux) != << 
732                 return false;                  << 
733                                                << 
734         down_read(&bpf_devs_lock);             << 
735         ret = lhs->aux->offload && rhs->aux->o << 
736               lhs->aux->offload->netdev &&     << 
737               lhs->aux->offload->netdev == rhs << 
738         up_read(&bpf_devs_lock);               << 
739                                                << 
740         return ret;                            << 
741 }                                              << 
742                                                << 
743 bool bpf_offload_prog_map_match(struct bpf_pro    579 bool bpf_offload_prog_map_match(struct bpf_prog *prog, struct bpf_map *map)
744 {                                                 580 {
745         struct bpf_offloaded_map *offmap;         581         struct bpf_offloaded_map *offmap;
746         bool ret;                                 582         bool ret;
747                                                   583 
748         if (!bpf_map_is_offloaded(map))        !! 584         if (!bpf_map_is_dev_bound(map))
749                 return bpf_map_offload_neutral    585                 return bpf_map_offload_neutral(map);
750         offmap = map_to_offmap(map);              586         offmap = map_to_offmap(map);
751                                                   587 
752         down_read(&bpf_devs_lock);                588         down_read(&bpf_devs_lock);
753         ret = __bpf_offload_dev_match(prog, of    589         ret = __bpf_offload_dev_match(prog, offmap->netdev);
754         up_read(&bpf_devs_lock);                  590         up_read(&bpf_devs_lock);
755                                                   591 
756         return ret;                               592         return ret;
757 }                                                 593 }
758                                                   594 
759 int bpf_offload_dev_netdev_register(struct bpf    595 int bpf_offload_dev_netdev_register(struct bpf_offload_dev *offdev,
760                                     struct net    596                                     struct net_device *netdev)
761 {                                                 597 {
                                                   >> 598         struct bpf_offload_netdev *ondev;
762         int err;                                  599         int err;
763                                                   600 
                                                   >> 601         ondev = kzalloc(sizeof(*ondev), GFP_KERNEL);
                                                   >> 602         if (!ondev)
                                                   >> 603                 return -ENOMEM;
                                                   >> 604 
                                                   >> 605         ondev->netdev = netdev;
                                                   >> 606         ondev->offdev = offdev;
                                                   >> 607         INIT_LIST_HEAD(&ondev->progs);
                                                   >> 608         INIT_LIST_HEAD(&ondev->maps);
                                                   >> 609 
764         down_write(&bpf_devs_lock);               610         down_write(&bpf_devs_lock);
765         err = __bpf_offload_dev_netdev_registe !! 611         err = rhashtable_insert_fast(&offdevs, &ondev->l, offdevs_params);
                                                   >> 612         if (err) {
                                                   >> 613                 netdev_warn(netdev, "failed to register for BPF offload\n");
                                                   >> 614                 goto err_unlock_free;
                                                   >> 615         }
                                                   >> 616 
                                                   >> 617         list_add(&ondev->offdev_netdevs, &offdev->netdevs);
766         up_write(&bpf_devs_lock);                 618         up_write(&bpf_devs_lock);
                                                   >> 619         return 0;
                                                   >> 620 
                                                   >> 621 err_unlock_free:
                                                   >> 622         up_write(&bpf_devs_lock);
                                                   >> 623         kfree(ondev);
767         return err;                               624         return err;
768 }                                                 625 }
769 EXPORT_SYMBOL_GPL(bpf_offload_dev_netdev_regis    626 EXPORT_SYMBOL_GPL(bpf_offload_dev_netdev_register);
770                                                   627 
771 void bpf_offload_dev_netdev_unregister(struct     628 void bpf_offload_dev_netdev_unregister(struct bpf_offload_dev *offdev,
772                                        struct     629                                        struct net_device *netdev)
773 {                                                 630 {
                                                   >> 631         struct bpf_offload_netdev *ondev, *altdev;
                                                   >> 632         struct bpf_offloaded_map *offmap, *mtmp;
                                                   >> 633         struct bpf_prog_offload *offload, *ptmp;
                                                   >> 634 
                                                   >> 635         ASSERT_RTNL();
                                                   >> 636 
774         down_write(&bpf_devs_lock);               637         down_write(&bpf_devs_lock);
775         __bpf_offload_dev_netdev_unregister(of !! 638         ondev = rhashtable_lookup_fast(&offdevs, &netdev, offdevs_params);
                                                   >> 639         if (WARN_ON(!ondev))
                                                   >> 640                 goto unlock;
                                                   >> 641 
                                                   >> 642         WARN_ON(rhashtable_remove_fast(&offdevs, &ondev->l, offdevs_params));
                                                   >> 643         list_del(&ondev->offdev_netdevs);
                                                   >> 644 
                                                   >> 645         /* Try to move the objects to another netdev of the device */
                                                   >> 646         altdev = list_first_entry_or_null(&offdev->netdevs,
                                                   >> 647                                           struct bpf_offload_netdev,
                                                   >> 648                                           offdev_netdevs);
                                                   >> 649         if (altdev) {
                                                   >> 650                 list_for_each_entry(offload, &ondev->progs, offloads)
                                                   >> 651                         offload->netdev = altdev->netdev;
                                                   >> 652                 list_splice_init(&ondev->progs, &altdev->progs);
                                                   >> 653 
                                                   >> 654                 list_for_each_entry(offmap, &ondev->maps, offloads)
                                                   >> 655                         offmap->netdev = altdev->netdev;
                                                   >> 656                 list_splice_init(&ondev->maps, &altdev->maps);
                                                   >> 657         } else {
                                                   >> 658                 list_for_each_entry_safe(offload, ptmp, &ondev->progs, offloads)
                                                   >> 659                         __bpf_prog_offload_destroy(offload->prog);
                                                   >> 660                 list_for_each_entry_safe(offmap, mtmp, &ondev->maps, offloads)
                                                   >> 661                         __bpf_map_offload_destroy(offmap);
                                                   >> 662         }
                                                   >> 663 
                                                   >> 664         WARN_ON(!list_empty(&ondev->progs));
                                                   >> 665         WARN_ON(!list_empty(&ondev->maps));
                                                   >> 666         kfree(ondev);
                                                   >> 667 unlock:
776         up_write(&bpf_devs_lock);                 668         up_write(&bpf_devs_lock);
777 }                                                 669 }
778 EXPORT_SYMBOL_GPL(bpf_offload_dev_netdev_unreg    670 EXPORT_SYMBOL_GPL(bpf_offload_dev_netdev_unregister);
779                                                   671 
780 struct bpf_offload_dev *                          672 struct bpf_offload_dev *
781 bpf_offload_dev_create(const struct bpf_prog_o    673 bpf_offload_dev_create(const struct bpf_prog_offload_ops *ops, void *priv)
782 {                                                 674 {
783         struct bpf_offload_dev *offdev;           675         struct bpf_offload_dev *offdev;
                                                   >> 676         int err;
                                                   >> 677 
                                                   >> 678         down_write(&bpf_devs_lock);
                                                   >> 679         if (!offdevs_inited) {
                                                   >> 680                 err = rhashtable_init(&offdevs, &offdevs_params);
                                                   >> 681                 if (err) {
                                                   >> 682                         up_write(&bpf_devs_lock);
                                                   >> 683                         return ERR_PTR(err);
                                                   >> 684                 }
                                                   >> 685                 offdevs_inited = true;
                                                   >> 686         }
                                                   >> 687         up_write(&bpf_devs_lock);
784                                                   688 
785         offdev = kzalloc(sizeof(*offdev), GFP_    689         offdev = kzalloc(sizeof(*offdev), GFP_KERNEL);
786         if (!offdev)                              690         if (!offdev)
787                 return ERR_PTR(-ENOMEM);          691                 return ERR_PTR(-ENOMEM);
788                                                   692 
789         offdev->ops = ops;                        693         offdev->ops = ops;
790         offdev->priv = priv;                      694         offdev->priv = priv;
791         INIT_LIST_HEAD(&offdev->netdevs);         695         INIT_LIST_HEAD(&offdev->netdevs);
792                                                   696 
793         return offdev;                            697         return offdev;
794 }                                                 698 }
795 EXPORT_SYMBOL_GPL(bpf_offload_dev_create);        699 EXPORT_SYMBOL_GPL(bpf_offload_dev_create);
796                                                   700 
797 void bpf_offload_dev_destroy(struct bpf_offloa    701 void bpf_offload_dev_destroy(struct bpf_offload_dev *offdev)
798 {                                                 702 {
799         WARN_ON(!list_empty(&offdev->netdevs))    703         WARN_ON(!list_empty(&offdev->netdevs));
800         kfree(offdev);                            704         kfree(offdev);
801 }                                                 705 }
802 EXPORT_SYMBOL_GPL(bpf_offload_dev_destroy);       706 EXPORT_SYMBOL_GPL(bpf_offload_dev_destroy);
803                                                   707 
804 void *bpf_offload_dev_priv(struct bpf_offload_    708 void *bpf_offload_dev_priv(struct bpf_offload_dev *offdev)
805 {                                                 709 {
806         return offdev->priv;                      710         return offdev->priv;
807 }                                                 711 }
808 EXPORT_SYMBOL_GPL(bpf_offload_dev_priv);          712 EXPORT_SYMBOL_GPL(bpf_offload_dev_priv);
809                                                << 
810 void bpf_dev_bound_netdev_unregister(struct ne << 
811 {                                              << 
812         struct bpf_offload_netdev *ondev;      << 
813                                                << 
814         ASSERT_RTNL();                         << 
815                                                << 
816         down_write(&bpf_devs_lock);            << 
817         ondev = bpf_offload_find_netdev(dev);  << 
818         if (ondev && !ondev->offdev)           << 
819                 __bpf_offload_dev_netdev_unreg << 
820         up_write(&bpf_devs_lock);              << 
821 }                                              << 
822                                                << 
823 int bpf_dev_bound_kfunc_check(struct bpf_verif << 
824                               struct bpf_prog_ << 
825 {                                              << 
826         if (!bpf_prog_is_dev_bound(prog_aux))  << 
827                 bpf_log(log, "metadata kfuncs  << 
828                 return -EINVAL;                << 
829         }                                      << 
830                                                << 
831         if (bpf_prog_is_offloaded(prog_aux)) { << 
832                 bpf_log(log, "metadata kfuncs  << 
833                 return -EINVAL;                << 
834         }                                      << 
835                                                << 
836         return 0;                              << 
837 }                                              << 
838                                                << 
839 void *bpf_dev_bound_resolve_kfunc(struct bpf_p << 
840 {                                              << 
841         const struct xdp_metadata_ops *ops;    << 
842         void *p = NULL;                        << 
843                                                << 
844         /* We don't hold bpf_devs_lock while r << 
845          * kfuncs and can race with the unregi << 
846          * We rely on bpf_dev_bound_match() ch << 
847          * to render this program unusable.    << 
848          */                                    << 
849         down_read(&bpf_devs_lock);             << 
850         if (!prog->aux->offload)               << 
851                 goto out;                      << 
852                                                << 
853         ops = prog->aux->offload->netdev->xdp_ << 
854         if (!ops)                              << 
855                 goto out;                      << 
856                                                << 
857 #define XDP_METADATA_KFUNC(name, _, __, xmo) \ << 
858         if (func_id == bpf_xdp_metadata_kfunc_ << 
859         XDP_METADATA_KFUNC_xxx                 << 
860 #undef XDP_METADATA_KFUNC                      << 
861                                                << 
862 out:                                           << 
863         up_read(&bpf_devs_lock);               << 
864                                                << 
865         return p;                              << 
866 }                                              << 
867                                                << 
868 static int __init bpf_offload_init(void)       << 
869 {                                              << 
870         return rhashtable_init(&offdevs, &offd << 
871 }                                              << 
872                                                << 
873 core_initcall(bpf_offload_init);               << 
874                                                   713 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php