xdp_umem.c 8.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
// SPDX-License-Identifier: GPL-2.0
/* XDP user-space packet buffer
 * Copyright(c) 2018 Intel Corporation.
 */

#include <linux/init.h>
#include <linux/sched/mm.h>
#include <linux/sched/signal.h>
#include <linux/sched/task.h>
#include <linux/uaccess.h>
#include <linux/slab.h>
#include <linux/bpf.h>
#include <linux/mm.h>
14 15
#include <linux/netdevice.h>
#include <linux/rtnetlink.h>
16 17

#include "xdp_umem.h"
18
#include "xsk_queue.h"
19

20
#define XDP_UMEM_MIN_CHUNK_SIZE 2048
21

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
void xdp_add_sk_umem(struct xdp_umem *umem, struct xdp_sock *xs)
{
	unsigned long flags;

	spin_lock_irqsave(&umem->xsk_list_lock, flags);
	list_add_rcu(&xs->list, &umem->xsk_list);
	spin_unlock_irqrestore(&umem->xsk_list_lock, flags);
}

void xdp_del_sk_umem(struct xdp_umem *umem, struct xdp_sock *xs)
{
	unsigned long flags;

	if (xs->dev) {
		spin_lock_irqsave(&umem->xsk_list_lock, flags);
		list_del_rcu(&xs->list);
		spin_unlock_irqrestore(&umem->xsk_list_lock, flags);

		if (umem->zc)
			synchronize_net();
	}
}

45 46 47 48 49 50
/* The umem is stored both in the _rx struct and the _tx struct as we do
 * not know if the device has more tx queues than rx, or the opposite.
 * This might also change during run time.
 */
static void xdp_reg_umem_at_qid(struct net_device *dev, struct xdp_umem *umem,
				u16 queue_id)
51
{
52 53 54 55 56
	if (queue_id < dev->real_num_rx_queues)
		dev->_rx[queue_id].umem = umem;
	if (queue_id < dev->real_num_tx_queues)
		dev->_tx[queue_id].umem = umem;
}
57

58 59
struct xdp_umem *xdp_get_umem_from_qid(struct net_device *dev,
				       u16 queue_id)
60 61 62 63 64
{
	if (queue_id < dev->real_num_rx_queues)
		return dev->_rx[queue_id].umem;
	if (queue_id < dev->real_num_tx_queues)
		return dev->_tx[queue_id].umem;
65

66 67
	return NULL;
}
68

69 70 71 72 73 74 75 76 77
static void xdp_clear_umem_at_qid(struct net_device *dev, u16 queue_id)
{
	/* Zero out the entry independent on how many queues are configured
	 * at this point in time, as it might be used in the future.
	 */
	if (queue_id < dev->num_rx_queues)
		dev->_rx[queue_id].umem = NULL;
	if (queue_id < dev->num_tx_queues)
		dev->_tx[queue_id].umem = NULL;
78 79
}

80
int xdp_umem_assign_dev(struct xdp_umem *umem, struct net_device *dev,
81
			u16 queue_id, u16 flags)
82 83 84
{
	bool force_zc, force_copy;
	struct netdev_bpf bpf;
85
	int err = 0;
86 87 88 89 90 91 92

	force_zc = flags & XDP_ZEROCOPY;
	force_copy = flags & XDP_COPY;

	if (force_zc && force_copy)
		return -EINVAL;

93 94 95 96 97
	rtnl_lock();
	if (xdp_get_umem_from_qid(dev, queue_id)) {
		err = -EBUSY;
		goto out_rtnl_unlock;
	}
98

99 100 101 102 103 104
	xdp_reg_umem_at_qid(dev, umem, queue_id);
	umem->dev = dev;
	umem->queue_id = queue_id;
	if (force_copy)
		/* For copy-mode, we are done. */
		goto out_rtnl_unlock;
105

106 107 108 109
	if (!dev->netdev_ops->ndo_bpf ||
	    !dev->netdev_ops->ndo_xsk_async_xmit) {
		err = -EOPNOTSUPP;
		goto err_unreg_umem;
110
	}
111

112 113 114
	bpf.command = XDP_SETUP_XSK_UMEM;
	bpf.xsk.umem = umem;
	bpf.xsk.queue_id = queue_id;
115

116 117
	err = dev->netdev_ops->ndo_bpf(dev, &bpf);
	if (err)
118
		goto err_unreg_umem;
119
	rtnl_unlock();
120

121 122 123
	dev_hold(dev);
	umem->zc = true;
	return 0;
124

125 126 127 128 129
err_unreg_umem:
	xdp_clear_umem_at_qid(dev, queue_id);
	if (!force_zc)
		err = 0; /* fallback to copy mode */
out_rtnl_unlock:
130
	rtnl_unlock();
131
	return err;
132 133
}

