mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
synced 2024-11-01 00:48:50 +00:00
ba3beec2ec
Fix a crash that happens if an Rx only socket is created first, then a
second socket is created that is Tx only and bound to the same umem as
the first socket and also the same netdev and queue_id together with the
XDP_SHARED_UMEM flag. In this specific case, the tx_descs array page
pool was not created by the first socket as it was an Rx only socket.
When the second socket is bound it needs this tx_descs array of this
shared page pool as it has a Tx component, but unfortunately it was
never allocated, leading to a crash. Note that this array is only used
for zero-copy drivers using the batched Tx APIs, currently only ice and
i40e.
[ 5511.150360] BUG: kernel NULL pointer dereference, address: 0000000000000008
[ 5511.158419] #PF: supervisor write access in kernel mode
[ 5511.164472] #PF: error_code(0x0002) - not-present page
[ 5511.170416] PGD 0 P4D 0
[ 5511.173347] Oops: 0002 [#1] PREEMPT SMP PTI
[ 5511.178186] CPU: 0 PID: 0 Comm: swapper/0 Tainted: G E 5.18.0-rc1+ #97
[ 5511.187245] Hardware name: Intel Corp. GRANTLEY/GRANTLEY, BIOS GRRFCRB1.86B.0276.D07.1605190235 05/19/2016
[ 5511.198418] RIP: 0010:xsk_tx_peek_release_desc_batch+0x198/0x310
[ 5511.205375] Code: c0 83 c6 01 84 c2 74 6d 8d 46 ff 23 07 44 89 e1 48 83 c0 14 48 c1 e1 04 48 c1 e0 04 48 03 47 10 4c 01 c1 48 8b 50 08 48 8b 00 <48> 89 51 08 48 89 01 41 80 bd d7 00 00 00 00 75 82 48 8b 19 49 8b
[ 5511.227091] RSP: 0018:ffffc90000003dd0 EFLAGS: 00010246
[ 5511.233135] RAX: 0000000000000000 RBX: ffff88810c8da600 RCX: 0000000000000000
[ 5511.241384] RDX: 000000000000003c RSI: 0000000000000001 RDI: ffff888115f555c0
[ 5511.249634] RBP: ffffc90000003e08 R08: 0000000000000000 R09: ffff889092296b48
[ 5511.257886] R10: 0000ffffffffffff R11: ffff889092296800 R12: 0000000000000000
[ 5511.266138] R13: ffff88810c8db500 R14: 0000000000000040 R15: 0000000000000100
[ 5511.274387] FS: 0000000000000000(0000) GS:ffff88903f800000(0000) knlGS:0000000000000000
[ 5511.283746] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[ 5511.290389] CR2: 0000000000000008 CR3: 00000001046e2001 CR4: 00000000003706f0
[ 5511.298640] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
[ 5511.306892] DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400
[ 5511.315142] Call Trace:
[ 5511.317972] <IRQ>
[ 5511.320301] ice_xmit_zc+0x68/0x2f0 [ice]
[ 5511.324977] ? ktime_get+0x38/0xa0
[ 5511.328913] ice_napi_poll+0x7a/0x6a0 [ice]
[ 5511.333784] __napi_poll+0x2c/0x160
[ 5511.337821] net_rx_action+0xdd/0x200
[ 5511.342058] __do_softirq+0xe6/0x2dd
[ 5511.346198] irq_exit_rcu+0xb5/0x100
[ 5511.350339] common_interrupt+0xa4/0xc0
[ 5511.354777] </IRQ>
[ 5511.357201] <TASK>
[ 5511.359625] asm_common_interrupt+0x1e/0x40
[ 5511.364466] RIP: 0010:cpuidle_enter_state+0xd2/0x360
[ 5511.370211] Code: 49 89 c5 0f 1f 44 00 00 31 ff e8 e9 00 7b ff 45 84 ff 74 12 9c 58 f6 c4 02 0f 85 72 02 00 00 31 ff e8 02 0c 80 ff fb 45 85 f6 <0f> 88 11 01 00 00 49 63 c6 4c 2b 2c 24 48 8d 14 40 48 8d 14 90 49
[ 5511.391921] RSP: 0018:ffffffff82a03e60 EFLAGS: 00000202
[ 5511.397962] RAX: ffff88903f800000 RBX: 0000000000000001 RCX: 000000000000001f
[ 5511.406214] RDX: 0000000000000000 RSI: ffffffff823400b9 RDI: ffffffff8234c046
[ 5511.424646] RBP: ffff88810a384800 R08: 000005032a28c046 R09: 0000000000000008
[ 5511.443233] R10: 000000000000000b R11: 0000000000000006 R12: ffffffff82bcf700
[ 5511.461922] R13: 000005032a28c046 R14: 0000000000000001 R15: 0000000000000000
[ 5511.480300] cpuidle_enter+0x29/0x40
[ 5511.494329] do_idle+0x1c7/0x250
[ 5511.507610] cpu_startup_entry+0x19/0x20
[ 5511.521394] start_kernel+0x649/0x66e
[ 5511.534626] secondary_startup_64_no_verify+0xc3/0xcb
[ 5511.549230] </TASK>
Detect such case during bind() and allocate this memory region via newly
introduced xp_alloc_tx_descs(). Also, use kvcalloc instead of kcalloc as
for other buffer pool allocations, so that it matches the kvfree() from
xp_destroy().
Fixes: d1bc532e99
("i40e: xsk: Move tmp desc array from driver to pool")
Signed-off-by: Maciej Fijalkowski <maciej.fijalkowski@intel.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Magnus Karlsson <magnus.karlsson@intel.com>
Link: https://lore.kernel.org/bpf/20220425153745.481322-1-maciej.fijalkowski@intel.com
675 lines
15 KiB
C
675 lines
15 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
#include <net/xsk_buff_pool.h>
|
|
#include <net/xdp_sock.h>
|
|
#include <net/xdp_sock_drv.h>
|
|
|
|
#include "xsk_queue.h"
|
|
#include "xdp_umem.h"
|
|
#include "xsk.h"
|
|
|
|
void xp_add_xsk(struct xsk_buff_pool *pool, struct xdp_sock *xs)
|
|
{
|
|
unsigned long flags;
|
|
|
|
if (!xs->tx)
|
|
return;
|
|
|
|
spin_lock_irqsave(&pool->xsk_tx_list_lock, flags);
|
|
list_add_rcu(&xs->tx_list, &pool->xsk_tx_list);
|
|
spin_unlock_irqrestore(&pool->xsk_tx_list_lock, flags);
|
|
}
|
|
|
|
void xp_del_xsk(struct xsk_buff_pool *pool, struct xdp_sock *xs)
|
|
{
|
|
unsigned long flags;
|
|
|
|
if (!xs->tx)
|
|
return;
|
|
|
|
spin_lock_irqsave(&pool->xsk_tx_list_lock, flags);
|
|
list_del_rcu(&xs->tx_list);
|
|
spin_unlock_irqrestore(&pool->xsk_tx_list_lock, flags);
|
|
}
|
|
|
|
void xp_destroy(struct xsk_buff_pool *pool)
|
|
{
|
|
if (!pool)
|
|
return;
|
|
|
|
kvfree(pool->tx_descs);
|
|
kvfree(pool->heads);
|
|
kvfree(pool);
|
|
}
|
|
|
|
int xp_alloc_tx_descs(struct xsk_buff_pool *pool, struct xdp_sock *xs)
|
|
{
|
|
pool->tx_descs = kvcalloc(xs->tx->nentries, sizeof(*pool->tx_descs),
|
|
GFP_KERNEL);
|
|
if (!pool->tx_descs)
|
|
return -ENOMEM;
|
|
|
|
return 0;
|
|
}
|
|
|
|
struct xsk_buff_pool *xp_create_and_assign_umem(struct xdp_sock *xs,
|
|
struct xdp_umem *umem)
|
|
{
|
|
bool unaligned = umem->flags & XDP_UMEM_UNALIGNED_CHUNK_FLAG;
|
|
struct xsk_buff_pool *pool;
|
|
struct xdp_buff_xsk *xskb;
|
|
u32 i, entries;
|
|
|
|
entries = unaligned ? umem->chunks : 0;
|
|
pool = kvzalloc(struct_size(pool, free_heads, entries), GFP_KERNEL);
|
|
if (!pool)
|
|
goto out;
|
|
|
|
pool->heads = kvcalloc(umem->chunks, sizeof(*pool->heads), GFP_KERNEL);
|
|
if (!pool->heads)
|
|
goto out;
|
|
|
|
if (xs->tx)
|
|
if (xp_alloc_tx_descs(pool, xs))
|
|
goto out;
|
|
|
|
pool->chunk_mask = ~((u64)umem->chunk_size - 1);
|
|
pool->addrs_cnt = umem->size;
|
|
pool->heads_cnt = umem->chunks;
|
|
pool->free_heads_cnt = umem->chunks;
|
|
pool->headroom = umem->headroom;
|
|
pool->chunk_size = umem->chunk_size;
|
|
pool->chunk_shift = ffs(umem->chunk_size) - 1;
|
|
pool->unaligned = unaligned;
|
|
pool->frame_len = umem->chunk_size - umem->headroom -
|
|
XDP_PACKET_HEADROOM;
|
|
pool->umem = umem;
|
|
pool->addrs = umem->addrs;
|
|
INIT_LIST_HEAD(&pool->free_list);
|
|
INIT_LIST_HEAD(&pool->xsk_tx_list);
|
|
spin_lock_init(&pool->xsk_tx_list_lock);
|
|
spin_lock_init(&pool->cq_lock);
|
|
refcount_set(&pool->users, 1);
|
|
|
|
pool->fq = xs->fq_tmp;
|
|
pool->cq = xs->cq_tmp;
|
|
|
|
for (i = 0; i < pool->free_heads_cnt; i++) {
|
|
xskb = &pool->heads[i];
|
|
xskb->pool = pool;
|
|
xskb->xdp.frame_sz = umem->chunk_size - umem->headroom;
|
|
INIT_LIST_HEAD(&xskb->free_list_node);
|
|
if (pool->unaligned)
|
|
pool->free_heads[i] = xskb;
|
|
else
|
|
xp_init_xskb_addr(xskb, pool, i * pool->chunk_size);
|
|
}
|
|
|
|
return pool;
|
|
|
|
out:
|
|
xp_destroy(pool);
|
|
return NULL;
|
|
}
|
|
|
|
void xp_set_rxq_info(struct xsk_buff_pool *pool, struct xdp_rxq_info *rxq)
|
|
{
|
|
u32 i;
|
|
|
|
for (i = 0; i < pool->heads_cnt; i++)
|
|
pool->heads[i].xdp.rxq = rxq;
|
|
}
|
|
EXPORT_SYMBOL(xp_set_rxq_info);
|
|
|
|
static void xp_disable_drv_zc(struct xsk_buff_pool *pool)
|
|
{
|
|
struct netdev_bpf bpf;
|
|
int err;
|
|
|
|
ASSERT_RTNL();
|
|
|
|
if (pool->umem->zc) {
|
|
bpf.command = XDP_SETUP_XSK_POOL;
|
|
bpf.xsk.pool = NULL;
|
|
bpf.xsk.queue_id = pool->queue_id;
|
|
|
|
err = pool->netdev->netdev_ops->ndo_bpf(pool->netdev, &bpf);
|
|
|
|
if (err)
|
|
WARN(1, "Failed to disable zero-copy!\n");
|
|
}
|
|
}
|
|
|
|
int xp_assign_dev(struct xsk_buff_pool *pool,
|
|
struct net_device *netdev, u16 queue_id, u16 flags)
|
|
{
|
|
bool force_zc, force_copy;
|
|
struct netdev_bpf bpf;
|
|
int err = 0;
|
|
|
|
ASSERT_RTNL();
|
|
|
|
force_zc = flags & XDP_ZEROCOPY;
|
|
force_copy = flags & XDP_COPY;
|
|
|
|
if (force_zc && force_copy)
|
|
return -EINVAL;
|
|
|
|
if (xsk_get_pool_from_qid(netdev, queue_id))
|
|
return -EBUSY;
|
|
|
|
pool->netdev = netdev;
|
|
pool->queue_id = queue_id;
|
|
err = xsk_reg_pool_at_qid(netdev, pool, queue_id);
|
|
if (err)
|
|
return err;
|
|
|
|
if (flags & XDP_USE_NEED_WAKEUP)
|
|
pool->uses_need_wakeup = true;
|
|
/* Tx needs to be explicitly woken up the first time. Also
|
|
* for supporting drivers that do not implement this
|
|
* feature. They will always have to call sendto() or poll().
|
|
*/
|
|
pool->cached_need_wakeup = XDP_WAKEUP_TX;
|
|
|
|
dev_hold(netdev);
|
|
|
|
if (force_copy)
|
|
/* For copy-mode, we are done. */
|
|
return 0;
|
|
|
|
if (!netdev->netdev_ops->ndo_bpf ||
|
|
!netdev->netdev_ops->ndo_xsk_wakeup) {
|
|
err = -EOPNOTSUPP;
|
|
goto err_unreg_pool;
|
|
}
|
|
|
|
bpf.command = XDP_SETUP_XSK_POOL;
|
|
bpf.xsk.pool = pool;
|
|
bpf.xsk.queue_id = queue_id;
|
|
|
|
err = netdev->netdev_ops->ndo_bpf(netdev, &bpf);
|
|
if (err)
|
|
goto err_unreg_pool;
|
|
|
|
if (!pool->dma_pages) {
|
|
WARN(1, "Driver did not DMA map zero-copy buffers");
|
|
err = -EINVAL;
|
|
goto err_unreg_xsk;
|
|
}
|
|
pool->umem->zc = true;
|
|
return 0;
|
|
|
|
err_unreg_xsk:
|
|
xp_disable_drv_zc(pool);
|
|
err_unreg_pool:
|
|
if (!force_zc)
|
|
err = 0; /* fallback to copy mode */
|
|
if (err) {
|
|
xsk_clear_pool_at_qid(netdev, queue_id);
|
|
dev_put(netdev);
|
|
}
|
|
return err;
|
|
}
|
|
|
|
int xp_assign_dev_shared(struct xsk_buff_pool *pool, struct xdp_umem *umem,
|
|
struct net_device *dev, u16 queue_id)
|
|
{
|
|
u16 flags;
|
|
|
|
/* One fill and completion ring required for each queue id. */
|
|
if (!pool->fq || !pool->cq)
|
|
return -EINVAL;
|
|
|
|
flags = umem->zc ? XDP_ZEROCOPY : XDP_COPY;
|
|
if (pool->uses_need_wakeup)
|
|
flags |= XDP_USE_NEED_WAKEUP;
|
|
|
|
return xp_assign_dev(pool, dev, queue_id, flags);
|
|
}
|
|
|
|
void xp_clear_dev(struct xsk_buff_pool *pool)
|
|
{
|
|
if (!pool->netdev)
|
|
return;
|
|
|
|
xp_disable_drv_zc(pool);
|
|
xsk_clear_pool_at_qid(pool->netdev, pool->queue_id);
|
|
dev_put(pool->netdev);
|
|
pool->netdev = NULL;
|
|
}
|
|
|
|
static void xp_release_deferred(struct work_struct *work)
|
|
{
|
|
struct xsk_buff_pool *pool = container_of(work, struct xsk_buff_pool,
|
|
work);
|
|
|
|
rtnl_lock();
|
|
xp_clear_dev(pool);
|
|
rtnl_unlock();
|
|
|
|
if (pool->fq) {
|
|
xskq_destroy(pool->fq);
|
|
pool->fq = NULL;
|
|
}
|
|
|
|
if (pool->cq) {
|
|
xskq_destroy(pool->cq);
|
|
pool->cq = NULL;
|
|
}
|
|
|
|
xdp_put_umem(pool->umem, false);
|
|
xp_destroy(pool);
|
|
}
|
|
|
|
void xp_get_pool(struct xsk_buff_pool *pool)
|
|
{
|
|
refcount_inc(&pool->users);
|
|
}
|
|
|
|
bool xp_put_pool(struct xsk_buff_pool *pool)
|
|
{
|
|
if (!pool)
|
|
return false;
|
|
|
|
if (refcount_dec_and_test(&pool->users)) {
|
|
INIT_WORK(&pool->work, xp_release_deferred);
|
|
schedule_work(&pool->work);
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
static struct xsk_dma_map *xp_find_dma_map(struct xsk_buff_pool *pool)
|
|
{
|
|
struct xsk_dma_map *dma_map;
|
|
|
|
list_for_each_entry(dma_map, &pool->umem->xsk_dma_list, list) {
|
|
if (dma_map->netdev == pool->netdev)
|
|
return dma_map;
|
|
}
|
|
|
|
return NULL;
|
|
}
|
|
|
|
static struct xsk_dma_map *xp_create_dma_map(struct device *dev, struct net_device *netdev,
|
|
u32 nr_pages, struct xdp_umem *umem)
|
|
{
|
|
struct xsk_dma_map *dma_map;
|
|
|
|
dma_map = kzalloc(sizeof(*dma_map), GFP_KERNEL);
|
|
if (!dma_map)
|
|
return NULL;
|
|
|
|
dma_map->dma_pages = kvcalloc(nr_pages, sizeof(*dma_map->dma_pages), GFP_KERNEL);
|
|
if (!dma_map->dma_pages) {
|
|
kfree(dma_map);
|
|
return NULL;
|
|
}
|
|
|
|
dma_map->netdev = netdev;
|
|
dma_map->dev = dev;
|
|
dma_map->dma_need_sync = false;
|
|
dma_map->dma_pages_cnt = nr_pages;
|
|
refcount_set(&dma_map->users, 1);
|
|
list_add(&dma_map->list, &umem->xsk_dma_list);
|
|
return dma_map;
|
|
}
|
|
|
|
static void xp_destroy_dma_map(struct xsk_dma_map *dma_map)
|
|
{
|
|
list_del(&dma_map->list);
|
|
kvfree(dma_map->dma_pages);
|
|
kfree(dma_map);
|
|
}
|
|
|
|
static void __xp_dma_unmap(struct xsk_dma_map *dma_map, unsigned long attrs)
|
|
{
|
|
dma_addr_t *dma;
|
|
u32 i;
|
|
|
|
for (i = 0; i < dma_map->dma_pages_cnt; i++) {
|
|
dma = &dma_map->dma_pages[i];
|
|
if (*dma) {
|
|
dma_unmap_page_attrs(dma_map->dev, *dma, PAGE_SIZE,
|
|
DMA_BIDIRECTIONAL, attrs);
|
|
*dma = 0;
|
|
}
|
|
}
|
|
|
|
xp_destroy_dma_map(dma_map);
|
|
}
|
|
|
|
void xp_dma_unmap(struct xsk_buff_pool *pool, unsigned long attrs)
|
|
{
|
|
struct xsk_dma_map *dma_map;
|
|
|
|
if (pool->dma_pages_cnt == 0)
|
|
return;
|
|
|
|
dma_map = xp_find_dma_map(pool);
|
|
if (!dma_map) {
|
|
WARN(1, "Could not find dma_map for device");
|
|
return;
|
|
}
|
|
|
|
if (!refcount_dec_and_test(&dma_map->users))
|
|
return;
|
|
|
|
__xp_dma_unmap(dma_map, attrs);
|
|
kvfree(pool->dma_pages);
|
|
pool->dma_pages_cnt = 0;
|
|
pool->dev = NULL;
|
|
}
|
|
EXPORT_SYMBOL(xp_dma_unmap);
|
|
|
|
static void xp_check_dma_contiguity(struct xsk_dma_map *dma_map)
|
|
{
|
|
u32 i;
|
|
|
|
for (i = 0; i < dma_map->dma_pages_cnt - 1; i++) {
|
|
if (dma_map->dma_pages[i] + PAGE_SIZE == dma_map->dma_pages[i + 1])
|
|
dma_map->dma_pages[i] |= XSK_NEXT_PG_CONTIG_MASK;
|
|
else
|
|
dma_map->dma_pages[i] &= ~XSK_NEXT_PG_CONTIG_MASK;
|
|
}
|
|
}
|
|
|
|
static int xp_init_dma_info(struct xsk_buff_pool *pool, struct xsk_dma_map *dma_map)
|
|
{
|
|
pool->dma_pages = kvcalloc(dma_map->dma_pages_cnt, sizeof(*pool->dma_pages), GFP_KERNEL);
|
|
if (!pool->dma_pages)
|
|
return -ENOMEM;
|
|
|
|
pool->dev = dma_map->dev;
|
|
pool->dma_pages_cnt = dma_map->dma_pages_cnt;
|
|
pool->dma_need_sync = dma_map->dma_need_sync;
|
|
memcpy(pool->dma_pages, dma_map->dma_pages,
|
|
pool->dma_pages_cnt * sizeof(*pool->dma_pages));
|
|
|
|
return 0;
|
|
}
|
|
|
|
int xp_dma_map(struct xsk_buff_pool *pool, struct device *dev,
|
|
unsigned long attrs, struct page **pages, u32 nr_pages)
|
|
{
|
|
struct xsk_dma_map *dma_map;
|
|
dma_addr_t dma;
|
|
int err;
|
|
u32 i;
|
|
|
|
dma_map = xp_find_dma_map(pool);
|
|
if (dma_map) {
|
|
err = xp_init_dma_info(pool, dma_map);
|
|
if (err)
|
|
return err;
|
|
|
|
refcount_inc(&dma_map->users);
|
|
return 0;
|
|
}
|
|
|
|
dma_map = xp_create_dma_map(dev, pool->netdev, nr_pages, pool->umem);
|
|
if (!dma_map)
|
|
return -ENOMEM;
|
|
|
|
for (i = 0; i < dma_map->dma_pages_cnt; i++) {
|
|
dma = dma_map_page_attrs(dev, pages[i], 0, PAGE_SIZE,
|
|
DMA_BIDIRECTIONAL, attrs);
|
|
if (dma_mapping_error(dev, dma)) {
|
|
__xp_dma_unmap(dma_map, attrs);
|
|
return -ENOMEM;
|
|
}
|
|
if (dma_need_sync(dev, dma))
|
|
dma_map->dma_need_sync = true;
|
|
dma_map->dma_pages[i] = dma;
|
|
}
|
|
|
|
if (pool->unaligned)
|
|
xp_check_dma_contiguity(dma_map);
|
|
else
|
|
for (i = 0; i < pool->heads_cnt; i++) {
|
|
struct xdp_buff_xsk *xskb = &pool->heads[i];
|
|
|
|
xp_init_xskb_dma(xskb, pool, dma_map->dma_pages, xskb->orig_addr);
|
|
}
|
|
|
|
err = xp_init_dma_info(pool, dma_map);
|
|
if (err) {
|
|
__xp_dma_unmap(dma_map, attrs);
|
|
return err;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
EXPORT_SYMBOL(xp_dma_map);
|
|
|
|
static bool xp_addr_crosses_non_contig_pg(struct xsk_buff_pool *pool,
|
|
u64 addr)
|
|
{
|
|
return xp_desc_crosses_non_contig_pg(pool, addr, pool->chunk_size);
|
|
}
|
|
|
|
static bool xp_check_unaligned(struct xsk_buff_pool *pool, u64 *addr)
|
|
{
|
|
*addr = xp_unaligned_extract_addr(*addr);
|
|
if (*addr >= pool->addrs_cnt ||
|
|
*addr + pool->chunk_size > pool->addrs_cnt ||
|
|
xp_addr_crosses_non_contig_pg(pool, *addr))
|
|
return false;
|
|
return true;
|
|
}
|
|
|
|
static bool xp_check_aligned(struct xsk_buff_pool *pool, u64 *addr)
|
|
{
|
|
*addr = xp_aligned_extract_addr(pool, *addr);
|
|
return *addr < pool->addrs_cnt;
|
|
}
|
|
|
|
static struct xdp_buff_xsk *__xp_alloc(struct xsk_buff_pool *pool)
|
|
{
|
|
struct xdp_buff_xsk *xskb;
|
|
u64 addr;
|
|
bool ok;
|
|
|
|
if (pool->free_heads_cnt == 0)
|
|
return NULL;
|
|
|
|
for (;;) {
|
|
if (!xskq_cons_peek_addr_unchecked(pool->fq, &addr)) {
|
|
pool->fq->queue_empty_descs++;
|
|
return NULL;
|
|
}
|
|
|
|
ok = pool->unaligned ? xp_check_unaligned(pool, &addr) :
|
|
xp_check_aligned(pool, &addr);
|
|
if (!ok) {
|
|
pool->fq->invalid_descs++;
|
|
xskq_cons_release(pool->fq);
|
|
continue;
|
|
}
|
|
break;
|
|
}
|
|
|
|
if (pool->unaligned) {
|
|
xskb = pool->free_heads[--pool->free_heads_cnt];
|
|
xp_init_xskb_addr(xskb, pool, addr);
|
|
if (pool->dma_pages_cnt)
|
|
xp_init_xskb_dma(xskb, pool, pool->dma_pages, addr);
|
|
} else {
|
|
xskb = &pool->heads[xp_aligned_extract_idx(pool, addr)];
|
|
}
|
|
|
|
xskq_cons_release(pool->fq);
|
|
return xskb;
|
|
}
|
|
|
|
struct xdp_buff *xp_alloc(struct xsk_buff_pool *pool)
|
|
{
|
|
struct xdp_buff_xsk *xskb;
|
|
|
|
if (!pool->free_list_cnt) {
|
|
xskb = __xp_alloc(pool);
|
|
if (!xskb)
|
|
return NULL;
|
|
} else {
|
|
pool->free_list_cnt--;
|
|
xskb = list_first_entry(&pool->free_list, struct xdp_buff_xsk,
|
|
free_list_node);
|
|
list_del_init(&xskb->free_list_node);
|
|
}
|
|
|
|
xskb->xdp.data = xskb->xdp.data_hard_start + XDP_PACKET_HEADROOM;
|
|
xskb->xdp.data_meta = xskb->xdp.data;
|
|
|
|
if (pool->dma_need_sync) {
|
|
dma_sync_single_range_for_device(pool->dev, xskb->dma, 0,
|
|
pool->frame_len,
|
|
DMA_BIDIRECTIONAL);
|
|
}
|
|
return &xskb->xdp;
|
|
}
|
|
EXPORT_SYMBOL(xp_alloc);
|
|
|
|
static u32 xp_alloc_new_from_fq(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 max)
|
|
{
|
|
u32 i, cached_cons, nb_entries;
|
|
|
|
if (max > pool->free_heads_cnt)
|
|
max = pool->free_heads_cnt;
|
|
max = xskq_cons_nb_entries(pool->fq, max);
|
|
|
|
cached_cons = pool->fq->cached_cons;
|
|
nb_entries = max;
|
|
i = max;
|
|
while (i--) {
|
|
struct xdp_buff_xsk *xskb;
|
|
u64 addr;
|
|
bool ok;
|
|
|
|
__xskq_cons_read_addr_unchecked(pool->fq, cached_cons++, &addr);
|
|
|
|
ok = pool->unaligned ? xp_check_unaligned(pool, &addr) :
|
|
xp_check_aligned(pool, &addr);
|
|
if (unlikely(!ok)) {
|
|
pool->fq->invalid_descs++;
|
|
nb_entries--;
|
|
continue;
|
|
}
|
|
|
|
if (pool->unaligned) {
|
|
xskb = pool->free_heads[--pool->free_heads_cnt];
|
|
xp_init_xskb_addr(xskb, pool, addr);
|
|
if (pool->dma_pages_cnt)
|
|
xp_init_xskb_dma(xskb, pool, pool->dma_pages, addr);
|
|
} else {
|
|
xskb = &pool->heads[xp_aligned_extract_idx(pool, addr)];
|
|
}
|
|
|
|
*xdp = &xskb->xdp;
|
|
xdp++;
|
|
}
|
|
|
|
xskq_cons_release_n(pool->fq, max);
|
|
return nb_entries;
|
|
}
|
|
|
|
static u32 xp_alloc_reused(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 nb_entries)
|
|
{
|
|
struct xdp_buff_xsk *xskb;
|
|
u32 i;
|
|
|
|
nb_entries = min_t(u32, nb_entries, pool->free_list_cnt);
|
|
|
|
i = nb_entries;
|
|
while (i--) {
|
|
xskb = list_first_entry(&pool->free_list, struct xdp_buff_xsk, free_list_node);
|
|
list_del_init(&xskb->free_list_node);
|
|
|
|
*xdp = &xskb->xdp;
|
|
xdp++;
|
|
}
|
|
pool->free_list_cnt -= nb_entries;
|
|
|
|
return nb_entries;
|
|
}
|
|
|
|
u32 xp_alloc_batch(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 max)
|
|
{
|
|
u32 nb_entries1 = 0, nb_entries2;
|
|
|
|
if (unlikely(pool->dma_need_sync)) {
|
|
struct xdp_buff *buff;
|
|
|
|
/* Slow path */
|
|
buff = xp_alloc(pool);
|
|
if (buff)
|
|
*xdp = buff;
|
|
return !!buff;
|
|
}
|
|
|
|
if (unlikely(pool->free_list_cnt)) {
|
|
nb_entries1 = xp_alloc_reused(pool, xdp, max);
|
|
if (nb_entries1 == max)
|
|
return nb_entries1;
|
|
|
|
max -= nb_entries1;
|
|
xdp += nb_entries1;
|
|
}
|
|
|
|
nb_entries2 = xp_alloc_new_from_fq(pool, xdp, max);
|
|
if (!nb_entries2)
|
|
pool->fq->queue_empty_descs++;
|
|
|
|
return nb_entries1 + nb_entries2;
|
|
}
|
|
EXPORT_SYMBOL(xp_alloc_batch);
|
|
|
|
bool xp_can_alloc(struct xsk_buff_pool *pool, u32 count)
|
|
{
|
|
if (pool->free_list_cnt >= count)
|
|
return true;
|
|
return xskq_cons_has_entries(pool->fq, count - pool->free_list_cnt);
|
|
}
|
|
EXPORT_SYMBOL(xp_can_alloc);
|
|
|
|
void xp_free(struct xdp_buff_xsk *xskb)
|
|
{
|
|
if (!list_empty(&xskb->free_list_node))
|
|
return;
|
|
|
|
xskb->pool->free_list_cnt++;
|
|
list_add(&xskb->free_list_node, &xskb->pool->free_list);
|
|
}
|
|
EXPORT_SYMBOL(xp_free);
|
|
|
|
void *xp_raw_get_data(struct xsk_buff_pool *pool, u64 addr)
|
|
{
|
|
addr = pool->unaligned ? xp_unaligned_add_offset_to_addr(addr) : addr;
|
|
return pool->addrs + addr;
|
|
}
|
|
EXPORT_SYMBOL(xp_raw_get_data);
|
|
|
|
dma_addr_t xp_raw_get_dma(struct xsk_buff_pool *pool, u64 addr)
|
|
{
|
|
addr = pool->unaligned ? xp_unaligned_add_offset_to_addr(addr) : addr;
|
|
return (pool->dma_pages[addr >> PAGE_SHIFT] &
|
|
~XSK_NEXT_PG_CONTIG_MASK) +
|
|
(addr & ~PAGE_MASK);
|
|
}
|
|
EXPORT_SYMBOL(xp_raw_get_dma);
|
|
|
|
void xp_dma_sync_for_cpu_slow(struct xdp_buff_xsk *xskb)
|
|
{
|
|
dma_sync_single_range_for_cpu(xskb->pool->dev, xskb->dma, 0,
|
|
xskb->pool->frame_len, DMA_BIDIRECTIONAL);
|
|
}
|
|
EXPORT_SYMBOL(xp_dma_sync_for_cpu_slow);
|
|
|
|
void xp_dma_sync_for_device_slow(struct xsk_buff_pool *pool, dma_addr_t dma,
|
|
size_t size)
|
|
{
|
|
dma_sync_single_range_for_device(pool->dev, dma, 0,
|
|
size, DMA_BIDIRECTIONAL);
|
|
}
|
|
EXPORT_SYMBOL(xp_dma_sync_for_device_slow);
|