net_devmem_lookup_dmabuf() has only one caller, net_devmem_get_binding(), which currently enters two separate RCU read-side critical sections: one inside net_devmem_lookup_dmabuf() to find the NET_DEVMEM_BINDING_LIVE entry and acquire a reference, and a second in net_devmem_get_binding() to verify dst_dev against binding->map_dev and binding->bind_dev. Because the lookup and device checks are split across two RCU read-side critical sections, waiting for synchronize_net() after clearing NET_DEVMEM_BINDING_LIVE does not guarantee that in-flight net_devmem_get_binding() callers have finished validating the binding and returned. Fold net_devmem_lookup_dmabuf() into net_devmem_get_binding() so the entire lookup, validation, device check, and net_devmem_dmabuf_binding_get() run inside a single rcu_read_lock() section. The binding stays alive for the duration of the RCU read-side critical section because net_devmem_unbind_dmabuf() calls synchronize_net() after clearing NET_DEVMEM_BINDING_LIVE and before percpu_ref_kill(). Deferring net_devmem_dmabuf_binding_get() until all checks pass also eliminates the error-path reference put. Signed-off-by: Mina Almasry --- net/core/devmem.c | 38 ++++++++++++-------------------------- net/core/devmem.h | 6 ------ 2 files changed, 12 insertions(+), 32 deletions(-) diff --git a/net/core/devmem.c b/net/core/devmem.c index 604718bed3cb2..6f85b90950f8b 100644 --- a/net/core/devmem.c +++ b/net/core/devmem.c @@ -211,7 +211,7 @@ void net_devmem_unbind_dmabuf(struct net_devmem_dmabuf_binding *binding) xa_clear_mark(&net_devmem_dmabuf_bindings, binding->id, NET_DEVMEM_BINDING_LIVE); - /* Ensure no tx net_devmem_lookup_dmabuf() are in flight after the + /* Ensure no tx net_devmem_get_binding() are in flight after the * mark is cleared. */ synchronize_net(); @@ -521,23 +521,6 @@ net_devmem_bind_dmabuf(struct net_device *map_dev, return ERR_PTR(err); } -struct net_devmem_dmabuf_binding *net_devmem_lookup_dmabuf(u32 id) -{ - struct net_devmem_dmabuf_binding *binding; - unsigned long xa_idx = id; - - rcu_read_lock(); - binding = xa_find(&net_devmem_dmabuf_bindings, &xa_idx, id, - NET_DEVMEM_BINDING_LIVE); - if (binding) { - if (!net_devmem_dmabuf_binding_get(binding)) - binding = NULL; - } - rcu_read_unlock(); - - return binding; -} - void net_devmem_net_iov_inc_ref(struct net_iov *niov) { net_devmem_dmabuf_binding_get(net_devmem_iov_binding(niov)); @@ -552,19 +535,21 @@ struct net_devmem_dmabuf_binding *net_devmem_get_binding(struct sock *sk, unsigned int dmabuf_id) { struct net_devmem_dmabuf_binding *binding; + unsigned long xa_idx = dmabuf_id; struct net_device *dst_dev; struct dst_entry *dst; - int err = 0; + int err; - binding = net_devmem_lookup_dmabuf(dmabuf_id); + rcu_read_lock(); + binding = xa_find(&net_devmem_dmabuf_bindings, &xa_idx, dmabuf_id, + NET_DEVMEM_BINDING_LIVE); if (!binding || !binding->tx_vec || !net_eq(sock_net(sk), read_pnet(&binding->net)) || !uid_eq(sk_uid(sk), binding->uid)) { err = -EINVAL; - goto out_err; + goto out_unlock; } - rcu_read_lock(); dst = __sk_dst_get(sk); /* If dst is NULL (route expired), attempt to rebuild it. */ if (unlikely(!dst)) { @@ -590,15 +575,16 @@ struct net_devmem_dmabuf_binding *net_devmem_get_binding(struct sock *sk, goto out_unlock; } + if (!net_devmem_dmabuf_binding_get(binding)) { + err = -EINVAL; + goto out_unlock; + } + rcu_read_unlock(); return binding; out_unlock: rcu_read_unlock(); -out_err: - if (binding) - net_devmem_dmabuf_binding_put(binding); - return ERR_PTR(err); } diff --git a/net/core/devmem.h b/net/core/devmem.h index 9dcb629e10bfd..5bfb1b21d19f5 100644 --- a/net/core/devmem.h +++ b/net/core/devmem.h @@ -98,7 +98,6 @@ net_devmem_bind_dmabuf(struct net_device *map_dev, struct netdev_nl_sock *priv, const struct sock *nl_sk, struct netlink_ext_ack *extack); -struct net_devmem_dmabuf_binding *net_devmem_lookup_dmabuf(u32 id); struct net_device * net_devmem_lock_dev(struct net_device **dev_ptr, netdevice_tracker *dev_tracker); @@ -184,11 +183,6 @@ net_devmem_bind_dmabuf(struct net_device *map_dev, return ERR_PTR(-EOPNOTSUPP); } -static inline struct net_devmem_dmabuf_binding *net_devmem_lookup_dmabuf(u32 id) -{ - return NULL; -} - static inline struct net_device * net_devmem_lock_dev(struct net_device **dev_ptr, netdevice_tracker *dev_tracker) -- 2.56.1.750.gf70dc682cf-goog