bpf_local_storage.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2019 Facebook */
  3. #include <linux/rculist.h>
  4. #include <linux/list.h>
  5. #include <linux/hash.h>
  6. #include <linux/types.h>
  7. #include <linux/spinlock.h>
  8. #include <linux/bpf.h>
  9. #include <linux/btf_ids.h>
  10. #include <linux/bpf_local_storage.h>
  11. #include <net/sock.h>
  12. #include <uapi/linux/sock_diag.h>
  13. #include <uapi/linux/btf.h>
  14. #include <linux/rcupdate.h>
  15. #include <linux/rcupdate_trace.h>
  16. #include <linux/rcupdate_wait.h>
  17. #define BPF_LOCAL_STORAGE_CREATE_FLAG_MASK (BPF_F_NO_PREALLOC | BPF_F_CLONE)
  18. static struct bpf_local_storage_map_bucket *
  19. select_bucket(struct bpf_local_storage_map *smap,
  20. struct bpf_local_storage_elem *selem)
  21. {
  22. return &smap->buckets[hash_ptr(selem, smap->bucket_log)];
  23. }
  24. static int mem_charge(struct bpf_local_storage_map *smap, void *owner, u32 size)
  25. {
  26. struct bpf_map *map = &smap->map;
  27. if (!map->ops->map_local_storage_charge)
  28. return 0;
  29. return map->ops->map_local_storage_charge(smap, owner, size);
  30. }
  31. static void mem_uncharge(struct bpf_local_storage_map *smap, void *owner,
  32. u32 size)
  33. {
  34. struct bpf_map *map = &smap->map;
  35. if (map->ops->map_local_storage_uncharge)
  36. map->ops->map_local_storage_uncharge(smap, owner, size);
  37. }
  38. static struct bpf_local_storage __rcu **
  39. owner_storage(struct bpf_local_storage_map *smap, void *owner)
  40. {
  41. struct bpf_map *map = &smap->map;
  42. return map->ops->map_owner_storage_ptr(owner);
  43. }
  44. static bool selem_linked_to_storage_lockless(const struct bpf_local_storage_elem *selem)
  45. {
  46. return !hlist_unhashed_lockless(&selem->snode);
  47. }
  48. static bool selem_linked_to_storage(const struct bpf_local_storage_elem *selem)
  49. {
  50. return !hlist_unhashed(&selem->snode);
  51. }
  52. static bool selem_linked_to_map_lockless(const struct bpf_local_storage_elem *selem)
  53. {
  54. return !hlist_unhashed_lockless(&selem->map_node);
  55. }
  56. static bool selem_linked_to_map(const struct bpf_local_storage_elem *selem)
  57. {
  58. return !hlist_unhashed(&selem->map_node);
  59. }
  60. struct bpf_local_storage_elem *
  61. bpf_selem_alloc(struct bpf_local_storage_map *smap, void *owner,
  62. void *value, bool charge_mem, gfp_t gfp_flags)
  63. {
  64. struct bpf_local_storage_elem *selem;
  65. if (charge_mem && mem_charge(smap, owner, smap->elem_size))
  66. return NULL;
  67. if (smap->bpf_ma) {
  68. migrate_disable();
  69. selem = bpf_mem_cache_alloc_flags(&smap->selem_ma, gfp_flags);
  70. migrate_enable();
  71. if (selem)
  72. /* Keep the original bpf_map_kzalloc behavior
  73. * before started using the bpf_mem_cache_alloc.
  74. *
  75. * No need to use zero_map_value. The bpf_selem_free()
  76. * only does bpf_mem_cache_free when there is
  77. * no other bpf prog is using the selem.
  78. */
  79. memset(SDATA(selem)->data, 0, smap->map.value_size);
  80. } else {
  81. selem = bpf_map_kzalloc(&smap->map, smap->elem_size,
  82. gfp_flags | __GFP_NOWARN);
  83. }
  84. if (selem) {
  85. if (value)
  86. copy_map_value(&smap->map, SDATA(selem)->data, value);
  87. /* No need to call check_and_init_map_value as memory is zero init */
  88. return selem;
  89. }
  90. if (charge_mem)
  91. mem_uncharge(smap, owner, smap->elem_size);
  92. return NULL;
  93. }
  94. /* rcu tasks trace callback for bpf_ma == false */
  95. static void __bpf_local_storage_free_trace_rcu(struct rcu_head *rcu)
  96. {
  97. struct bpf_local_storage *local_storage;
  98. /* If RCU Tasks Trace grace period implies RCU grace period, do
  99. * kfree(), else do kfree_rcu().
  100. */
  101. local_storage = container_of(rcu, struct bpf_local_storage, rcu);
  102. if (rcu_trace_implies_rcu_gp())
  103. kfree(local_storage);
  104. else
  105. kfree_rcu(local_storage, rcu);
  106. }
  107. static void bpf_local_storage_free_rcu(struct rcu_head *rcu)
  108. {
  109. struct bpf_local_storage *local_storage;
  110. local_storage = container_of(rcu, struct bpf_local_storage, rcu);
  111. bpf_mem_cache_raw_free(local_storage);
  112. }
  113. static void bpf_local_storage_free_trace_rcu(struct rcu_head *rcu)
  114. {
  115. if (rcu_trace_implies_rcu_gp())
  116. bpf_local_storage_free_rcu(rcu);
  117. else
  118. call_rcu(rcu, bpf_local_storage_free_rcu);
  119. }
  120. /* Handle bpf_ma == false */
  121. static void __bpf_local_storage_free(struct bpf_local_storage *local_storage,
  122. bool vanilla_rcu)
  123. {
  124. if (vanilla_rcu)
  125. kfree_rcu(local_storage, rcu);
  126. else
  127. call_rcu_tasks_trace(&local_storage->rcu,
  128. __bpf_local_storage_free_trace_rcu);
  129. }
  130. static void bpf_local_storage_free(struct bpf_local_storage *local_storage,
  131. struct bpf_local_storage_map *smap,
  132. bool bpf_ma, bool reuse_now)
  133. {
  134. if (!local_storage)
  135. return;
  136. if (!bpf_ma) {
  137. __bpf_local_storage_free(local_storage, reuse_now);
  138. return;
  139. }
  140. if (!reuse_now) {
  141. call_rcu_tasks_trace(&local_storage->rcu,
  142. bpf_local_storage_free_trace_rcu);
  143. return;
  144. }
  145. if (smap) {
  146. migrate_disable();
  147. bpf_mem_cache_free(&smap->storage_ma, local_storage);
  148. migrate_enable();
  149. } else {
  150. /* smap could be NULL if the selem that triggered
  151. * this 'local_storage' creation had been long gone.
  152. * In this case, directly do call_rcu().
  153. */
  154. call_rcu(&local_storage->rcu, bpf_local_storage_free_rcu);
  155. }
  156. }
  157. /* rcu tasks trace callback for bpf_ma == false */
  158. static void __bpf_selem_free_trace_rcu(struct rcu_head *rcu)
  159. {
  160. struct bpf_local_storage_elem *selem;
  161. selem = container_of(rcu, struct bpf_local_storage_elem, rcu);
  162. if (rcu_trace_implies_rcu_gp())
  163. kfree(selem);
  164. else
  165. kfree_rcu(selem, rcu);
  166. }
  167. /* Handle bpf_ma == false */
  168. static void __bpf_selem_free(struct bpf_local_storage_elem *selem,
  169. bool vanilla_rcu)
  170. {
  171. if (vanilla_rcu)
  172. kfree_rcu(selem, rcu);
  173. else
  174. call_rcu_tasks_trace(&selem->rcu, __bpf_selem_free_trace_rcu);
  175. }
  176. static void bpf_selem_free_rcu(struct rcu_head *rcu)
  177. {
  178. struct bpf_local_storage_elem *selem;
  179. selem = container_of(rcu, struct bpf_local_storage_elem, rcu);
  180. bpf_mem_cache_raw_free(selem);
  181. }
  182. static void bpf_selem_free_trace_rcu(struct rcu_head *rcu)
  183. {
  184. if (rcu_trace_implies_rcu_gp())
  185. bpf_selem_free_rcu(rcu);
  186. else
  187. call_rcu(rcu, bpf_selem_free_rcu);
  188. }
  189. void bpf_selem_free(struct bpf_local_storage_elem *selem,
  190. struct bpf_local_storage_map *smap,
  191. bool reuse_now)
  192. {
  193. bpf_obj_free_fields(smap->map.record, SDATA(selem)->data);
  194. if (!smap->bpf_ma) {
  195. __bpf_selem_free(selem, reuse_now);
  196. return;
  197. }
  198. if (!reuse_now) {
  199. call_rcu_tasks_trace(&selem->rcu, bpf_selem_free_trace_rcu);
  200. } else {
  201. /* Instead of using the vanilla call_rcu(),
  202. * bpf_mem_cache_free will be able to reuse selem
  203. * immediately.
  204. */
  205. migrate_disable();
  206. bpf_mem_cache_free(&smap->selem_ma, selem);
  207. migrate_enable();
  208. }
  209. }
  210. /* local_storage->lock must be held and selem->local_storage == local_storage.
  211. * The caller must ensure selem->smap is still valid to be
  212. * dereferenced for its smap->elem_size and smap->cache_idx.
  213. */
  214. static bool bpf_selem_unlink_storage_nolock(struct bpf_local_storage *local_storage,
  215. struct bpf_local_storage_elem *selem,
  216. bool uncharge_mem, bool reuse_now)
  217. {
  218. struct bpf_local_storage_map *smap;
  219. bool free_local_storage;
  220. void *owner;
  221. smap = rcu_dereference_check(SDATA(selem)->smap, bpf_rcu_lock_held());
  222. owner = local_storage->owner;
  223. /* All uncharging on the owner must be done first.
  224. * The owner may be freed once the last selem is unlinked
  225. * from local_storage.
  226. */
  227. if (uncharge_mem)
  228. mem_uncharge(smap, owner, smap->elem_size);
  229. free_local_storage = hlist_is_singular_node(&selem->snode,
  230. &local_storage->list);
  231. if (free_local_storage) {
  232. mem_uncharge(smap, owner, sizeof(struct bpf_local_storage));
  233. local_storage->owner = NULL;
  234. /* After this RCU_INIT, owner may be freed and cannot be used */
  235. RCU_INIT_POINTER(*owner_storage(smap, owner), NULL);
  236. /* local_storage is not freed now. local_storage->lock is
  237. * still held and raw_spin_unlock_bh(&local_storage->lock)
  238. * will be done by the caller.
  239. *
  240. * Although the unlock will be done under
  241. * rcu_read_lock(), it is more intuitive to
  242. * read if the freeing of the storage is done
  243. * after the raw_spin_unlock_bh(&local_storage->lock).
  244. *
  245. * Hence, a "bool free_local_storage" is returned
  246. * to the caller which then calls then frees the storage after
  247. * all the RCU grace periods have expired.
  248. */
  249. }
  250. hlist_del_init_rcu(&selem->snode);
  251. if (rcu_access_pointer(local_storage->cache[smap->cache_idx]) ==
  252. SDATA(selem))
  253. RCU_INIT_POINTER(local_storage->cache[smap->cache_idx], NULL);
  254. bpf_selem_free(selem, smap, reuse_now);
  255. if (rcu_access_pointer(local_storage->smap) == smap)
  256. RCU_INIT_POINTER(local_storage->smap, NULL);
  257. return free_local_storage;
  258. }
  259. static bool check_storage_bpf_ma(struct bpf_local_storage *local_storage,
  260. struct bpf_local_storage_map *storage_smap,
  261. struct bpf_local_storage_elem *selem)
  262. {
  263. struct bpf_local_storage_map *selem_smap;
  264. /* local_storage->smap may be NULL. If it is, get the bpf_ma
  265. * from any selem in the local_storage->list. The bpf_ma of all
  266. * local_storage and selem should have the same value
  267. * for the same map type.
  268. *
  269. * If the local_storage->list is already empty, the caller will not
  270. * care about the bpf_ma value also because the caller is not
  271. * responsible to free the local_storage.
  272. */
  273. if (storage_smap)
  274. return storage_smap->bpf_ma;
  275. if (!selem) {
  276. struct hlist_node *n;
  277. n = rcu_dereference_check(hlist_first_rcu(&local_storage->list),
  278. bpf_rcu_lock_held());
  279. if (!n)
  280. return false;
  281. selem = hlist_entry(n, struct bpf_local_storage_elem, snode);
  282. }
  283. selem_smap = rcu_dereference_check(SDATA(selem)->smap, bpf_rcu_lock_held());
  284. return selem_smap->bpf_ma;
  285. }
  286. static void bpf_selem_unlink_storage(struct bpf_local_storage_elem *selem,
  287. bool reuse_now)
  288. {
  289. struct bpf_local_storage_map *storage_smap;
  290. struct bpf_local_storage *local_storage;
  291. bool bpf_ma, free_local_storage = false;
  292. unsigned long flags;
  293. if (unlikely(!selem_linked_to_storage_lockless(selem)))
  294. /* selem has already been unlinked from sk */
  295. return;
  296. local_storage = rcu_dereference_check(selem->local_storage,
  297. bpf_rcu_lock_held());
  298. storage_smap = rcu_dereference_check(local_storage->smap,
  299. bpf_rcu_lock_held());
  300. bpf_ma = check_storage_bpf_ma(local_storage, storage_smap, selem);
  301. raw_spin_lock_irqsave(&local_storage->lock, flags);
  302. if (likely(selem_linked_to_storage(selem)))
  303. free_local_storage = bpf_selem_unlink_storage_nolock(
  304. local_storage, selem, true, reuse_now);
  305. raw_spin_unlock_irqrestore(&local_storage->lock, flags);
  306. if (free_local_storage)
  307. bpf_local_storage_free(local_storage, storage_smap, bpf_ma, reuse_now);
  308. }
  309. void bpf_selem_link_storage_nolock(struct bpf_local_storage *local_storage,
  310. struct bpf_local_storage_elem *selem)
  311. {
  312. RCU_INIT_POINTER(selem->local_storage, local_storage);
  313. hlist_add_head_rcu(&selem->snode, &local_storage->list);
  314. }
  315. static void bpf_selem_unlink_map(struct bpf_local_storage_elem *selem)
  316. {
  317. struct bpf_local_storage_map *smap;
  318. struct bpf_local_storage_map_bucket *b;
  319. unsigned long flags;
  320. if (unlikely(!selem_linked_to_map_lockless(selem)))
  321. /* selem has already be unlinked from smap */
  322. return;
  323. smap = rcu_dereference_check(SDATA(selem)->smap, bpf_rcu_lock_held());
  324. b = select_bucket(smap, selem);
  325. raw_spin_lock_irqsave(&b->lock, flags);
  326. if (likely(selem_linked_to_map(selem)))
  327. hlist_del_init_rcu(&selem->map_node);
  328. raw_spin_unlock_irqrestore(&b->lock, flags);
  329. }
  330. void bpf_selem_link_map(struct bpf_local_storage_map *smap,
  331. struct bpf_local_storage_elem *selem)
  332. {
  333. struct bpf_local_storage_map_bucket *b = select_bucket(smap, selem);
  334. unsigned long flags;
  335. raw_spin_lock_irqsave(&b->lock, flags);
  336. RCU_INIT_POINTER(SDATA(selem)->smap, smap);
  337. hlist_add_head_rcu(&selem->map_node, &b->list);
  338. raw_spin_unlock_irqrestore(&b->lock, flags);
  339. }
  340. void bpf_selem_unlink(struct bpf_local_storage_elem *selem, bool reuse_now)
  341. {
  342. /* Always unlink from map before unlinking from local_storage
  343. * because selem will be freed after successfully unlinked from
  344. * the local_storage.
  345. */
  346. bpf_selem_unlink_map(selem);
  347. bpf_selem_unlink_storage(selem, reuse_now);
  348. }
  349. void __bpf_local_storage_insert_cache(struct bpf_local_storage *local_storage,
  350. struct bpf_local_storage_map *smap,
  351. struct bpf_local_storage_elem *selem)
  352. {
  353. unsigned long flags;
  354. /* spinlock is needed to avoid racing with the
  355. * parallel delete. Otherwise, publishing an already
  356. * deleted sdata to the cache will become a use-after-free
  357. * problem in the next bpf_local_storage_lookup().
  358. */
  359. raw_spin_lock_irqsave(&local_storage->lock, flags);
  360. if (selem_linked_to_storage(selem))
  361. rcu_assign_pointer(local_storage->cache[smap->cache_idx], SDATA(selem));
  362. raw_spin_unlock_irqrestore(&local_storage->lock, flags);
  363. }
  364. static int check_flags(const struct bpf_local_storage_data *old_sdata,
  365. u64 map_flags)
  366. {
  367. if (old_sdata && (map_flags & ~BPF_F_LOCK) == BPF_NOEXIST)
  368. /* elem already exists */
  369. return -EEXIST;
  370. if (!old_sdata && (map_flags & ~BPF_F_LOCK) == BPF_EXIST)
  371. /* elem doesn't exist, cannot update it */
  372. return -ENOENT;
  373. return 0;
  374. }
  375. int bpf_local_storage_alloc(void *owner,
  376. struct bpf_local_storage_map *smap,
  377. struct bpf_local_storage_elem *first_selem,
  378. gfp_t gfp_flags)
  379. {
  380. struct bpf_local_storage *prev_storage, *storage;
  381. struct bpf_local_storage **owner_storage_ptr;
  382. int err;
  383. err = mem_charge(smap, owner, sizeof(*storage));
  384. if (err)
  385. return err;
  386. if (smap->bpf_ma) {
  387. migrate_disable();
  388. storage = bpf_mem_cache_alloc_flags(&smap->storage_ma, gfp_flags);
  389. migrate_enable();
  390. } else {
  391. storage = bpf_map_kzalloc(&smap->map, sizeof(*storage),
  392. gfp_flags | __GFP_NOWARN);
  393. }
  394. if (!storage) {
  395. err = -ENOMEM;
  396. goto uncharge;
  397. }
  398. RCU_INIT_POINTER(storage->smap, smap);
  399. INIT_HLIST_HEAD(&storage->list);
  400. raw_spin_lock_init(&storage->lock);
  401. storage->owner = owner;
  402. bpf_selem_link_storage_nolock(storage, first_selem);
  403. bpf_selem_link_map(smap, first_selem);
  404. owner_storage_ptr =
  405. (struct bpf_local_storage **)owner_storage(smap, owner);
  406. /* Publish storage to the owner.
  407. * Instead of using any lock of the kernel object (i.e. owner),
  408. * cmpxchg will work with any kernel object regardless what
  409. * the running context is, bh, irq...etc.
  410. *
  411. * From now on, the owner->storage pointer (e.g. sk->sk_bpf_storage)
  412. * is protected by the storage->lock. Hence, when freeing
  413. * the owner->storage, the storage->lock must be held before
  414. * setting owner->storage ptr to NULL.
  415. */
  416. prev_storage = cmpxchg(owner_storage_ptr, NULL, storage);
  417. if (unlikely(prev_storage)) {
  418. bpf_selem_unlink_map(first_selem);
  419. err = -EAGAIN;
  420. goto uncharge;
  421. /* Note that even first_selem was linked to smap's
  422. * bucket->list, first_selem can be freed immediately
  423. * (instead of kfree_rcu) because
  424. * bpf_local_storage_map_free() does a
  425. * synchronize_rcu_mult (waiting for both sleepable and
  426. * normal programs) before walking the bucket->list.
  427. * Hence, no one is accessing selem from the
  428. * bucket->list under rcu_read_lock().
  429. */
  430. }
  431. return 0;
  432. uncharge:
  433. bpf_local_storage_free(storage, smap, smap->bpf_ma, true);
  434. mem_uncharge(smap, owner, sizeof(*storage));
  435. return err;
  436. }
  437. /* sk cannot be going away because it is linking new elem
  438. * to sk->sk_bpf_storage. (i.e. sk->sk_refcnt cannot be 0).
  439. * Otherwise, it will become a leak (and other memory issues
  440. * during map destruction).
  441. */
  442. struct bpf_local_storage_data *
  443. bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap,
  444. void *value, u64 map_flags, gfp_t gfp_flags)
  445. {
  446. struct bpf_local_storage_data *old_sdata = NULL;
  447. struct bpf_local_storage_elem *alloc_selem, *selem = NULL;
  448. struct bpf_local_storage *local_storage;
  449. unsigned long flags;
  450. int err;
  451. /* BPF_EXIST and BPF_NOEXIST cannot be both set */
  452. if (unlikely((map_flags & ~BPF_F_LOCK) > BPF_EXIST) ||
  453. /* BPF_F_LOCK can only be used in a value with spin_lock */
  454. unlikely((map_flags & BPF_F_LOCK) &&
  455. !btf_record_has_field(smap->map.record, BPF_SPIN_LOCK)))
  456. return ERR_PTR(-EINVAL);
  457. if (gfp_flags == GFP_KERNEL && (map_flags & ~BPF_F_LOCK) != BPF_NOEXIST)
  458. return ERR_PTR(-EINVAL);
  459. local_storage = rcu_dereference_check(*owner_storage(smap, owner),
  460. bpf_rcu_lock_held());
  461. if (!local_storage || hlist_empty(&local_storage->list)) {
  462. /* Very first elem for the owner */
  463. err = check_flags(NULL, map_flags);
  464. if (err)
  465. return ERR_PTR(err);
  466. selem = bpf_selem_alloc(smap, owner, value, true, gfp_flags);
  467. if (!selem)
  468. return ERR_PTR(-ENOMEM);
  469. err = bpf_local_storage_alloc(owner, smap, selem, gfp_flags);
  470. if (err) {
  471. bpf_selem_free(selem, smap, true);
  472. mem_uncharge(smap, owner, smap->elem_size);
  473. return ERR_PTR(err);
  474. }
  475. return SDATA(selem);
  476. }
  477. if ((map_flags & BPF_F_LOCK) && !(map_flags & BPF_NOEXIST)) {
  478. /* Hoping to find an old_sdata to do inline update
  479. * such that it can avoid taking the local_storage->lock
  480. * and changing the lists.
  481. */
  482. old_sdata =
  483. bpf_local_storage_lookup(local_storage, smap, false);
  484. err = check_flags(old_sdata, map_flags);
  485. if (err)
  486. return ERR_PTR(err);
  487. if (old_sdata && selem_linked_to_storage_lockless(SELEM(old_sdata))) {
  488. copy_map_value_locked(&smap->map, old_sdata->data,
  489. value, false);
  490. return old_sdata;
  491. }
  492. }
  493. /* A lookup has just been done before and concluded a new selem is
  494. * needed. The chance of an unnecessary alloc is unlikely.
  495. */
  496. alloc_selem = selem = bpf_selem_alloc(smap, owner, value, true, gfp_flags);
  497. if (!alloc_selem)
  498. return ERR_PTR(-ENOMEM);
  499. raw_spin_lock_irqsave(&local_storage->lock, flags);
  500. /* Recheck local_storage->list under local_storage->lock */
  501. if (unlikely(hlist_empty(&local_storage->list))) {
  502. /* A parallel del is happening and local_storage is going
  503. * away. It has just been checked before, so very
  504. * unlikely. Return instead of retry to keep things
  505. * simple.
  506. */
  507. err = -EAGAIN;
  508. goto unlock;
  509. }
  510. old_sdata = bpf_local_storage_lookup(local_storage, smap, false);
  511. err = check_flags(old_sdata, map_flags);
  512. if (err)
  513. goto unlock;
  514. if (old_sdata && (map_flags & BPF_F_LOCK)) {
  515. copy_map_value_locked(&smap->map, old_sdata->data, value,
  516. false);
  517. selem = SELEM(old_sdata);
  518. goto unlock;
  519. }
  520. alloc_selem = NULL;
  521. /* First, link the new selem to the map */
  522. bpf_selem_link_map(smap, selem);
  523. /* Second, link (and publish) the new selem to local_storage */
  524. bpf_selem_link_storage_nolock(local_storage, selem);
  525. /* Third, remove old selem, SELEM(old_sdata) */
  526. if (old_sdata) {
  527. bpf_selem_unlink_map(SELEM(old_sdata));
  528. bpf_selem_unlink_storage_nolock(local_storage, SELEM(old_sdata),
  529. true, false);
  530. }
  531. unlock:
  532. raw_spin_unlock_irqrestore(&local_storage->lock, flags);
  533. if (alloc_selem) {
  534. mem_uncharge(smap, owner, smap->elem_size);
  535. bpf_selem_free(alloc_selem, smap, true);
  536. }
  537. return err ? ERR_PTR(err) : SDATA(selem);
  538. }
  539. static u16 bpf_local_storage_cache_idx_get(struct bpf_local_storage_cache *cache)
  540. {
  541. u64 min_usage = U64_MAX;
  542. u16 i, res = 0;
  543. spin_lock(&cache->idx_lock);
  544. for (i = 0; i < BPF_LOCAL_STORAGE_CACHE_SIZE; i++) {
  545. if (cache->idx_usage_counts[i] < min_usage) {
  546. min_usage = cache->idx_usage_counts[i];
  547. res = i;
  548. /* Found a free cache_idx */
  549. if (!min_usage)
  550. break;
  551. }
  552. }
  553. cache->idx_usage_counts[res]++;
  554. spin_unlock(&cache->idx_lock);
  555. return res;
  556. }
  557. static void bpf_local_storage_cache_idx_free(struct bpf_local_storage_cache *cache,
  558. u16 idx)
  559. {
  560. spin_lock(&cache->idx_lock);
  561. cache->idx_usage_counts[idx]--;
  562. spin_unlock(&cache->idx_lock);
  563. }
  564. int bpf_local_storage_map_alloc_check(union bpf_attr *attr)
  565. {
  566. if (attr->map_flags & ~BPF_LOCAL_STORAGE_CREATE_FLAG_MASK ||
  567. !(attr->map_flags & BPF_F_NO_PREALLOC) ||
  568. attr->max_entries ||
  569. attr->key_size != sizeof(int) || !attr->value_size ||
  570. /* Enforce BTF for userspace sk dumping */
  571. !attr->btf_key_type_id || !attr->btf_value_type_id)
  572. return -EINVAL;
  573. if (attr->value_size > BPF_LOCAL_STORAGE_MAX_VALUE_SIZE)
  574. return -E2BIG;
  575. return 0;
  576. }
  577. int bpf_local_storage_map_check_btf(const struct bpf_map *map,
  578. const struct btf *btf,
  579. const struct btf_type *key_type,
  580. const struct btf_type *value_type)
  581. {
  582. u32 int_data;
  583. if (BTF_INFO_KIND(key_type->info) != BTF_KIND_INT)
  584. return -EINVAL;
  585. int_data = *(u32 *)(key_type + 1);
  586. if (BTF_INT_BITS(int_data) != 32 || BTF_INT_OFFSET(int_data))
  587. return -EINVAL;
  588. return 0;
  589. }
  590. void bpf_local_storage_destroy(struct bpf_local_storage *local_storage)
  591. {
  592. struct bpf_local_storage_map *storage_smap;
  593. struct bpf_local_storage_elem *selem;
  594. bool bpf_ma, free_storage = false;
  595. struct hlist_node *n;
  596. unsigned long flags;
  597. storage_smap = rcu_dereference_check(local_storage->smap, bpf_rcu_lock_held());
  598. bpf_ma = check_storage_bpf_ma(local_storage, storage_smap, NULL);
  599. /* Neither the bpf_prog nor the bpf_map's syscall
  600. * could be modifying the local_storage->list now.
  601. * Thus, no elem can be added to or deleted from the
  602. * local_storage->list by the bpf_prog or by the bpf_map's syscall.
  603. *
  604. * It is racing with bpf_local_storage_map_free() alone
  605. * when unlinking elem from the local_storage->list and
  606. * the map's bucket->list.
  607. */
  608. raw_spin_lock_irqsave(&local_storage->lock, flags);
  609. hlist_for_each_entry_safe(selem, n, &local_storage->list, snode) {
  610. /* Always unlink from map before unlinking from
  611. * local_storage.
  612. */
  613. bpf_selem_unlink_map(selem);
  614. /* If local_storage list has only one element, the
  615. * bpf_selem_unlink_storage_nolock() will return true.
  616. * Otherwise, it will return false. The current loop iteration
  617. * intends to remove all local storage. So the last iteration
  618. * of the loop will set the free_cgroup_storage to true.
  619. */
  620. free_storage = bpf_selem_unlink_storage_nolock(
  621. local_storage, selem, true, true);
  622. }
  623. raw_spin_unlock_irqrestore(&local_storage->lock, flags);
  624. if (free_storage)
  625. bpf_local_storage_free(local_storage, storage_smap, bpf_ma, true);
  626. }
  627. u64 bpf_local_storage_map_mem_usage(const struct bpf_map *map)
  628. {
  629. struct bpf_local_storage_map *smap = (struct bpf_local_storage_map *)map;
  630. u64 usage = sizeof(*smap);
  631. /* The dynamically callocated selems are not counted currently. */
  632. usage += sizeof(*smap->buckets) * (1ULL << smap->bucket_log);
  633. return usage;
  634. }
  635. /* When bpf_ma == true, the bpf_mem_alloc is used to allocate and free memory.
  636. * A deadlock free allocator is useful for storage that the bpf prog can easily
  637. * get a hold of the owner PTR_TO_BTF_ID in any context. eg. bpf_get_current_task_btf.
  638. * The task and cgroup storage fall into this case. The bpf_mem_alloc reuses
  639. * memory immediately. To be reuse-immediate safe, the owner destruction
  640. * code path needs to go through a rcu grace period before calling
  641. * bpf_local_storage_destroy().
  642. *
  643. * When bpf_ma == false, the kmalloc and kfree are used.
  644. */
  645. struct bpf_map *
  646. bpf_local_storage_map_alloc(union bpf_attr *attr,
  647. struct bpf_local_storage_cache *cache,
  648. bool bpf_ma)
  649. {
  650. struct bpf_local_storage_map *smap;
  651. unsigned int i;
  652. u32 nbuckets;
  653. int err;
  654. smap = bpf_map_area_alloc(sizeof(*smap), NUMA_NO_NODE);
  655. if (!smap)
  656. return ERR_PTR(-ENOMEM);
  657. bpf_map_init_from_attr(&smap->map, attr);
  658. nbuckets = roundup_pow_of_two(num_possible_cpus());
  659. /* Use at least 2 buckets, select_bucket() is undefined behavior with 1 bucket */
  660. nbuckets = max_t(u32, 2, nbuckets);
  661. smap->bucket_log = ilog2(nbuckets);
  662. smap->buckets = bpf_map_kvcalloc(&smap->map, nbuckets,
  663. sizeof(*smap->buckets), GFP_USER | __GFP_NOWARN);
  664. if (!smap->buckets) {
  665. err = -ENOMEM;
  666. goto free_smap;
  667. }
  668. for (i = 0; i < nbuckets; i++) {
  669. INIT_HLIST_HEAD(&smap->buckets[i].list);
  670. raw_spin_lock_init(&smap->buckets[i].lock);
  671. }
  672. smap->elem_size = offsetof(struct bpf_local_storage_elem,
  673. sdata.data[attr->value_size]);
  674. /* In PREEMPT_RT, kmalloc(GFP_ATOMIC) is still not safe in non
  675. * preemptible context. Thus, enforce all storages to use
  676. * bpf_mem_alloc when CONFIG_PREEMPT_RT is enabled.
  677. */
  678. smap->bpf_ma = IS_ENABLED(CONFIG_PREEMPT_RT) ? true : bpf_ma;
  679. if (smap->bpf_ma) {
  680. err = bpf_mem_alloc_init(&smap->selem_ma, smap->elem_size, false);
  681. if (err)
  682. goto free_smap;
  683. err = bpf_mem_alloc_init(&smap->storage_ma, sizeof(struct bpf_local_storage), false);
  684. if (err) {
  685. bpf_mem_alloc_destroy(&smap->selem_ma);
  686. goto free_smap;
  687. }
  688. }
  689. smap->cache_idx = bpf_local_storage_cache_idx_get(cache);
  690. return &smap->map;
  691. free_smap:
  692. kvfree(smap->buckets);
  693. bpf_map_area_free(smap);
  694. return ERR_PTR(err);
  695. }
  696. void bpf_local_storage_map_free(struct bpf_map *map,
  697. struct bpf_local_storage_cache *cache,
  698. int __percpu *busy_counter)
  699. {
  700. struct bpf_local_storage_map_bucket *b;
  701. struct bpf_local_storage_elem *selem;
  702. struct bpf_local_storage_map *smap;
  703. unsigned int i;
  704. smap = (struct bpf_local_storage_map *)map;
  705. bpf_local_storage_cache_idx_free(cache, smap->cache_idx);
  706. /* Note that this map might be concurrently cloned from
  707. * bpf_sk_storage_clone. Wait for any existing bpf_sk_storage_clone
  708. * RCU read section to finish before proceeding. New RCU
  709. * read sections should be prevented via bpf_map_inc_not_zero.
  710. */
  711. synchronize_rcu();
  712. /* bpf prog and the userspace can no longer access this map
  713. * now. No new selem (of this map) can be added
  714. * to the owner->storage or to the map bucket's list.
  715. *
  716. * The elem of this map can be cleaned up here
  717. * or when the storage is freed e.g.
  718. * by bpf_sk_storage_free() during __sk_destruct().
  719. */
  720. for (i = 0; i < (1U << smap->bucket_log); i++) {
  721. b = &smap->buckets[i];
  722. rcu_read_lock();
  723. /* No one is adding to b->list now */
  724. while ((selem = hlist_entry_safe(
  725. rcu_dereference_raw(hlist_first_rcu(&b->list)),
  726. struct bpf_local_storage_elem, map_node))) {
  727. if (busy_counter) {
  728. migrate_disable();
  729. this_cpu_inc(*busy_counter);
  730. }
  731. bpf_selem_unlink(selem, true);
  732. if (busy_counter) {
  733. this_cpu_dec(*busy_counter);
  734. migrate_enable();
  735. }
  736. cond_resched_rcu();
  737. }
  738. rcu_read_unlock();
  739. }
  740. /* While freeing the storage we may still need to access the map.
  741. *
  742. * e.g. when bpf_sk_storage_free() has unlinked selem from the map
  743. * which then made the above while((selem = ...)) loop
  744. * exit immediately.
  745. *
  746. * However, while freeing the storage one still needs to access the
  747. * smap->elem_size to do the uncharging in
  748. * bpf_selem_unlink_storage_nolock().
  749. *
  750. * Hence, wait another rcu grace period for the storage to be freed.
  751. */
  752. synchronize_rcu();
  753. if (smap->bpf_ma) {
  754. bpf_mem_alloc_destroy(&smap->selem_ma);
  755. bpf_mem_alloc_destroy(&smap->storage_ma);
  756. }
  757. kvfree(smap->buckets);
  758. bpf_map_area_free(smap);
  759. }