xsk: add id to umem
[linux-2.6-microblaze.git] / net / xdp / xdp_umem.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* XDP user-space packet buffer
3  * Copyright(c) 2018 Intel Corporation.
4  */
5
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>
13 #include <linux/mm.h>
14 #include <linux/netdevice.h>
15 #include <linux/rtnetlink.h>
16 #include <linux/idr.h>
17
18 #include "xdp_umem.h"
19 #include "xsk_queue.h"
20
21 #define XDP_UMEM_MIN_CHUNK_SIZE 2048
22
23 static DEFINE_IDA(umem_ida);
24
25 void xdp_add_sk_umem(struct xdp_umem *umem, struct xdp_sock *xs)
26 {
27         unsigned long flags;
28
29         spin_lock_irqsave(&umem->xsk_list_lock, flags);
30         list_add_rcu(&xs->list, &umem->xsk_list);
31         spin_unlock_irqrestore(&umem->xsk_list_lock, flags);
32 }
33
34 void xdp_del_sk_umem(struct xdp_umem *umem, struct xdp_sock *xs)
35 {
36         unsigned long flags;
37
38         spin_lock_irqsave(&umem->xsk_list_lock, flags);
39         list_del_rcu(&xs->list);
40         spin_unlock_irqrestore(&umem->xsk_list_lock, flags);
41 }
42
43 /* The umem is stored both in the _rx struct and the _tx struct as we do
44  * not know if the device has more tx queues than rx, or the opposite.
45  * This might also change during run time.
46  */
47 static void xdp_reg_umem_at_qid(struct net_device *dev, struct xdp_umem *umem,
48                                 u16 queue_id)
49 {
50         if (queue_id < dev->real_num_rx_queues)
51                 dev->_rx[queue_id].umem = umem;
52         if (queue_id < dev->real_num_tx_queues)
53                 dev->_tx[queue_id].umem = umem;
54 }
55
56 struct xdp_umem *xdp_get_umem_from_qid(struct net_device *dev,
57                                        u16 queue_id)
58 {
59         if (queue_id < dev->real_num_rx_queues)
60                 return dev->_rx[queue_id].umem;
61         if (queue_id < dev->real_num_tx_queues)
62                 return dev->_tx[queue_id].umem;
63
64         return NULL;
65 }
66
67 static void xdp_clear_umem_at_qid(struct net_device *dev, u16 queue_id)
68 {
69         if (queue_id < dev->real_num_rx_queues)
70                 dev->_rx[queue_id].umem = NULL;
71         if (queue_id < dev->real_num_tx_queues)
72                 dev->_tx[queue_id].umem = NULL;
73 }
74
75 int xdp_umem_assign_dev(struct xdp_umem *umem, struct net_device *dev,
76                         u16 queue_id, u16 flags)
77 {
78         bool force_zc, force_copy;
79         struct netdev_bpf bpf;
80         int err = 0;
81
82         force_zc = flags & XDP_ZEROCOPY;
83         force_copy = flags & XDP_COPY;
84
85         if (force_zc && force_copy)
86                 return -EINVAL;
87
88         rtnl_lock();
89         if (xdp_get_umem_from_qid(dev, queue_id)) {
90                 err = -EBUSY;
91                 goto out_rtnl_unlock;
92         }
93
94         xdp_reg_umem_at_qid(dev, umem, queue_id);
95         umem->dev = dev;
96         umem->queue_id = queue_id;
97         if (force_copy)
98                 /* For copy-mode, we are done. */
99                 goto out_rtnl_unlock;
100
101         if (!dev->netdev_ops->ndo_bpf ||
102             !dev->netdev_ops->ndo_xsk_async_xmit) {
103                 err = -EOPNOTSUPP;
104                 goto err_unreg_umem;
105         }
106
107         bpf.command = XDP_SETUP_XSK_UMEM;
108         bpf.xsk.umem = umem;
109         bpf.xsk.queue_id = queue_id;
110
111         err = dev->netdev_ops->ndo_bpf(dev, &bpf);
112         if (err)
113                 goto err_unreg_umem;
114         rtnl_unlock();
115
116         dev_hold(dev);
117         umem->zc = true;
118         return 0;
119
120 err_unreg_umem:
121         xdp_clear_umem_at_qid(dev, queue_id);
122         if (!force_zc)
123                 err = 0; /* fallback to copy mode */
124 out_rtnl_unlock:
125         rtnl_unlock();
126         return err;
127 }
128
129 static void xdp_umem_clear_dev(struct xdp_umem *umem)
130 {
131         struct netdev_bpf bpf;
132         int err;
133
134         if (umem->zc) {
135                 bpf.command = XDP_SETUP_XSK_UMEM;
136                 bpf.xsk.umem = NULL;
137                 bpf.xsk.queue_id = umem->queue_id;
138
139                 rtnl_lock();
140                 err = umem->dev->netdev_ops->ndo_bpf(umem->dev, &bpf);
141                 rtnl_unlock();
142
143                 if (err)
144                         WARN(1, "failed to disable umem!\n");
145         }
146
147         if (umem->dev) {
148                 rtnl_lock();
149                 xdp_clear_umem_at_qid(umem->dev, umem->queue_id);
150                 rtnl_unlock();
151         }
152
153         if (umem->zc) {
154                 dev_put(umem->dev);
155                 umem->zc = false;
156         }
157 }
158
159 static void xdp_umem_unpin_pages(struct xdp_umem *umem)
160 {
161         unsigned int i;
162
163         for (i = 0; i < umem->npgs; i++) {
164                 struct page *page = umem->pgs[i];
165
166                 set_page_dirty_lock(page);
167                 put_page(page);
168         }
169
170         kfree(umem->pgs);
171         umem->pgs = NULL;
172 }
173
174 static void xdp_umem_unaccount_pages(struct xdp_umem *umem)
175 {
176         if (umem->user) {
177                 atomic_long_sub(umem->npgs, &umem->user->locked_vm);
178                 free_uid(umem->user);
179         }
180 }
181
182 static void xdp_umem_release(struct xdp_umem *umem)
183 {
184         struct task_struct *task;
185         struct mm_struct *mm;
186
187         xdp_umem_clear_dev(umem);
188
189         ida_simple_remove(&umem_ida, umem->id);
190
191         if (umem->fq) {
192                 xskq_destroy(umem->fq);
193                 umem->fq = NULL;
194         }
195
196         if (umem->cq) {
197                 xskq_destroy(umem->cq);
198                 umem->cq = NULL;
199         }
200
201         xsk_reuseq_destroy(umem);
202
203         xdp_umem_unpin_pages(umem);
204
205         task = get_pid_task(umem->pid, PIDTYPE_PID);
206         put_pid(umem->pid);
207         if (!task)
208                 goto out;
209         mm = get_task_mm(task);
210         put_task_struct(task);
211         if (!mm)
212                 goto out;
213
214         mmput(mm);
215         kfree(umem->pages);
216         umem->pages = NULL;
217
218         xdp_umem_unaccount_pages(umem);
219 out:
220         kfree(umem);
221 }
222
223 static void xdp_umem_release_deferred(struct work_struct *work)
224 {
225         struct xdp_umem *umem = container_of(work, struct xdp_umem, work);
226
227         xdp_umem_release(umem);
228 }
229
230 void xdp_get_umem(struct xdp_umem *umem)
231 {
232         refcount_inc(&umem->users);
233 }
234
235 void xdp_put_umem(struct xdp_umem *umem)
236 {
237         if (!umem)
238                 return;
239
240         if (refcount_dec_and_test(&umem->users)) {
241                 INIT_WORK(&umem->work, xdp_umem_release_deferred);
242                 schedule_work(&umem->work);
243         }
244 }
245
246 static int xdp_umem_pin_pages(struct xdp_umem *umem)
247 {
248         unsigned int gup_flags = FOLL_WRITE;
249         long npgs;
250         int err;
251
252         umem->pgs = kcalloc(umem->npgs, sizeof(*umem->pgs),
253                             GFP_KERNEL | __GFP_NOWARN);
254         if (!umem->pgs)
255                 return -ENOMEM;
256
257         down_write(&current->mm->mmap_sem);
258         npgs = get_user_pages(umem->address, umem->npgs,
259                               gup_flags, &umem->pgs[0], NULL);
260         up_write(&current->mm->mmap_sem);
261
262         if (npgs != umem->npgs) {
263                 if (npgs >= 0) {
264                         umem->npgs = npgs;
265                         err = -ENOMEM;
266                         goto out_pin;
267                 }
268                 err = npgs;
269                 goto out_pgs;
270         }
271         return 0;
272
273 out_pin:
274         xdp_umem_unpin_pages(umem);
275 out_pgs:
276         kfree(umem->pgs);
277         umem->pgs = NULL;
278         return err;
279 }
280
281 static int xdp_umem_account_pages(struct xdp_umem *umem)
282 {
283         unsigned long lock_limit, new_npgs, old_npgs;
284
285         if (capable(CAP_IPC_LOCK))
286                 return 0;
287
288         lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
289         umem->user = get_uid(current_user());
290
291         do {
292                 old_npgs = atomic_long_read(&umem->user->locked_vm);
293                 new_npgs = old_npgs + umem->npgs;
294                 if (new_npgs > lock_limit) {
295                         free_uid(umem->user);
296                         umem->user = NULL;
297                         return -ENOBUFS;
298                 }
299         } while (atomic_long_cmpxchg(&umem->user->locked_vm, old_npgs,
300                                      new_npgs) != old_npgs);
301         return 0;
302 }
303
304 static int xdp_umem_reg(struct xdp_umem *umem, struct xdp_umem_reg *mr)
305 {
306         u32 chunk_size = mr->chunk_size, headroom = mr->headroom;
307         unsigned int chunks, chunks_per_page;
308         u64 addr = mr->addr, size = mr->len;
309         int size_chk, err, i;
310
311         if (chunk_size < XDP_UMEM_MIN_CHUNK_SIZE || chunk_size > PAGE_SIZE) {
312                 /* Strictly speaking we could support this, if:
313                  * - huge pages, or*
314                  * - using an IOMMU, or
315                  * - making sure the memory area is consecutive
316                  * but for now, we simply say "computer says no".
317                  */
318                 return -EINVAL;
319         }
320
321         if (!is_power_of_2(chunk_size))
322                 return -EINVAL;
323
324         if (!PAGE_ALIGNED(addr)) {
325                 /* Memory area has to be page size aligned. For
326                  * simplicity, this might change.
327                  */
328                 return -EINVAL;
329         }
330
331         if ((addr + size) < addr)
332                 return -EINVAL;
333
334         chunks = (unsigned int)div_u64(size, chunk_size);
335         if (chunks == 0)
336                 return -EINVAL;
337
338         chunks_per_page = PAGE_SIZE / chunk_size;
339         if (chunks < chunks_per_page || chunks % chunks_per_page)
340                 return -EINVAL;
341
342         headroom = ALIGN(headroom, 64);
343
344         size_chk = chunk_size - headroom - XDP_PACKET_HEADROOM;
345         if (size_chk < 0)
346                 return -EINVAL;
347
348         umem->pid = get_task_pid(current, PIDTYPE_PID);
349         umem->address = (unsigned long)addr;
350         umem->chunk_mask = ~((u64)chunk_size - 1);
351         umem->size = size;
352         umem->headroom = headroom;
353         umem->chunk_size_nohr = chunk_size - headroom;
354         umem->npgs = size / PAGE_SIZE;
355         umem->pgs = NULL;
356         umem->user = NULL;
357         INIT_LIST_HEAD(&umem->xsk_list);
358         spin_lock_init(&umem->xsk_list_lock);
359
360         refcount_set(&umem->users, 1);
361
362         err = xdp_umem_account_pages(umem);
363         if (err)
364                 goto out;
365
366         err = xdp_umem_pin_pages(umem);
367         if (err)
368                 goto out_account;
369
370         umem->pages = kcalloc(umem->npgs, sizeof(*umem->pages), GFP_KERNEL);
371         if (!umem->pages) {
372                 err = -ENOMEM;
373                 goto out_account;
374         }
375
376         for (i = 0; i < umem->npgs; i++)
377                 umem->pages[i].addr = page_address(umem->pgs[i]);
378
379         return 0;
380
381 out_account:
382         xdp_umem_unaccount_pages(umem);
383 out:
384         put_pid(umem->pid);
385         return err;
386 }
387
388 struct xdp_umem *xdp_umem_create(struct xdp_umem_reg *mr)
389 {
390         struct xdp_umem *umem;
391         int err;
392
393         umem = kzalloc(sizeof(*umem), GFP_KERNEL);
394         if (!umem)
395                 return ERR_PTR(-ENOMEM);
396
397         err = ida_simple_get(&umem_ida, 0, 0, GFP_KERNEL);
398         if (err < 0) {
399                 kfree(umem);
400                 return ERR_PTR(err);
401         }
402         umem->id = err;
403
404         err = xdp_umem_reg(umem, mr);
405         if (err) {
406                 ida_simple_remove(&umem_ida, umem->id);
407                 kfree(umem);
408                 return ERR_PTR(err);
409         }
410
411         return umem;
412 }
413
414 bool xdp_umem_validate_queues(struct xdp_umem *umem)
415 {
416         return umem->fq && umem->cq;
417 }