Skip to content

Commit 18d3f41

Browse files
kuba-mooNipaLocal
authored and
NipaLocal
committed
net: move mp dev config validation to __net_mp_open_rxq()
devmem code performs a number of safety checks to avoid having to reimplement all of them in the drivers. Move those to __net_mp_open_rxq() and reuse that function for binding to make sure that io_uring ZC also benefits from them. While at it rename the queue ID variable to rxq_idx in __net_mp_open_rxq(), we touch most of the relevant lines. Fixes: 6e18ed9 ("net: add helpers for setting a memory provider on an rx queue") Signed-off-by: Jakub Kicinski <[email protected]> Acked-by: Stanislav Fomichev <[email protected]> Signed-off-by: NipaLocal <nipa@local>
1 parent 1bbd534 commit 18d3f41

File tree

4 files changed

+54
-57
lines changed

4 files changed

+54
-57
lines changed

include/net/page_pool/memory_provider.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <net/page_pool/types.h>
77

88
struct netdev_rx_queue;
9+
struct netlink_ext_ack;
910
struct sk_buff;
1011

1112
struct memory_provider_ops {
@@ -24,8 +25,13 @@ void net_mp_niov_clear_page_pool(struct net_iov *niov);
2425

2526
int net_mp_open_rxq(struct net_device *dev, unsigned ifq_idx,
2627
struct pp_memory_provider_params *p);
28+
int __net_mp_open_rxq(struct net_device *dev, unsigned int ifq_idx,
29+
const struct pp_memory_provider_params *p,
30+
struct netlink_ext_ack *extack);
2731
void net_mp_close_rxq(struct net_device *dev, unsigned ifq_idx,
2832
struct pp_memory_provider_params *old_p);
33+
void __net_mp_close_rxq(struct net_device *dev, unsigned int ifq_idx,
34+
const struct pp_memory_provider_params *old_p);
2935

3036
/**
3137
* net_mp_netmem_place_in_cache() - give a netmem to a page pool

net/core/devmem.c

Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
*/
99

1010
#include <linux/dma-buf.h>
11-
#include <linux/ethtool_netlink.h>
1211
#include <linux/genalloc.h>
1312
#include <linux/mm.h>
1413
#include <linux/netdevice.h>
@@ -143,57 +142,28 @@ int net_devmem_bind_dmabuf_to_queue(struct net_device *dev, u32 rxq_idx,
143142
struct net_devmem_dmabuf_binding *binding,
144143
struct netlink_ext_ack *extack)
145144
{
145+
struct pp_memory_provider_params mp_params = {
146+
.mp_priv = binding,
147+
.mp_ops = &dmabuf_devmem_ops,
148+
};
146149
struct netdev_rx_queue *rxq;
147150
u32 xa_idx;
148151
int err;
149152

150-
if (rxq_idx >= dev->real_num_rx_queues) {
151-
NL_SET_ERR_MSG(extack, "rx queue index out of range");
152-
return -ERANGE;
153-
}
154-
155-
if (dev->cfg->hds_config != ETHTOOL_TCP_DATA_SPLIT_ENABLED) {
156-
NL_SET_ERR_MSG(extack, "tcp-data-split is disabled");
157-
return -EINVAL;
158-
}
159-
160-
if (dev->cfg->hds_thresh) {
161-
NL_SET_ERR_MSG(extack, "hds-thresh is not zero");
162-
return -EINVAL;
163-
}
153+
err = __net_mp_open_rxq(dev, rxq_idx, &mp_params, extack);
154+
if (err)
155+
return err;
164156

165157
rxq = __netif_get_rx_queue(dev, rxq_idx);
166-
if (rxq->mp_params.mp_ops) {
167-
NL_SET_ERR_MSG(extack, "designated queue already memory provider bound");
168-
return -EEXIST;
169-
}
170-
171-
#ifdef CONFIG_XDP_SOCKETS
172-
if (rxq->pool) {
173-
NL_SET_ERR_MSG(extack, "designated queue already in use by AF_XDP");
174-
return -EBUSY;
175-
}
176-
#endif
177-
178158
err = xa_alloc(&binding->bound_rxqs, &xa_idx, rxq, xa_limit_32b,
179159
GFP_KERNEL);
180160
if (err)
181-
return err;
182-
183-
rxq->mp_params.mp_priv = binding;
184-
rxq->mp_params.mp_ops = &dmabuf_devmem_ops;
185-
186-
err = netdev_rx_queue_restart(dev, rxq_idx);
187-
if (err)
188-
goto err_xa_erase;
161+
goto err_close_rxq;
189162

190163
return 0;
191164

192-
err_xa_erase:
193-
rxq->mp_params.mp_priv = NULL;
194-
rxq->mp_params.mp_ops = NULL;
195-
xa_erase(&binding->bound_rxqs, xa_idx);
196-
165+
err_close_rxq:
166+
__net_mp_close_rxq(dev, rxq_idx, &mp_params);
197167
return err;
198168
}
199169

net/core/netdev-genl.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -874,12 +874,6 @@ int netdev_nl_bind_rx_doit(struct sk_buff *skb, struct genl_info *info)
874874
goto err_unlock;
875875
}
876876

