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> 16c0c77d8fSBjörn Töpel 17c0c77d8fSBjörn Töpel #include "xdp_umem.h" 18e61e62b9SBjörn Töpel #include "xsk_queue.h" 19c0c77d8fSBjörn Töpel 20bbff2f32SBjörn Töpel #define XDP_UMEM_MIN_CHUNK_SIZE 2048 21c0c77d8fSBjörn Töpel 22ac98d8aaSMagnus Karlsson void xdp_add_sk_umem(struct xdp_umem *umem, struct xdp_sock *xs) 23ac98d8aaSMagnus Karlsson { 24ac98d8aaSMagnus Karlsson unsigned long flags; 25ac98d8aaSMagnus Karlsson 26ac98d8aaSMagnus Karlsson spin_lock_irqsave(&umem->xsk_list_lock, flags); 27ac98d8aaSMagnus Karlsson list_add_rcu(&xs->list, &umem->xsk_list); 28ac98d8aaSMagnus Karlsson spin_unlock_irqrestore(&umem->xsk_list_lock, flags); 29ac98d8aaSMagnus Karlsson } 30ac98d8aaSMagnus Karlsson 31ac98d8aaSMagnus Karlsson void xdp_del_sk_umem(struct xdp_umem *umem, struct xdp_sock *xs) 32ac98d8aaSMagnus Karlsson { 33ac98d8aaSMagnus Karlsson unsigned long flags; 34ac98d8aaSMagnus Karlsson 35ac98d8aaSMagnus Karlsson if (xs->dev) { 36ac98d8aaSMagnus Karlsson spin_lock_irqsave(&umem->xsk_list_lock, flags); 37ac98d8aaSMagnus Karlsson list_del_rcu(&xs->list); 38ac98d8aaSMagnus Karlsson spin_unlock_irqrestore(&umem->xsk_list_lock, flags); 39ac98d8aaSMagnus Karlsson 40ac98d8aaSMagnus Karlsson if (umem->zc) 41ac98d8aaSMagnus Karlsson synchronize_net(); 42ac98d8aaSMagnus Karlsson } 43ac98d8aaSMagnus Karlsson } 44ac98d8aaSMagnus Karlsson 45c9b47cc1SMagnus Karlsson /* The umem is stored both in the _rx struct and the _tx struct as we do 46c9b47cc1SMagnus Karlsson * not know if the device has more tx queues than rx, or the opposite. 47c9b47cc1SMagnus Karlsson * This might also change during run time. 48c9b47cc1SMagnus Karlsson */ 49c9b47cc1SMagnus Karlsson static void xdp_reg_umem_at_qid(struct net_device *dev, struct xdp_umem *umem, 50c9b47cc1SMagnus Karlsson u16 queue_id) 5184c6b868SJakub Kicinski { 52c9b47cc1SMagnus Karlsson if (queue_id < dev->real_num_rx_queues) 53c9b47cc1SMagnus Karlsson dev->_rx[queue_id].umem = umem; 54c9b47cc1SMagnus Karlsson if (queue_id < dev->real_num_tx_queues) 55c9b47cc1SMagnus Karlsson dev->_tx[queue_id].umem = umem; 56c9b47cc1SMagnus Karlsson } 5784c6b868SJakub Kicinski 58*1661d346SJakub Kicinski struct xdp_umem *xdp_get_umem_from_qid(struct net_device *dev, 59c9b47cc1SMagnus Karlsson u16 queue_id) 60c9b47cc1SMagnus Karlsson { 61c9b47cc1SMagnus Karlsson if (queue_id < dev->real_num_rx_queues) 62c9b47cc1SMagnus Karlsson return dev->_rx[queue_id].umem; 63c9b47cc1SMagnus Karlsson if (queue_id < dev->real_num_tx_queues) 64c9b47cc1SMagnus Karlsson return dev->_tx[queue_id].umem; 6584c6b868SJakub Kicinski 66c9b47cc1SMagnus Karlsson return NULL; 67c9b47cc1SMagnus Karlsson } 6884c6b868SJakub Kicinski 69c9b47cc1SMagnus Karlsson static void xdp_clear_umem_at_qid(struct net_device *dev, u16 queue_id) 70c9b47cc1SMagnus Karlsson { 71c9b47cc1SMagnus Karlsson /* Zero out the entry independent on how many queues are configured 72c9b47cc1SMagnus Karlsson * at this point in time, as it might be used in the future. 73c9b47cc1SMagnus Karlsson */ 74c9b47cc1SMagnus Karlsson if (queue_id < dev->num_rx_queues) 75c9b47cc1SMagnus Karlsson dev->_rx[queue_id].umem = NULL; 76c9b47cc1SMagnus Karlsson if (queue_id < dev->num_tx_queues) 77c9b47cc1SMagnus Karlsson dev->_tx[queue_id].umem = NULL; 7884c6b868SJakub Kicinski } 7984c6b868SJakub Kicinski 80173d3adbSBjörn Töpel int xdp_umem_assign_dev(struct xdp_umem *umem, struct net_device *dev, 81c9b47cc1SMagnus Karlsson u16 queue_id, u16 flags) 82173d3adbSBjörn Töpel { 83173d3adbSBjörn Töpel bool force_zc, force_copy; 84173d3adbSBjörn Töpel struct netdev_bpf bpf; 85c9b47cc1SMagnus Karlsson int err = 0; 86173d3adbSBjörn Töpel 87173d3adbSBjörn Töpel force_zc = flags & XDP_ZEROCOPY; 88173d3adbSBjörn Töpel force_copy = flags & XDP_COPY; 89173d3adbSBjörn Töpel 90173d3adbSBjörn Töpel if (force_zc && force_copy) 91173d3adbSBjörn Töpel return -EINVAL; 92173d3adbSBjörn Töpel 93173d3adbSBjörn Töpel rtnl_lock(); 94c9b47cc1SMagnus Karlsson if (xdp_get_umem_from_qid(dev, queue_id)) { 95c9b47cc1SMagnus Karlsson err = -EBUSY; 96c9b47cc1SMagnus Karlsson goto out_rtnl_unlock; 97c9b47cc1SMagnus Karlsson } 98c9b47cc1SMagnus Karlsson 99c9b47cc1SMagnus Karlsson xdp_reg_umem_at_qid(dev, umem, queue_id); 100c9b47cc1SMagnus Karlsson umem->dev = dev; 101c9b47cc1SMagnus Karlsson umem->queue_id = queue_id; 102c9b47cc1SMagnus Karlsson if (force_copy) 103c9b47cc1SMagnus Karlsson /* For copy-mode, we are done. */ 104c9b47cc1SMagnus Karlsson goto out_rtnl_unlock; 105c9b47cc1SMagnus Karlsson 106c9b47cc1SMagnus Karlsson if (!dev->netdev_ops->ndo_bpf || 107c9b47cc1SMagnus Karlsson !dev->netdev_ops->ndo_xsk_async_xmit) { 108c9b47cc1SMagnus Karlsson err = -EOPNOTSUPP; 109c9b47cc1SMagnus Karlsson goto err_unreg_umem; 11084c6b868SJakub Kicinski } 111173d3adbSBjörn Töpel 112173d3adbSBjörn Töpel bpf.command = XDP_SETUP_XSK_UMEM; 113173d3adbSBjörn Töpel bpf.xsk.umem = umem; 114173d3adbSBjörn Töpel bpf.xsk.queue_id = queue_id; 115173d3adbSBjörn Töpel 116173d3adbSBjörn Töpel err = dev->netdev_ops->ndo_bpf(dev, &bpf); 117f734607eSJakub Kicinski if (err) 118c9b47cc1SMagnus Karlsson goto err_unreg_umem; 11984c6b868SJakub Kicinski rtnl_unlock(); 120173d3adbSBjörn Töpel 121f734607eSJakub Kicinski dev_hold(dev); 122173d3adbSBjörn Töpel umem->zc = true; 123173d3adbSBjörn Töpel return 0; 12484c6b868SJakub Kicinski 125c9b47cc1SMagnus Karlsson err_unreg_umem: 126c9b47cc1SMagnus Karlsson xdp_clear_umem_at_qid(dev, queue_id); 127c9b47cc1SMagnus Karlsson if (!force_zc) 128c9b47cc1SMagnus Karlsson err = 0; /* fallback to copy mode */ 129c9b47cc1SMagnus Karlsson out_rtnl_unlock: 13084c6b868SJakub Kicinski rtnl_unlock(); 131c9b47cc1SMagnus Karlsson return err; 132173d3adbSBjörn Töpel } 133173d3adbSBjörn Töpel 134ac98d8aaSMagnus Karlsson static void xdp_umem_clear_dev(struct xdp_umem *umem) 135173d3adbSBjörn Töpel { 136173d3adbSBjörn Töpel struct netdev_bpf bpf; 137173d3adbSBjörn Töpel int err; 138173d3adbSBjörn Töpel 139c9b47cc1SMagnus Karlsson if (umem->zc) { 140173d3adbSBjörn Töpel bpf.command = XDP_SETUP_XSK_UMEM; 141173d3adbSBjörn Töpel bpf.xsk.umem = NULL; 142173d3adbSBjörn Töpel bpf.xsk.queue_id = umem->queue_id; 143173d3adbSBjörn Töpel 144173d3adbSBjörn Töpel rtnl_lock(); 145173d3adbSBjörn Töpel err = umem->dev->netdev_ops->ndo_bpf(umem->dev, &bpf); 146173d3adbSBjörn Töpel rtnl_unlock(); 147173d3adbSBjörn Töpel 148173d3adbSBjörn Töpel if (err) 149173d3adbSBjörn Töpel WARN(1, "failed to disable umem!\n"); 150c9b47cc1SMagnus Karlsson } 151173d3adbSBjörn Töpel 152c9b47cc1SMagnus Karlsson if (umem->dev) { 153c9b47cc1SMagnus Karlsson rtnl_lock(); 154c9b47cc1SMagnus Karlsson xdp_clear_umem_at_qid(umem->dev, umem->queue_id); 155c9b47cc1SMagnus Karlsson rtnl_unlock(); 156c9b47cc1SMagnus Karlsson } 157c9b47cc1SMagnus Karlsson 158c9b47cc1SMagnus Karlsson if (umem->zc) { 159173d3adbSBjörn Töpel dev_put(umem->dev); 160c9b47cc1SMagnus Karlsson umem->zc = false; 161173d3adbSBjörn Töpel } 162173d3adbSBjörn Töpel } 163173d3adbSBjörn Töpel 164c0c77d8fSBjörn Töpel static void xdp_umem_unpin_pages(struct xdp_umem *umem) 165c0c77d8fSBjörn Töpel { 166c0c77d8fSBjörn Töpel unsigned int i; 167c0c77d8fSBjörn Töpel 168c0c77d8fSBjörn Töpel for (i = 0; i < umem->npgs; i++) { 169c0c77d8fSBjörn Töpel struct page *page = umem->pgs[i]; 170c0c77d8fSBjörn Töpel 171c0c77d8fSBjörn Töpel set_page_dirty_lock(page); 172c0c77d8fSBjörn Töpel put_page(page); 173c0c77d8fSBjörn Töpel } 174c0c77d8fSBjörn Töpel 175c0c77d8fSBjörn Töpel kfree(umem->pgs); 176c0c77d8fSBjörn Töpel umem->pgs = NULL; 177c0c77d8fSBjörn Töpel } 178c0c77d8fSBjörn Töpel 179c0c77d8fSBjörn Töpel static void xdp_umem_unaccount_pages(struct xdp_umem *umem) 180c0c77d8fSBjörn Töpel { 181c09290c5SDaniel Borkmann if (umem->user) { 182c0c77d8fSBjörn Töpel atomic_long_sub(umem->npgs, &umem->user->locked_vm); 183c0c77d8fSBjörn Töpel free_uid(umem->user); 184c0c77d8fSBjörn Töpel } 185c09290c5SDaniel Borkmann } 186c0c77d8fSBjörn Töpel 187c0c77d8fSBjörn Töpel static void xdp_umem_release(struct xdp_umem *umem) 188c0c77d8fSBjörn Töpel { 189c0c77d8fSBjörn Töpel struct task_struct *task; 190c0c77d8fSBjörn Töpel struct mm_struct *mm; 191c0c77d8fSBjörn Töpel 192173d3adbSBjörn Töpel xdp_umem_clear_dev(umem); 193173d3adbSBjörn Töpel 194423f3832SMagnus Karlsson if (umem->fq) { 195423f3832SMagnus Karlsson xskq_destroy(umem->fq); 196423f3832SMagnus Karlsson umem->fq = NULL; 197423f3832SMagnus Karlsson } 198423f3832SMagnus Karlsson 199fe230832SMagnus Karlsson if (umem->cq) { 200fe230832SMagnus Karlsson xskq_destroy(umem->cq); 201fe230832SMagnus Karlsson umem->cq = NULL; 202fe230832SMagnus Karlsson } 203fe230832SMagnus Karlsson 204f5bd9138SJakub Kicinski xsk_reuseq_destroy(umem); 205f5bd9138SJakub Kicinski 206c0c77d8fSBjörn Töpel xdp_umem_unpin_pages(umem); 207c0c77d8fSBjörn Töpel 208c0c77d8fSBjörn Töpel task = get_pid_task(umem->pid, PIDTYPE_PID); 209c0c77d8fSBjörn Töpel put_pid(umem->pid); 210c0c77d8fSBjörn Töpel if (!task) 211c0c77d8fSBjörn Töpel goto out; 212c0c77d8fSBjörn Töpel mm = get_task_mm(task); 213c0c77d8fSBjörn Töpel put_task_struct(task); 214c0c77d8fSBjörn Töpel if (!mm) 215c0c77d8fSBjörn Töpel goto out; 216c0c77d8fSBjörn Töpel 217c0c77d8fSBjörn Töpel mmput(mm); 2188aef7340SBjörn Töpel kfree(umem->pages); 2198aef7340SBjörn Töpel umem->pages = NULL; 2208aef7340SBjörn Töpel 221c0c77d8fSBjörn Töpel xdp_umem_unaccount_pages(umem); 222c0c77d8fSBjörn Töpel out: 223c0c77d8fSBjörn Töpel kfree(umem); 224c0c77d8fSBjörn Töpel } 225c0c77d8fSBjörn Töpel 226c0c77d8fSBjörn Töpel static void xdp_umem_release_deferred(struct work_struct *work) 227c0c77d8fSBjörn Töpel { 228c0c77d8fSBjörn Töpel struct xdp_umem *umem = container_of(work, struct xdp_umem, work); 229c0c77d8fSBjörn Töpel 230c0c77d8fSBjörn Töpel xdp_umem_release(umem); 231c0c77d8fSBjörn Töpel } 232c0c77d8fSBjörn Töpel 233c0c77d8fSBjörn Töpel void xdp_get_umem(struct xdp_umem *umem) 234c0c77d8fSBjörn Töpel { 235d3b42f14SBjörn Töpel refcount_inc(&umem->users); 236c0c77d8fSBjörn Töpel } 237c0c77d8fSBjörn Töpel 238c0c77d8fSBjörn Töpel void xdp_put_umem(struct xdp_umem *umem) 239c0c77d8fSBjörn Töpel { 240c0c77d8fSBjörn Töpel if (!umem) 241c0c77d8fSBjörn Töpel return; 242c0c77d8fSBjörn Töpel 243d3b42f14SBjörn Töpel if (refcount_dec_and_test(&umem->users)) { 244c0c77d8fSBjörn Töpel INIT_WORK(&umem->work, xdp_umem_release_deferred); 245c0c77d8fSBjörn Töpel schedule_work(&umem->work); 246c0c77d8fSBjörn Töpel } 247c0c77d8fSBjörn Töpel } 248c0c77d8fSBjörn Töpel 249c0c77d8fSBjörn Töpel static int xdp_umem_pin_pages(struct xdp_umem *umem) 250c0c77d8fSBjörn Töpel { 251c0c77d8fSBjörn Töpel unsigned int gup_flags = FOLL_WRITE; 252c0c77d8fSBjörn Töpel long npgs; 253c0c77d8fSBjörn Töpel int err; 254c0c77d8fSBjörn Töpel 255a343993cSBjörn Töpel umem->pgs = kcalloc(umem->npgs, sizeof(*umem->pgs), 256a343993cSBjörn Töpel GFP_KERNEL | __GFP_NOWARN); 257c0c77d8fSBjörn Töpel if (!umem->pgs) 258c0c77d8fSBjörn Töpel return -ENOMEM; 259c0c77d8fSBjörn Töpel 260c0c77d8fSBjörn Töpel down_write(¤t->mm->mmap_sem); 261c0c77d8fSBjörn Töpel npgs = get_user_pages(umem->address, umem->npgs, 262c0c77d8fSBjörn Töpel gup_flags, &umem->pgs[0], NULL); 263c0c77d8fSBjörn Töpel up_write(¤t->mm->mmap_sem); 264c0c77d8fSBjörn Töpel 265c0c77d8fSBjörn Töpel if (npgs != umem->npgs) { 266c0c77d8fSBjörn Töpel if (npgs >= 0) { 267c0c77d8fSBjörn Töpel umem->npgs = npgs; 268c0c77d8fSBjörn Töpel err = -ENOMEM; 269c0c77d8fSBjörn Töpel goto out_pin; 270c0c77d8fSBjörn Töpel } 271c0c77d8fSBjörn Töpel err = npgs; 272c0c77d8fSBjörn Töpel goto out_pgs; 273c0c77d8fSBjörn Töpel } 274c0c77d8fSBjörn Töpel return 0; 275c0c77d8fSBjörn Töpel 276c0c77d8fSBjörn Töpel out_pin: 277c0c77d8fSBjörn Töpel xdp_umem_unpin_pages(umem); 278c0c77d8fSBjörn Töpel out_pgs: 279c0c77d8fSBjörn Töpel kfree(umem->pgs); 280c0c77d8fSBjörn Töpel umem->pgs = NULL; 281c0c77d8fSBjörn Töpel return err; 282c0c77d8fSBjörn Töpel } 283c0c77d8fSBjörn Töpel 284c0c77d8fSBjörn Töpel static int xdp_umem_account_pages(struct xdp_umem *umem) 285c0c77d8fSBjörn Töpel { 286c0c77d8fSBjörn Töpel unsigned long lock_limit, new_npgs, old_npgs; 287c0c77d8fSBjörn Töpel 288c0c77d8fSBjörn Töpel if (capable(CAP_IPC_LOCK)) 289c0c77d8fSBjörn Töpel return 0; 290c0c77d8fSBjörn Töpel 291c0c77d8fSBjörn Töpel lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; 292c0c77d8fSBjörn Töpel umem->user = get_uid(current_user()); 293c0c77d8fSBjörn Töpel 294c0c77d8fSBjörn Töpel do { 295c0c77d8fSBjörn Töpel old_npgs = atomic_long_read(&umem->user->locked_vm); 296c0c77d8fSBjörn Töpel new_npgs = old_npgs + umem->npgs; 297c0c77d8fSBjörn Töpel if (new_npgs > lock_limit) { 298c0c77d8fSBjörn Töpel free_uid(umem->user); 299c0c77d8fSBjörn Töpel umem->user = NULL; 300c0c77d8fSBjörn Töpel return -ENOBUFS; 301c0c77d8fSBjörn Töpel } 302c0c77d8fSBjörn Töpel } while (atomic_long_cmpxchg(&umem->user->locked_vm, old_npgs, 303c0c77d8fSBjörn Töpel new_npgs) != old_npgs); 304c0c77d8fSBjörn Töpel return 0; 305c0c77d8fSBjörn Töpel } 306c0c77d8fSBjörn Töpel 307a49049eaSBjörn Töpel static int xdp_umem_reg(struct xdp_umem *umem, struct xdp_umem_reg *mr) 308c0c77d8fSBjörn Töpel { 309bbff2f32SBjörn Töpel u32 chunk_size = mr->chunk_size, headroom = mr->headroom; 310bbff2f32SBjörn Töpel unsigned int chunks, chunks_per_page; 311c0c77d8fSBjörn Töpel u64 addr = mr->addr, size = mr->len; 3128aef7340SBjörn Töpel int size_chk, err, i; 313c0c77d8fSBjörn Töpel 314bbff2f32SBjörn Töpel if (chunk_size < XDP_UMEM_MIN_CHUNK_SIZE || chunk_size > PAGE_SIZE) { 315c0c77d8fSBjörn Töpel /* Strictly speaking we could support this, if: 316c0c77d8fSBjörn Töpel * - huge pages, or* 317c0c77d8fSBjörn Töpel * - using an IOMMU, or 318c0c77d8fSBjörn Töpel * - making sure the memory area is consecutive 319c0c77d8fSBjörn Töpel * but for now, we simply say "computer says no". 320c0c77d8fSBjörn Töpel */ 321c0c77d8fSBjörn Töpel return -EINVAL; 322c0c77d8fSBjörn Töpel } 323c0c77d8fSBjörn Töpel 324bbff2f32SBjörn Töpel if (!is_power_of_2(chunk_size)) 325c0c77d8fSBjörn Töpel return -EINVAL; 326c0c77d8fSBjörn Töpel 327c0c77d8fSBjörn Töpel if (!PAGE_ALIGNED(addr)) { 328c0c77d8fSBjörn Töpel /* Memory area has to be page size aligned. For 329c0c77d8fSBjörn Töpel * simplicity, this might change. 330c0c77d8fSBjörn Töpel */ 331c0c77d8fSBjörn Töpel return -EINVAL; 332c0c77d8fSBjörn Töpel } 333c0c77d8fSBjörn Töpel 334c0c77d8fSBjörn Töpel if ((addr + size) < addr) 335c0c77d8fSBjörn Töpel return -EINVAL; 336c0c77d8fSBjörn Töpel 337bbff2f32SBjörn Töpel chunks = (unsigned int)div_u64(size, chunk_size); 338bbff2f32SBjörn Töpel if (chunks == 0) 339c0c77d8fSBjörn Töpel return -EINVAL; 340c0c77d8fSBjörn Töpel 341bbff2f32SBjörn Töpel chunks_per_page = PAGE_SIZE / chunk_size; 342bbff2f32SBjörn Töpel if (chunks < chunks_per_page || chunks % chunks_per_page) 343c0c77d8fSBjörn Töpel return -EINVAL; 344c0c77d8fSBjörn Töpel 345bbff2f32SBjörn Töpel headroom = ALIGN(headroom, 64); 346c0c77d8fSBjörn Töpel 347bbff2f32SBjörn Töpel size_chk = chunk_size - headroom - XDP_PACKET_HEADROOM; 348c0c77d8fSBjörn Töpel if (size_chk < 0) 349c0c77d8fSBjörn Töpel return -EINVAL; 350c0c77d8fSBjörn Töpel 351c0c77d8fSBjörn Töpel umem->pid = get_task_pid(current, PIDTYPE_PID); 352c0c77d8fSBjörn Töpel umem->address = (unsigned long)addr; 35393ee30f3SMagnus Karlsson umem->chunk_mask = ~((u64)chunk_size - 1); 35493ee30f3SMagnus Karlsson umem->size = size; 355bbff2f32SBjörn Töpel umem->headroom = headroom; 356bbff2f32SBjörn Töpel umem->chunk_size_nohr = chunk_size - headroom; 357c0c77d8fSBjörn Töpel umem->npgs = size / PAGE_SIZE; 358c0c77d8fSBjörn Töpel umem->pgs = NULL; 359c0c77d8fSBjörn Töpel umem->user = NULL; 360ac98d8aaSMagnus Karlsson INIT_LIST_HEAD(&umem->xsk_list); 361ac98d8aaSMagnus Karlsson spin_lock_init(&umem->xsk_list_lock); 362c0c77d8fSBjörn Töpel 363d3b42f14SBjörn Töpel refcount_set(&umem->users, 1); 364c0c77d8fSBjörn Töpel 365c0c77d8fSBjörn Töpel err = xdp_umem_account_pages(umem); 366c0c77d8fSBjörn Töpel if (err) 367c0c77d8fSBjörn Töpel goto out; 368c0c77d8fSBjörn Töpel 369c0c77d8fSBjörn Töpel err = xdp_umem_pin_pages(umem); 370c0c77d8fSBjörn Töpel if (err) 371c0c77d8fSBjörn Töpel goto out_account; 3728aef7340SBjörn Töpel 3738aef7340SBjörn Töpel umem->pages = kcalloc(umem->npgs, sizeof(*umem->pages), GFP_KERNEL); 3748aef7340SBjörn Töpel if (!umem->pages) { 3758aef7340SBjörn Töpel err = -ENOMEM; 3768aef7340SBjörn Töpel goto out_account; 3778aef7340SBjörn Töpel } 3788aef7340SBjörn Töpel 3798aef7340SBjörn Töpel for (i = 0; i < umem->npgs; i++) 3808aef7340SBjörn Töpel umem->pages[i].addr = page_address(umem->pgs[i]); 3818aef7340SBjörn Töpel 382c0c77d8fSBjörn Töpel return 0; 383c0c77d8fSBjörn Töpel 384c0c77d8fSBjörn Töpel out_account: 385c0c77d8fSBjörn Töpel xdp_umem_unaccount_pages(umem); 386c0c77d8fSBjörn Töpel out: 387c0c77d8fSBjörn Töpel put_pid(umem->pid); 388c0c77d8fSBjörn Töpel return err; 389c0c77d8fSBjörn Töpel } 390965a9909SMagnus Karlsson 391a49049eaSBjörn Töpel struct xdp_umem *xdp_umem_create(struct xdp_umem_reg *mr) 392a49049eaSBjörn Töpel { 393a49049eaSBjörn Töpel struct xdp_umem *umem; 394a49049eaSBjörn Töpel int err; 395a49049eaSBjörn Töpel 396a49049eaSBjörn Töpel umem = kzalloc(sizeof(*umem), GFP_KERNEL); 397a49049eaSBjörn Töpel if (!umem) 398a49049eaSBjörn Töpel return ERR_PTR(-ENOMEM); 399a49049eaSBjörn Töpel 400a49049eaSBjörn Töpel err = xdp_umem_reg(umem, mr); 401a49049eaSBjörn Töpel if (err) { 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