bpf_struct_ops.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2019 Facebook */
  3. #include <linux/bpf.h>
  4. #include <linux/bpf_verifier.h>
  5. #include <linux/btf.h>
  6. #include <linux/filter.h>
  7. #include <linux/slab.h>
  8. #include <linux/numa.h>
  9. #include <linux/seq_file.h>
  10. #include <linux/refcount.h>
  11. #include <linux/mutex.h>
  12. #include <linux/btf_ids.h>
  13. #include <linux/rcupdate_wait.h>
  14. #include <linux/poll.h>
  15. struct bpf_struct_ops_value {
  16. struct bpf_struct_ops_common_value common;
  17. char data[] ____cacheline_aligned_in_smp;
  18. };
  19. #define MAX_TRAMP_IMAGE_PAGES 8
  20. struct bpf_struct_ops_map {
  21. struct bpf_map map;
  22. struct rcu_head rcu;
  23. const struct bpf_struct_ops_desc *st_ops_desc;
  24. /* protect map_update */
  25. struct mutex lock;
  26. /* link has all the bpf_links that is populated
  27. * to the func ptr of the kernel's struct
  28. * (in kvalue.data).
  29. */
  30. struct bpf_link **links;
  31. /* ksyms for bpf trampolines */
  32. struct bpf_ksym **ksyms;
  33. u32 funcs_cnt;
  34. u32 image_pages_cnt;
  35. /* image_pages is an array of pages that has all the trampolines
  36. * that stores the func args before calling the bpf_prog.
  37. */
  38. void *image_pages[MAX_TRAMP_IMAGE_PAGES];
  39. /* The owner moduler's btf. */
  40. struct btf *btf;
  41. /* uvalue->data stores the kernel struct
  42. * (e.g. tcp_congestion_ops) that is more useful
  43. * to userspace than the kvalue. For example,
  44. * the bpf_prog's id is stored instead of the kernel
  45. * address of a func ptr.
  46. */
  47. struct bpf_struct_ops_value *uvalue;
  48. /* kvalue.data stores the actual kernel's struct
  49. * (e.g. tcp_congestion_ops) that will be
  50. * registered to the kernel subsystem.
  51. */
  52. struct bpf_struct_ops_value kvalue;
  53. };
  54. struct bpf_struct_ops_link {
  55. struct bpf_link link;
  56. struct bpf_map __rcu *map;
  57. wait_queue_head_t wait_hup;
  58. };
  59. static DEFINE_MUTEX(update_mutex);
  60. #define VALUE_PREFIX "bpf_struct_ops_"
  61. #define VALUE_PREFIX_LEN (sizeof(VALUE_PREFIX) - 1)
  62. const struct bpf_verifier_ops bpf_struct_ops_verifier_ops = {
  63. };
  64. const struct bpf_prog_ops bpf_struct_ops_prog_ops = {
  65. #ifdef CONFIG_NET
  66. .test_run = bpf_struct_ops_test_run,
  67. #endif
  68. };
  69. BTF_ID_LIST(st_ops_ids)
  70. BTF_ID(struct, module)
  71. BTF_ID(struct, bpf_struct_ops_common_value)
  72. enum {
  73. IDX_MODULE_ID,
  74. IDX_ST_OPS_COMMON_VALUE_ID,
  75. };
  76. extern struct btf *btf_vmlinux;
  77. static bool is_valid_value_type(struct btf *btf, s32 value_id,
  78. const struct btf_type *type,
  79. const char *value_name)
  80. {
  81. const struct btf_type *common_value_type;
  82. const struct btf_member *member;
  83. const struct btf_type *vt, *mt;
  84. vt = btf_type_by_id(btf, value_id);
  85. if (btf_vlen(vt) != 2) {
  86. pr_warn("The number of %s's members should be 2, but we get %d\n",
  87. value_name, btf_vlen(vt));
  88. return false;
  89. }
  90. member = btf_type_member(vt);
  91. mt = btf_type_by_id(btf, member->type);
  92. common_value_type = btf_type_by_id(btf_vmlinux,
  93. st_ops_ids[IDX_ST_OPS_COMMON_VALUE_ID]);
  94. if (mt != common_value_type) {
  95. pr_warn("The first member of %s should be bpf_struct_ops_common_value\n",
  96. value_name);
  97. return false;
  98. }
  99. member++;
  100. mt = btf_type_by_id(btf, member->type);
  101. if (mt != type) {
  102. pr_warn("The second member of %s should be %s\n",
  103. value_name, btf_name_by_offset(btf, type->name_off));
  104. return false;
  105. }
  106. return true;
  107. }
  108. static void *bpf_struct_ops_image_alloc(void)
  109. {
  110. void *image;
  111. int err;
  112. err = bpf_jit_charge_modmem(PAGE_SIZE);
  113. if (err)
  114. return ERR_PTR(err);
  115. image = arch_alloc_bpf_trampoline(PAGE_SIZE);
  116. if (!image) {
  117. bpf_jit_uncharge_modmem(PAGE_SIZE);
  118. return ERR_PTR(-ENOMEM);
  119. }
  120. return image;
  121. }
  122. void bpf_struct_ops_image_free(void *image)
  123. {
  124. if (image) {
  125. arch_free_bpf_trampoline(image, PAGE_SIZE);
  126. bpf_jit_uncharge_modmem(PAGE_SIZE);
  127. }
  128. }
  129. #define MAYBE_NULL_SUFFIX "__nullable"
  130. /* Prepare argument info for every nullable argument of a member of a
  131. * struct_ops type.
  132. *
  133. * Initialize a struct bpf_struct_ops_arg_info according to type info of
  134. * the arguments of a stub function. (Check kCFI for more information about
  135. * stub functions.)
  136. *
  137. * Each member in the struct_ops type has a struct bpf_struct_ops_arg_info
  138. * to provide an array of struct bpf_ctx_arg_aux, which in turn provides
  139. * the information that used by the verifier to check the arguments of the
  140. * BPF struct_ops program assigned to the member. Here, we only care about
  141. * the arguments that are marked as __nullable.
  142. *
  143. * The array of struct bpf_ctx_arg_aux is eventually assigned to
  144. * prog->aux->ctx_arg_info of BPF struct_ops programs and passed to the
  145. * verifier. (See check_struct_ops_btf_id())
  146. *
  147. * arg_info->info will be the list of struct bpf_ctx_arg_aux if success. If
  148. * fails, it will be kept untouched.
  149. */
  150. static int prepare_arg_info(struct btf *btf,
  151. const char *st_ops_name,
  152. const char *member_name,
  153. const struct btf_type *func_proto, void *stub_func_addr,
  154. struct bpf_struct_ops_arg_info *arg_info)
  155. {
  156. const struct btf_type *stub_func_proto, *pointed_type;
  157. const struct btf_param *stub_args, *args;
  158. struct bpf_ctx_arg_aux *info, *info_buf;
  159. u32 nargs, arg_no, info_cnt = 0;
  160. char ksym[KSYM_SYMBOL_LEN];
  161. const char *stub_fname;
  162. s32 stub_func_id;
  163. u32 arg_btf_id;
  164. int offset;
  165. stub_fname = kallsyms_lookup((unsigned long)stub_func_addr, NULL, NULL, NULL, ksym);
  166. if (!stub_fname) {
  167. pr_warn("Cannot find the stub function name for the %s in struct %s\n",
  168. member_name, st_ops_name);
  169. return -ENOENT;
  170. }
  171. stub_func_id = btf_find_by_name_kind(btf, stub_fname, BTF_KIND_FUNC);
  172. if (stub_func_id < 0) {
  173. pr_warn("Cannot find the stub function %s in btf\n", stub_fname);
  174. return -ENOENT;
  175. }
  176. stub_func_proto = btf_type_by_id(btf, stub_func_id);
  177. stub_func_proto = btf_type_by_id(btf, stub_func_proto->type);
  178. /* Check if the number of arguments of the stub function is the same
  179. * as the number of arguments of the function pointer.
  180. */
  181. nargs = btf_type_vlen(func_proto);
  182. if (nargs != btf_type_vlen(stub_func_proto)) {
  183. pr_warn("the number of arguments of the stub function %s does not match the number of arguments of the member %s of struct %s\n",
  184. stub_fname, member_name, st_ops_name);
  185. return -EINVAL;
  186. }
  187. if (!nargs)
  188. return 0;
  189. args = btf_params(func_proto);
  190. stub_args = btf_params(stub_func_proto);
  191. info_buf = kcalloc(nargs, sizeof(*info_buf), GFP_KERNEL);
  192. if (!info_buf)
  193. return -ENOMEM;
  194. /* Prepare info for every nullable argument */
  195. info = info_buf;
  196. for (arg_no = 0; arg_no < nargs; arg_no++) {
  197. /* Skip arguments that is not suffixed with
  198. * "__nullable".
  199. */
  200. if (!btf_param_match_suffix(btf, &stub_args[arg_no],
  201. MAYBE_NULL_SUFFIX))
  202. continue;
  203. /* Should be a pointer to struct */
  204. pointed_type = btf_type_resolve_ptr(btf,
  205. args[arg_no].type,
  206. &arg_btf_id);
  207. if (!pointed_type ||
  208. !btf_type_is_struct(pointed_type)) {
  209. pr_warn("stub function %s has %s tagging to an unsupported type\n",
  210. stub_fname, MAYBE_NULL_SUFFIX);
  211. goto err_out;
  212. }
  213. offset = btf_ctx_arg_offset(btf, func_proto, arg_no);
  214. if (offset < 0) {
  215. pr_warn("stub function %s has an invalid trampoline ctx offset for arg#%u\n",
  216. stub_fname, arg_no);
  217. goto err_out;
  218. }
  219. if (args[arg_no].type != stub_args[arg_no].type) {
  220. pr_warn("arg#%u type in stub function %s does not match with its original func_proto\n",
  221. arg_no, stub_fname);
  222. goto err_out;
  223. }
  224. /* Fill the information of the new argument */
  225. info->reg_type =
  226. PTR_TRUSTED | PTR_TO_BTF_ID | PTR_MAYBE_NULL;
  227. info->btf_id = arg_btf_id;
  228. info->btf = btf;
  229. info->offset = offset;
  230. info++;
  231. info_cnt++;
  232. }
  233. if (info_cnt) {
  234. arg_info->info = info_buf;
  235. arg_info->cnt = info_cnt;
  236. } else {
  237. kfree(info_buf);
  238. }
  239. return 0;
  240. err_out:
  241. kfree(info_buf);
  242. return -EINVAL;
  243. }
  244. /* Clean up the arg_info in a struct bpf_struct_ops_desc. */
  245. void bpf_struct_ops_desc_release(struct bpf_struct_ops_desc *st_ops_desc)
  246. {
  247. struct bpf_struct_ops_arg_info *arg_info;
  248. int i;
  249. arg_info = st_ops_desc->arg_info;
  250. for (i = 0; i < btf_type_vlen(st_ops_desc->type); i++)
  251. kfree(arg_info[i].info);
  252. kfree(arg_info);
  253. }
  254. static bool is_module_member(const struct btf *btf, u32 id)
  255. {
  256. const struct btf_type *t;
  257. t = btf_type_resolve_ptr(btf, id, NULL);
  258. if (!t)
  259. return false;
  260. if (!__btf_type_is_struct(t) && !btf_type_is_fwd(t))
  261. return false;
  262. return !strcmp(btf_name_by_offset(btf, t->name_off), "module");
  263. }
  264. int bpf_struct_ops_supported(const struct bpf_struct_ops *st_ops, u32 moff)
  265. {
  266. void *func_ptr = *(void **)(st_ops->cfi_stubs + moff);
  267. return func_ptr ? 0 : -ENOTSUPP;
  268. }
  269. int bpf_struct_ops_desc_init(struct bpf_struct_ops_desc *st_ops_desc,
  270. struct btf *btf,
  271. struct bpf_verifier_log *log)
  272. {
  273. struct bpf_struct_ops *st_ops = st_ops_desc->st_ops;
  274. struct bpf_struct_ops_arg_info *arg_info;
  275. const struct btf_member *member;
  276. const struct btf_type *t;
  277. s32 type_id, value_id;
  278. char value_name[128];
  279. const char *mname;
  280. int i, err;
  281. if (strlen(st_ops->name) + VALUE_PREFIX_LEN >=
  282. sizeof(value_name)) {
  283. pr_warn("struct_ops name %s is too long\n",
  284. st_ops->name);
  285. return -EINVAL;
  286. }
  287. sprintf(value_name, "%s%s", VALUE_PREFIX, st_ops->name);
  288. if (!st_ops->cfi_stubs) {
  289. pr_warn("struct_ops for %s has no cfi_stubs\n", st_ops->name);
  290. return -EINVAL;
  291. }
  292. type_id = btf_find_by_name_kind(btf, st_ops->name,
  293. BTF_KIND_STRUCT);
  294. if (type_id < 0) {
  295. pr_warn("Cannot find struct %s in %s\n",
  296. st_ops->name, btf_get_name(btf));
  297. return -EINVAL;
  298. }
  299. t = btf_type_by_id(btf, type_id);
  300. if (btf_type_vlen(t) > BPF_STRUCT_OPS_MAX_NR_MEMBERS) {
  301. pr_warn("Cannot support #%u members in struct %s\n",
  302. btf_type_vlen(t), st_ops->name);
  303. return -EINVAL;
  304. }
  305. value_id = btf_find_by_name_kind(btf, value_name,
  306. BTF_KIND_STRUCT);
  307. if (value_id < 0) {
  308. pr_warn("Cannot find struct %s in %s\n",
  309. value_name, btf_get_name(btf));
  310. return -EINVAL;
  311. }
  312. if (!is_valid_value_type(btf, value_id, t, value_name))
  313. return -EINVAL;
  314. arg_info = kcalloc(btf_type_vlen(t), sizeof(*arg_info),
  315. GFP_KERNEL);
  316. if (!arg_info)
  317. return -ENOMEM;
  318. st_ops_desc->arg_info = arg_info;
  319. st_ops_desc->type = t;
  320. st_ops_desc->type_id = type_id;
  321. st_ops_desc->value_id = value_id;
  322. st_ops_desc->value_type = btf_type_by_id(btf, value_id);
  323. for_each_member(i, t, member) {
  324. const struct btf_type *func_proto;
  325. void **stub_func_addr;
  326. u32 moff;
  327. moff = __btf_member_bit_offset(t, member) / 8;
  328. mname = btf_name_by_offset(btf, member->name_off);
  329. if (!*mname) {
  330. pr_warn("anon member in struct %s is not supported\n",
  331. st_ops->name);
  332. err = -EOPNOTSUPP;
  333. goto errout;
  334. }
  335. if (__btf_member_bitfield_size(t, member)) {
  336. pr_warn("bit field member %s in struct %s is not supported\n",
  337. mname, st_ops->name);
  338. err = -EOPNOTSUPP;
  339. goto errout;
  340. }
  341. if (!st_ops_ids[IDX_MODULE_ID] && is_module_member(btf, member->type)) {
  342. pr_warn("'struct module' btf id not found. Is CONFIG_MODULES enabled? bpf_struct_ops '%s' needs module support.\n",
  343. st_ops->name);
  344. err = -EOPNOTSUPP;
  345. goto errout;
  346. }
  347. func_proto = btf_type_resolve_func_ptr(btf,
  348. member->type,
  349. NULL);
  350. /* The member is not a function pointer or
  351. * the function pointer is not supported.
  352. */
  353. if (!func_proto || bpf_struct_ops_supported(st_ops, moff))
  354. continue;
  355. if (btf_distill_func_proto(log, btf,
  356. func_proto, mname,
  357. &st_ops->func_models[i])) {
  358. pr_warn("Error in parsing func ptr %s in struct %s\n",
  359. mname, st_ops->name);
  360. err = -EINVAL;
  361. goto errout;
  362. }
  363. stub_func_addr = *(void **)(st_ops->cfi_stubs + moff);
  364. err = prepare_arg_info(btf, st_ops->name, mname,
  365. func_proto, stub_func_addr,
  366. arg_info + i);
  367. if (err)
  368. goto errout;
  369. }
  370. if (st_ops->init(btf)) {
  371. pr_warn("Error in init bpf_struct_ops %s\n",
  372. st_ops->name);
  373. err = -EINVAL;
  374. goto errout;
  375. }
  376. return 0;
  377. errout:
  378. bpf_struct_ops_desc_release(st_ops_desc);
  379. return err;
  380. }
  381. static int bpf_struct_ops_map_get_next_key(struct bpf_map *map, void *key,
  382. void *next_key)
  383. {
  384. if (key && *(u32 *)key == 0)
  385. return -ENOENT;
  386. *(u32 *)next_key = 0;
  387. return 0;
  388. }
  389. int bpf_struct_ops_map_sys_lookup_elem(struct bpf_map *map, void *key,
  390. void *value)
  391. {
  392. struct bpf_struct_ops_map *st_map = (struct bpf_struct_ops_map *)map;
  393. struct bpf_struct_ops_value *uvalue, *kvalue;
  394. enum bpf_struct_ops_state state;
  395. s64 refcnt;
  396. if (unlikely(*(u32 *)key != 0))
  397. return -ENOENT;
  398. kvalue = &st_map->kvalue;
  399. /* Pair with smp_store_release() during map_update */
  400. state = smp_load_acquire(&kvalue->common.state);
  401. if (state == BPF_STRUCT_OPS_STATE_INIT) {
  402. memset(value, 0, map->value_size);
  403. return 0;
  404. }
  405. /* No lock is needed. state and refcnt do not need
  406. * to be updated together under atomic context.
  407. */
  408. uvalue = value;
  409. memcpy(uvalue, st_map->uvalue, map->value_size);
  410. uvalue->common.state = state;
  411. /* This value offers the user space a general estimate of how
  412. * many sockets are still utilizing this struct_ops for TCP
  413. * congestion control. The number might not be exact, but it
  414. * should sufficiently meet our present goals.
  415. */
  416. refcnt = atomic64_read(&map->refcnt) - atomic64_read(&map->usercnt);
  417. refcount_set(&uvalue->common.refcnt, max_t(s64, refcnt, 0));
  418. return 0;
  419. }
  420. static void *bpf_struct_ops_map_lookup_elem(struct bpf_map *map, void *key)
  421. {
  422. return ERR_PTR(-EINVAL);
  423. }
  424. static void bpf_struct_ops_map_put_progs(struct bpf_struct_ops_map *st_map)
  425. {
  426. u32 i;
  427. for (i = 0; i < st_map->funcs_cnt; i++) {
  428. if (!st_map->links[i])
  429. break;
  430. bpf_link_put(st_map->links[i]);
  431. st_map->links[i] = NULL;
  432. }
  433. }
  434. static void bpf_struct_ops_map_free_image(struct bpf_struct_ops_map *st_map)
  435. {
  436. int i;
  437. for (i = 0; i < st_map->image_pages_cnt; i++)
  438. bpf_struct_ops_image_free(st_map->image_pages[i]);
  439. st_map->image_pages_cnt = 0;
  440. }
  441. static int check_zero_holes(const struct btf *btf, const struct btf_type *t, void *data)
  442. {
  443. const struct btf_member *member;
  444. u32 i, moff, msize, prev_mend = 0;
  445. const struct btf_type *mtype;
  446. for_each_member(i, t, member) {
  447. moff = __btf_member_bit_offset(t, member) / 8;
  448. if (moff > prev_mend &&
  449. memchr_inv(data + prev_mend, 0, moff - prev_mend))
  450. return -EINVAL;
  451. mtype = btf_type_by_id(btf, member->type);
  452. mtype = btf_resolve_size(btf, mtype, &msize);
  453. if (IS_ERR(mtype))
  454. return PTR_ERR(mtype);
  455. prev_mend = moff + msize;
  456. }
  457. if (t->size > prev_mend &&
  458. memchr_inv(data + prev_mend, 0, t->size - prev_mend))
  459. return -EINVAL;
  460. return 0;
  461. }
  462. static void bpf_struct_ops_link_release(struct bpf_link *link)
  463. {
  464. }
  465. static void bpf_struct_ops_link_dealloc(struct bpf_link *link)
  466. {
  467. struct bpf_tramp_link *tlink = container_of(link, struct bpf_tramp_link, link);
  468. kfree(tlink);
  469. }
  470. const struct bpf_link_ops bpf_struct_ops_link_lops = {
  471. .release = bpf_struct_ops_link_release,
  472. .dealloc = bpf_struct_ops_link_dealloc,
  473. };
  474. int bpf_struct_ops_prepare_trampoline(struct bpf_tramp_links *tlinks,
  475. struct bpf_tramp_link *link,
  476. const struct btf_func_model *model,
  477. void *stub_func,
  478. void **_image, u32 *_image_off,
  479. bool allow_alloc)
  480. {
  481. u32 image_off = *_image_off, flags = BPF_TRAMP_F_INDIRECT;
  482. void *image = *_image;
  483. int size;
  484. tlinks[BPF_TRAMP_FENTRY].links[0] = link;
  485. tlinks[BPF_TRAMP_FENTRY].nr_links = 1;
  486. if (model->ret_size > 0)
  487. flags |= BPF_TRAMP_F_RET_FENTRY_RET;
  488. size = arch_bpf_trampoline_size(model, flags, tlinks, stub_func);
  489. if (size <= 0)
  490. return size ? : -EFAULT;
  491. /* Allocate image buffer if necessary */
  492. if (!image || size > PAGE_SIZE - image_off) {
  493. if (!allow_alloc)
  494. return -E2BIG;
  495. image = bpf_struct_ops_image_alloc();
  496. if (IS_ERR(image))
  497. return PTR_ERR(image);
  498. image_off = 0;
  499. }
  500. size = arch_prepare_bpf_trampoline(NULL, image + image_off,
  501. image + image_off + size,
  502. model, flags, tlinks, stub_func);
  503. if (size <= 0) {
  504. if (image != *_image)
  505. bpf_struct_ops_image_free(image);
  506. return size ? : -EFAULT;
  507. }
  508. *_image = image;
  509. *_image_off = image_off + size;
  510. return 0;
  511. }
  512. static void bpf_struct_ops_ksym_init(const char *tname, const char *mname,
  513. void *image, unsigned int size,
  514. struct bpf_ksym *ksym)
  515. {
  516. snprintf(ksym->name, KSYM_NAME_LEN, "bpf__%s_%s", tname, mname);
  517. INIT_LIST_HEAD_RCU(&ksym->lnode);
  518. bpf_image_ksym_init(image, size, ksym);
  519. }
  520. static void bpf_struct_ops_map_add_ksyms(struct bpf_struct_ops_map *st_map)
  521. {
  522. u32 i;
  523. for (i = 0; i < st_map->funcs_cnt; i++) {
  524. if (!st_map->ksyms[i])
  525. break;
  526. bpf_image_ksym_add(st_map->ksyms[i]);
  527. }
  528. }
  529. static void bpf_struct_ops_map_del_ksyms(struct bpf_struct_ops_map *st_map)
  530. {
  531. u32 i;
  532. for (i = 0; i < st_map->funcs_cnt; i++) {
  533. if (!st_map->ksyms[i])
  534. break;
  535. bpf_image_ksym_del(st_map->ksyms[i]);
  536. }
  537. }
  538. static void bpf_struct_ops_map_free_ksyms(struct bpf_struct_ops_map *st_map)
  539. {
  540. u32 i;
  541. for (i = 0; i < st_map->funcs_cnt; i++) {
  542. if (!st_map->ksyms[i])
  543. break;
  544. kfree(st_map->ksyms[i]);
  545. st_map->ksyms[i] = NULL;
  546. }
  547. }
  548. static long bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key,
  549. void *value, u64 flags)
  550. {
  551. struct bpf_struct_ops_map *st_map = (struct bpf_struct_ops_map *)map;
  552. const struct bpf_struct_ops_desc *st_ops_desc = st_map->st_ops_desc;
  553. const struct bpf_struct_ops *st_ops = st_ops_desc->st_ops;
  554. struct bpf_struct_ops_value *uvalue, *kvalue;
  555. const struct btf_type *module_type;
  556. const struct btf_member *member;
  557. const struct btf_type *t = st_ops_desc->type;
  558. struct bpf_tramp_links *tlinks;
  559. void *udata, *kdata;
  560. int prog_fd, err;
  561. u32 i, trampoline_start, image_off = 0;
  562. void *cur_image = NULL, *image = NULL;
  563. struct bpf_link **plink;
  564. struct bpf_ksym **pksym;
  565. const char *tname, *mname;
  566. if (flags)
  567. return -EINVAL;
  568. if (*(u32 *)key != 0)
  569. return -E2BIG;
  570. err = check_zero_holes(st_map->btf, st_ops_desc->value_type, value);
  571. if (err)
  572. return err;
  573. uvalue = value;
  574. err = check_zero_holes(st_map->btf, t, uvalue->data);
  575. if (err)
  576. return err;
  577. if (uvalue->common.state || refcount_read(&uvalue->common.refcnt))
  578. return -EINVAL;
  579. tlinks = kcalloc(BPF_TRAMP_MAX, sizeof(*tlinks), GFP_KERNEL);
  580. if (!tlinks)
  581. return -ENOMEM;
  582. uvalue = (struct bpf_struct_ops_value *)st_map->uvalue;
  583. kvalue = (struct bpf_struct_ops_value *)&st_map->kvalue;
  584. mutex_lock(&st_map->lock);
  585. if (kvalue->common.state != BPF_STRUCT_OPS_STATE_INIT) {
  586. err = -EBUSY;
  587. goto unlock;
  588. }
  589. memcpy(uvalue, value, map->value_size);
  590. udata = &uvalue->data;
  591. kdata = &kvalue->data;
  592. plink = st_map->links;
  593. pksym = st_map->ksyms;
  594. tname = btf_name_by_offset(st_map->btf, t->name_off);
  595. module_type = btf_type_by_id(btf_vmlinux, st_ops_ids[IDX_MODULE_ID]);
  596. for_each_member(i, t, member) {
  597. const struct btf_type *mtype, *ptype;
  598. struct bpf_prog *prog;
  599. struct bpf_tramp_link *link;
  600. struct bpf_ksym *ksym;
  601. u32 moff;
  602. moff = __btf_member_bit_offset(t, member) / 8;
  603. mname = btf_name_by_offset(st_map->btf, member->name_off);
  604. ptype = btf_type_resolve_ptr(st_map->btf, member->type, NULL);
  605. if (ptype == module_type) {
  606. if (*(void **)(udata + moff))
  607. goto reset_unlock;
  608. *(void **)(kdata + moff) = BPF_MODULE_OWNER;
  609. continue;
  610. }
  611. err = st_ops->init_member(t, member, kdata, udata);
  612. if (err < 0)
  613. goto reset_unlock;
  614. /* The ->init_member() has handled this member */
  615. if (err > 0)
  616. continue;
  617. /* If st_ops->init_member does not handle it,
  618. * we will only handle func ptrs and zero-ed members
  619. * here. Reject everything else.
  620. */
  621. /* All non func ptr member must be 0 */
  622. if (!ptype || !btf_type_is_func_proto(ptype)) {
  623. u32 msize;
  624. mtype = btf_type_by_id(st_map->btf, member->type);
  625. mtype = btf_resolve_size(st_map->btf, mtype, &msize);
  626. if (IS_ERR(mtype)) {
  627. err = PTR_ERR(mtype);
  628. goto reset_unlock;
  629. }
  630. if (memchr_inv(udata + moff, 0, msize)) {
  631. err = -EINVAL;
  632. goto reset_unlock;
  633. }
  634. continue;
  635. }
  636. prog_fd = (int)(*(unsigned long *)(udata + moff));
  637. /* Similar check as the attr->attach_prog_fd */
  638. if (!prog_fd)
  639. continue;
  640. prog = bpf_prog_get(prog_fd);
  641. if (IS_ERR(prog)) {
  642. err = PTR_ERR(prog);
  643. goto reset_unlock;
  644. }
  645. if (prog->type != BPF_PROG_TYPE_STRUCT_OPS ||
  646. prog->aux->attach_btf_id != st_ops_desc->type_id ||
  647. prog->expected_attach_type != i) {
  648. bpf_prog_put(prog);
  649. err = -EINVAL;
  650. goto reset_unlock;
  651. }
  652. link = kzalloc(sizeof(*link), GFP_USER);
  653. if (!link) {
  654. bpf_prog_put(prog);
  655. err = -ENOMEM;
  656. goto reset_unlock;
  657. }
  658. bpf_link_init(&link->link, BPF_LINK_TYPE_STRUCT_OPS,
  659. &bpf_struct_ops_link_lops, prog);
  660. *plink++ = &link->link;
  661. ksym = kzalloc(sizeof(*ksym), GFP_USER);
  662. if (!ksym) {
  663. err = -ENOMEM;
  664. goto reset_unlock;
  665. }
  666. *pksym++ = ksym;
  667. trampoline_start = image_off;
  668. err = bpf_struct_ops_prepare_trampoline(tlinks, link,
  669. &st_ops->func_models[i],
  670. *(void **)(st_ops->cfi_stubs + moff),
  671. &image, &image_off,
  672. st_map->image_pages_cnt < MAX_TRAMP_IMAGE_PAGES);
  673. if (err)
  674. goto reset_unlock;
  675. if (cur_image != image) {
  676. st_map->image_pages[st_map->image_pages_cnt++] = image;
  677. cur_image = image;
  678. trampoline_start = 0;
  679. }
  680. *(void **)(kdata + moff) = image + trampoline_start + cfi_get_offset();
  681. /* put prog_id to udata */
  682. *(unsigned long *)(udata + moff) = prog->aux->id;
  683. /* init ksym for this trampoline */
  684. bpf_struct_ops_ksym_init(tname, mname,
  685. image + trampoline_start,
  686. image_off - trampoline_start,
  687. ksym);
  688. }
  689. if (st_ops->validate) {
  690. err = st_ops->validate(kdata);
  691. if (err)
  692. goto reset_unlock;
  693. }
  694. for (i = 0; i < st_map->image_pages_cnt; i++) {
  695. err = arch_protect_bpf_trampoline(st_map->image_pages[i],
  696. PAGE_SIZE);
  697. if (err)
  698. goto reset_unlock;
  699. }
  700. if (st_map->map.map_flags & BPF_F_LINK) {
  701. err = 0;
  702. /* Let bpf_link handle registration & unregistration.
  703. *
  704. * Pair with smp_load_acquire() during lookup_elem().
  705. */
  706. smp_store_release(&kvalue->common.state, BPF_STRUCT_OPS_STATE_READY);
  707. goto unlock;
  708. }
  709. err = st_ops->reg(kdata, NULL);
  710. if (likely(!err)) {
  711. /* This refcnt increment on the map here after
  712. * 'st_ops->reg()' is secure since the state of the
  713. * map must be set to INIT at this moment, and thus
  714. * bpf_struct_ops_map_delete_elem() can't unregister
  715. * or transition it to TOBEFREE concurrently.
  716. */
  717. bpf_map_inc(map);
  718. /* Pair with smp_load_acquire() during lookup_elem().
  719. * It ensures the above udata updates (e.g. prog->aux->id)
  720. * can be seen once BPF_STRUCT_OPS_STATE_INUSE is set.
  721. */
  722. smp_store_release(&kvalue->common.state, BPF_STRUCT_OPS_STATE_INUSE);
  723. goto unlock;
  724. }
  725. /* Error during st_ops->reg(). Can happen if this struct_ops needs to be
  726. * verified as a whole, after all init_member() calls. Can also happen if
  727. * there was a race in registering the struct_ops (under the same name) to
  728. * a sub-system through different struct_ops's maps.
  729. */
  730. reset_unlock:
  731. bpf_struct_ops_map_free_ksyms(st_map);
  732. bpf_struct_ops_map_free_image(st_map);
  733. bpf_struct_ops_map_put_progs(st_map);
  734. memset(uvalue, 0, map->value_size);
  735. memset(kvalue, 0, map->value_size);
  736. unlock:
  737. kfree(tlinks);
  738. mutex_unlock(&st_map->lock);
  739. if (!err)
  740. bpf_struct_ops_map_add_ksyms(st_map);
  741. return err;
  742. }
  743. static long bpf_struct_ops_map_delete_elem(struct bpf_map *map, void *key)
  744. {
  745. enum bpf_struct_ops_state prev_state;
  746. struct bpf_struct_ops_map *st_map;
  747. st_map = (struct bpf_struct_ops_map *)map;
  748. if (st_map->map.map_flags & BPF_F_LINK)
  749. return -EOPNOTSUPP;
  750. prev_state = cmpxchg(&st_map->kvalue.common.state,
  751. BPF_STRUCT_OPS_STATE_INUSE,
  752. BPF_STRUCT_OPS_STATE_TOBEFREE);
  753. switch (prev_state) {
  754. case BPF_STRUCT_OPS_STATE_INUSE:
  755. st_map->st_ops_desc->st_ops->unreg(&st_map->kvalue.data, NULL);
  756. bpf_map_put(map);
  757. return 0;
  758. case BPF_STRUCT_OPS_STATE_TOBEFREE:
  759. return -EINPROGRESS;
  760. case BPF_STRUCT_OPS_STATE_INIT:
  761. return -ENOENT;
  762. default:
  763. WARN_ON_ONCE(1);
  764. /* Should never happen. Treat it as not found. */
  765. return -ENOENT;
  766. }
  767. }
  768. static void bpf_struct_ops_map_seq_show_elem(struct bpf_map *map, void *key,
  769. struct seq_file *m)
  770. {
  771. struct bpf_struct_ops_map *st_map = (struct bpf_struct_ops_map *)map;
  772. void *value;
  773. int err;
  774. value = kmalloc(map->value_size, GFP_USER | __GFP_NOWARN);
  775. if (!value)
  776. return;
  777. err = bpf_struct_ops_map_sys_lookup_elem(map, key, value);
  778. if (!err) {
  779. btf_type_seq_show(st_map->btf,
  780. map->btf_vmlinux_value_type_id,
  781. value, m);
  782. seq_putc(m, '\n');
  783. }
  784. kfree(value);
  785. }
  786. static void __bpf_struct_ops_map_free(struct bpf_map *map)
  787. {
  788. struct bpf_struct_ops_map *st_map = (struct bpf_struct_ops_map *)map;
  789. if (st_map->links)
  790. bpf_struct_ops_map_put_progs(st_map);
  791. if (st_map->ksyms)
  792. bpf_struct_ops_map_free_ksyms(st_map);
  793. bpf_map_area_free(st_map->links);
  794. bpf_map_area_free(st_map->ksyms);
  795. bpf_struct_ops_map_free_image(st_map);
  796. bpf_map_area_free(st_map->uvalue);
  797. bpf_map_area_free(st_map);
  798. }
  799. static void bpf_struct_ops_map_free(struct bpf_map *map)
  800. {
  801. struct bpf_struct_ops_map *st_map = (struct bpf_struct_ops_map *)map;
  802. /* st_ops->owner was acquired during map_alloc to implicitly holds
  803. * the btf's refcnt. The acquire was only done when btf_is_module()
  804. * st_map->btf cannot be NULL here.
  805. */
  806. if (btf_is_module(st_map->btf))
  807. module_put(st_map->st_ops_desc->st_ops->owner);
  808. bpf_struct_ops_map_del_ksyms(st_map);
  809. /* The struct_ops's function may switch to another struct_ops.
  810. *
  811. * For example, bpf_tcp_cc_x->init() may switch to
  812. * another tcp_cc_y by calling
  813. * setsockopt(TCP_CONGESTION, "tcp_cc_y").
  814. * During the switch, bpf_struct_ops_put(tcp_cc_x) is called
  815. * and its refcount may reach 0 which then free its
  816. * trampoline image while tcp_cc_x is still running.
  817. *
  818. * A vanilla rcu gp is to wait for all bpf-tcp-cc prog
  819. * to finish. bpf-tcp-cc prog is non sleepable.
  820. * A rcu_tasks gp is to wait for the last few insn
  821. * in the tramopline image to finish before releasing
  822. * the trampoline image.
  823. */
  824. synchronize_rcu_mult(call_rcu, call_rcu_tasks);
  825. __bpf_struct_ops_map_free(map);
  826. }
  827. static int bpf_struct_ops_map_alloc_check(union bpf_attr *attr)
  828. {
  829. if (attr->key_size != sizeof(unsigned int) || attr->max_entries != 1 ||
  830. (attr->map_flags & ~(BPF_F_LINK | BPF_F_VTYPE_BTF_OBJ_FD)) ||
  831. !attr->btf_vmlinux_value_type_id)
  832. return -EINVAL;
  833. return 0;
  834. }
  835. static u32 count_func_ptrs(const struct btf *btf, const struct btf_type *t)
  836. {
  837. int i;
  838. u32 count;
  839. const struct btf_member *member;
  840. count = 0;
  841. for_each_member(i, t, member)
  842. if (btf_type_resolve_func_ptr(btf, member->type, NULL))
  843. count++;
  844. return count;
  845. }
  846. static struct bpf_map *bpf_struct_ops_map_alloc(union bpf_attr *attr)
  847. {
  848. const struct bpf_struct_ops_desc *st_ops_desc;
  849. size_t st_map_size;
  850. struct bpf_struct_ops_map *st_map;
  851. const struct btf_type *t, *vt;
  852. struct module *mod = NULL;
  853. struct bpf_map *map;
  854. struct btf *btf;
  855. int ret;
  856. if (attr->map_flags & BPF_F_VTYPE_BTF_OBJ_FD) {
  857. /* The map holds btf for its whole life time. */
  858. btf = btf_get_by_fd(attr->value_type_btf_obj_fd);
  859. if (IS_ERR(btf))
  860. return ERR_CAST(btf);
  861. if (!btf_is_module(btf)) {
  862. btf_put(btf);
  863. return ERR_PTR(-EINVAL);
  864. }
  865. mod = btf_try_get_module(btf);
  866. /* mod holds a refcnt to btf. We don't need an extra refcnt
  867. * here.
  868. */
  869. btf_put(btf);
  870. if (!mod)
  871. return ERR_PTR(-EINVAL);
  872. } else {
  873. btf = bpf_get_btf_vmlinux();
  874. if (IS_ERR(btf))
  875. return ERR_CAST(btf);
  876. if (!btf)
  877. return ERR_PTR(-ENOTSUPP);
  878. }
  879. st_ops_desc = bpf_struct_ops_find_value(btf, attr->btf_vmlinux_value_type_id);
  880. if (!st_ops_desc) {
  881. ret = -ENOTSUPP;
  882. goto errout;
  883. }
  884. vt = st_ops_desc->value_type;
  885. if (attr->value_size != vt->size) {
  886. ret = -EINVAL;
  887. goto errout;
  888. }
  889. t = st_ops_desc->type;
  890. st_map_size = sizeof(*st_map) +
  891. /* kvalue stores the
  892. * struct bpf_struct_ops_tcp_congestions_ops
  893. */
  894. (vt->size - sizeof(struct bpf_struct_ops_value));
  895. st_map = bpf_map_area_alloc(st_map_size, NUMA_NO_NODE);
  896. if (!st_map) {
  897. ret = -ENOMEM;
  898. goto errout;
  899. }
  900. st_map->st_ops_desc = st_ops_desc;
  901. map = &st_map->map;
  902. st_map->uvalue = bpf_map_area_alloc(vt->size, NUMA_NO_NODE);
  903. st_map->funcs_cnt = count_func_ptrs(btf, t);
  904. st_map->links =
  905. bpf_map_area_alloc(st_map->funcs_cnt * sizeof(struct bpf_link *),
  906. NUMA_NO_NODE);
  907. st_map->ksyms =
  908. bpf_map_area_alloc(st_map->funcs_cnt * sizeof(struct bpf_ksym *),
  909. NUMA_NO_NODE);
  910. if (!st_map->uvalue || !st_map->links || !st_map->ksyms) {
  911. ret = -ENOMEM;
  912. goto errout_free;
  913. }
  914. st_map->btf = btf;
  915. mutex_init(&st_map->lock);
  916. bpf_map_init_from_attr(map, attr);
  917. return map;
  918. errout_free:
  919. __bpf_struct_ops_map_free(map);
  920. errout:
  921. module_put(mod);
  922. return ERR_PTR(ret);
  923. }
  924. static u64 bpf_struct_ops_map_mem_usage(const struct bpf_map *map)
  925. {
  926. struct bpf_struct_ops_map *st_map = (struct bpf_struct_ops_map *)map;
  927. const struct bpf_struct_ops_desc *st_ops_desc = st_map->st_ops_desc;
  928. const struct btf_type *vt = st_ops_desc->value_type;
  929. u64 usage;
  930. usage = sizeof(*st_map) +
  931. vt->size - sizeof(struct bpf_struct_ops_value);
  932. usage += vt->size;
  933. usage += st_map->funcs_cnt * sizeof(struct bpf_link *);
  934. usage += st_map->funcs_cnt * sizeof(struct bpf_ksym *);
  935. usage += PAGE_SIZE;
  936. return usage;
  937. }
  938. BTF_ID_LIST_SINGLE(bpf_struct_ops_map_btf_ids, struct, bpf_struct_ops_map)
  939. const struct bpf_map_ops bpf_struct_ops_map_ops = {
  940. .map_alloc_check = bpf_struct_ops_map_alloc_check,
  941. .map_alloc = bpf_struct_ops_map_alloc,
  942. .map_free = bpf_struct_ops_map_free,
  943. .map_get_next_key = bpf_struct_ops_map_get_next_key,
  944. .map_lookup_elem = bpf_struct_ops_map_lookup_elem,
  945. .map_delete_elem = bpf_struct_ops_map_delete_elem,
  946. .map_update_elem = bpf_struct_ops_map_update_elem,
  947. .map_seq_show_elem = bpf_struct_ops_map_seq_show_elem,
  948. .map_mem_usage = bpf_struct_ops_map_mem_usage,
  949. .map_btf_id = &bpf_struct_ops_map_btf_ids[0],
  950. };
  951. /* "const void *" because some subsystem is
  952. * passing a const (e.g. const struct tcp_congestion_ops *)
  953. */
  954. bool bpf_struct_ops_get(const void *kdata)
  955. {
  956. struct bpf_struct_ops_value *kvalue;
  957. struct bpf_struct_ops_map *st_map;
  958. struct bpf_map *map;
  959. kvalue = container_of(kdata, struct bpf_struct_ops_value, data);
  960. st_map = container_of(kvalue, struct bpf_struct_ops_map, kvalue);
  961. map = __bpf_map_inc_not_zero(&st_map->map, false);
  962. return !IS_ERR(map);
  963. }
  964. void bpf_struct_ops_put(const void *kdata)
  965. {
  966. struct bpf_struct_ops_value *kvalue;
  967. struct bpf_struct_ops_map *st_map;
  968. kvalue = container_of(kdata, struct bpf_struct_ops_value, data);
  969. st_map = container_of(kvalue, struct bpf_struct_ops_map, kvalue);
  970. bpf_map_put(&st_map->map);
  971. }
  972. static bool bpf_struct_ops_valid_to_reg(struct bpf_map *map)
  973. {
  974. struct bpf_struct_ops_map *st_map = (struct bpf_struct_ops_map *)map;
  975. return map->map_type == BPF_MAP_TYPE_STRUCT_OPS &&
  976. map->map_flags & BPF_F_LINK &&
  977. /* Pair with smp_store_release() during map_update */
  978. smp_load_acquire(&st_map->kvalue.common.state) == BPF_STRUCT_OPS_STATE_READY;
  979. }
  980. static void bpf_struct_ops_map_link_dealloc(struct bpf_link *link)
  981. {
  982. struct bpf_struct_ops_link *st_link;
  983. struct bpf_struct_ops_map *st_map;
  984. st_link = container_of(link, struct bpf_struct_ops_link, link);
  985. st_map = (struct bpf_struct_ops_map *)
  986. rcu_dereference_protected(st_link->map, true);
  987. if (st_map) {
  988. st_map->st_ops_desc->st_ops->unreg(&st_map->kvalue.data, link);
  989. bpf_map_put(&st_map->map);
  990. }
  991. kfree(st_link);
  992. }
  993. static void bpf_struct_ops_map_link_show_fdinfo(const struct bpf_link *link,
  994. struct seq_file *seq)
  995. {
  996. struct bpf_struct_ops_link *st_link;
  997. struct bpf_map *map;
  998. st_link = container_of(link, struct bpf_struct_ops_link, link);
  999. rcu_read_lock();
  1000. map = rcu_dereference(st_link->map);
  1001. if (map)
  1002. seq_printf(seq, "map_id:\t%d\n", map->id);
  1003. rcu_read_unlock();
  1004. }
  1005. static int bpf_struct_ops_map_link_fill_link_info(const struct bpf_link *link,
  1006. struct bpf_link_info *info)
  1007. {
  1008. struct bpf_struct_ops_link *st_link;
  1009. struct bpf_map *map;
  1010. st_link = container_of(link, struct bpf_struct_ops_link, link);
  1011. rcu_read_lock();
  1012. map = rcu_dereference(st_link->map);
  1013. if (map)
  1014. info->struct_ops.map_id = map->id;
  1015. rcu_read_unlock();
  1016. return 0;
  1017. }
  1018. static int bpf_struct_ops_map_link_update(struct bpf_link *link, struct bpf_map *new_map,
  1019. struct bpf_map *expected_old_map)
  1020. {
  1021. struct bpf_struct_ops_map *st_map, *old_st_map;
  1022. struct bpf_map *old_map;
  1023. struct bpf_struct_ops_link *st_link;
  1024. int err;
  1025. st_link = container_of(link, struct bpf_struct_ops_link, link);
  1026. st_map = container_of(new_map, struct bpf_struct_ops_map, map);
  1027. if (!bpf_struct_ops_valid_to_reg(new_map))
  1028. return -EINVAL;
  1029. if (!st_map->st_ops_desc->st_ops->update)
  1030. return -EOPNOTSUPP;
  1031. mutex_lock(&update_mutex);
  1032. old_map = rcu_dereference_protected(st_link->map, lockdep_is_held(&update_mutex));
  1033. if (!old_map) {
  1034. err = -ENOLINK;
  1035. goto err_out;
  1036. }
  1037. if (expected_old_map && old_map != expected_old_map) {
  1038. err = -EPERM;
  1039. goto err_out;
  1040. }
  1041. old_st_map = container_of(old_map, struct bpf_struct_ops_map, map);
  1042. /* The new and old struct_ops must be the same type. */
  1043. if (st_map->st_ops_desc != old_st_map->st_ops_desc) {
  1044. err = -EINVAL;
  1045. goto err_out;
  1046. }
  1047. err = st_map->st_ops_desc->st_ops->update(st_map->kvalue.data, old_st_map->kvalue.data, link);
  1048. if (err)
  1049. goto err_out;
  1050. bpf_map_inc(new_map);
  1051. rcu_assign_pointer(st_link->map, new_map);
  1052. bpf_map_put(old_map);
  1053. err_out:
  1054. mutex_unlock(&update_mutex);
  1055. return err;
  1056. }
  1057. static int bpf_struct_ops_map_link_detach(struct bpf_link *link)
  1058. {
  1059. struct bpf_struct_ops_link *st_link = container_of(link, struct bpf_struct_ops_link, link);
  1060. struct bpf_struct_ops_map *st_map;
  1061. struct bpf_map *map;
  1062. mutex_lock(&update_mutex);
  1063. map = rcu_dereference_protected(st_link->map, lockdep_is_held(&update_mutex));
  1064. if (!map) {
  1065. mutex_unlock(&update_mutex);
  1066. return 0;
  1067. }
  1068. st_map = container_of(map, struct bpf_struct_ops_map, map);
  1069. st_map->st_ops_desc->st_ops->unreg(&st_map->kvalue.data, link);
  1070. RCU_INIT_POINTER(st_link->map, NULL);
  1071. /* Pair with bpf_map_get() in bpf_struct_ops_link_create() or
  1072. * bpf_map_inc() in bpf_struct_ops_map_link_update().
  1073. */
  1074. bpf_map_put(&st_map->map);
  1075. mutex_unlock(&update_mutex);
  1076. wake_up_interruptible_poll(&st_link->wait_hup, EPOLLHUP);
  1077. return 0;
  1078. }
  1079. static __poll_t bpf_struct_ops_map_link_poll(struct file *file,
  1080. struct poll_table_struct *pts)
  1081. {
  1082. struct bpf_struct_ops_link *st_link = file->private_data;
  1083. poll_wait(file, &st_link->wait_hup, pts);
  1084. return rcu_access_pointer(st_link->map) ? 0 : EPOLLHUP;
  1085. }
  1086. static const struct bpf_link_ops bpf_struct_ops_map_lops = {
  1087. .dealloc = bpf_struct_ops_map_link_dealloc,
  1088. .detach = bpf_struct_ops_map_link_detach,
  1089. .show_fdinfo = bpf_struct_ops_map_link_show_fdinfo,
  1090. .fill_link_info = bpf_struct_ops_map_link_fill_link_info,
  1091. .update_map = bpf_struct_ops_map_link_update,
  1092. .poll = bpf_struct_ops_map_link_poll,
  1093. };
  1094. int bpf_struct_ops_link_create(union bpf_attr *attr)
  1095. {
  1096. struct bpf_struct_ops_link *link = NULL;
  1097. struct bpf_link_primer link_primer;
  1098. struct bpf_struct_ops_map *st_map;
  1099. struct bpf_map *map;
  1100. int err;
  1101. map = bpf_map_get(attr->link_create.map_fd);
  1102. if (IS_ERR(map))
  1103. return PTR_ERR(map);
  1104. st_map = (struct bpf_struct_ops_map *)map;
  1105. if (!bpf_struct_ops_valid_to_reg(map)) {
  1106. err = -EINVAL;
  1107. goto err_out;
  1108. }
  1109. link = kzalloc(sizeof(*link), GFP_USER);
  1110. if (!link) {
  1111. err = -ENOMEM;
  1112. goto err_out;
  1113. }
  1114. bpf_link_init(&link->link, BPF_LINK_TYPE_STRUCT_OPS, &bpf_struct_ops_map_lops, NULL);
  1115. err = bpf_link_prime(&link->link, &link_primer);
  1116. if (err)
  1117. goto err_out;
  1118. init_waitqueue_head(&link->wait_hup);
  1119. /* Hold the update_mutex such that the subsystem cannot
  1120. * do link->ops->detach() before the link is fully initialized.
  1121. */
  1122. mutex_lock(&update_mutex);
  1123. err = st_map->st_ops_desc->st_ops->reg(st_map->kvalue.data, &link->link);
  1124. if (err) {
  1125. mutex_unlock(&update_mutex);
  1126. bpf_link_cleanup(&link_primer);
  1127. link = NULL;
  1128. goto err_out;
  1129. }
  1130. RCU_INIT_POINTER(link->map, map);
  1131. mutex_unlock(&update_mutex);
  1132. return bpf_link_settle(&link_primer);
  1133. err_out:
  1134. bpf_map_put(map);
  1135. kfree(link);
  1136. return err;
  1137. }
  1138. void bpf_map_struct_ops_info_fill(struct bpf_map_info *info, struct bpf_map *map)
  1139. {
  1140. struct bpf_struct_ops_map *st_map = (struct bpf_struct_ops_map *)map;
  1141. info->btf_vmlinux_id = btf_obj_id(st_map->btf);
  1142. }