bpf_local_storage.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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. selem = bpf_map_kzalloc(&smap->map, smap->elem_size,
  68. gfp_flags | __GFP_NOWARN);
  69. if (selem) {
  70. if (value)
  71. copy_map_value(&smap->map, SDATA(selem)->data, value);
  72. return selem;
  73. }
  74. if (charge_mem)
  75. mem_uncharge(smap, owner, smap->elem_size);
  76. return NULL;
  77. }
  78. void bpf_local_storage_free_rcu(struct rcu_head *rcu)
  79. {
  80. struct bpf_local_storage *local_storage;
  81. local_storage = container_of(rcu, struct bpf_local_storage, rcu);
  82. kfree_rcu(local_storage, rcu);
  83. }
  84. static void bpf_selem_free_rcu(struct rcu_head *rcu)
  85. {
  86. struct bpf_local_storage_elem *selem;
  87. selem = container_of(rcu, struct bpf_local_storage_elem, rcu);
  88. kfree_rcu(selem, rcu);
  89. }
  90. /* local_storage->lock must be held and selem->local_storage == local_storage.
  91. * The caller must ensure selem->smap is still valid to be
  92. * dereferenced for its smap->elem_size and smap->cache_idx.
  93. */
  94. bool bpf_selem_unlink_storage_nolock(struct bpf_local_storage *local_storage,
  95. struct bpf_local_storage_elem *selem,
  96. bool uncharge_mem, bool use_trace_rcu)
  97. {
  98. struct bpf_local_storage_map *smap;
  99. bool free_local_storage;
  100. void *owner;
  101. smap = rcu_dereference_check(SDATA(selem)->smap, bpf_rcu_lock_held());
  102. owner = local_storage->owner;
  103. /* All uncharging on the owner must be done first.
  104. * The owner may be freed once the last selem is unlinked
  105. * from local_storage.
  106. */
  107. if (uncharge_mem)
  108. mem_uncharge(smap, owner, smap->elem_size);
  109. free_local_storage = hlist_is_singular_node(&selem->snode,
  110. &local_storage->list);
  111. if (free_local_storage) {
  112. mem_uncharge(smap, owner, sizeof(struct bpf_local_storage));
  113. local_storage->owner = NULL;
  114. /* After this RCU_INIT, owner may be freed and cannot be used */
  115. RCU_INIT_POINTER(*owner_storage(smap, owner), NULL);
  116. /* local_storage is not freed now. local_storage->lock is
  117. * still held and raw_spin_unlock_bh(&local_storage->lock)
  118. * will be done by the caller.
  119. *
  120. * Although the unlock will be done under
  121. * rcu_read_lock(), it is more intuitive to
  122. * read if the freeing of the storage is done
  123. * after the raw_spin_unlock_bh(&local_storage->lock).
  124. *
  125. * Hence, a "bool free_local_storage" is returned
  126. * to the caller which then calls then frees the storage after
  127. * all the RCU grace periods have expired.
  128. */
  129. }
  130. hlist_del_init_rcu(&selem->snode);
  131. if (rcu_access_pointer(local_storage->cache[smap->cache_idx]) ==
  132. SDATA(selem))
  133. RCU_INIT_POINTER(local_storage->cache[smap->cache_idx], NULL);
  134. if (use_trace_rcu)
  135. call_rcu_tasks_trace(&selem->rcu, bpf_selem_free_rcu);
  136. else
  137. kfree_rcu(selem, rcu);
  138. return free_local_storage;
  139. }
  140. static void __bpf_selem_unlink_storage(struct bpf_local_storage_elem *selem,
  141. bool use_trace_rcu)
  142. {
  143. struct bpf_local_storage *local_storage;
  144. bool free_local_storage = false;
  145. unsigned long flags;
  146. if (unlikely(!selem_linked_to_storage_lockless(selem)))
  147. /* selem has already been unlinked from sk */
  148. return;
  149. local_storage = rcu_dereference_check(selem->local_storage,
  150. bpf_rcu_lock_held());
  151. raw_spin_lock_irqsave(&local_storage->lock, flags);
  152. if (likely(selem_linked_to_storage(selem)))
  153. free_local_storage = bpf_selem_unlink_storage_nolock(
  154. local_storage, selem, true, use_trace_rcu);
  155. raw_spin_unlock_irqrestore(&local_storage->lock, flags);
  156. if (free_local_storage) {
  157. if (use_trace_rcu)
  158. call_rcu_tasks_trace(&local_storage->rcu,
  159. bpf_local_storage_free_rcu);
  160. else
  161. kfree_rcu(local_storage, rcu);
  162. }
  163. }
  164. void bpf_selem_link_storage_nolock(struct bpf_local_storage *local_storage,
  165. struct bpf_local_storage_elem *selem)
  166. {
  167. RCU_INIT_POINTER(selem->local_storage, local_storage);
  168. hlist_add_head_rcu(&selem->snode, &local_storage->list);
  169. }
  170. void bpf_selem_unlink_map(struct bpf_local_storage_elem *selem)
  171. {
  172. struct bpf_local_storage_map *smap;
  173. struct bpf_local_storage_map_bucket *b;
  174. unsigned long flags;
  175. if (unlikely(!selem_linked_to_map_lockless(selem)))
  176. /* selem has already be unlinked from smap */
  177. return;
  178. smap = rcu_dereference_check(SDATA(selem)->smap, bpf_rcu_lock_held());
  179. b = select_bucket(smap, selem);
  180. raw_spin_lock_irqsave(&b->lock, flags);
  181. if (likely(selem_linked_to_map(selem)))
  182. hlist_del_init_rcu(&selem->map_node);
  183. raw_spin_unlock_irqrestore(&b->lock, flags);
  184. }
  185. void bpf_selem_link_map(struct bpf_local_storage_map *smap,
  186. struct bpf_local_storage_elem *selem)
  187. {
  188. struct bpf_local_storage_map_bucket *b = select_bucket(smap, selem);
  189. unsigned long flags;
  190. raw_spin_lock_irqsave(&b->lock, flags);
  191. RCU_INIT_POINTER(SDATA(selem)->smap, smap);
  192. hlist_add_head_rcu(&selem->map_node, &b->list);
  193. raw_spin_unlock_irqrestore(&b->lock, flags);
  194. }
  195. void bpf_selem_unlink(struct bpf_local_storage_elem *selem, bool use_trace_rcu)
  196. {
  197. /* Always unlink from map before unlinking from local_storage
  198. * because selem will be freed after successfully unlinked from
  199. * the local_storage.
  200. */
  201. bpf_selem_unlink_map(selem);
  202. __bpf_selem_unlink_storage(selem, use_trace_rcu);
  203. }
  204. struct bpf_local_storage_data *
  205. bpf_local_storage_lookup(struct bpf_local_storage *local_storage,
  206. struct bpf_local_storage_map *smap,
  207. bool cacheit_lockit)
  208. {
  209. struct bpf_local_storage_data *sdata;
  210. struct bpf_local_storage_elem *selem;
  211. /* Fast path (cache hit) */
  212. sdata = rcu_dereference_check(local_storage->cache[smap->cache_idx],
  213. bpf_rcu_lock_held());
  214. if (sdata && rcu_access_pointer(sdata->smap) == smap)
  215. return sdata;
  216. /* Slow path (cache miss) */
  217. hlist_for_each_entry_rcu(selem, &local_storage->list, snode,
  218. rcu_read_lock_trace_held())
  219. if (rcu_access_pointer(SDATA(selem)->smap) == smap)
  220. break;
  221. if (!selem)
  222. return NULL;
  223. sdata = SDATA(selem);
  224. if (cacheit_lockit) {
  225. unsigned long flags;
  226. /* spinlock is needed to avoid racing with the
  227. * parallel delete. Otherwise, publishing an already
  228. * deleted sdata to the cache will become a use-after-free
  229. * problem in the next bpf_local_storage_lookup().
  230. */
  231. raw_spin_lock_irqsave(&local_storage->lock, flags);
  232. if (selem_linked_to_storage(selem))
  233. rcu_assign_pointer(local_storage->cache[smap->cache_idx],
  234. sdata);
  235. raw_spin_unlock_irqrestore(&local_storage->lock, flags);
  236. }
  237. return sdata;
  238. }
  239. static int check_flags(const struct bpf_local_storage_data *old_sdata,
  240. u64 map_flags)
  241. {
  242. if (old_sdata && (map_flags & ~BPF_F_LOCK) == BPF_NOEXIST)
  243. /* elem already exists */
  244. return -EEXIST;
  245. if (!old_sdata && (map_flags & ~BPF_F_LOCK) == BPF_EXIST)
  246. /* elem doesn't exist, cannot update it */
  247. return -ENOENT;
  248. return 0;
  249. }
  250. int bpf_local_storage_alloc(void *owner,
  251. struct bpf_local_storage_map *smap,
  252. struct bpf_local_storage_elem *first_selem,
  253. gfp_t gfp_flags)
  254. {
  255. struct bpf_local_storage *prev_storage, *storage;
  256. struct bpf_local_storage **owner_storage_ptr;
  257. int err;
  258. err = mem_charge(smap, owner, sizeof(*storage));
  259. if (err)
  260. return err;
  261. storage = bpf_map_kzalloc(&smap->map, sizeof(*storage),
  262. gfp_flags | __GFP_NOWARN);
  263. if (!storage) {
  264. err = -ENOMEM;
  265. goto uncharge;
  266. }
  267. INIT_HLIST_HEAD(&storage->list);
  268. raw_spin_lock_init(&storage->lock);
  269. storage->owner = owner;
  270. bpf_selem_link_storage_nolock(storage, first_selem);
  271. bpf_selem_link_map(smap, first_selem);
  272. owner_storage_ptr =
  273. (struct bpf_local_storage **)owner_storage(smap, owner);
  274. /* Publish storage to the owner.
  275. * Instead of using any lock of the kernel object (i.e. owner),
  276. * cmpxchg will work with any kernel object regardless what
  277. * the running context is, bh, irq...etc.
  278. *
  279. * From now on, the owner->storage pointer (e.g. sk->sk_bpf_storage)
  280. * is protected by the storage->lock. Hence, when freeing
  281. * the owner->storage, the storage->lock must be held before
  282. * setting owner->storage ptr to NULL.
  283. */
  284. prev_storage = cmpxchg(owner_storage_ptr, NULL, storage);
  285. if (unlikely(prev_storage)) {
  286. bpf_selem_unlink_map(first_selem);
  287. err = -EAGAIN;
  288. goto uncharge;
  289. /* Note that even first_selem was linked to smap's
  290. * bucket->list, first_selem can be freed immediately
  291. * (instead of kfree_rcu) because
  292. * bpf_local_storage_map_free() does a
  293. * synchronize_rcu_mult (waiting for both sleepable and
  294. * normal programs) before walking the bucket->list.
  295. * Hence, no one is accessing selem from the
  296. * bucket->list under rcu_read_lock().
  297. */
  298. }
  299. return 0;
  300. uncharge:
  301. kfree(storage);
  302. mem_uncharge(smap, owner, sizeof(*storage));
  303. return err;
  304. }
  305. /* sk cannot be going away because it is linking new elem
  306. * to sk->sk_bpf_storage. (i.e. sk->sk_refcnt cannot be 0).
  307. * Otherwise, it will become a leak (and other memory issues
  308. * during map destruction).
  309. */
  310. struct bpf_local_storage_data *
  311. bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap,
  312. void *value, u64 map_flags, gfp_t gfp_flags)
  313. {
  314. struct bpf_local_storage_data *old_sdata = NULL;
  315. struct bpf_local_storage_elem *selem = NULL;
  316. struct bpf_local_storage *local_storage;
  317. unsigned long flags;
  318. int err;
  319. /* BPF_EXIST and BPF_NOEXIST cannot be both set */
  320. if (unlikely((map_flags & ~BPF_F_LOCK) > BPF_EXIST) ||
  321. /* BPF_F_LOCK can only be used in a value with spin_lock */
  322. unlikely((map_flags & BPF_F_LOCK) &&
  323. !map_value_has_spin_lock(&smap->map)))
  324. return ERR_PTR(-EINVAL);
  325. if (gfp_flags == GFP_KERNEL && (map_flags & ~BPF_F_LOCK) != BPF_NOEXIST)
  326. return ERR_PTR(-EINVAL);
  327. local_storage = rcu_dereference_check(*owner_storage(smap, owner),
  328. bpf_rcu_lock_held());
  329. if (!local_storage || hlist_empty(&local_storage->list)) {
  330. /* Very first elem for the owner */
  331. err = check_flags(NULL, map_flags);
  332. if (err)
  333. return ERR_PTR(err);
  334. selem = bpf_selem_alloc(smap, owner, value, true, gfp_flags);
  335. if (!selem)
  336. return ERR_PTR(-ENOMEM);
  337. err = bpf_local_storage_alloc(owner, smap, selem, gfp_flags);
  338. if (err) {
  339. kfree(selem);
  340. mem_uncharge(smap, owner, smap->elem_size);
  341. return ERR_PTR(err);
  342. }
  343. return SDATA(selem);
  344. }
  345. if ((map_flags & BPF_F_LOCK) && !(map_flags & BPF_NOEXIST)) {
  346. /* Hoping to find an old_sdata to do inline update
  347. * such that it can avoid taking the local_storage->lock
  348. * and changing the lists.
  349. */
  350. old_sdata =
  351. bpf_local_storage_lookup(local_storage, smap, false);
  352. err = check_flags(old_sdata, map_flags);
  353. if (err)
  354. return ERR_PTR(err);
  355. if (old_sdata && selem_linked_to_storage_lockless(SELEM(old_sdata))) {
  356. copy_map_value_locked(&smap->map, old_sdata->data,
  357. value, false);
  358. return old_sdata;
  359. }
  360. }
  361. if (gfp_flags == GFP_KERNEL) {
  362. selem = bpf_selem_alloc(smap, owner, value, true, gfp_flags);
  363. if (!selem)
  364. return ERR_PTR(-ENOMEM);
  365. }
  366. raw_spin_lock_irqsave(&local_storage->lock, flags);
  367. /* Recheck local_storage->list under local_storage->lock */
  368. if (unlikely(hlist_empty(&local_storage->list))) {
  369. /* A parallel del is happening and local_storage is going
  370. * away. It has just been checked before, so very
  371. * unlikely. Return instead of retry to keep things
  372. * simple.
  373. */
  374. err = -EAGAIN;
  375. goto unlock_err;
  376. }
  377. old_sdata = bpf_local_storage_lookup(local_storage, smap, false);
  378. err = check_flags(old_sdata, map_flags);
  379. if (err)
  380. goto unlock_err;
  381. if (old_sdata && (map_flags & BPF_F_LOCK)) {
  382. copy_map_value_locked(&smap->map, old_sdata->data, value,
  383. false);
  384. selem = SELEM(old_sdata);
  385. goto unlock;
  386. }
  387. if (gfp_flags != GFP_KERNEL) {
  388. /* local_storage->lock is held. Hence, we are sure
  389. * we can unlink and uncharge the old_sdata successfully
  390. * later. Hence, instead of charging the new selem now
  391. * and then uncharge the old selem later (which may cause
  392. * a potential but unnecessary charge failure), avoid taking
  393. * a charge at all here (the "!old_sdata" check) and the
  394. * old_sdata will not be uncharged later during
  395. * bpf_selem_unlink_storage_nolock().
  396. */
  397. selem = bpf_selem_alloc(smap, owner, value, !old_sdata, gfp_flags);
  398. if (!selem) {
  399. err = -ENOMEM;
  400. goto unlock_err;
  401. }
  402. }
  403. /* First, link the new selem to the map */
  404. bpf_selem_link_map(smap, selem);
  405. /* Second, link (and publish) the new selem to local_storage */
  406. bpf_selem_link_storage_nolock(local_storage, selem);
  407. /* Third, remove old selem, SELEM(old_sdata) */
  408. if (old_sdata) {
  409. bpf_selem_unlink_map(SELEM(old_sdata));
  410. bpf_selem_unlink_storage_nolock(local_storage, SELEM(old_sdata),
  411. false, true);
  412. }
  413. unlock:
  414. raw_spin_unlock_irqrestore(&local_storage->lock, flags);
  415. return SDATA(selem);
  416. unlock_err:
  417. raw_spin_unlock_irqrestore(&local_storage->lock, flags);
  418. if (selem) {
  419. mem_uncharge(smap, owner, smap->elem_size);
  420. kfree(selem);
  421. }
  422. return ERR_PTR(err);
  423. }
  424. u16 bpf_local_storage_cache_idx_get(struct bpf_local_storage_cache *cache)
  425. {
  426. u64 min_usage = U64_MAX;
  427. u16 i, res = 0;
  428. spin_lock(&cache->idx_lock);
  429. for (i = 0; i < BPF_LOCAL_STORAGE_CACHE_SIZE; i++) {
  430. if (cache->idx_usage_counts[i] < min_usage) {
  431. min_usage = cache->idx_usage_counts[i];
  432. res = i;
  433. /* Found a free cache_idx */
  434. if (!min_usage)
  435. break;
  436. }
  437. }
  438. cache->idx_usage_counts[res]++;
  439. spin_unlock(&cache->idx_lock);
  440. return res;
  441. }
  442. void bpf_local_storage_cache_idx_free(struct bpf_local_storage_cache *cache,
  443. u16 idx)
  444. {
  445. spin_lock(&cache->idx_lock);
  446. cache->idx_usage_counts[idx]--;
  447. spin_unlock(&cache->idx_lock);
  448. }
  449. void bpf_local_storage_map_free(struct bpf_local_storage_map *smap,
  450. int __percpu *busy_counter)
  451. {
  452. struct bpf_local_storage_elem *selem;
  453. struct bpf_local_storage_map_bucket *b;
  454. unsigned int i;
  455. /* Note that this map might be concurrently cloned from
  456. * bpf_sk_storage_clone. Wait for any existing bpf_sk_storage_clone
  457. * RCU read section to finish before proceeding. New RCU
  458. * read sections should be prevented via bpf_map_inc_not_zero.
  459. */
  460. synchronize_rcu();
  461. /* bpf prog and the userspace can no longer access this map
  462. * now. No new selem (of this map) can be added
  463. * to the owner->storage or to the map bucket's list.
  464. *
  465. * The elem of this map can be cleaned up here
  466. * or when the storage is freed e.g.
  467. * by bpf_sk_storage_free() during __sk_destruct().
  468. */
  469. for (i = 0; i < (1U << smap->bucket_log); i++) {
  470. b = &smap->buckets[i];
  471. rcu_read_lock();
  472. /* No one is adding to b->list now */
  473. while ((selem = hlist_entry_safe(
  474. rcu_dereference_raw(hlist_first_rcu(&b->list)),
  475. struct bpf_local_storage_elem, map_node))) {
  476. if (busy_counter) {
  477. migrate_disable();
  478. this_cpu_inc(*busy_counter);
  479. }
  480. bpf_selem_unlink(selem, false);
  481. if (busy_counter) {
  482. this_cpu_dec(*busy_counter);
  483. migrate_enable();
  484. }
  485. cond_resched_rcu();
  486. }
  487. rcu_read_unlock();
  488. }
  489. /* While freeing the storage we may still need to access the map.
  490. *
  491. * e.g. when bpf_sk_storage_free() has unlinked selem from the map
  492. * which then made the above while((selem = ...)) loop
  493. * exit immediately.
  494. *
  495. * However, while freeing the storage one still needs to access the
  496. * smap->elem_size to do the uncharging in
  497. * bpf_selem_unlink_storage_nolock().
  498. *
  499. * Hence, wait another rcu grace period for the storage to be freed.
  500. */
  501. synchronize_rcu();
  502. kvfree(smap->buckets);
  503. bpf_map_area_free(smap);
  504. }
  505. int bpf_local_storage_map_alloc_check(union bpf_attr *attr)
  506. {
  507. if (attr->map_flags & ~BPF_LOCAL_STORAGE_CREATE_FLAG_MASK ||
  508. !(attr->map_flags & BPF_F_NO_PREALLOC) ||
  509. attr->max_entries ||
  510. attr->key_size != sizeof(int) || !attr->value_size ||
  511. /* Enforce BTF for userspace sk dumping */
  512. !attr->btf_key_type_id || !attr->btf_value_type_id)
  513. return -EINVAL;
  514. if (!bpf_capable())
  515. return -EPERM;
  516. if (attr->value_size > BPF_LOCAL_STORAGE_MAX_VALUE_SIZE)
  517. return -E2BIG;
  518. return 0;
  519. }
  520. struct bpf_local_storage_map *bpf_local_storage_map_alloc(union bpf_attr *attr)
  521. {
  522. struct bpf_local_storage_map *smap;
  523. unsigned int i;
  524. u32 nbuckets;
  525. smap = bpf_map_area_alloc(sizeof(*smap), NUMA_NO_NODE);
  526. if (!smap)
  527. return ERR_PTR(-ENOMEM);
  528. bpf_map_init_from_attr(&smap->map, attr);
  529. nbuckets = roundup_pow_of_two(num_possible_cpus());
  530. /* Use at least 2 buckets, select_bucket() is undefined behavior with 1 bucket */
  531. nbuckets = max_t(u32, 2, nbuckets);
  532. smap->bucket_log = ilog2(nbuckets);
  533. smap->buckets = kvcalloc(sizeof(*smap->buckets), nbuckets,
  534. GFP_USER | __GFP_NOWARN | __GFP_ACCOUNT);
  535. if (!smap->buckets) {
  536. bpf_map_area_free(smap);
  537. return ERR_PTR(-ENOMEM);
  538. }
  539. for (i = 0; i < nbuckets; i++) {
  540. INIT_HLIST_HEAD(&smap->buckets[i].list);
  541. raw_spin_lock_init(&smap->buckets[i].lock);
  542. }
  543. smap->elem_size =
  544. sizeof(struct bpf_local_storage_elem) + attr->value_size;
  545. return smap;
  546. }
  547. int bpf_local_storage_map_check_btf(const struct bpf_map *map,
  548. const struct btf *btf,
  549. const struct btf_type *key_type,
  550. const struct btf_type *value_type)
  551. {
  552. u32 int_data;
  553. if (BTF_INFO_KIND(key_type->info) != BTF_KIND_INT)
  554. return -EINVAL;
  555. int_data = *(u32 *)(key_type + 1);
  556. if (BTF_INT_BITS(int_data) != 32 || BTF_INT_OFFSET(int_data))
  557. return -EINVAL;
  558. return 0;
  559. }