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 spin_lock_irqsave(&umem->xsk_list_lock, flags); 36ac98d8aaSMagnus Karlsson list_del_rcu(&xs->list); 37ac98d8aaSMagnus Karlsson spin_unlock_irqrestore(&umem->xsk_list_lock, flags); 38ac98d8aaSMagnus Karlsson } 39ac98d8aaSMagnus Karlsson 40c9b47cc1SMagnus Karlsson /* The umem is stored both in the _rx struct and the _tx struct as we do 41c9b47cc1SMagnus Karlsson * not know if the device has more tx queues than rx, or the opposite. 42c9b47cc1SMagnus Karlsson * This might also change during run time. 43c9b47cc1SMagnus Karlsson */ 44*cc5b5d35SKrzysztof Kazimierczak static int xdp_reg_umem_at_qid(struct net_device *dev, struct xdp_umem *umem, 45c9b47cc1SMagnus Karlsson u16 queue_id) 4684c6b868SJakub Kicinski { 47*cc5b5d35SKrzysztof Kazimierczak if (queue_id >= max_t(unsigned int, 48*cc5b5d35SKrzysztof Kazimierczak dev->real_num_rx_queues, 49*cc5b5d35SKrzysztof Kazimierczak dev->real_num_tx_queues)) 50*cc5b5d35SKrzysztof Kazimierczak return -EINVAL; 51*cc5b5d35SKrzysztof Kazimierczak 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; 56*cc5b5d35SKrzysztof Kazimierczak 57*cc5b5d35SKrzysztof Kazimierczak return 0; 58c9b47cc1SMagnus Karlsson } 5984c6b868SJakub Kicinski 601661d346SJakub Kicinski struct xdp_umem *xdp_get_umem_from_qid(struct net_device *dev, 61c9b47cc1SMagnus Karlsson u16 queue_id) 62c9b47cc1SMagnus Karlsson { 63c9b47cc1SMagnus Karlsson if (queue_id < dev->real_num_rx_queues) 64c9b47cc1SMagnus Karlsson return dev->_rx[queue_id].umem; 65c9b47cc1SMagnus Karlsson if (queue_id < dev->real_num_tx_queues) 66c9b47cc1SMagnus Karlsson return dev->_tx[queue_id].umem; 6784c6b868SJakub Kicinski 68c9b47cc1SMagnus Karlsson return NULL; 69c9b47cc1SMagnus Karlsson } 7084c6b868SJakub Kicinski 71c9b47cc1SMagnus Karlsson static void xdp_clear_umem_at_qid(struct net_device *dev, u16 queue_id) 72c9b47cc1SMagnus Karlsson { 73a41b4f3cSMagnus Karlsson if (queue_id < dev->real_num_rx_queues) 74c9b47cc1SMagnus Karlsson dev->_rx[queue_id].umem = NULL; 75a41b4f3cSMagnus Karlsson if (queue_id < dev->real_num_tx_queues) 76c9b47cc1SMagnus Karlsson dev->_tx[queue_id].umem = NULL; 7784c6b868SJakub Kicinski } 7884c6b868SJakub Kicinski 79173d3adbSBjörn Töpel int xdp_umem_assign_dev(struct xdp_umem *umem, struct net_device *dev, 80c9b47cc1SMagnus Karlsson u16 queue_id, u16 flags) 81173d3adbSBjörn Töpel { 82173d3adbSBjörn Töpel bool force_zc, force_copy; 83173d3adbSBjörn Töpel struct netdev_bpf bpf; 84c9b47cc1SMagnus Karlsson int err = 0; 85173d3adbSBjörn Töpel 86173d3adbSBjörn Töpel force_zc = flags & XDP_ZEROCOPY; 87173d3adbSBjörn Töpel force_copy = flags & XDP_COPY; 88173d3adbSBjörn Töpel 89173d3adbSBjörn Töpel if (force_zc && force_copy) 90173d3adbSBjörn Töpel return -EINVAL; 91173d3adbSBjörn Töpel 92173d3adbSBjörn Töpel rtnl_lock(); 93c9b47cc1SMagnus Karlsson if (xdp_get_umem_from_qid(dev, queue_id)) { 94c9b47cc1SMagnus Karlsson err = -EBUSY; 95c9b47cc1SMagnus Karlsson goto out_rtnl_unlock; 96c9b47cc1SMagnus Karlsson } 97c9b47cc1SMagnus Karlsson 98*cc5b5d35SKrzysztof Kazimierczak err = xdp_reg_umem_at_qid(dev, umem, queue_id); 99*cc5b5d35SKrzysztof Kazimierczak if (err) 100*cc5b5d35SKrzysztof Kazimierczak goto out_rtnl_unlock; 101*cc5b5d35SKrzysztof Kazimierczak 102c9b47cc1SMagnus Karlsson umem->dev = dev; 103c9b47cc1SMagnus Karlsson umem->queue_id = queue_id; 104c9b47cc1SMagnus Karlsson if (force_copy) 105c9b47cc1SMagnus Karlsson /* For copy-mode, we are done. */ 106c9b47cc1SMagnus Karlsson goto out_rtnl_unlock; 107c9b47cc1SMagnus Karlsson 108c9b47cc1SMagnus Karlsson if (!dev->netdev_ops->ndo_bpf || 109c9b47cc1SMagnus Karlsson !dev->netdev_ops->ndo_xsk_async_xmit) { 110c9b47cc1SMagnus Karlsson err = -EOPNOTSUPP; 111c9b47cc1SMagnus Karlsson goto err_unreg_umem; 11284c6b868SJakub Kicinski } 113173d3adbSBjörn Töpel 114173d3adbSBjörn Töpel bpf.command = XDP_SETUP_XSK_UMEM; 115173d3adbSBjörn Töpel bpf.xsk.umem = umem; 116173d3adbSBjörn Töpel bpf.xsk.queue_id = queue_id; 117173d3adbSBjörn Töpel 118173d3adbSBjörn Töpel err = dev->netdev_ops->ndo_bpf(dev, &bpf); 119f734607eSJakub Kicinski if (err) 120c9b47cc1SMagnus Karlsson goto err_unreg_umem; 12184c6b868SJakub Kicinski rtnl_unlock(); 122173d3adbSBjörn Töpel 123f734607eSJakub Kicinski dev_hold(dev); 124173d3adbSBjörn Töpel umem->zc = true; 125173d3adbSBjörn Töpel return 0; 12684c6b868SJakub Kicinski 127c9b47cc1SMagnus Karlsson err_unreg_umem: 128c9b47cc1SMagnus Karlsson xdp_clear_umem_at_qid(dev, queue_id); 129c9b47cc1SMagnus Karlsson if (!force_zc) 130c9b47cc1SMagnus Karlsson err = 0; /* fallback to copy mode */ 131c9b47cc1SMagnus Karlsson out_rtnl_unlock: 13284c6b868SJakub Kicinski rtnl_unlock(); 133c9b47cc1SMagnus Karlsson return err; 134173d3adbSBjörn Töpel } 135173d3adbSBjörn Töpel 136ac98d8aaSMagnus Karlsson static void xdp_umem_clear_dev(struct xdp_umem *umem) 137173d3adbSBjörn Töpel { 138173d3adbSBjörn Töpel struct netdev_bpf bpf; 139173d3adbSBjörn Töpel int err; 140173d3adbSBjörn Töpel 141c9b47cc1SMagnus Karlsson if (umem->zc) { 142173d3adbSBjörn Töpel bpf.command = XDP_SETUP_XSK_UMEM; 143173d3adbSBjörn Töpel bpf.xsk.umem = NULL; 144173d3adbSBjörn Töpel bpf.xsk.queue_id = umem->queue_id; 145173d3adbSBjörn Töpel 146173d3adbSBjörn Töpel rtnl_lock(); 147173d3adbSBjörn Töpel err = umem->dev->netdev_ops->ndo_bpf(umem->dev, &bpf); 148173d3adbSBjörn Töpel rtnl_unlock(); 149173d3adbSBjörn Töpel 150173d3adbSBjörn Töpel if (err) 151173d3adbSBjörn Töpel WARN(1, "failed to disable umem!\n"); 152c9b47cc1SMagnus Karlsson } 153173d3adbSBjörn Töpel 154c9b47cc1SMagnus Karlsson if (umem->dev) { 155c9b47cc1SMagnus Karlsson rtnl_lock(); 156c9b47cc1SMagnus Karlsson xdp_clear_umem_at_qid(umem->dev, umem->queue_id); 157c9b47cc1SMagnus Karlsson rtnl_unlock(); 158c9b47cc1SMagnus Karlsson } 159c9b47cc1SMagnus Karlsson 160c9b47cc1SMagnus Karlsson if (umem->zc) { 161173d3adbSBjörn Töpel dev_put(umem->dev); 162c9b47cc1SMagnus Karlsson umem->zc = false; 163173d3adbSBjörn Töpel } 164173d3adbSBjörn Töpel } 165173d3adbSBjörn Töpel 166c0c77d8fSBjörn Töpel static void xdp_umem_unpin_pages(struct xdp_umem *umem) 167c0c77d8fSBjörn Töpel { 168c0c77d8fSBjörn Töpel unsigned int i; 169c0c77d8fSBjörn Töpel 170c0c77d8fSBjörn Töpel for (i = 0; i < umem->npgs; i++) { 171c0c77d8fSBjörn Töpel struct page *page = umem->pgs[i]; 172c0c77d8fSBjörn Töpel 173c0c77d8fSBjörn Töpel set_page_dirty_lock(page); 174c0c77d8fSBjörn Töpel put_page(page); 175c0c77d8fSBjörn Töpel } 176c0c77d8fSBjörn Töpel 177c0c77d8fSBjörn Töpel kfree(umem->pgs); 178c0c77d8fSBjörn Töpel umem->pgs = NULL; 179c0c77d8fSBjörn Töpel } 180c0c77d8fSBjörn Töpel 181c0c77d8fSBjörn Töpel static void xdp_umem_unaccount_pages(struct xdp_umem *umem) 182c0c77d8fSBjörn Töpel { 183c09290c5SDaniel Borkmann if (umem->user) { 184c0c77d8fSBjörn Töpel atomic_long_sub(umem->npgs, &umem->user->locked_vm); 185c0c77d8fSBjörn Töpel free_uid(umem->user); 186c0c77d8fSBjörn Töpel } 187c09290c5SDaniel Borkmann } 188c0c77d8fSBjörn Töpel 189c0c77d8fSBjörn Töpel static void xdp_umem_release(struct xdp_umem *umem) 190c0c77d8fSBjörn Töpel { 191c0c77d8fSBjörn Töpel struct task_struct *task; 192c0c77d8fSBjörn Töpel struct mm_struct *mm; 193c0c77d8fSBjörn Töpel 194173d3adbSBjörn Töpel xdp_umem_clear_dev(umem); 195173d3adbSBjörn Töpel 196423f3832SMagnus Karlsson if (umem->fq) { 197423f3832SMagnus Karlsson xskq_destroy(umem->fq); 198423f3832SMagnus Karlsson umem->fq = NULL; 199423f3832SMagnus Karlsson } 200423f3832SMagnus Karlsson 201fe230832SMagnus Karlsson if (umem->cq) { 202fe230832SMagnus Karlsson xskq_destroy(umem->cq); 203fe230832SMagnus Karlsson umem->cq = NULL; 204fe230832SMagnus Karlsson } 205fe230832SMagnus Karlsson 206f5bd9138SJakub Kicinski xsk_reuseq_destroy(umem); 207f5bd9138SJakub Kicinski 208c0c77d8fSBjörn Töpel xdp_umem_unpin_pages(umem); 209c0c77d8fSBjörn Töpel 210c0c77d8fSBjörn Töpel task = get_pid_task(umem->pid, PIDTYPE_PID); 211c0c77d8fSBjörn Töpel put_pid(umem->pid); 212c0c77d8fSBjörn Töpel if (!task) 213c0c77d8fSBjörn Töpel goto out; 214c0c77d8fSBjörn Töpel mm = get_task_mm(task); 215c0c77d8fSBjörn Töpel put_task_struct(task); 216c0c77d8fSBjörn Töpel if (!mm) 217c0c77d8fSBjörn Töpel goto out; 218c0c77d8fSBjörn Töpel 219c0c77d8fSBjörn Töpel mmput(mm); 2208aef7340SBjörn Töpel kfree(umem->pages); 2218aef7340SBjörn Töpel umem->pages = NULL; 2228aef7340SBjörn Töpel 223c0c77d8fSBjörn Töpel xdp_umem_unaccount_pages(umem); 224c0c77d8fSBjörn Töpel out: 225c0c77d8fSBjörn Töpel kfree(umem); 226c0c77d8fSBjörn Töpel } 227c0c77d8fSBjörn Töpel 228c0c77d8fSBjörn Töpel static void xdp_umem_release_deferred(struct work_struct *work) 229c0c77d8fSBjörn Töpel { 230c0c77d8fSBjörn Töpel struct xdp_umem *umem = container_of(work, struct xdp_umem, work); 231c0c77d8fSBjörn Töpel 232c0c77d8fSBjörn Töpel xdp_umem_release(umem); 233c0c77d8fSBjörn Töpel } 234c0c77d8fSBjörn Töpel 235c0c77d8fSBjörn Töpel void xdp_get_umem(struct xdp_umem *umem) 236c0c77d8fSBjörn Töpel { 237d3b42f14SBjörn Töpel refcount_inc(&umem->users); 238c0c77d8fSBjörn Töpel } 239c0c77d8fSBjörn Töpel 240c0c77d8fSBjörn Töpel void xdp_put_umem(struct xdp_umem *umem) 241c0c77d8fSBjörn Töpel { 242c0c77d8fSBjörn Töpel if (!umem) 243c0c77d8fSBjörn Töpel return; 244c0c77d8fSBjörn Töpel 245d3b42f14SBjörn Töpel if (refcount_dec_and_test(&umem->users)) { 246c0c77d8fSBjörn Töpel INIT_WORK(&umem->work, xdp_umem_release_deferred); 247c0c77d8fSBjörn Töpel schedule_work(&umem->work); 248c0c77d8fSBjörn Töpel } 249c0c77d8fSBjörn Töpel } 250c0c77d8fSBjörn Töpel 251c0c77d8fSBjörn Töpel static int xdp_umem_pin_pages(struct xdp_umem *umem) 252c0c77d8fSBjörn Töpel { 253c0c77d8fSBjörn Töpel unsigned int gup_flags = FOLL_WRITE; 254c0c77d8fSBjörn Töpel long npgs; 255c0c77d8fSBjörn Töpel int err; 256c0c77d8fSBjörn Töpel 257a343993cSBjörn Töpel umem->pgs = kcalloc(umem->npgs, sizeof(*umem->pgs), 258a343993cSBjörn Töpel GFP_KERNEL | __GFP_NOWARN); 259c0c77d8fSBjörn Töpel if (!umem->pgs) 260c0c77d8fSBjörn Töpel return -ENOMEM; 261c0c77d8fSBjörn Töpel 262c0c77d8fSBjörn Töpel down_write(¤t->mm->mmap_sem); 263c0c77d8fSBjörn Töpel npgs = get_user_pages(umem->address, umem->npgs, 264c0c77d8fSBjörn Töpel gup_flags, &umem->pgs[0], NULL); 265c0c77d8fSBjörn Töpel up_write(¤t->mm->mmap_sem); 266c0c77d8fSBjörn Töpel 267c0c77d8fSBjörn Töpel if (npgs != umem->npgs) { 268c0c77d8fSBjörn Töpel if (npgs >= 0) { 269c0c77d8fSBjörn Töpel umem->npgs = npgs; 270c0c77d8fSBjörn Töpel err = -ENOMEM; 271c0c77d8fSBjörn Töpel goto out_pin; 272c0c77d8fSBjörn Töpel } 273c0c77d8fSBjörn Töpel err = npgs; 274c0c77d8fSBjörn Töpel goto out_pgs; 275c0c77d8fSBjörn Töpel } 276c0c77d8fSBjörn Töpel return 0; 277c0c77d8fSBjörn Töpel 278c0c77d8fSBjörn Töpel out_pin: 279c0c77d8fSBjörn Töpel xdp_umem_unpin_pages(umem); 280c0c77d8fSBjörn Töpel out_pgs: 281c0c77d8fSBjörn Töpel kfree(umem->pgs); 282c0c77d8fSBjörn Töpel umem->pgs = NULL; 283c0c77d8fSBjörn Töpel return err; 284c0c77d8fSBjörn Töpel } 285c0c77d8fSBjörn Töpel 286c0c77d8fSBjörn Töpel static int xdp_umem_account_pages(struct xdp_umem *umem) 287c0c77d8fSBjörn Töpel { 288c0c77d8fSBjörn Töpel unsigned long lock_limit, new_npgs, old_npgs; 289c0c77d8fSBjörn Töpel 290c0c77d8fSBjörn Töpel if (capable(CAP_IPC_LOCK)) 291c0c77d8fSBjörn Töpel return 0; 292c0c77d8fSBjörn Töpel 293c0c77d8fSBjörn Töpel lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; 294c0c77d8fSBjörn Töpel umem->user = get_uid(current_user()); 295c0c77d8fSBjörn Töpel 296c0c77d8fSBjörn Töpel do { 297c0c77d8fSBjörn Töpel old_npgs = atomic_long_read(&umem->user->locked_vm); 298c0c77d8fSBjörn Töpel new_npgs = old_npgs + umem->npgs; 299c0c77d8fSBjörn Töpel if (new_npgs > lock_limit) { 300c0c77d8fSBjörn Töpel free_uid(umem->user); 301c0c77d8fSBjörn Töpel umem->user = NULL; 302c0c77d8fSBjörn Töpel return -ENOBUFS; 303c0c77d8fSBjörn Töpel } 304c0c77d8fSBjörn Töpel } while (atomic_long_cmpxchg(&umem->user->locked_vm, old_npgs, 305c0c77d8fSBjörn Töpel new_npgs) != old_npgs); 306c0c77d8fSBjörn Töpel return 0; 307c0c77d8fSBjörn Töpel } 308c0c77d8fSBjörn Töpel 309a49049eaSBjörn Töpel static int xdp_umem_reg(struct xdp_umem *umem, struct xdp_umem_reg *mr) 310c0c77d8fSBjörn Töpel { 311bbff2f32SBjörn Töpel u32 chunk_size = mr->chunk_size, headroom = mr->headroom; 312bbff2f32SBjörn Töpel unsigned int chunks, chunks_per_page; 313c0c77d8fSBjörn Töpel u64 addr = mr->addr, size = mr->len; 3148aef7340SBjörn Töpel int size_chk, err, i; 315c0c77d8fSBjörn Töpel 316bbff2f32SBjörn Töpel if (chunk_size < XDP_UMEM_MIN_CHUNK_SIZE || chunk_size > PAGE_SIZE) { 317c0c77d8fSBjörn Töpel /* Strictly speaking we could support this, if: 318c0c77d8fSBjörn Töpel * - huge pages, or* 319c0c77d8fSBjörn Töpel * - using an IOMMU, or 320c0c77d8fSBjörn Töpel * - making sure the memory area is consecutive 321c0c77d8fSBjörn Töpel * but for now, we simply say "computer says no". 322c0c77d8fSBjörn Töpel */ 323c0c77d8fSBjörn Töpel return -EINVAL; 324c0c77d8fSBjörn Töpel } 325c0c77d8fSBjörn Töpel 326bbff2f32SBjörn Töpel if (!is_power_of_2(chunk_size)) 327c0c77d8fSBjörn Töpel return -EINVAL; 328c0c77d8fSBjörn Töpel 329c0c77d8fSBjörn Töpel if (!PAGE_ALIGNED(addr)) { 330c0c77d8fSBjörn Töpel /* Memory area has to be page size aligned. For 331c0c77d8fSBjörn Töpel * simplicity, this might change. 332c0c77d8fSBjörn Töpel */ 333c0c77d8fSBjörn Töpel return -EINVAL; 334c0c77d8fSBjörn Töpel } 335c0c77d8fSBjörn Töpel 336c0c77d8fSBjörn Töpel if ((addr + size) < addr) 337c0c77d8fSBjörn Töpel return -EINVAL; 338c0c77d8fSBjörn Töpel 339bbff2f32SBjörn Töpel chunks = (unsigned int)div_u64(size, chunk_size); 340bbff2f32SBjörn Töpel if (chunks == 0) 341c0c77d8fSBjörn Töpel return -EINVAL; 342c0c77d8fSBjörn Töpel 343bbff2f32SBjörn Töpel chunks_per_page = PAGE_SIZE / chunk_size; 344bbff2f32SBjörn Töpel if (chunks < chunks_per_page || chunks % chunks_per_page) 345c0c77d8fSBjörn Töpel return -EINVAL; 346c0c77d8fSBjörn Töpel 347bbff2f32SBjörn Töpel headroom = ALIGN(headroom, 64); 348c0c77d8fSBjörn Töpel 349bbff2f32SBjörn Töpel size_chk = chunk_size - headroom - XDP_PACKET_HEADROOM; 350c0c77d8fSBjörn Töpel if (size_chk < 0) 351c0c77d8fSBjörn Töpel return -EINVAL; 352c0c77d8fSBjörn Töpel 353c0c77d8fSBjörn Töpel umem->pid = get_task_pid(current, PIDTYPE_PID); 354c0c77d8fSBjörn Töpel umem->address = (unsigned long)addr; 35593ee30f3SMagnus Karlsson umem->chunk_mask = ~((u64)chunk_size - 1); 35693ee30f3SMagnus Karlsson umem->size = size; 357bbff2f32SBjörn Töpel umem->headroom = headroom; 358bbff2f32SBjörn Töpel umem->chunk_size_nohr = chunk_size - headroom; 359c0c77d8fSBjörn Töpel umem->npgs = size / PAGE_SIZE; 360c0c77d8fSBjörn Töpel umem->pgs = NULL; 361c0c77d8fSBjörn Töpel umem->user = NULL; 362ac98d8aaSMagnus Karlsson INIT_LIST_HEAD(&umem->xsk_list); 363ac98d8aaSMagnus Karlsson spin_lock_init(&umem->xsk_list_lock); 364c0c77d8fSBjörn Töpel 365d3b42f14SBjörn Töpel refcount_set(&umem->users, 1); 366c0c77d8fSBjörn Töpel 367c0c77d8fSBjörn Töpel err = xdp_umem_account_pages(umem); 368c0c77d8fSBjörn Töpel if (err) 369c0c77d8fSBjörn Töpel goto out; 370c0c77d8fSBjörn Töpel 371c0c77d8fSBjörn Töpel err = xdp_umem_pin_pages(umem); 372c0c77d8fSBjörn Töpel if (err) 373c0c77d8fSBjörn Töpel goto out_account; 3748aef7340SBjörn Töpel 3758aef7340SBjörn Töpel umem->pages = kcalloc(umem->npgs, sizeof(*umem->pages), GFP_KERNEL); 3768aef7340SBjörn Töpel if (!umem->pages) { 3778aef7340SBjörn Töpel err = -ENOMEM; 3788aef7340SBjörn Töpel goto out_account; 3798aef7340SBjörn Töpel } 3808aef7340SBjörn Töpel 3818aef7340SBjörn Töpel for (i = 0; i < umem->npgs; i++) 3828aef7340SBjörn Töpel umem->pages[i].addr = page_address(umem->pgs[i]); 3838aef7340SBjörn Töpel 384c0c77d8fSBjörn Töpel return 0; 385c0c77d8fSBjörn Töpel 386c0c77d8fSBjörn Töpel out_account: 387c0c77d8fSBjörn Töpel xdp_umem_unaccount_pages(umem); 388c0c77d8fSBjörn Töpel out: 389c0c77d8fSBjörn Töpel put_pid(umem->pid); 390c0c77d8fSBjörn Töpel return err; 391c0c77d8fSBjörn Töpel } 392965a9909SMagnus Karlsson 393a49049eaSBjörn Töpel struct xdp_umem *xdp_umem_create(struct xdp_umem_reg *mr) 394a49049eaSBjörn Töpel { 395a49049eaSBjörn Töpel struct xdp_umem *umem; 396a49049eaSBjörn Töpel int err; 397a49049eaSBjörn Töpel 398a49049eaSBjörn Töpel umem = kzalloc(sizeof(*umem), GFP_KERNEL); 399a49049eaSBjörn Töpel if (!umem) 400a49049eaSBjörn Töpel return ERR_PTR(-ENOMEM); 401a49049eaSBjörn Töpel 402a49049eaSBjörn Töpel err = xdp_umem_reg(umem, mr); 403a49049eaSBjörn Töpel if (err) { 404a49049eaSBjörn Töpel kfree(umem); 405a49049eaSBjörn Töpel return ERR_PTR(err); 406a49049eaSBjörn Töpel } 407a49049eaSBjörn Töpel 408a49049eaSBjörn Töpel return umem; 409a49049eaSBjörn Töpel } 410a49049eaSBjörn Töpel 411965a9909SMagnus Karlsson bool xdp_umem_validate_queues(struct xdp_umem *umem) 412965a9909SMagnus Karlsson { 413da60cf00SBjörn Töpel return umem->fq && umem->cq; 414965a9909SMagnus Karlsson } 415