Skip to content

Commit 97f51bf

Browse files
committed
net: ethtool: make dev->hwprov ops-protected
dev->hwprov tracks the active hwtstamp provider for the device. Make it ops protected (instance lock if the netdev driver opts into holding instance lock around callbacks, otherwise rtnl_lock). hwprov is written and read in: - drivers/net/phy/phy_device.c phydev and ops protection don't currently mix, add a comment - net/ethtool/ as of now holds both rtnl lock and ops lock, this one will soon only hold one lock or the other read in: - net/core/dev_ioctl.c holds both rtnl lock and ops lock - net/core/timestamping.c RCU reader The new netdev_ops_lock_dereference() helper does not have "compat" in the name. The name would be quite long and I think in this case it should be obvious that we need _a_ lock. netdev_lock_dereference() already exists and means dev->lock is always expected. Reviewed-by: Eric Dumazet <edumazet@google.com> Acked-by: Stanislav Fomichev <sdf@fomichev.me> Reviewed-by: Jacob Keller <jacob.e.keller@intel.com> Link: https://patch.msgid.link/20260605002912.3456868-4-kuba@kernel.org Signed-off-by: Jakub Kicinski <kuba@kernel.org>
1 parent ded86da commit 97f51bf

6 files changed

Lines changed: 28 additions & 7 deletions

File tree

drivers/net/phy/phy_device.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1935,6 +1935,9 @@ void phy_detach(struct phy_device *phydev)
19351935
if (dev) {
19361936
struct hwtstamp_provider *hwprov;
19371937

1938+
/* hwprov may technically be protected by ops lock but
1939+
* not for devices with a phydev, see phy_link_topo_add_phy()
1940+
*/
19381941
hwprov = rtnl_dereference(dev->hwprov);
19391942
/* Disable timestamp if it is the one selected */
19401943
if (hwprov && hwprov->phydev == phydev) {

drivers/net/phy/phy_link_topology.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ int phy_link_topo_add_phy(struct net_device *dev,
3838

3939
/* ethtool ops may run without rtnl_lock, and rtnl_lock is what
4040
* currently protects the PHY topology. No driver currently mixes
41-
* the two, flag if someone tries. See also ethnl_req_get_phydev().
41+
* the two, flag if someone tries. See also:
42+
* - ethnl_req_get_phydev()
43+
* - phy_detach()
4244
*/
4345
if (WARN_ON_ONCE(netdev_need_ops_lock(dev)))
4446
return -EOPNOTSUPP;

include/linux/netdevice.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2583,6 +2583,9 @@ struct net_device {
25832583
* Double protects:
25842584
* @up, @moving_ns, @nd_net, @xdp_features
25852585
*
2586+
* Ops protects:
2587+
* @hwprov
2588+
*
25862589
* Double ops protects:
25872590
* @real_num_rx_queues, @real_num_tx_queues
25882591
*

include/net/netdev_lock.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@ static inline void netdev_unlock_ops_compat(struct net_device *dev)
102102
rtnl_unlock();
103103
}
104104

105+
/* Matching "ops protected" category from netdevice.h */
106+
static inline int netdev_is_locked_ops_compat(const struct net_device *dev)
107+
{
108+
if (netdev_need_ops_lock(dev))
109+
return lockdep_is_held(&dev->lock);
110+
return lockdep_rtnl_is_held();
111+
}
112+
105113
static inline int netdev_lock_cmp_fn(const struct lockdep_map *a,
106114
const struct lockdep_map *b)
107115
{
@@ -138,6 +146,9 @@ static inline int netdev_lock_cmp_fn(const struct lockdep_map *a,
138146
#define netdev_lock_dereference(p, dev) \
139147
rcu_dereference_protected(p, lockdep_is_held(&(dev)->lock))
140148

149+
#define netdev_ops_lock_dereference(p, dev) \
150+
rcu_dereference_protected(p, netdev_is_locked_ops_compat(dev))
151+
141152
int netdev_debug_event(struct notifier_block *nb, unsigned long event,
142153
void *ptr);
143154

net/core/dev_ioctl.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ int dev_get_hwtstamp_phylib(struct net_device *dev,
260260
{
261261
struct hwtstamp_provider *hwprov;
262262

263-
hwprov = rtnl_dereference(dev->hwprov);
263+
hwprov = netdev_ops_lock_dereference(dev->hwprov, dev);
264264
if (hwprov) {
265265
cfg->qualifier = hwprov->desc.qualifier;
266266
if (hwprov->source == HWTSTAMP_SOURCE_PHYLIB &&
@@ -337,7 +337,7 @@ int dev_set_hwtstamp_phylib(struct net_device *dev,
337337
bool phy_ts;
338338
int err;
339339

340-
hwprov = rtnl_dereference(dev->hwprov);
340+
hwprov = netdev_ops_lock_dereference(dev->hwprov, dev);
341341
if (hwprov) {
342342
if (hwprov->source == HWTSTAMP_SOURCE_PHYLIB &&
343343
hwprov->phydev) {

net/ethtool/tsconfig.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <linux/net_tstamp.h>
44
#include <linux/ptp_clock_kernel.h>
5+
#include <net/netdev_lock.h>
56

67
#include "bitset.h"
78
#include "common.h"
@@ -57,7 +58,7 @@ static int tsconfig_prepare_data(const struct ethnl_req_info *req_base,
5758
data->hwtst_config.flags = cfg.flags;
5859

5960
data->hwprov_desc.index = -1;
60-
hwprov = rtnl_dereference(dev->hwprov);
61+
hwprov = netdev_ops_lock_dereference(dev->hwprov, dev);
6162
if (hwprov) {
6263
data->hwprov_desc.index = hwprov->desc.index;
6364
data->hwprov_desc.qualifier = hwprov->desc.qualifier;
@@ -213,7 +214,7 @@ static int tsconfig_send_reply(struct net_device *dev, struct genl_info *info)
213214
return -ENOMEM;
214215
}
215216

216-
ASSERT_RTNL();
217+
netdev_assert_locked_ops_compat(dev);
217218
reply_data->base.dev = dev;
218219
ret = tsconfig_prepare_data(&req_info->base, &reply_data->base, info);
219220
if (ret < 0)
@@ -316,7 +317,7 @@ static int ethnl_set_tsconfig(struct ethnl_req_info *req_base,
316317
struct hwtstamp_provider_desc __hwprov_desc = {.index = -1};
317318
struct hwtstamp_provider *__hwprov;
318319

319-
__hwprov = rtnl_dereference(dev->hwprov);
320+
__hwprov = netdev_ops_lock_dereference(dev->hwprov, dev);
320321
if (__hwprov) {
321322
__hwprov_desc.index = __hwprov->desc.index;
322323
__hwprov_desc.qualifier = __hwprov->desc.qualifier;
@@ -414,7 +415,8 @@ static int ethnl_set_tsconfig(struct ethnl_req_info *req_base,
414415
goto err_free_hwprov;
415416

416417
/* Change the selected hwtstamp source */
417-
__hwprov = rcu_replace_pointer_rtnl(dev->hwprov, hwprov);
418+
__hwprov = rcu_replace_pointer(dev->hwprov, hwprov,
419+
netdev_is_locked_ops_compat(dev));
418420
if (__hwprov)
419421
kfree_rcu(__hwprov, rcu_head);
420422
}

0 commit comments

Comments
 (0)