877-
if (dev_xdp_prog_count(netdev)) {
878-
NL_SET_ERR_MSG(info->extack, "unable to bind dmabuf to device with XDP program attached");
879-
err = -EEXIST;
880-
goto err_unlock;
881-
}
882-
883877
binding = net_devmem_bind_dmabuf(netdev, dmabuf_fd, info->extack);
884878
if (IS_ERR(binding)) {
885879
err = PTR_ERR(binding);

net/core/netdev_rx_queue.c

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// SPDX-License-Identifier: GPL-2.0-or-later
22

3+
#include <linux/ethtool_netlink.h>
34
#include <linux/netdevice.h>
45
#include <net/netdev_lock.h>
56
#include <net/netdev_queues.h>
@@ -86,45 +87,71 @@ int netdev_rx_queue_restart(struct net_device *dev, unsigned int rxq_idx)
8687
}
8788
EXPORT_SYMBOL_NS_GPL(netdev_rx_queue_restart, "NETDEV_INTERNAL");
8889

89-
static int __net_mp_open_rxq(struct net_device *dev, unsigned ifq_idx,
90-
struct pp_memory_provider_params *p)
90+
int __net_mp_open_rxq(struct net_device *dev, unsigned int rxq_idx,
91+
const struct pp_memory_provider_params *p,
92+
struct netlink_ext_ack *extack)
9193
{
9294
struct netdev_rx_queue *rxq;
9395
int ret;
9496

9597
if (!netdev_need_ops_lock(dev))
9698
return -EOPNOTSUPP;
9799

98-
if (ifq_idx >= dev->real_num_rx_queues)
100+
if (rxq_idx >= dev->real_num_rx_queues)
99101
return -EINVAL;
100-
ifq_idx = array_index_nospec(ifq_idx, dev->real_num_rx_queues);
102+
rxq_idx = array_index_nospec(rxq_idx, dev->real_num_rx_queues);
101103

102-
rxq = __netif_get_rx_queue(dev, ifq_idx);
103-
if (rxq->mp_params.mp_ops)
104+
if (rxq_idx >= dev->real_num_rx_queues) {
105+
NL_SET_ERR_MSG(extack, "rx queue index out of range");
106+
return -ERANGE;
107+
}
108+
if (dev->cfg->hds_config != ETHTOOL_TCP_DATA_SPLIT_ENABLED) {
109+
NL_SET_ERR_MSG(extack, "tcp-data-split is disabled");
110+
return -EINVAL;
111+
}
112+
if (dev->cfg->hds_thresh) {
113+
NL_SET_ERR_MSG(extack, "hds-thresh is not zero");
114+
return -EINVAL;
115+
}
116+
if (dev_xdp_prog_count(dev)) {
117+
NL_SET_ERR_MSG(extack, "unable to custom memory provider to device with XDP program attached");
104118
return -EEXIST;
119+
}
120+
121+
rxq = __netif_get_rx_queue(dev, rxq_idx);
122+
if (rxq->mp_params.mp_ops) {
123+
NL_SET_ERR_MSG(extack, "designated queue already memory provider bound");
124+
return -EEXIST;
125+
}
126+
#ifdef CONFIG_XDP_SOCKETS
127+
if (rxq->pool) {
128+
NL_SET_ERR_MSG(extack, "designated queue already in use by AF_XDP");
129+
return -EBUSY;
130+
}
131+
#endif
105132

106133
rxq->mp_params = *p;
107-
ret = netdev_rx_queue_restart(dev, ifq_idx);
134+
ret = netdev_rx_queue_restart(dev, rxq_idx);
108135
if (ret) {
109136
rxq->mp_params.mp_ops = NULL;
110137
rxq->mp_params.mp_priv = NULL;
111138
}
112139
return ret;
113140
}
114141

115-
int net_mp_open_rxq(struct net_device *dev, unsigned ifq_idx,
142+
int net_mp_open_rxq(struct net_device *dev, unsigned int rxq_idx,
116143
struct pp_memory_provider_params *p)
117144
{
118145
int ret;
119146

120147
netdev_lock(dev);
121-
ret = __net_mp_open_rxq(dev, ifq_idx, p);
148+
ret = __net_mp_open_rxq(dev, rxq_idx, p, NULL);
122149
netdev_unlock(dev);
123150
return ret;
124151
}
125152

126-
static void __net_mp_close_rxq(struct net_device *dev, unsigned ifq_idx,
127-
struct pp_memory_provider_params *old_p)
153+
void __net_mp_close_rxq(struct net_device *dev, unsigned int ifq_idx,
154+
const struct pp_memory_provider_params *old_p)
128155
{
129156
struct netdev_rx_queue *rxq;
130157

0 commit comments

Comments
 (0)