134
static void xdp_umem_clear_dev(struct xdp_umem *umem)
135 136 137 138
{
	struct netdev_bpf bpf;
	int err;

139
	if (umem->zc) {
140 141 142 143 144 145 146 147 148 149
		bpf.command = XDP_SETUP_XSK_UMEM;
		bpf.xsk.umem = NULL;
		bpf.xsk.queue_id = umem->queue_id;

		rtnl_lock();
		err = umem->dev->netdev_ops->ndo_bpf(umem->dev, &bpf);
		rtnl_unlock();

		if (err)
			WARN(1, "failed to disable umem!\n");
150 151 152 153 154 155 156
	}

	if (umem->dev) {
		rtnl_lock();
		xdp_clear_umem_at_qid(umem->dev, umem->queue_id);
		rtnl_unlock();
	}
157

158
	if (umem->zc) {
159
		dev_put(umem->dev);
160
		umem->zc = false;
161 162 163
	}
}

164 165 166 167
static void xdp_umem_unpin_pages(struct xdp_umem *umem)
{
	unsigned int i;

B
Björn Töpel 已提交
168 169
	for (i = 0; i < umem->npgs; i++) {
		struct page *page = umem->pgs[i];
170

B
Björn Töpel 已提交
171 172
		set_page_dirty_lock(page);
		put_page(page);
173
	}
B
Björn Töpel 已提交
174 175 176

	kfree(umem->pgs);
	umem->pgs = NULL;
177 178 179 180
}

static void xdp_umem_unaccount_pages(struct xdp_umem *umem)
{
181 182 183 184
	if (umem->user) {
		atomic_long_sub(umem->npgs, &umem->user->locked_vm);
		free_uid(umem->user);
	}
185 186 187 188 189 190 191
}

static void xdp_umem_release(struct xdp_umem *umem)
{
	struct task_struct *task;
	struct mm_struct *mm;

192 193
	xdp_umem_clear_dev(umem);

194 195 196 197 198
	if (umem->fq) {
		xskq_destroy(umem->fq);
		umem->fq = NULL;
	}

199 200 201 202 203
	if (umem->cq) {
		xskq_destroy(umem->cq);
		umem->cq = NULL;
	}

204 205
	xsk_reuseq_destroy(umem);

B
Björn Töpel 已提交
206
	xdp_umem_unpin_pages(umem);
207

B
Björn Töpel 已提交
208 209 210 211 212 213 214 215
	task = get_pid_task(umem->pid, PIDTYPE_PID);
	put_pid(umem->pid);
	if (!task)
		goto out;
	mm = get_task_mm(task);
	put_task_struct(task);
	if (!mm)
		goto out;
216

B
Björn Töpel 已提交
217
	mmput(mm);
B
Björn Töpel 已提交
218 219 220
	kfree(umem->pages);
	umem->pages = NULL;

221 222 223 224 225 226 227 228 229 230 231 232 233 234
	xdp_umem_unaccount_pages(umem);
out:
	kfree(umem);
}

static void xdp_umem_release_deferred(struct work_struct *work)
{
	struct xdp_umem *umem = container_of(work, struct xdp_umem, work);

	xdp_umem_release(umem);
}

void xdp_get_umem(struct xdp_umem *umem)
{
235
	refcount_inc(&umem->users);
236 237 238 239 240 241 242
}

void xdp_put_umem(struct xdp_umem *umem)
{
	if (!umem)
		return;

243
	if (refcount_dec_and_test(&umem->users)) {
244 245 246 247 248 249 250 251 252 253 254
		INIT_WORK(&umem->work, xdp_umem_release_deferred);
		schedule_work(&umem->work);
	}
}

static int xdp_umem_pin_pages(struct xdp_umem *umem)
{
	unsigned int gup_flags = FOLL_WRITE;
	long npgs;
	int err;

255 256
	umem->pgs = kcalloc(umem->npgs, sizeof(*umem->pgs),
			    GFP_KERNEL | __GFP_NOWARN);
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
	if (!umem->pgs)
		return -ENOMEM;

	down_write(&current->mm->mmap_sem);
	npgs = get_user_pages(umem->address, umem->npgs,
			      gup_flags, &umem->pgs[0], NULL);
	up_write(&current->mm->mmap_sem);

	if (npgs != umem->npgs) {
		if (npgs >= 0) {
			umem->npgs = npgs;
			err = -ENOMEM;
			goto out_pin;
		}
		err = npgs;
		goto out_pgs;
	}
	return 0;

out_pin:
	xdp_umem_unpin_pages(umem);
out_pgs:
	kfree(umem->pgs);
	umem->pgs = NULL;
	return err;
}

