1 // SPDX-License-Identifier: GPL-2.0
2 /* XDP user-space packet buffer
3 * Copyright(c) 2018 Intel Corporation.
6 #include <linux/init.h>
7 #include <linux/sched/mm.h>
8 #include <linux/sched/signal.h>
9 #include <linux/sched/task.h>
10 #include <linux/uaccess.h>
11 #include <linux/slab.h>
12 #include <linux/bpf.h>
14 #include <linux/netdevice.h>
15 #include <linux/rtnetlink.h>
18 #include "xsk_queue.h"
20 #define XDP_UMEM_MIN_CHUNK_SIZE 2048
22 void xdp_add_sk_umem(struct xdp_umem *umem, struct xdp_sock *xs)
26 spin_lock_irqsave(&umem->xsk_list_lock, flags);
27 list_add_rcu(&xs->list, &umem->xsk_list);
28 spin_unlock_irqrestore(&umem->xsk_list_lock, flags);
31 void xdp_del_sk_umem(struct xdp_umem *umem, struct xdp_sock *xs)
35 spin_lock_irqsave(&umem->xsk_list_lock, flags);
36 list_del_rcu(&xs->list);
37 spin_unlock_irqrestore(&umem->xsk_list_lock, flags);
40 /* The umem is stored both in the _rx struct and the _tx struct as we do
41 * not know if the device has more tx queues than rx, or the opposite.
42 * This might also change during run time.
44 static int xdp_reg_umem_at_qid(struct net_device *dev, struct xdp_umem *umem,
47 if (queue_id >= max_t(unsigned int,
48 dev->real_num_rx_queues,
49 dev->real_num_tx_queues))
52 if (queue_id < dev->real_num_rx_queues)
53 dev->_rx[queue_id].umem = umem;
54 if (queue_id < dev->real_num_tx_queues)
55 dev->_tx[queue_id].umem = umem;
60 struct xdp_umem *xdp_get_umem_from_qid(struct net_device *dev,
63 if (queue_id < dev->real_num_rx_queues)
64 return dev->_rx[queue_id].umem;
65 if (queue_id < dev->real_num_tx_queues)
66 return dev->_tx[queue_id].umem;
71 static void xdp_clear_umem_at_qid(struct net_device *dev, u16 queue_id)
73 if (queue_id < dev->real_num_rx_queues)
74 dev->_rx[queue_id].umem = NULL;
75 if (queue_id < dev->real_num_tx_queues)
76 dev->_tx[queue_id].umem = NULL;
79 int xdp_umem_assign_dev(struct xdp_umem *umem, struct net_device *dev,
80 u16 queue_id, u16 flags)
82 bool force_zc, force_copy;
83 struct netdev_bpf bpf;
86 force_zc = flags & XDP_ZEROCOPY;
87 force_copy = flags & XDP_COPY;
89 if (force_zc && force_copy)
93 if (xdp_get_umem_from_qid(dev, queue_id)) {
98 err = xdp_reg_umem_at_qid(dev, umem, queue_id);
100 goto out_rtnl_unlock;
103 umem->queue_id = queue_id;
105 /* For copy-mode, we are done. */
106 goto out_rtnl_unlock;
108 if (!dev->netdev_ops->ndo_bpf ||
109 !dev->netdev_ops->ndo_xsk_async_xmit) {
114 bpf.command = XDP_SETUP_XSK_UMEM;
116 bpf.xsk.queue_id = queue_id;
118 err = dev->netdev_ops->ndo_bpf(dev, &bpf);
128 xdp_clear_umem_at_qid(dev, queue_id);
130 err = 0; /* fallback to copy mode */
136 static void xdp_umem_clear_dev(struct xdp_umem *umem)
138 struct netdev_bpf bpf;
142 bpf.command = XDP_SETUP_XSK_UMEM;
144 bpf.xsk.queue_id = umem->queue_id;
147 err = umem->dev->netdev_ops->ndo_bpf(umem->dev, &bpf);
151 WARN(1, "failed to disable umem!\n");
156 xdp_clear_umem_at_qid(umem->dev, umem->queue_id);
166 static void xdp_umem_unpin_pages(struct xdp_umem *umem)
170 for (i = 0; i < umem->npgs; i++) {
171 struct page *page = umem->pgs[i];
173 set_page_dirty_lock(page);
181 static void xdp_umem_unaccount_pages(struct xdp_umem *umem)
184 atomic_long_sub(umem->npgs, &umem->user->locked_vm);
185 free_uid(umem->user);
189 static void xdp_umem_release(struct xdp_umem *umem)
191 struct task_struct *task;
192 struct mm_struct *mm;
194 xdp_umem_clear_dev(umem);
197 xskq_destroy(umem->fq);
202 xskq_destroy(umem->cq);
206 xsk_reuseq_destroy(umem);
208 xdp_umem_unpin_pages(umem);
210 task = get_pid_task(umem->pid, PIDTYPE_PID);
214 mm = get_task_mm(task);
215 put_task_struct(task);
223 xdp_umem_unaccount_pages(umem);
228 static void xdp_umem_release_deferred(struct work_struct *work)
230 struct xdp_umem *umem = container_of(work, struct xdp_umem, work);
232 xdp_umem_release(umem);
235 void xdp_get_umem(struct xdp_umem *umem)
237 refcount_inc(&umem->users);
240 void xdp_put_umem(struct xdp_umem *umem)
245 if (refcount_dec_and_test(&umem->users)) {
246 INIT_WORK(&umem->work, xdp_umem_release_deferred);
247 schedule_work(&umem->work);
251 static int xdp_umem_pin_pages(struct xdp_umem *umem)
253 unsigned int gup_flags = FOLL_WRITE;
257 umem->pgs = kcalloc(umem->npgs, sizeof(*umem->pgs),
258 GFP_KERNEL | __GFP_NOWARN);
262 down_write(¤t->mm->mmap_sem);
263 npgs = get_user_pages(umem->address, umem->npgs,
264 gup_flags, &umem->pgs[0], NULL);
265 up_write(¤t->mm->mmap_sem);
267 if (npgs != umem->npgs) {
279 xdp_umem_unpin_pages(umem);
286 static int xdp_umem_account_pages(struct xdp_umem *umem)
288 unsigned long lock_limit, new_npgs, old_npgs;
290 if (capable(CAP_IPC_LOCK))
293 lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
294 umem->user = get_uid(current_user());
297 old_npgs = atomic_long_read(&umem->user->locked_vm);
298 new_npgs = old_npgs + umem->npgs;
299 if (new_npgs > lock_limit) {
300 free_uid(umem->user);
304 } while (atomic_long_cmpxchg(&umem->user->locked_vm, old_npgs,
305 new_npgs) != old_npgs);
309 static int xdp_umem_reg(struct xdp_umem *umem, struct xdp_umem_reg *mr)
311 u32 chunk_size = mr->chunk_size, headroom = mr->headroom;
312 unsigned int chunks, chunks_per_page;
313 u64 addr = mr->addr, size = mr->len;
314 int size_chk, err, i;
316 if (chunk_size < XDP_UMEM_MIN_CHUNK_SIZE || chunk_size > PAGE_SIZE) {
317 /* Strictly speaking we could support this, if:
319 * - using an IOMMU, or
320 * - making sure the memory area is consecutive
321 * but for now, we simply say "computer says no".
326 if (!is_power_of_2(chunk_size))
329 if (!PAGE_ALIGNED(addr)) {
330 /* Memory area has to be page size aligned. For
331 * simplicity, this might change.
336 if ((addr + size) < addr)
339 chunks = (unsigned int)div_u64(size, chunk_size);
343 chunks_per_page = PAGE_SIZE / chunk_size;
344 if (chunks < chunks_per_page || chunks % chunks_per_page)
347 headroom = ALIGN(headroom, 64);
349 size_chk = chunk_size - headroom - XDP_PACKET_HEADROOM;
353 umem->pid = get_task_pid(current, PIDTYPE_PID);
354 umem->address = (unsigned long)addr;
355 umem->chunk_mask = ~((u64)chunk_size - 1);
357 umem->headroom = headroom;
358 umem->chunk_size_nohr = chunk_size - headroom;
359 umem->npgs = size / PAGE_SIZE;
362 INIT_LIST_HEAD(&umem->xsk_list);
363 spin_lock_init(&umem->xsk_list_lock);
365 refcount_set(&umem->users, 1);
367 err = xdp_umem_account_pages(umem);
371 err = xdp_umem_pin_pages(umem);
375 umem->pages = kcalloc(umem->npgs, sizeof(*umem->pages), GFP_KERNEL);
381 for (i = 0; i < umem->npgs; i++)
382 umem->pages[i].addr = page_address(umem->pgs[i]);
387 xdp_umem_unaccount_pages(umem);
393 struct xdp_umem *xdp_umem_create(struct xdp_umem_reg *mr)
395 struct xdp_umem *umem;
398 umem = kzalloc(sizeof(*umem), GFP_KERNEL);
400 return ERR_PTR(-ENOMEM);
402 err = xdp_umem_reg(umem, mr);
411 bool xdp_umem_validate_queues(struct xdp_umem *umem)
413 return umem->fq && umem->cq;