net: add netdev_lock() / netdev_unlock() helpers

Add helpers for locking the netdev instance, use it in drivers
and the shaper code. This will make grepping for the lock usage
much easier, as we extend the lock to cover more fields.

Reviewed-by: Joe Damato <jdamato@fastly.com>
Reviewed-by: Eric Dumazet <edumazet@google.com>
Reviewed-by: Kuniyuki Iwashima <kuniyu@amazon.com>
Reviewed-by: Przemek Kitszel <przemyslaw.kitszel@intel.com>
Link: https://patch.msgid.link/20250115035319.559603-2-kuba@kernel.org
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
This commit is contained in:
Jakub Kicinski
2025-01-14 19:53:09 -08:00
parent 0b6f6593aa
commit ebda2f0bbd
4 changed files with 63 additions and 44 deletions

View File

@@ -2444,8 +2444,12 @@ struct net_device {
u32 napi_defer_hard_irqs;
/**
* @lock: protects @net_shaper_hierarchy, feel free to use for other
* netdev-scope protection. Ordering: take after rtnl_lock.
* @lock: netdev-scope lock, protects a small selection of fields.
* Should always be taken using netdev_lock() / netdev_unlock() helpers.
* Drivers are free to use it for other protection.
*
* Protects: @net_shaper_hierarchy.
* Ordering: take after rtnl_lock.
*/
struct mutex lock;
@@ -2671,6 +2675,21 @@ void netif_queue_set_napi(struct net_device *dev, unsigned int queue_index,
enum netdev_queue_type type,
struct napi_struct *napi);
static inline void netdev_lock(struct net_device *dev)
{
mutex_lock(&dev->lock);
}
static inline void netdev_unlock(struct net_device *dev)
{
mutex_unlock(&dev->lock);
}
static inline void netdev_assert_locked(struct net_device *dev)
{
lockdep_assert_held(&dev->lock);
}
static inline void netif_napi_set_irq(struct napi_struct *napi, int irq)
{
napi->irq = irq;