static int xdp_umem_account_pages(struct xdp_umem *umem)
{
	unsigned long lock_limit, new_npgs, old_npgs;

	if (capable(CAP_IPC_LOCK))
		return 0;

	lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
	umem->user = get_uid(current_user());

	do {
		old_npgs = atomic_long_read(&umem->user->locked_vm);
		new_npgs = old_npgs + umem->npgs;
		if (new_npgs > lock_limit) {
			free_uid(umem->user);
			umem->user = NULL;
			return -ENOBUFS;
		}
	} while (atomic_long_cmpxchg(&umem->user->locked_vm, old_npgs,
				     new_npgs) != old_npgs);
	return 0;
}

B
Björn Töpel 已提交
307
static int xdp_umem_reg(struct xdp_umem *umem, struct xdp_umem_reg *mr)
308
{
309 310
	u32 chunk_size = mr->chunk_size, headroom = mr->headroom;
	unsigned int chunks, chunks_per_page;
311
	u64 addr = mr->addr, size = mr->len;
B
Björn Töpel 已提交
312
	int size_chk, err, i;
313

314
	if (chunk_size < XDP_UMEM_MIN_CHUNK_SIZE || chunk_size > PAGE_SIZE) {
315 316 317 318 319 320 321 322 323
		/* Strictly speaking we could support this, if:
		 * - huge pages, or*
		 * - using an IOMMU, or
		 * - making sure the memory area is consecutive
		 * but for now, we simply say "computer says no".
		 */
		return -EINVAL;
	}

324
	if (!is_power_of_2(chunk_size))
325 326 327 328 329 330 331 332 333 334 335 336
		return -EINVAL;

	if (!PAGE_ALIGNED(addr)) {
		/* Memory area has to be page size aligned. For
		 * simplicity, this might change.
		 */
		return -EINVAL;
	}

	if ((addr + size) < addr)
		return -EINVAL;

337 338
	chunks = (unsigned int)div_u64(size, chunk_size);
	if (chunks == 0)
339 340
		return -EINVAL;

341 342
	chunks_per_page = PAGE_SIZE / chunk_size;
	if (chunks < chunks_per_page || chunks % chunks_per_page)
343 344
		return -EINVAL;

345
	headroom = ALIGN(headroom, 64);
346

347
	size_chk = chunk_size - headroom - XDP_PACKET_HEADROOM;
348 349 350 351 352
	if (size_chk < 0)
		return -EINVAL;

	umem->pid = get_task_pid(current, PIDTYPE_PID);
	umem->address = (unsigned long)addr;
353 354
	umem->chunk_mask = ~((u64)chunk_size - 1);
	umem->size = size;
355 356
	umem->headroom = headroom;
	umem->chunk_size_nohr = chunk_size - headroom;
357 358 359
	umem->npgs = size / PAGE_SIZE;
	umem->pgs = NULL;
	umem->user = NULL;
360 361
	INIT_LIST_HEAD(&umem->xsk_list);
	spin_lock_init(&umem->xsk_list_lock);
362

363
	refcount_set(&umem->users, 1);
364 365 366 367 368 369 370 371

	err = xdp_umem_account_pages(umem);
	if (err)
		goto out;

	err = xdp_umem_pin_pages(umem);
	if (err)
		goto out_account;
B
Björn Töpel 已提交
372 373 374 375 376 377 378 379 380 381

	umem->pages = kcalloc(umem->npgs, sizeof(*umem->pages), GFP_KERNEL);
	if (!umem->pages) {
		err = -ENOMEM;
		goto out_account;
	}

	for (i = 0; i < umem->npgs; i++)
		umem->pages[i].addr = page_address(umem->pgs[i]);

382 383 384 385 386 387 388 389
	return 0;

out_account:
	xdp_umem_unaccount_pages(umem);
out:
	put_pid(umem->pid);
	return err;
}
390

B
Björn Töpel 已提交
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
struct xdp_umem *xdp_umem_create(struct xdp_umem_reg *mr)
{
	struct xdp_umem *umem;
	int err;

	umem = kzalloc(sizeof(*umem), GFP_KERNEL);
	if (!umem)
		return ERR_PTR(-ENOMEM);

	err = xdp_umem_reg(umem, mr);
	if (err) {
		kfree(umem);
		return ERR_PTR(err);
	}

	return umem;
}

409 410
bool xdp_umem_validate_queues(struct xdp_umem *umem)
{
411
	return umem->fq && umem->cq;
412
}