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> 17624676e7SIvan Khoronzhuk #include <linux/vmalloc.h> 18c0c77d8fSBjörn Töpel 19c0c77d8fSBjörn Töpel #include "xdp_umem.h" 20e61e62b9SBjörn Töpel #include "xsk_queue.h" 21c0c77d8fSBjörn Töpel 22bbff2f32SBjörn Töpel #define XDP_UMEM_MIN_CHUNK_SIZE 2048 23c0c77d8fSBjörn Töpel 2450e74c01SBjörn Töpel static DEFINE_IDA(umem_ida); 2550e74c01SBjörn Töpel 26ac98d8aaSMagnus Karlsson void xdp_add_sk_umem(struct xdp_umem *umem, struct xdp_sock *xs) 27ac98d8aaSMagnus Karlsson { 28ac98d8aaSMagnus Karlsson unsigned long flags; 29ac98d8aaSMagnus Karlsson 302afd23f7SMagnus Karlsson if (!xs->tx) 312afd23f7SMagnus Karlsson return; 322afd23f7SMagnus Karlsson 33*e4e5aefcSMagnus Karlsson spin_lock_irqsave(&umem->xsk_tx_list_lock, flags); 34*e4e5aefcSMagnus Karlsson list_add_rcu(&xs->list, &umem->xsk_tx_list); 35*e4e5aefcSMagnus Karlsson spin_unlock_irqrestore(&umem->xsk_tx_list_lock, flags); 36ac98d8aaSMagnus Karlsson } 37ac98d8aaSMagnus Karlsson 38ac98d8aaSMagnus Karlsson void xdp_del_sk_umem(struct xdp_umem *umem, struct xdp_sock *xs) 39ac98d8aaSMagnus Karlsson { 40ac98d8aaSMagnus Karlsson unsigned long flags; 41ac98d8aaSMagnus Karlsson 422afd23f7SMagnus Karlsson if (!xs->tx) 432afd23f7SMagnus Karlsson return; 442afd23f7SMagnus Karlsson 45*e4e5aefcSMagnus Karlsson spin_lock_irqsave(&umem->xsk_tx_list_lock, flags); 46ac98d8aaSMagnus Karlsson list_del_rcu(&xs->list); 47*e4e5aefcSMagnus Karlsson spin_unlock_irqrestore(&umem->xsk_tx_list_lock, flags); 48ac98d8aaSMagnus Karlsson } 49ac98d8aaSMagnus Karlsson 50c9b47cc1SMagnus Karlsson /* The umem is stored both in the _rx struct and the _tx struct as we do 51c9b47cc1SMagnus Karlsson * not know if the device has more tx queues than rx, or the opposite. 52c9b47cc1SMagnus Karlsson * This might also change during run time. 53c9b47cc1SMagnus Karlsson */ 54cc5b5d35SKrzysztof Kazimierczak static int xdp_reg_umem_at_qid(struct net_device *dev, struct xdp_umem *umem, 55c9b47cc1SMagnus Karlsson u16 queue_id) 5684c6b868SJakub Kicinski { 57cc5b5d35SKrzysztof Kazimierczak if (queue_id >= max_t(unsigned int, 58cc5b5d35SKrzysztof Kazimierczak dev->real_num_rx_queues, 59cc5b5d35SKrzysztof Kazimierczak dev->real_num_tx_queues)) 60cc5b5d35SKrzysztof Kazimierczak return -EINVAL; 61cc5b5d35SKrzysztof Kazimierczak 62c9b47cc1SMagnus Karlsson if (queue_id < dev->real_num_rx_queues) 63c9b47cc1SMagnus Karlsson dev->_rx[queue_id].umem = umem; 64c9b47cc1SMagnus Karlsson if (queue_id < dev->real_num_tx_queues) 65c9b47cc1SMagnus Karlsson dev->_tx[queue_id].umem = umem; 66cc5b5d35SKrzysztof Kazimierczak 67cc5b5d35SKrzysztof Kazimierczak return 0; 68c9b47cc1SMagnus Karlsson } 6984c6b868SJakub Kicinski 701661d346SJakub Kicinski struct xdp_umem *xdp_get_umem_from_qid(struct net_device *dev, 71c9b47cc1SMagnus Karlsson u16 queue_id) 72c9b47cc1SMagnus Karlsson { 73c9b47cc1SMagnus Karlsson if (queue_id < dev->real_num_rx_queues) 74c9b47cc1SMagnus Karlsson return dev->_rx[queue_id].umem; 75c9b47cc1SMagnus Karlsson if (queue_id < dev->real_num_tx_queues) 76c9b47cc1SMagnus Karlsson return dev->_tx[queue_id].umem; 7784c6b868SJakub Kicinski 78c9b47cc1SMagnus Karlsson return NULL; 79c9b47cc1SMagnus Karlsson } 805f4f3b2dSJan Sokolowski EXPORT_SYMBOL(xdp_get_umem_from_qid); 8184c6b868SJakub Kicinski 82c9b47cc1SMagnus Karlsson static void xdp_clear_umem_at_qid(struct net_device *dev, u16 queue_id) 83c9b47cc1SMagnus Karlsson { 84a41b4f3cSMagnus Karlsson if (queue_id < dev->real_num_rx_queues) 85c9b47cc1SMagnus Karlsson dev->_rx[queue_id].umem = NULL; 86a41b4f3cSMagnus Karlsson if (queue_id < dev->real_num_tx_queues) 87c9b47cc1SMagnus Karlsson dev->_tx[queue_id].umem = NULL; 8884c6b868SJakub Kicinski } 8984c6b868SJakub Kicinski 90173d3adbSBjörn Töpel int xdp_umem_assign_dev(struct xdp_umem *umem, struct net_device *dev, 91c9b47cc1SMagnus Karlsson u16 queue_id, u16 flags) 92173d3adbSBjörn Töpel { 93173d3adbSBjörn Töpel bool force_zc, force_copy; 94173d3adbSBjörn Töpel struct netdev_bpf bpf; 95c9b47cc1SMagnus Karlsson int err = 0; 96173d3adbSBjörn Töpel 975464c3a0SIlya Maximets ASSERT_RTNL(); 985464c3a0SIlya Maximets 99173d3adbSBjörn Töpel force_zc = flags & XDP_ZEROCOPY; 100173d3adbSBjörn Töpel force_copy = flags & XDP_COPY; 101173d3adbSBjörn Töpel 102173d3adbSBjörn Töpel if (force_zc && force_copy) 103173d3adbSBjörn Töpel return -EINVAL; 104173d3adbSBjörn Töpel 1055464c3a0SIlya Maximets if (xdp_get_umem_from_qid(dev, queue_id)) 1065464c3a0SIlya Maximets return -EBUSY; 107c9b47cc1SMagnus Karlsson 108cc5b5d35SKrzysztof Kazimierczak err = xdp_reg_umem_at_qid(dev, umem, queue_id); 109cc5b5d35SKrzysztof Kazimierczak if (err) 1105464c3a0SIlya Maximets return err; 111cc5b5d35SKrzysztof Kazimierczak 112c9b47cc1SMagnus Karlsson umem->dev = dev; 113c9b47cc1SMagnus Karlsson umem->queue_id = queue_id; 114162c820eSIlya Maximets 11577cd0d7bSMagnus Karlsson if (flags & XDP_USE_NEED_WAKEUP) { 11677cd0d7bSMagnus Karlsson umem->flags |= XDP_UMEM_USES_NEED_WAKEUP; 11777cd0d7bSMagnus Karlsson /* Tx needs to be explicitly woken up the first time. 11877cd0d7bSMagnus Karlsson * Also for supporting drivers that do not implement this 11977cd0d7bSMagnus Karlsson * feature. They will always have to call sendto(). 12077cd0d7bSMagnus Karlsson */ 12177cd0d7bSMagnus Karlsson xsk_set_tx_need_wakeup(umem); 12277cd0d7bSMagnus Karlsson } 12377cd0d7bSMagnus Karlsson 124162c820eSIlya Maximets dev_hold(dev); 125162c820eSIlya Maximets 126c9b47cc1SMagnus Karlsson if (force_copy) 127c9b47cc1SMagnus Karlsson /* For copy-mode, we are done. */ 1285464c3a0SIlya Maximets return 0; 129c9b47cc1SMagnus Karlsson 1309116e5e2SMagnus Karlsson if (!dev->netdev_ops->ndo_bpf || !dev->netdev_ops->ndo_xsk_wakeup) { 131c9b47cc1SMagnus Karlsson err = -EOPNOTSUPP; 132c9b47cc1SMagnus Karlsson goto err_unreg_umem; 13384c6b868SJakub Kicinski } 134173d3adbSBjörn Töpel 135173d3adbSBjörn Töpel bpf.command = XDP_SETUP_XSK_UMEM; 136173d3adbSBjörn Töpel bpf.xsk.umem = umem; 137173d3adbSBjörn Töpel bpf.xsk.queue_id = queue_id; 138173d3adbSBjörn Töpel 139173d3adbSBjörn Töpel err = dev->netdev_ops->ndo_bpf(dev, &bpf); 140f734607eSJakub Kicinski if (err) 141c9b47cc1SMagnus Karlsson goto err_unreg_umem; 142173d3adbSBjörn Töpel 143173d3adbSBjörn Töpel umem->zc = true; 144173d3adbSBjörn Töpel return 0; 14584c6b868SJakub Kicinski 146c9b47cc1SMagnus Karlsson err_unreg_umem: 147c9b47cc1SMagnus Karlsson if (!force_zc) 148c9b47cc1SMagnus Karlsson err = 0; /* fallback to copy mode */ 1491e405c1aSBjörn Töpel if (err) 1501e405c1aSBjörn Töpel xdp_clear_umem_at_qid(dev, queue_id); 151c9b47cc1SMagnus Karlsson return err; 152173d3adbSBjörn Töpel } 153173d3adbSBjörn Töpel 154455302d1SIlya Maximets void xdp_umem_clear_dev(struct xdp_umem *umem) 155173d3adbSBjörn Töpel { 156173d3adbSBjörn Töpel struct netdev_bpf bpf; 157173d3adbSBjörn Töpel int err; 158173d3adbSBjörn Töpel 159455302d1SIlya Maximets ASSERT_RTNL(); 160455302d1SIlya Maximets 16101d76b53SIlya Maximets if (!umem->dev) 16201d76b53SIlya Maximets return; 16301d76b53SIlya Maximets 164c9b47cc1SMagnus Karlsson if (umem->zc) { 165173d3adbSBjörn Töpel bpf.command = XDP_SETUP_XSK_UMEM; 166173d3adbSBjörn Töpel bpf.xsk.umem = NULL; 167173d3adbSBjörn Töpel bpf.xsk.queue_id = umem->queue_id; 168173d3adbSBjörn Töpel 169173d3adbSBjörn Töpel err = umem->dev->netdev_ops->ndo_bpf(umem->dev, &bpf); 170173d3adbSBjörn Töpel 171173d3adbSBjörn Töpel if (err) 172173d3adbSBjörn Töpel WARN(1, "failed to disable umem!\n"); 173c9b47cc1SMagnus Karlsson } 174173d3adbSBjörn Töpel 175c9b47cc1SMagnus Karlsson xdp_clear_umem_at_qid(umem->dev, umem->queue_id); 176c9b47cc1SMagnus Karlsson 177173d3adbSBjörn Töpel dev_put(umem->dev); 178162c820eSIlya Maximets umem->dev = NULL; 179c9b47cc1SMagnus Karlsson umem->zc = false; 180173d3adbSBjörn Töpel } 181173d3adbSBjörn Töpel 182d9973cecSIvan Khoronzhuk static void xdp_umem_unmap_pages(struct xdp_umem *umem) 183d9973cecSIvan Khoronzhuk { 184d9973cecSIvan Khoronzhuk unsigned int i; 185d9973cecSIvan Khoronzhuk 186d9973cecSIvan Khoronzhuk for (i = 0; i < umem->npgs; i++) 187624676e7SIvan Khoronzhuk if (PageHighMem(umem->pgs[i])) 188624676e7SIvan Khoronzhuk vunmap(umem->pages[i].addr); 189624676e7SIvan Khoronzhuk } 190624676e7SIvan Khoronzhuk 191624676e7SIvan Khoronzhuk static int xdp_umem_map_pages(struct xdp_umem *umem) 192624676e7SIvan Khoronzhuk { 193624676e7SIvan Khoronzhuk unsigned int i; 194624676e7SIvan Khoronzhuk void *addr; 195624676e7SIvan Khoronzhuk 196624676e7SIvan Khoronzhuk for (i = 0; i < umem->npgs; i++) { 197624676e7SIvan Khoronzhuk if (PageHighMem(umem->pgs[i])) 198624676e7SIvan Khoronzhuk addr = vmap(&umem->pgs[i], 1, VM_MAP, PAGE_KERNEL); 199624676e7SIvan Khoronzhuk else 200624676e7SIvan Khoronzhuk addr = page_address(umem->pgs[i]); 201624676e7SIvan Khoronzhuk 202624676e7SIvan Khoronzhuk if (!addr) { 203624676e7SIvan Khoronzhuk xdp_umem_unmap_pages(umem); 204624676e7SIvan Khoronzhuk return -ENOMEM; 205624676e7SIvan Khoronzhuk } 206624676e7SIvan Khoronzhuk 207624676e7SIvan Khoronzhuk umem->pages[i].addr = addr; 208624676e7SIvan Khoronzhuk } 209624676e7SIvan Khoronzhuk 210624676e7SIvan Khoronzhuk return 0; 211d9973cecSIvan Khoronzhuk } 212d9973cecSIvan Khoronzhuk 213c0c77d8fSBjörn Töpel static void xdp_umem_unpin_pages(struct xdp_umem *umem) 214c0c77d8fSBjörn Töpel { 215f1f6a7ddSJohn Hubbard unpin_user_pages_dirty_lock(umem->pgs, umem->npgs, true); 216c0c77d8fSBjörn Töpel 217c0c77d8fSBjörn Töpel kfree(umem->pgs); 218c0c77d8fSBjörn Töpel umem->pgs = NULL; 219c0c77d8fSBjörn Töpel } 220c0c77d8fSBjörn Töpel 221c0c77d8fSBjörn Töpel static void xdp_umem_unaccount_pages(struct xdp_umem *umem) 222c0c77d8fSBjörn Töpel { 223c09290c5SDaniel Borkmann if (umem->user) { 224c0c77d8fSBjörn Töpel atomic_long_sub(umem->npgs, &umem->user->locked_vm); 225c0c77d8fSBjörn Töpel free_uid(umem->user); 226c0c77d8fSBjörn Töpel } 227c09290c5SDaniel Borkmann } 228c0c77d8fSBjörn Töpel 229c0c77d8fSBjörn Töpel static void xdp_umem_release(struct xdp_umem *umem) 230c0c77d8fSBjörn Töpel { 231455302d1SIlya Maximets rtnl_lock(); 232173d3adbSBjörn Töpel xdp_umem_clear_dev(umem); 233455302d1SIlya Maximets rtnl_unlock(); 234173d3adbSBjörn Töpel 23550e74c01SBjörn Töpel ida_simple_remove(&umem_ida, umem->id); 23650e74c01SBjörn Töpel 237423f3832SMagnus Karlsson if (umem->fq) { 238423f3832SMagnus Karlsson xskq_destroy(umem->fq); 239423f3832SMagnus Karlsson umem->fq = NULL; 240423f3832SMagnus Karlsson } 241423f3832SMagnus Karlsson 242fe230832SMagnus Karlsson if (umem->cq) { 243fe230832SMagnus Karlsson xskq_destroy(umem->cq); 244fe230832SMagnus Karlsson umem->cq = NULL; 245fe230832SMagnus Karlsson } 246fe230832SMagnus Karlsson 247f5bd9138SJakub Kicinski xsk_reuseq_destroy(umem); 248f5bd9138SJakub Kicinski 249d9973cecSIvan Khoronzhuk xdp_umem_unmap_pages(umem); 250c0c77d8fSBjörn Töpel xdp_umem_unpin_pages(umem); 251c0c77d8fSBjörn Töpel 252d3a56931SMagnus Karlsson kvfree(umem->pages); 2538aef7340SBjörn Töpel umem->pages = NULL; 2548aef7340SBjörn Töpel 255c0c77d8fSBjörn Töpel xdp_umem_unaccount_pages(umem); 256c0c77d8fSBjörn Töpel kfree(umem); 257c0c77d8fSBjörn Töpel } 258c0c77d8fSBjörn Töpel 259c0c77d8fSBjörn Töpel static void xdp_umem_release_deferred(struct work_struct *work) 260c0c77d8fSBjörn Töpel { 261c0c77d8fSBjörn Töpel struct xdp_umem *umem = container_of(work, struct xdp_umem, work); 262c0c77d8fSBjörn Töpel 263c0c77d8fSBjörn Töpel xdp_umem_release(umem); 264c0c77d8fSBjörn Töpel } 265c0c77d8fSBjörn Töpel 266c0c77d8fSBjörn Töpel void xdp_get_umem(struct xdp_umem *umem) 267c0c77d8fSBjörn Töpel { 268d3b42f14SBjörn Töpel refcount_inc(&umem->users); 269c0c77d8fSBjörn Töpel } 270c0c77d8fSBjörn Töpel 271c0c77d8fSBjörn Töpel void xdp_put_umem(struct xdp_umem *umem) 272c0c77d8fSBjörn Töpel { 273c0c77d8fSBjörn Töpel if (!umem) 274c0c77d8fSBjörn Töpel return; 275c0c77d8fSBjörn Töpel 276d3b42f14SBjörn Töpel if (refcount_dec_and_test(&umem->users)) { 277c0c77d8fSBjörn Töpel INIT_WORK(&umem->work, xdp_umem_release_deferred); 278c0c77d8fSBjörn Töpel schedule_work(&umem->work); 279c0c77d8fSBjörn Töpel } 280c0c77d8fSBjörn Töpel } 281c0c77d8fSBjörn Töpel 282c0c77d8fSBjörn Töpel static int xdp_umem_pin_pages(struct xdp_umem *umem) 283c0c77d8fSBjörn Töpel { 284c0c77d8fSBjörn Töpel unsigned int gup_flags = FOLL_WRITE; 285c0c77d8fSBjörn Töpel long npgs; 286c0c77d8fSBjörn Töpel int err; 287c0c77d8fSBjörn Töpel 288a343993cSBjörn Töpel umem->pgs = kcalloc(umem->npgs, sizeof(*umem->pgs), 289a343993cSBjörn Töpel GFP_KERNEL | __GFP_NOWARN); 290c0c77d8fSBjörn Töpel if (!umem->pgs) 291c0c77d8fSBjörn Töpel return -ENOMEM; 292c0c77d8fSBjörn Töpel 293e451eb51SDavidlohr Bueso down_read(¤t->mm->mmap_sem); 294fb48b474SJohn Hubbard npgs = pin_user_pages(umem->address, umem->npgs, 295932f4a63SIra Weiny gup_flags | FOLL_LONGTERM, &umem->pgs[0], NULL); 296e451eb51SDavidlohr Bueso up_read(¤t->mm->mmap_sem); 297c0c77d8fSBjörn Töpel 298c0c77d8fSBjörn Töpel if (npgs != umem->npgs) { 299c0c77d8fSBjörn Töpel if (npgs >= 0) { 300c0c77d8fSBjörn Töpel umem->npgs = npgs; 301c0c77d8fSBjörn Töpel err = -ENOMEM; 302c0c77d8fSBjörn Töpel goto out_pin; 303c0c77d8fSBjörn Töpel } 304c0c77d8fSBjörn Töpel err = npgs; 305c0c77d8fSBjörn Töpel goto out_pgs; 306c0c77d8fSBjörn Töpel } 307c0c77d8fSBjörn Töpel return 0; 308c0c77d8fSBjörn Töpel 309c0c77d8fSBjörn Töpel out_pin: 310c0c77d8fSBjörn Töpel xdp_umem_unpin_pages(umem); 311c0c77d8fSBjörn Töpel out_pgs: 312c0c77d8fSBjörn Töpel kfree(umem->pgs); 313c0c77d8fSBjörn Töpel umem->pgs = NULL; 314c0c77d8fSBjörn Töpel return err; 315c0c77d8fSBjörn Töpel } 316c0c77d8fSBjörn Töpel 317c0c77d8fSBjörn Töpel static int xdp_umem_account_pages(struct xdp_umem *umem) 318c0c77d8fSBjörn Töpel { 319c0c77d8fSBjörn Töpel unsigned long lock_limit, new_npgs, old_npgs; 320c0c77d8fSBjörn Töpel 321c0c77d8fSBjörn Töpel if (capable(CAP_IPC_LOCK)) 322c0c77d8fSBjörn Töpel return 0; 323c0c77d8fSBjörn Töpel 324c0c77d8fSBjörn Töpel lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; 325c0c77d8fSBjörn Töpel umem->user = get_uid(current_user()); 326c0c77d8fSBjörn Töpel 327c0c77d8fSBjörn Töpel do { 328c0c77d8fSBjörn Töpel old_npgs = atomic_long_read(&umem->user->locked_vm); 329c0c77d8fSBjörn Töpel new_npgs = old_npgs + umem->npgs; 330c0c77d8fSBjörn Töpel if (new_npgs > lock_limit) { 331c0c77d8fSBjörn Töpel free_uid(umem->user); 332c0c77d8fSBjörn Töpel umem->user = NULL; 333c0c77d8fSBjörn Töpel return -ENOBUFS; 334c0c77d8fSBjörn Töpel } 335c0c77d8fSBjörn Töpel } while (atomic_long_cmpxchg(&umem->user->locked_vm, old_npgs, 336c0c77d8fSBjörn Töpel new_npgs) != old_npgs); 337c0c77d8fSBjörn Töpel return 0; 338c0c77d8fSBjörn Töpel } 339c0c77d8fSBjörn Töpel 340a49049eaSBjörn Töpel static int xdp_umem_reg(struct xdp_umem *umem, struct xdp_umem_reg *mr) 341c0c77d8fSBjörn Töpel { 342c05cd364SKevin Laatz bool unaligned_chunks = mr->flags & XDP_UMEM_UNALIGNED_CHUNK_FLAG; 343bbff2f32SBjörn Töpel u32 chunk_size = mr->chunk_size, headroom = mr->headroom; 344bbff2f32SBjörn Töpel unsigned int chunks, chunks_per_page; 345c0c77d8fSBjörn Töpel u64 addr = mr->addr, size = mr->len; 34699e3a236SMagnus Karlsson int err; 347c0c77d8fSBjörn Töpel 348bbff2f32SBjörn Töpel if (chunk_size < XDP_UMEM_MIN_CHUNK_SIZE || chunk_size > PAGE_SIZE) { 349c0c77d8fSBjörn Töpel /* Strictly speaking we could support this, if: 350c0c77d8fSBjörn Töpel * - huge pages, or* 351c0c77d8fSBjörn Töpel * - using an IOMMU, or 352c0c77d8fSBjörn Töpel * - making sure the memory area is consecutive 353c0c77d8fSBjörn Töpel * but for now, we simply say "computer says no". 354c0c77d8fSBjörn Töpel */ 355c0c77d8fSBjörn Töpel return -EINVAL; 356c0c77d8fSBjörn Töpel } 357c0c77d8fSBjörn Töpel 358c05cd364SKevin Laatz if (mr->flags & ~(XDP_UMEM_UNALIGNED_CHUNK_FLAG | 359c05cd364SKevin Laatz XDP_UMEM_USES_NEED_WAKEUP)) 360c05cd364SKevin Laatz return -EINVAL; 361c05cd364SKevin Laatz 362c05cd364SKevin Laatz if (!unaligned_chunks && !is_power_of_2(chunk_size)) 363c0c77d8fSBjörn Töpel return -EINVAL; 364c0c77d8fSBjörn Töpel 365c0c77d8fSBjörn Töpel if (!PAGE_ALIGNED(addr)) { 366c0c77d8fSBjörn Töpel /* Memory area has to be page size aligned. For 367c0c77d8fSBjörn Töpel * simplicity, this might change. 368c0c77d8fSBjörn Töpel */ 369c0c77d8fSBjörn Töpel return -EINVAL; 370c0c77d8fSBjörn Töpel } 371c0c77d8fSBjörn Töpel 372c0c77d8fSBjörn Töpel if ((addr + size) < addr) 373c0c77d8fSBjörn Töpel return -EINVAL; 374c0c77d8fSBjörn Töpel 375bbff2f32SBjörn Töpel chunks = (unsigned int)div_u64(size, chunk_size); 376bbff2f32SBjörn Töpel if (chunks == 0) 377c0c77d8fSBjörn Töpel return -EINVAL; 378c0c77d8fSBjörn Töpel 379c05cd364SKevin Laatz if (!unaligned_chunks) { 380bbff2f32SBjörn Töpel chunks_per_page = PAGE_SIZE / chunk_size; 381bbff2f32SBjörn Töpel if (chunks < chunks_per_page || chunks % chunks_per_page) 382c0c77d8fSBjörn Töpel return -EINVAL; 383c05cd364SKevin Laatz } 384c0c77d8fSBjörn Töpel 38599e3a236SMagnus Karlsson if (headroom >= chunk_size - XDP_PACKET_HEADROOM) 386c0c77d8fSBjörn Töpel return -EINVAL; 387c0c77d8fSBjörn Töpel 388c0c77d8fSBjörn Töpel umem->address = (unsigned long)addr; 389c05cd364SKevin Laatz umem->chunk_mask = unaligned_chunks ? XSK_UNALIGNED_BUF_ADDR_MASK 390c05cd364SKevin Laatz : ~((u64)chunk_size - 1); 39193ee30f3SMagnus Karlsson umem->size = size; 392bbff2f32SBjörn Töpel umem->headroom = headroom; 393bbff2f32SBjörn Töpel umem->chunk_size_nohr = chunk_size - headroom; 394c0c77d8fSBjörn Töpel umem->npgs = size / PAGE_SIZE; 395c0c77d8fSBjörn Töpel umem->pgs = NULL; 396c0c77d8fSBjörn Töpel umem->user = NULL; 397c05cd364SKevin Laatz umem->flags = mr->flags; 398*e4e5aefcSMagnus Karlsson INIT_LIST_HEAD(&umem->xsk_tx_list); 399*e4e5aefcSMagnus Karlsson spin_lock_init(&umem->xsk_tx_list_lock); 400c0c77d8fSBjörn Töpel 401d3b42f14SBjörn Töpel refcount_set(&umem->users, 1); 402c0c77d8fSBjörn Töpel 403c0c77d8fSBjörn Töpel err = xdp_umem_account_pages(umem); 404c0c77d8fSBjörn Töpel if (err) 405044175a0SBjörn Töpel return err; 406c0c77d8fSBjörn Töpel 407c0c77d8fSBjörn Töpel err = xdp_umem_pin_pages(umem); 408c0c77d8fSBjörn Töpel if (err) 409c0c77d8fSBjörn Töpel goto out_account; 4108aef7340SBjörn Töpel 411d3a56931SMagnus Karlsson umem->pages = kvcalloc(umem->npgs, sizeof(*umem->pages), 412d3a56931SMagnus Karlsson GFP_KERNEL_ACCOUNT); 4138aef7340SBjörn Töpel if (!umem->pages) { 4148aef7340SBjörn Töpel err = -ENOMEM; 415fb89c394SIvan Khoronzhuk goto out_pin; 4168aef7340SBjörn Töpel } 4178aef7340SBjörn Töpel 418624676e7SIvan Khoronzhuk err = xdp_umem_map_pages(umem); 419624676e7SIvan Khoronzhuk if (!err) 420c0c77d8fSBjörn Töpel return 0; 421c0c77d8fSBjörn Töpel 422d3a56931SMagnus Karlsson kvfree(umem->pages); 423624676e7SIvan Khoronzhuk 424fb89c394SIvan Khoronzhuk out_pin: 425fb89c394SIvan Khoronzhuk xdp_umem_unpin_pages(umem); 426c0c77d8fSBjörn Töpel out_account: 427c0c77d8fSBjörn Töpel xdp_umem_unaccount_pages(umem); 428c0c77d8fSBjörn Töpel return err; 429c0c77d8fSBjörn Töpel } 430965a9909SMagnus Karlsson 431a49049eaSBjörn Töpel struct xdp_umem *xdp_umem_create(struct xdp_umem_reg *mr) 432a49049eaSBjörn Töpel { 433a49049eaSBjörn Töpel struct xdp_umem *umem; 434a49049eaSBjörn Töpel int err; 435a49049eaSBjörn Töpel 436a49049eaSBjörn Töpel umem = kzalloc(sizeof(*umem), GFP_KERNEL); 437a49049eaSBjörn Töpel if (!umem) 438a49049eaSBjörn Töpel return ERR_PTR(-ENOMEM); 439a49049eaSBjörn Töpel 44050e74c01SBjörn Töpel err = ida_simple_get(&umem_ida, 0, 0, GFP_KERNEL); 44150e74c01SBjörn Töpel if (err < 0) { 44250e74c01SBjörn Töpel kfree(umem); 44350e74c01SBjörn Töpel return ERR_PTR(err); 44450e74c01SBjörn Töpel } 44550e74c01SBjörn Töpel umem->id = err; 44650e74c01SBjörn Töpel 447a49049eaSBjörn Töpel err = xdp_umem_reg(umem, mr); 448a49049eaSBjörn Töpel if (err) { 44950e74c01SBjörn Töpel ida_simple_remove(&umem_ida, umem->id); 450a49049eaSBjörn Töpel kfree(umem); 451a49049eaSBjörn Töpel return ERR_PTR(err); 452a49049eaSBjörn Töpel } 453a49049eaSBjörn Töpel 454a49049eaSBjörn Töpel return umem; 455a49049eaSBjörn Töpel } 456a49049eaSBjörn Töpel 457965a9909SMagnus Karlsson bool xdp_umem_validate_queues(struct xdp_umem *umem) 458965a9909SMagnus Karlsson { 459da60cf00SBjörn Töpel return umem->fq && umem->cq; 460965a9909SMagnus Karlsson } 461