1c0c77d8fSBjörn Töpel // SPDX-License-Identifier: GPL-2.0 2c0c77d8fSBjörn Töpel /* XDP user-space packet buffer 3c0c77d8fSBjörn Töpel * Copyright(c) 2018 Intel Corporation. 4c0c77d8fSBjörn Töpel */ 5c0c77d8fSBjörn Töpel 6c0c77d8fSBjörn Töpel #include <linux/init.h> 7c0c77d8fSBjörn Töpel #include <linux/sched/mm.h> 8c0c77d8fSBjörn Töpel #include <linux/sched/signal.h> 9c0c77d8fSBjörn Töpel #include <linux/sched/task.h> 10c0c77d8fSBjörn Töpel #include <linux/uaccess.h> 11c0c77d8fSBjörn Töpel #include <linux/slab.h> 12c0c77d8fSBjörn Töpel #include <linux/bpf.h> 13c0c77d8fSBjörn Töpel #include <linux/mm.h> 1484c6b868SJakub Kicinski #include <linux/netdevice.h> 1584c6b868SJakub Kicinski #include <linux/rtnetlink.h> 1650e74c01SBjörn Töpel #include <linux/idr.h> 17c0c77d8fSBjörn Töpel 18c0c77d8fSBjörn Töpel #include "xdp_umem.h" 19e61e62b9SBjörn Töpel #include "xsk_queue.h" 20c0c77d8fSBjörn Töpel 21bbff2f32SBjörn Töpel #define XDP_UMEM_MIN_CHUNK_SIZE 2048 22c0c77d8fSBjörn Töpel 2350e74c01SBjörn Töpel static DEFINE_IDA(umem_ida); 2450e74c01SBjörn Töpel 25ac98d8aaSMagnus Karlsson void xdp_add_sk_umem(struct xdp_umem *umem, struct xdp_sock *xs) 26ac98d8aaSMagnus Karlsson { 27ac98d8aaSMagnus Karlsson unsigned long flags; 28ac98d8aaSMagnus Karlsson 29ac98d8aaSMagnus Karlsson spin_lock_irqsave(&umem->xsk_list_lock, flags); 30ac98d8aaSMagnus Karlsson list_add_rcu(&xs->list, &umem->xsk_list); 31ac98d8aaSMagnus Karlsson spin_unlock_irqrestore(&umem->xsk_list_lock, flags); 32ac98d8aaSMagnus Karlsson } 33ac98d8aaSMagnus Karlsson 34ac98d8aaSMagnus Karlsson void xdp_del_sk_umem(struct xdp_umem *umem, struct xdp_sock *xs) 35ac98d8aaSMagnus Karlsson { 36ac98d8aaSMagnus Karlsson unsigned long flags; 37ac98d8aaSMagnus Karlsson 38ac98d8aaSMagnus Karlsson spin_lock_irqsave(&umem->xsk_list_lock, flags); 39ac98d8aaSMagnus Karlsson list_del_rcu(&xs->list); 40ac98d8aaSMagnus Karlsson spin_unlock_irqrestore(&umem->xsk_list_lock, flags); 41ac98d8aaSMagnus Karlsson } 42ac98d8aaSMagnus Karlsson 43c9b47cc1SMagnus Karlsson /* The umem is stored both in the _rx struct and the _tx struct as we do 44c9b47cc1SMagnus Karlsson * not know if the device has more tx queues than rx, or the opposite. 45c9b47cc1SMagnus Karlsson * This might also change during run time. 46c9b47cc1SMagnus Karlsson */ 47cc5b5d35SKrzysztof Kazimierczak static int xdp_reg_umem_at_qid(struct net_device *dev, struct xdp_umem *umem, 48c9b47cc1SMagnus Karlsson u16 queue_id) 4984c6b868SJakub Kicinski { 50cc5b5d35SKrzysztof Kazimierczak if (queue_id >= max_t(unsigned int, 51cc5b5d35SKrzysztof Kazimierczak dev->real_num_rx_queues, 52cc5b5d35SKrzysztof Kazimierczak dev->real_num_tx_queues)) 53cc5b5d35SKrzysztof Kazimierczak return -EINVAL; 54cc5b5d35SKrzysztof Kazimierczak 55c9b47cc1SMagnus Karlsson if (queue_id < dev->real_num_rx_queues) 56c9b47cc1SMagnus Karlsson dev->_rx[queue_id].umem = umem; 57c9b47cc1SMagnus Karlsson if (queue_id < dev->real_num_tx_queues) 58c9b47cc1SMagnus Karlsson dev->_tx[queue_id].umem = umem; 59cc5b5d35SKrzysztof Kazimierczak 60cc5b5d35SKrzysztof Kazimierczak return 0; 61c9b47cc1SMagnus Karlsson } 6284c6b868SJakub Kicinski 631661d346SJakub Kicinski struct xdp_umem *xdp_get_umem_from_qid(struct net_device *dev, 64c9b47cc1SMagnus Karlsson u16 queue_id) 65c9b47cc1SMagnus Karlsson { 66c9b47cc1SMagnus Karlsson if (queue_id < dev->real_num_rx_queues) 67c9b47cc1SMagnus Karlsson return dev->_rx[queue_id].umem; 68c9b47cc1SMagnus Karlsson if (queue_id < dev->real_num_tx_queues) 69c9b47cc1SMagnus Karlsson return dev->_tx[queue_id].umem; 7084c6b868SJakub Kicinski 71c9b47cc1SMagnus Karlsson return NULL; 72c9b47cc1SMagnus Karlsson } 735f4f3b2dSJan Sokolowski EXPORT_SYMBOL(xdp_get_umem_from_qid); 7484c6b868SJakub Kicinski 75c9b47cc1SMagnus Karlsson static void xdp_clear_umem_at_qid(struct net_device *dev, u16 queue_id) 76c9b47cc1SMagnus Karlsson { 77a41b4f3cSMagnus Karlsson if (queue_id < dev->real_num_rx_queues) 78c9b47cc1SMagnus Karlsson dev->_rx[queue_id].umem = NULL; 79a41b4f3cSMagnus Karlsson if (queue_id < dev->real_num_tx_queues) 80c9b47cc1SMagnus Karlsson dev->_tx[queue_id].umem = NULL; 8184c6b868SJakub Kicinski } 8284c6b868SJakub Kicinski 83173d3adbSBjörn Töpel int xdp_umem_assign_dev(struct xdp_umem *umem, struct net_device *dev, 84c9b47cc1SMagnus Karlsson u16 queue_id, u16 flags) 85173d3adbSBjörn Töpel { 86173d3adbSBjörn Töpel bool force_zc, force_copy; 87173d3adbSBjörn Töpel struct netdev_bpf bpf; 88c9b47cc1SMagnus Karlsson int err = 0; 89173d3adbSBjörn Töpel 90173d3adbSBjörn Töpel force_zc = flags & XDP_ZEROCOPY; 91173d3adbSBjörn Töpel force_copy = flags & XDP_COPY; 92173d3adbSBjörn Töpel 93173d3adbSBjörn Töpel if (force_zc && force_copy) 94173d3adbSBjörn Töpel return -EINVAL; 95173d3adbSBjörn Töpel 96173d3adbSBjörn Töpel rtnl_lock(); 97c9b47cc1SMagnus Karlsson if (xdp_get_umem_from_qid(dev, queue_id)) { 98c9b47cc1SMagnus Karlsson err = -EBUSY; 99c9b47cc1SMagnus Karlsson goto out_rtnl_unlock; 100c9b47cc1SMagnus Karlsson } 101c9b47cc1SMagnus Karlsson 102cc5b5d35SKrzysztof Kazimierczak err = xdp_reg_umem_at_qid(dev, umem, queue_id); 103cc5b5d35SKrzysztof Kazimierczak if (err) 104cc5b5d35SKrzysztof Kazimierczak goto out_rtnl_unlock; 105cc5b5d35SKrzysztof Kazimierczak 106c9b47cc1SMagnus Karlsson umem->dev = dev; 107c9b47cc1SMagnus Karlsson umem->queue_id = queue_id; 108c9b47cc1SMagnus Karlsson if (force_copy) 109c9b47cc1SMagnus Karlsson /* For copy-mode, we are done. */ 110c9b47cc1SMagnus Karlsson goto out_rtnl_unlock; 111c9b47cc1SMagnus Karlsson 112c9b47cc1SMagnus Karlsson if (!dev->netdev_ops->ndo_bpf || 113c9b47cc1SMagnus Karlsson !dev->netdev_ops->ndo_xsk_async_xmit) { 114c9b47cc1SMagnus Karlsson err = -EOPNOTSUPP; 115c9b47cc1SMagnus Karlsson goto err_unreg_umem; 11684c6b868SJakub Kicinski } 117173d3adbSBjörn Töpel 118173d3adbSBjörn Töpel bpf.command = XDP_SETUP_XSK_UMEM; 119173d3adbSBjörn Töpel bpf.xsk.umem = umem; 120173d3adbSBjörn Töpel bpf.xsk.queue_id = queue_id; 121173d3adbSBjörn Töpel 122173d3adbSBjörn Töpel err = dev->netdev_ops->ndo_bpf(dev, &bpf); 123f734607eSJakub Kicinski if (err) 124c9b47cc1SMagnus Karlsson goto err_unreg_umem; 12584c6b868SJakub Kicinski rtnl_unlock(); 126173d3adbSBjörn Töpel 127f734607eSJakub Kicinski dev_hold(dev); 128173d3adbSBjörn Töpel umem->zc = true; 129173d3adbSBjörn Töpel return 0; 13084c6b868SJakub Kicinski 131c9b47cc1SMagnus Karlsson err_unreg_umem: 132c9b47cc1SMagnus Karlsson if (!force_zc) 133c9b47cc1SMagnus Karlsson err = 0; /* fallback to copy mode */ 1341e405c1aSBjörn Töpel if (err) 1351e405c1aSBjörn Töpel xdp_clear_umem_at_qid(dev, queue_id); 136c9b47cc1SMagnus Karlsson out_rtnl_unlock: 13784c6b868SJakub Kicinski rtnl_unlock(); 138c9b47cc1SMagnus Karlsson return err; 139173d3adbSBjörn Töpel } 140173d3adbSBjörn Töpel 141ac98d8aaSMagnus Karlsson static void xdp_umem_clear_dev(struct xdp_umem *umem) 142173d3adbSBjörn Töpel { 143173d3adbSBjörn Töpel struct netdev_bpf bpf; 144173d3adbSBjörn Töpel int err; 145173d3adbSBjörn Töpel 146c9b47cc1SMagnus Karlsson if (umem->zc) { 147173d3adbSBjörn Töpel bpf.command = XDP_SETUP_XSK_UMEM; 148173d3adbSBjörn Töpel bpf.xsk.umem = NULL; 149173d3adbSBjörn Töpel bpf.xsk.queue_id = umem->queue_id; 150173d3adbSBjörn Töpel 151173d3adbSBjörn Töpel rtnl_lock(); 152173d3adbSBjörn Töpel err = umem->dev->netdev_ops->ndo_bpf(umem->dev, &bpf); 153173d3adbSBjörn Töpel rtnl_unlock(); 154173d3adbSBjörn Töpel 155173d3adbSBjörn Töpel if (err) 156173d3adbSBjörn Töpel WARN(1, "failed to disable umem!\n"); 157c9b47cc1SMagnus Karlsson } 158173d3adbSBjörn Töpel 159c9b47cc1SMagnus Karlsson if (umem->dev) { 160c9b47cc1SMagnus Karlsson rtnl_lock(); 161c9b47cc1SMagnus Karlsson xdp_clear_umem_at_qid(umem->dev, umem->queue_id); 162c9b47cc1SMagnus Karlsson rtnl_unlock(); 163c9b47cc1SMagnus Karlsson } 164c9b47cc1SMagnus Karlsson 165c9b47cc1SMagnus Karlsson if (umem->zc) { 166173d3adbSBjörn Töpel dev_put(umem->dev); 167c9b47cc1SMagnus Karlsson umem->zc = false; 168173d3adbSBjörn Töpel } 169173d3adbSBjörn Töpel } 170173d3adbSBjörn Töpel 171c0c77d8fSBjörn Töpel static void xdp_umem_unpin_pages(struct xdp_umem *umem) 172c0c77d8fSBjörn Töpel { 173c0c77d8fSBjörn Töpel unsigned int i; 174c0c77d8fSBjörn Töpel 175c0c77d8fSBjörn Töpel for (i = 0; i < umem->npgs; i++) { 176c0c77d8fSBjörn Töpel struct page *page = umem->pgs[i]; 177c0c77d8fSBjörn Töpel 178c0c77d8fSBjörn Töpel set_page_dirty_lock(page); 179c0c77d8fSBjörn Töpel put_page(page); 180c0c77d8fSBjörn Töpel } 181c0c77d8fSBjörn Töpel 182c0c77d8fSBjörn Töpel kfree(umem->pgs); 183c0c77d8fSBjörn Töpel umem->pgs = NULL; 184c0c77d8fSBjörn Töpel } 185c0c77d8fSBjörn Töpel 186c0c77d8fSBjörn Töpel static void xdp_umem_unaccount_pages(struct xdp_umem *umem) 187c0c77d8fSBjörn Töpel { 188c09290c5SDaniel Borkmann if (umem->user) { 189c0c77d8fSBjörn Töpel atomic_long_sub(umem->npgs, &umem->user->locked_vm); 190c0c77d8fSBjörn Töpel free_uid(umem->user); 191c0c77d8fSBjörn Töpel } 192c09290c5SDaniel Borkmann } 193c0c77d8fSBjörn Töpel 194c0c77d8fSBjörn Töpel static void xdp_umem_release(struct xdp_umem *umem) 195c0c77d8fSBjörn Töpel { 196173d3adbSBjörn Töpel xdp_umem_clear_dev(umem); 197173d3adbSBjörn Töpel 19850e74c01SBjörn Töpel ida_simple_remove(&umem_ida, umem->id); 19950e74c01SBjörn Töpel 200423f3832SMagnus Karlsson if (umem->fq) { 201423f3832SMagnus Karlsson xskq_destroy(umem->fq); 202423f3832SMagnus Karlsson umem->fq = NULL; 203423f3832SMagnus Karlsson } 204423f3832SMagnus Karlsson 205fe230832SMagnus Karlsson if (umem->cq) { 206fe230832SMagnus Karlsson xskq_destroy(umem->cq); 207fe230832SMagnus Karlsson umem->cq = NULL; 208fe230832SMagnus Karlsson } 209fe230832SMagnus Karlsson 210f5bd9138SJakub Kicinski xsk_reuseq_destroy(umem); 211f5bd9138SJakub Kicinski 212c0c77d8fSBjörn Töpel xdp_umem_unpin_pages(umem); 213c0c77d8fSBjörn Töpel 2148aef7340SBjörn Töpel kfree(umem->pages); 2158aef7340SBjörn Töpel umem->pages = NULL; 2168aef7340SBjörn Töpel 217c0c77d8fSBjörn Töpel xdp_umem_unaccount_pages(umem); 218c0c77d8fSBjörn Töpel kfree(umem); 219c0c77d8fSBjörn Töpel } 220c0c77d8fSBjörn Töpel 221c0c77d8fSBjörn Töpel static void xdp_umem_release_deferred(struct work_struct *work) 222c0c77d8fSBjörn Töpel { 223c0c77d8fSBjörn Töpel struct xdp_umem *umem = container_of(work, struct xdp_umem, work); 224c0c77d8fSBjörn Töpel 225c0c77d8fSBjörn Töpel xdp_umem_release(umem); 226c0c77d8fSBjörn Töpel } 227c0c77d8fSBjörn Töpel 228c0c77d8fSBjörn Töpel void xdp_get_umem(struct xdp_umem *umem) 229c0c77d8fSBjörn Töpel { 230d3b42f14SBjörn Töpel refcount_inc(&umem->users); 231c0c77d8fSBjörn Töpel } 232c0c77d8fSBjörn Töpel 233c0c77d8fSBjörn Töpel void xdp_put_umem(struct xdp_umem *umem) 234c0c77d8fSBjörn Töpel { 235c0c77d8fSBjörn Töpel if (!umem) 236c0c77d8fSBjörn Töpel return; 237c0c77d8fSBjörn Töpel 238d3b42f14SBjörn Töpel if (refcount_dec_and_test(&umem->users)) { 239c0c77d8fSBjörn Töpel INIT_WORK(&umem->work, xdp_umem_release_deferred); 240c0c77d8fSBjörn Töpel schedule_work(&umem->work); 241c0c77d8fSBjörn Töpel } 242c0c77d8fSBjörn Töpel } 243c0c77d8fSBjörn Töpel 244c0c77d8fSBjörn Töpel static int xdp_umem_pin_pages(struct xdp_umem *umem) 245c0c77d8fSBjörn Töpel { 246c0c77d8fSBjörn Töpel unsigned int gup_flags = FOLL_WRITE; 247c0c77d8fSBjörn Töpel long npgs; 248c0c77d8fSBjörn Töpel int err; 249c0c77d8fSBjörn Töpel 250a343993cSBjörn Töpel umem->pgs = kcalloc(umem->npgs, sizeof(*umem->pgs), 251a343993cSBjörn Töpel GFP_KERNEL | __GFP_NOWARN); 252c0c77d8fSBjörn Töpel if (!umem->pgs) 253c0c77d8fSBjörn Töpel return -ENOMEM; 254c0c77d8fSBjörn Töpel 255e451eb51SDavidlohr Bueso down_read(¤t->mm->mmap_sem); 256*932f4a63SIra Weiny npgs = get_user_pages(umem->address, umem->npgs, 257*932f4a63SIra Weiny gup_flags | FOLL_LONGTERM, &umem->pgs[0], NULL); 258e451eb51SDavidlohr Bueso up_read(¤t->mm->mmap_sem); 259c0c77d8fSBjörn Töpel 260c0c77d8fSBjörn Töpel if (npgs != umem->npgs) { 261c0c77d8fSBjörn Töpel if (npgs >= 0) { 262c0c77d8fSBjörn Töpel umem->npgs = npgs; 263c0c77d8fSBjörn Töpel err = -ENOMEM; 264c0c77d8fSBjörn Töpel goto out_pin; 265c0c77d8fSBjörn Töpel } 266c0c77d8fSBjörn Töpel err = npgs; 267c0c77d8fSBjörn Töpel goto out_pgs; 268c0c77d8fSBjörn Töpel } 269c0c77d8fSBjörn Töpel return 0; 270c0c77d8fSBjörn Töpel 271c0c77d8fSBjörn Töpel out_pin: 272c0c77d8fSBjörn Töpel xdp_umem_unpin_pages(umem); 273c0c77d8fSBjörn Töpel out_pgs: 274c0c77d8fSBjörn Töpel kfree(umem->pgs); 275c0c77d8fSBjörn Töpel umem->pgs = NULL; 276c0c77d8fSBjörn Töpel return err; 277c0c77d8fSBjörn Töpel } 278c0c77d8fSBjörn Töpel 279c0c77d8fSBjörn Töpel static int xdp_umem_account_pages(struct xdp_umem *umem) 280c0c77d8fSBjörn Töpel { 281c0c77d8fSBjörn Töpel unsigned long lock_limit, new_npgs, old_npgs; 282c0c77d8fSBjörn Töpel 283c0c77d8fSBjörn Töpel if (capable(CAP_IPC_LOCK)) 284c0c77d8fSBjörn Töpel return 0; 285c0c77d8fSBjörn Töpel 286c0c77d8fSBjörn Töpel lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; 287c0c77d8fSBjörn Töpel umem->user = get_uid(current_user()); 288c0c77d8fSBjörn Töpel 289c0c77d8fSBjörn Töpel do { 290c0c77d8fSBjörn Töpel old_npgs = atomic_long_read(&umem->user->locked_vm); 291c0c77d8fSBjörn Töpel new_npgs = old_npgs + umem->npgs; 292c0c77d8fSBjörn Töpel if (new_npgs > lock_limit) { 293c0c77d8fSBjörn Töpel free_uid(umem->user); 294c0c77d8fSBjörn Töpel umem->user = NULL; 295c0c77d8fSBjörn Töpel return -ENOBUFS; 296c0c77d8fSBjörn Töpel } 297c0c77d8fSBjörn Töpel } while (atomic_long_cmpxchg(&umem->user->locked_vm, old_npgs, 298c0c77d8fSBjörn Töpel new_npgs) != old_npgs); 299c0c77d8fSBjörn Töpel return 0; 300c0c77d8fSBjörn Töpel } 301c0c77d8fSBjörn Töpel 302a49049eaSBjörn Töpel static int xdp_umem_reg(struct xdp_umem *umem, struct xdp_umem_reg *mr) 303c0c77d8fSBjörn Töpel { 304bbff2f32SBjörn Töpel u32 chunk_size = mr->chunk_size, headroom = mr->headroom; 305bbff2f32SBjörn Töpel unsigned int chunks, chunks_per_page; 306c0c77d8fSBjörn Töpel u64 addr = mr->addr, size = mr->len; 3078aef7340SBjörn Töpel int size_chk, err, i; 308c0c77d8fSBjörn Töpel 309bbff2f32SBjörn Töpel if (chunk_size < XDP_UMEM_MIN_CHUNK_SIZE || chunk_size > PAGE_SIZE) { 310c0c77d8fSBjörn Töpel /* Strictly speaking we could support this, if: 311c0c77d8fSBjörn Töpel * - huge pages, or* 312c0c77d8fSBjörn Töpel * - using an IOMMU, or 313c0c77d8fSBjörn Töpel * - making sure the memory area is consecutive 314c0c77d8fSBjörn Töpel * but for now, we simply say "computer says no". 315c0c77d8fSBjörn Töpel */ 316c0c77d8fSBjörn Töpel return -EINVAL; 317c0c77d8fSBjörn Töpel } 318c0c77d8fSBjörn Töpel 319bbff2f32SBjörn Töpel if (!is_power_of_2(chunk_size)) 320c0c77d8fSBjörn Töpel return -EINVAL; 321c0c77d8fSBjörn Töpel 322c0c77d8fSBjörn Töpel if (!PAGE_ALIGNED(addr)) { 323c0c77d8fSBjörn Töpel /* Memory area has to be page size aligned. For 324c0c77d8fSBjörn Töpel * simplicity, this might change. 325c0c77d8fSBjörn Töpel */ 326c0c77d8fSBjörn Töpel return -EINVAL; 327c0c77d8fSBjörn Töpel } 328c0c77d8fSBjörn Töpel 329c0c77d8fSBjörn Töpel if ((addr + size) < addr) 330c0c77d8fSBjörn Töpel return -EINVAL; 331c0c77d8fSBjörn Töpel 332bbff2f32SBjörn Töpel chunks = (unsigned int)div_u64(size, chunk_size); 333bbff2f32SBjörn Töpel if (chunks == 0) 334c0c77d8fSBjörn Töpel return -EINVAL; 335c0c77d8fSBjörn Töpel 336bbff2f32SBjörn Töpel chunks_per_page = PAGE_SIZE / chunk_size; 337bbff2f32SBjörn Töpel if (chunks < chunks_per_page || chunks % chunks_per_page) 338c0c77d8fSBjörn Töpel return -EINVAL; 339c0c77d8fSBjörn Töpel 340bbff2f32SBjörn Töpel headroom = ALIGN(headroom, 64); 341c0c77d8fSBjörn Töpel 342bbff2f32SBjörn Töpel size_chk = chunk_size - headroom - XDP_PACKET_HEADROOM; 343c0c77d8fSBjörn Töpel if (size_chk < 0) 344c0c77d8fSBjörn Töpel return -EINVAL; 345c0c77d8fSBjörn Töpel 346c0c77d8fSBjörn Töpel umem->address = (unsigned long)addr; 34793ee30f3SMagnus Karlsson umem->chunk_mask = ~((u64)chunk_size - 1); 34893ee30f3SMagnus Karlsson umem->size = size; 349bbff2f32SBjörn Töpel umem->headroom = headroom; 350bbff2f32SBjörn Töpel umem->chunk_size_nohr = chunk_size - headroom; 351c0c77d8fSBjörn Töpel umem->npgs = size / PAGE_SIZE; 352c0c77d8fSBjörn Töpel umem->pgs = NULL; 353c0c77d8fSBjörn Töpel umem->user = NULL; 354ac98d8aaSMagnus Karlsson INIT_LIST_HEAD(&umem->xsk_list); 355ac98d8aaSMagnus Karlsson spin_lock_init(&umem->xsk_list_lock); 356c0c77d8fSBjörn Töpel 357d3b42f14SBjörn Töpel refcount_set(&umem->users, 1); 358c0c77d8fSBjörn Töpel 359c0c77d8fSBjörn Töpel err = xdp_umem_account_pages(umem); 360c0c77d8fSBjörn Töpel if (err) 361044175a0SBjörn Töpel return err; 362c0c77d8fSBjörn Töpel 363c0c77d8fSBjörn Töpel err = xdp_umem_pin_pages(umem); 364c0c77d8fSBjörn Töpel if (err) 365c0c77d8fSBjörn Töpel goto out_account; 3668aef7340SBjörn Töpel 3678aef7340SBjörn Töpel umem->pages = kcalloc(umem->npgs, sizeof(*umem->pages), GFP_KERNEL); 3688aef7340SBjörn Töpel if (!umem->pages) { 3698aef7340SBjörn Töpel err = -ENOMEM; 3708aef7340SBjörn Töpel goto out_account; 3718aef7340SBjörn Töpel } 3728aef7340SBjörn Töpel 3738aef7340SBjörn Töpel for (i = 0; i < umem->npgs; i++) 3748aef7340SBjörn Töpel umem->pages[i].addr = page_address(umem->pgs[i]); 3758aef7340SBjörn Töpel 376c0c77d8fSBjörn Töpel return 0; 377c0c77d8fSBjörn Töpel 378c0c77d8fSBjörn Töpel out_account: 379c0c77d8fSBjörn Töpel xdp_umem_unaccount_pages(umem); 380c0c77d8fSBjörn Töpel return err; 381c0c77d8fSBjörn Töpel } 382965a9909SMagnus Karlsson 383a49049eaSBjörn Töpel struct xdp_umem *xdp_umem_create(struct xdp_umem_reg *mr) 384a49049eaSBjörn Töpel { 385a49049eaSBjörn Töpel struct xdp_umem *umem; 386a49049eaSBjörn Töpel int err; 387a49049eaSBjörn Töpel 388a49049eaSBjörn Töpel umem = kzalloc(sizeof(*umem), GFP_KERNEL); 389a49049eaSBjörn Töpel if (!umem) 390a49049eaSBjörn Töpel return ERR_PTR(-ENOMEM); 391a49049eaSBjörn Töpel 39250e74c01SBjörn Töpel err = ida_simple_get(&umem_ida, 0, 0, GFP_KERNEL); 39350e74c01SBjörn Töpel if (err < 0) { 39450e74c01SBjörn Töpel kfree(umem); 39550e74c01SBjörn Töpel return ERR_PTR(err); 39650e74c01SBjörn Töpel } 39750e74c01SBjörn Töpel umem->id = err; 39850e74c01SBjörn Töpel 399a49049eaSBjörn Töpel err = xdp_umem_reg(umem, mr); 400a49049eaSBjörn Töpel if (err) { 40150e74c01SBjörn Töpel ida_simple_remove(&umem_ida, umem->id); 402a49049eaSBjörn Töpel kfree(umem); 403a49049eaSBjörn Töpel return ERR_PTR(err); 404a49049eaSBjörn Töpel } 405a49049eaSBjörn Töpel 406a49049eaSBjörn Töpel return umem; 407a49049eaSBjörn Töpel } 408a49049eaSBjörn Töpel 409965a9909SMagnus Karlsson bool xdp_umem_validate_queues(struct xdp_umem *umem) 410965a9909SMagnus Karlsson { 411da60cf00SBjörn Töpel return umem->fq && umem->cq; 412965a9909SMagnus Karlsson } 413