xdp.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /* net/core/xdp.c
  2. *
  3. * Copyright (c) 2017 Jesper Dangaard Brouer, Red Hat Inc.
  4. * Released under terms in GPL version 2. See COPYING.
  5. */
  6. #include <linux/types.h>
  7. #include <linux/mm.h>
  8. #include <linux/slab.h>
  9. #include <linux/idr.h>
  10. #include <linux/rhashtable.h>
  11. #include <net/page_pool.h>
  12. #include <net/xdp.h>
  13. #define REG_STATE_NEW 0x0
  14. #define REG_STATE_REGISTERED 0x1
  15. #define REG_STATE_UNREGISTERED 0x2
  16. #define REG_STATE_UNUSED 0x3
  17. static DEFINE_IDA(mem_id_pool);
  18. static DEFINE_MUTEX(mem_id_lock);
  19. #define MEM_ID_MAX 0xFFFE
  20. #define MEM_ID_MIN 1
  21. static int mem_id_next = MEM_ID_MIN;
  22. static bool mem_id_init; /* false */
  23. static struct rhashtable *mem_id_ht;
  24. struct xdp_mem_allocator {
  25. struct xdp_mem_info mem;
  26. union {
  27. void *allocator;
  28. struct page_pool *page_pool;
  29. struct zero_copy_allocator *zc_alloc;
  30. };
  31. struct rhash_head node;
  32. struct rcu_head rcu;
  33. };
  34. static u32 xdp_mem_id_hashfn(const void *data, u32 len, u32 seed)
  35. {
  36. const u32 *k = data;
  37. const u32 key = *k;
  38. BUILD_BUG_ON(FIELD_SIZEOF(struct xdp_mem_allocator, mem.id)
  39. != sizeof(u32));
  40. /* Use cyclic increasing ID as direct hash key, see rht_bucket_index */
  41. return key << RHT_HASH_RESERVED_SPACE;
  42. }
  43. static int xdp_mem_id_cmp(struct rhashtable_compare_arg *arg,
  44. const void *ptr)
  45. {
  46. const struct xdp_mem_allocator *xa = ptr;
  47. u32 mem_id = *(u32 *)arg->key;
  48. return xa->mem.id != mem_id;
  49. }
  50. static const struct rhashtable_params mem_id_rht_params = {
  51. .nelem_hint = 64,
  52. .head_offset = offsetof(struct xdp_mem_allocator, node),
  53. .key_offset = offsetof(struct xdp_mem_allocator, mem.id),
  54. .key_len = FIELD_SIZEOF(struct xdp_mem_allocator, mem.id),
  55. .max_size = MEM_ID_MAX,
  56. .min_size = 8,
  57. .automatic_shrinking = true,
  58. .hashfn = xdp_mem_id_hashfn,
  59. .obj_cmpfn = xdp_mem_id_cmp,
  60. };
  61. static void __xdp_mem_allocator_rcu_free(struct rcu_head *rcu)
  62. {
  63. struct xdp_mem_allocator *xa;
  64. xa = container_of(rcu, struct xdp_mem_allocator, rcu);
  65. /* Allow this ID to be reused */
  66. ida_simple_remove(&mem_id_pool, xa->mem.id);
  67. /* Notice, driver is expected to free the *allocator,
  68. * e.g. page_pool, and MUST also use RCU free.
  69. */
  70. /* Poison memory */
  71. xa->mem.id = 0xFFFF;
  72. xa->mem.type = 0xF0F0;
  73. xa->allocator = (void *)0xDEAD9001;
  74. kfree(xa);
  75. }
  76. static void __xdp_rxq_info_unreg_mem_model(struct xdp_rxq_info *xdp_rxq)
  77. {
  78. struct xdp_mem_allocator *xa;
  79. int id = xdp_rxq->mem.id;
  80. int err;
  81. if (id == 0)
  82. return;
  83. mutex_lock(&mem_id_lock);
  84. xa = rhashtable_lookup(mem_id_ht, &id, mem_id_rht_params);
  85. if (!xa) {
  86. mutex_unlock(&mem_id_lock);
  87. return;
  88. }
  89. err = rhashtable_remove_fast(mem_id_ht, &xa->node, mem_id_rht_params);
  90. WARN_ON(err);
  91. call_rcu(&xa->rcu, __xdp_mem_allocator_rcu_free);
  92. mutex_unlock(&mem_id_lock);
  93. }
  94. void xdp_rxq_info_unreg(struct xdp_rxq_info *xdp_rxq)
  95. {
  96. /* Simplify driver cleanup code paths, allow unreg "unused" */
  97. if (xdp_rxq->reg_state == REG_STATE_UNUSED)
  98. return;
  99. WARN(!(xdp_rxq->reg_state == REG_STATE_REGISTERED), "Driver BUG");
  100. __xdp_rxq_info_unreg_mem_model(xdp_rxq);
  101. xdp_rxq->reg_state = REG_STATE_UNREGISTERED;
  102. xdp_rxq->dev = NULL;
  103. /* Reset mem info to defaults */
  104. xdp_rxq->mem.id = 0;
  105. xdp_rxq->mem.type = 0;
  106. }
  107. EXPORT_SYMBOL_GPL(xdp_rxq_info_unreg);
  108. static void xdp_rxq_info_init(struct xdp_rxq_info *xdp_rxq)
  109. {
  110. memset(xdp_rxq, 0, sizeof(*xdp_rxq));
  111. }
  112. /* Returns 0 on success, negative on failure */
  113. int xdp_rxq_info_reg(struct xdp_rxq_info *xdp_rxq,
  114. struct net_device *dev, u32 queue_index)
  115. {
  116. if (xdp_rxq->reg_state == REG_STATE_UNUSED) {
  117. WARN(1, "Driver promised not to register this");
  118. return -EINVAL;
  119. }
  120. if (xdp_rxq->reg_state == REG_STATE_REGISTERED) {
  121. WARN(1, "Missing unregister, handled but fix driver");
  122. xdp_rxq_info_unreg(xdp_rxq);
  123. }
  124. if (!dev) {
  125. WARN(1, "Missing net_device from driver");
  126. return -ENODEV;
  127. }
  128. /* State either UNREGISTERED or NEW */
  129. xdp_rxq_info_init(xdp_rxq);
  130. xdp_rxq->dev = dev;
  131. xdp_rxq->queue_index = queue_index;
  132. xdp_rxq->reg_state = REG_STATE_REGISTERED;
  133. return 0;
  134. }
  135. EXPORT_SYMBOL_GPL(xdp_rxq_info_reg);
  136. void xdp_rxq_info_unused(struct xdp_rxq_info *xdp_rxq)
  137. {
  138. xdp_rxq->reg_state = REG_STATE_UNUSED;
  139. }
  140. EXPORT_SYMBOL_GPL(xdp_rxq_info_unused);
  141. bool xdp_rxq_info_is_reg(struct xdp_rxq_info *xdp_rxq)
  142. {
  143. return (xdp_rxq->reg_state == REG_STATE_REGISTERED);
  144. }
  145. EXPORT_SYMBOL_GPL(xdp_rxq_info_is_reg);
  146. static int __mem_id_init_hash_table(void)
  147. {
  148. struct rhashtable *rht;
  149. int ret;
  150. if (unlikely(mem_id_init))
  151. return 0;
  152. rht = kzalloc(sizeof(*rht), GFP_KERNEL);
  153. if (!rht)
  154. return -ENOMEM;
  155. ret = rhashtable_init(rht, &mem_id_rht_params);
  156. if (ret < 0) {
  157. kfree(rht);
  158. return ret;
  159. }
  160. mem_id_ht = rht;
  161. smp_mb(); /* mutex lock should provide enough pairing */
  162. mem_id_init = true;
  163. return 0;
  164. }
  165. /* Allocate a cyclic ID that maps to allocator pointer.
  166. * See: https://www.kernel.org/doc/html/latest/core-api/idr.html
  167. *
  168. * Caller must lock mem_id_lock.
  169. */
  170. static int __mem_id_cyclic_get(gfp_t gfp)
  171. {
  172. int retries = 1;
  173. int id;
  174. again:
  175. id = ida_simple_get(&mem_id_pool, mem_id_next, MEM_ID_MAX, gfp);
  176. if (id < 0) {
  177. if (id == -ENOSPC) {
  178. /* Cyclic allocator, reset next id */
  179. if (retries--) {
  180. mem_id_next = MEM_ID_MIN;
  181. goto again;
  182. }
  183. }
  184. return id; /* errno */
  185. }
  186. mem_id_next = id + 1;
  187. return id;
  188. }
  189. static bool __is_supported_mem_type(enum xdp_mem_type type)
  190. {
  191. if (type == MEM_TYPE_PAGE_POOL)
  192. return is_page_pool_compiled_in();
  193. if (type >= MEM_TYPE_MAX)
  194. return false;
  195. return true;
  196. }
  197. int xdp_rxq_info_reg_mem_model(struct xdp_rxq_info *xdp_rxq,
  198. enum xdp_mem_type type, void *allocator)
  199. {
  200. struct xdp_mem_allocator *xdp_alloc;
  201. gfp_t gfp = GFP_KERNEL;
  202. int id, errno, ret;
  203. void *ptr;
  204. if (xdp_rxq->reg_state != REG_STATE_REGISTERED) {
  205. WARN(1, "Missing register, driver bug");
  206. return -EFAULT;
  207. }
  208. if (!__is_supported_mem_type(type))
  209. return -EOPNOTSUPP;
  210. xdp_rxq->mem.type = type;
  211. if (!allocator) {
  212. if (type == MEM_TYPE_PAGE_POOL || type == MEM_TYPE_ZERO_COPY)
  213. return -EINVAL; /* Setup time check page_pool req */
  214. return 0;
  215. }
  216. /* Delay init of rhashtable to save memory if feature isn't used */
  217. if (!mem_id_init) {
  218. mutex_lock(&mem_id_lock);
  219. ret = __mem_id_init_hash_table();
  220. mutex_unlock(&mem_id_lock);
  221. if (ret < 0) {
  222. WARN_ON(1);
  223. return ret;
  224. }
  225. }
  226. xdp_alloc = kzalloc(sizeof(*xdp_alloc), gfp);
  227. if (!xdp_alloc)
  228. return -ENOMEM;
  229. mutex_lock(&mem_id_lock);
  230. id = __mem_id_cyclic_get(gfp);
  231. if (id < 0) {
  232. errno = id;
  233. goto err;
  234. }
  235. xdp_rxq->mem.id = id;
  236. xdp_alloc->mem = xdp_rxq->mem;
  237. xdp_alloc->allocator = allocator;
  238. /* Insert allocator into ID lookup table */
  239. ptr = rhashtable_insert_slow(mem_id_ht, &id, &xdp_alloc->node);
  240. if (IS_ERR(ptr)) {
  241. errno = PTR_ERR(ptr);
  242. goto err;
  243. }
  244. mutex_unlock(&mem_id_lock);
  245. return 0;
  246. err:
  247. mutex_unlock(&mem_id_lock);
  248. kfree(xdp_alloc);
  249. return errno;
  250. }
  251. EXPORT_SYMBOL_GPL(xdp_rxq_info_reg_mem_model);
  252. /* XDP RX runs under NAPI protection, and in different delivery error
  253. * scenarios (e.g. queue full), it is possible to return the xdp_frame
  254. * while still leveraging this protection. The @napi_direct boolian
  255. * is used for those calls sites. Thus, allowing for faster recycling
  256. * of xdp_frames/pages in those cases.
  257. */
  258. static void __xdp_return(void *data, struct xdp_mem_info *mem, bool napi_direct,
  259. unsigned long handle)
  260. {
  261. struct xdp_mem_allocator *xa;
  262. struct page *page;
  263. switch (mem->type) {
  264. case MEM_TYPE_PAGE_POOL:
  265. rcu_read_lock();
  266. /* mem->id is valid, checked in xdp_rxq_info_reg_mem_model() */
  267. xa = rhashtable_lookup(mem_id_ht, &mem->id, mem_id_rht_params);
  268. page = virt_to_head_page(data);
  269. if (xa)
  270. page_pool_put_page(xa->page_pool, page, napi_direct);
  271. else
  272. put_page(page);
  273. rcu_read_unlock();
  274. break;
  275. case MEM_TYPE_PAGE_SHARED:
  276. page_frag_free(data);
  277. break;
  278. case MEM_TYPE_PAGE_ORDER0:
  279. page = virt_to_page(data); /* Assumes order0 page*/
  280. put_page(page);
  281. break;
  282. case MEM_TYPE_ZERO_COPY:
  283. /* NB! Only valid from an xdp_buff! */
  284. rcu_read_lock();
  285. /* mem->id is valid, checked in xdp_rxq_info_reg_mem_model() */
  286. xa = rhashtable_lookup(mem_id_ht, &mem->id, mem_id_rht_params);
  287. xa->zc_alloc->free(xa->zc_alloc, handle);
  288. rcu_read_unlock();
  289. default:
  290. /* Not possible, checked in xdp_rxq_info_reg_mem_model() */
  291. break;
  292. }
  293. }
  294. void xdp_return_frame(struct xdp_frame *xdpf)
  295. {
  296. __xdp_return(xdpf->data, &xdpf->mem, false, 0);
  297. }
  298. EXPORT_SYMBOL_GPL(xdp_return_frame);
  299. void xdp_return_frame_rx_napi(struct xdp_frame *xdpf)
  300. {
  301. __xdp_return(xdpf->data, &xdpf->mem, true, 0);
  302. }
  303. EXPORT_SYMBOL_GPL(xdp_return_frame_rx_napi);
  304. void xdp_return_buff(struct xdp_buff *xdp)
  305. {
  306. __xdp_return(xdp->data, &xdp->rxq->mem, true, xdp->handle);
  307. }
  308. EXPORT_SYMBOL_GPL(xdp_return_buff);