forked from mirrors/linux
		
	xdp: Allow registering memory model without rxq reference
The functions that register an XDP memory model take a struct xdp_rxq as parameter, but the RXQ is not actually used for anything other than pulling out the struct xdp_mem_info that it embeds. So refactor the register functions and export variants that just take a pointer to the xdp_mem_info. This is in preparation for enabling XDP_REDIRECT in bpf_prog_run(), using a page_pool instance that is not connected to any network device. Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://lore.kernel.org/bpf/20220103150812.87914-2-toke@redhat.com
This commit is contained in:
		
							parent
							
								
									640a171c93
								
							
						
					
					
						commit
						4a48ef70b9
					
				
					 2 changed files with 65 additions and 30 deletions
				
			
		| 
						 | 
					@ -260,6 +260,9 @@ bool xdp_rxq_info_is_reg(struct xdp_rxq_info *xdp_rxq);
 | 
				
			||||||
int xdp_rxq_info_reg_mem_model(struct xdp_rxq_info *xdp_rxq,
 | 
					int xdp_rxq_info_reg_mem_model(struct xdp_rxq_info *xdp_rxq,
 | 
				
			||||||
			       enum xdp_mem_type type, void *allocator);
 | 
								       enum xdp_mem_type type, void *allocator);
 | 
				
			||||||
void xdp_rxq_info_unreg_mem_model(struct xdp_rxq_info *xdp_rxq);
 | 
					void xdp_rxq_info_unreg_mem_model(struct xdp_rxq_info *xdp_rxq);
 | 
				
			||||||
 | 
					int xdp_reg_mem_model(struct xdp_mem_info *mem,
 | 
				
			||||||
 | 
							      enum xdp_mem_type type, void *allocator);
 | 
				
			||||||
 | 
					void xdp_unreg_mem_model(struct xdp_mem_info *mem);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* Drivers not supporting XDP metadata can use this helper, which
 | 
					/* Drivers not supporting XDP metadata can use this helper, which
 | 
				
			||||||
 * rejects any room expansion for metadata as a result.
 | 
					 * rejects any room expansion for metadata as a result.
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -110,20 +110,15 @@ static void mem_allocator_disconnect(void *allocator)
 | 
				
			||||||
	mutex_unlock(&mem_id_lock);
 | 
						mutex_unlock(&mem_id_lock);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void xdp_rxq_info_unreg_mem_model(struct xdp_rxq_info *xdp_rxq)
 | 
					void xdp_unreg_mem_model(struct xdp_mem_info *mem)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	struct xdp_mem_allocator *xa;
 | 
						struct xdp_mem_allocator *xa;
 | 
				
			||||||
	int type = xdp_rxq->mem.type;
 | 
						int type = mem->type;
 | 
				
			||||||
	int id = xdp_rxq->mem.id;
 | 
						int id = mem->id;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/* Reset mem info to defaults */
 | 
						/* Reset mem info to defaults */
 | 
				
			||||||
	xdp_rxq->mem.id = 0;
 | 
						mem->id = 0;
 | 
				
			||||||
	xdp_rxq->mem.type = 0;
 | 
						mem->type = 0;
 | 
				
			||||||
 | 
					 | 
				
			||||||
	if (xdp_rxq->reg_state != REG_STATE_REGISTERED) {
 | 
					 | 
				
			||||||
		WARN(1, "Missing register, driver bug");
 | 
					 | 
				
			||||||
		return;
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (id == 0)
 | 
						if (id == 0)
 | 
				
			||||||
		return;
 | 
							return;
 | 
				
			||||||
| 
						 | 
					@ -135,6 +130,17 @@ void xdp_rxq_info_unreg_mem_model(struct xdp_rxq_info *xdp_rxq)
 | 
				
			||||||
		rcu_read_unlock();
 | 
							rcu_read_unlock();
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					EXPORT_SYMBOL_GPL(xdp_unreg_mem_model);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void xdp_rxq_info_unreg_mem_model(struct xdp_rxq_info *xdp_rxq)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						if (xdp_rxq->reg_state != REG_STATE_REGISTERED) {
 | 
				
			||||||
 | 
							WARN(1, "Missing register, driver bug");
 | 
				
			||||||
 | 
							return;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						xdp_unreg_mem_model(&xdp_rxq->mem);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
EXPORT_SYMBOL_GPL(xdp_rxq_info_unreg_mem_model);
 | 
					EXPORT_SYMBOL_GPL(xdp_rxq_info_unreg_mem_model);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void xdp_rxq_info_unreg(struct xdp_rxq_info *xdp_rxq)
 | 
					void xdp_rxq_info_unreg(struct xdp_rxq_info *xdp_rxq)
 | 
				
			||||||
| 
						 | 
					@ -259,28 +265,24 @@ static bool __is_supported_mem_type(enum xdp_mem_type type)
 | 
				
			||||||
	return true;
 | 
						return true;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int xdp_rxq_info_reg_mem_model(struct xdp_rxq_info *xdp_rxq,
 | 
					static struct xdp_mem_allocator *__xdp_reg_mem_model(struct xdp_mem_info *mem,
 | 
				
			||||||
			       enum xdp_mem_type type, void *allocator)
 | 
											     enum xdp_mem_type type,
 | 
				
			||||||
 | 
											     void *allocator)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	struct xdp_mem_allocator *xdp_alloc;
 | 
						struct xdp_mem_allocator *xdp_alloc;
 | 
				
			||||||
	gfp_t gfp = GFP_KERNEL;
 | 
						gfp_t gfp = GFP_KERNEL;
 | 
				
			||||||
	int id, errno, ret;
 | 
						int id, errno, ret;
 | 
				
			||||||
	void *ptr;
 | 
						void *ptr;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (xdp_rxq->reg_state != REG_STATE_REGISTERED) {
 | 
					 | 
				
			||||||
		WARN(1, "Missing register, driver bug");
 | 
					 | 
				
			||||||
		return -EFAULT;
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if (!__is_supported_mem_type(type))
 | 
						if (!__is_supported_mem_type(type))
 | 
				
			||||||
		return -EOPNOTSUPP;
 | 
							return ERR_PTR(-EOPNOTSUPP);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	xdp_rxq->mem.type = type;
 | 
						mem->type = type;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (!allocator) {
 | 
						if (!allocator) {
 | 
				
			||||||
		if (type == MEM_TYPE_PAGE_POOL)
 | 
							if (type == MEM_TYPE_PAGE_POOL)
 | 
				
			||||||
			return -EINVAL; /* Setup time check page_pool req */
 | 
								return ERR_PTR(-EINVAL); /* Setup time check page_pool req */
 | 
				
			||||||
		return 0;
 | 
							return NULL;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/* Delay init of rhashtable to save memory if feature isn't used */
 | 
						/* Delay init of rhashtable to save memory if feature isn't used */
 | 
				
			||||||
| 
						 | 
					@ -290,13 +292,13 @@ int xdp_rxq_info_reg_mem_model(struct xdp_rxq_info *xdp_rxq,
 | 
				
			||||||
		mutex_unlock(&mem_id_lock);
 | 
							mutex_unlock(&mem_id_lock);
 | 
				
			||||||
		if (ret < 0) {
 | 
							if (ret < 0) {
 | 
				
			||||||
			WARN_ON(1);
 | 
								WARN_ON(1);
 | 
				
			||||||
			return ret;
 | 
								return ERR_PTR(ret);
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	xdp_alloc = kzalloc(sizeof(*xdp_alloc), gfp);
 | 
						xdp_alloc = kzalloc(sizeof(*xdp_alloc), gfp);
 | 
				
			||||||
	if (!xdp_alloc)
 | 
						if (!xdp_alloc)
 | 
				
			||||||
		return -ENOMEM;
 | 
							return ERR_PTR(-ENOMEM);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	mutex_lock(&mem_id_lock);
 | 
						mutex_lock(&mem_id_lock);
 | 
				
			||||||
	id = __mem_id_cyclic_get(gfp);
 | 
						id = __mem_id_cyclic_get(gfp);
 | 
				
			||||||
| 
						 | 
					@ -304,15 +306,15 @@ int xdp_rxq_info_reg_mem_model(struct xdp_rxq_info *xdp_rxq,
 | 
				
			||||||
		errno = id;
 | 
							errno = id;
 | 
				
			||||||
		goto err;
 | 
							goto err;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	xdp_rxq->mem.id = id;
 | 
						mem->id = id;
 | 
				
			||||||
	xdp_alloc->mem  = xdp_rxq->mem;
 | 
						xdp_alloc->mem = *mem;
 | 
				
			||||||
	xdp_alloc->allocator = allocator;
 | 
						xdp_alloc->allocator = allocator;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/* Insert allocator into ID lookup table */
 | 
						/* Insert allocator into ID lookup table */
 | 
				
			||||||
	ptr = rhashtable_insert_slow(mem_id_ht, &id, &xdp_alloc->node);
 | 
						ptr = rhashtable_insert_slow(mem_id_ht, &id, &xdp_alloc->node);
 | 
				
			||||||
	if (IS_ERR(ptr)) {
 | 
						if (IS_ERR(ptr)) {
 | 
				
			||||||
		ida_simple_remove(&mem_id_pool, xdp_rxq->mem.id);
 | 
							ida_simple_remove(&mem_id_pool, mem->id);
 | 
				
			||||||
		xdp_rxq->mem.id = 0;
 | 
							mem->id = 0;
 | 
				
			||||||
		errno = PTR_ERR(ptr);
 | 
							errno = PTR_ERR(ptr);
 | 
				
			||||||
		goto err;
 | 
							goto err;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					@ -322,13 +324,43 @@ int xdp_rxq_info_reg_mem_model(struct xdp_rxq_info *xdp_rxq,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	mutex_unlock(&mem_id_lock);
 | 
						mutex_unlock(&mem_id_lock);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	trace_mem_connect(xdp_alloc, xdp_rxq);
 | 
						return xdp_alloc;
 | 
				
			||||||
	return 0;
 | 
					 | 
				
			||||||
err:
 | 
					err:
 | 
				
			||||||
	mutex_unlock(&mem_id_lock);
 | 
						mutex_unlock(&mem_id_lock);
 | 
				
			||||||
	kfree(xdp_alloc);
 | 
						kfree(xdp_alloc);
 | 
				
			||||||
	return errno;
 | 
						return ERR_PTR(errno);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					int xdp_reg_mem_model(struct xdp_mem_info *mem,
 | 
				
			||||||
 | 
							      enum xdp_mem_type type, void *allocator)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						struct xdp_mem_allocator *xdp_alloc;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						xdp_alloc = __xdp_reg_mem_model(mem, type, allocator);
 | 
				
			||||||
 | 
						if (IS_ERR(xdp_alloc))
 | 
				
			||||||
 | 
							return PTR_ERR(xdp_alloc);
 | 
				
			||||||
 | 
						return 0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					EXPORT_SYMBOL_GPL(xdp_reg_mem_model);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					int xdp_rxq_info_reg_mem_model(struct xdp_rxq_info *xdp_rxq,
 | 
				
			||||||
 | 
								       enum xdp_mem_type type, void *allocator)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						struct xdp_mem_allocator *xdp_alloc;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (xdp_rxq->reg_state != REG_STATE_REGISTERED) {
 | 
				
			||||||
 | 
							WARN(1, "Missing register, driver bug");
 | 
				
			||||||
 | 
							return -EFAULT;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						xdp_alloc = __xdp_reg_mem_model(&xdp_rxq->mem, type, allocator);
 | 
				
			||||||
 | 
						if (IS_ERR(xdp_alloc))
 | 
				
			||||||
 | 
							return PTR_ERR(xdp_alloc);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						trace_mem_connect(xdp_alloc, xdp_rxq);
 | 
				
			||||||
 | 
						return 0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
EXPORT_SYMBOL_GPL(xdp_rxq_info_reg_mem_model);
 | 
					EXPORT_SYMBOL_GPL(xdp_rxq_info_reg_mem_model);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* XDP RX runs under NAPI protection, and in different delivery error
 | 
					/* XDP RX runs under NAPI protection, and in different delivery error
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue