net: introduce napi_is_scheduled helper
authorChristian Marangi <ansuelsmth@gmail.com>
Wed, 18 Oct 2023 12:35:47 +0000 (14:35 +0200)
committerPaolo Abeni <pabeni@redhat.com>
Thu, 19 Oct 2023 13:41:31 +0000 (15:41 +0200)
We currently have napi_if_scheduled_mark_missed that can be used to
check if napi is scheduled but that does more thing than simply checking
it and return a bool. Some driver already implement custom function to
check if napi is scheduled.

Drop these custom function and introduce napi_is_scheduled that simply
check if napi is scheduled atomically.

Update any driver and code that implement a similar check and instead
use this new helper.

Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
Signed-off-by: Paolo Abeni <pabeni@redhat.com>
drivers/net/ethernet/chelsio/cxgb3/sge.c
drivers/net/wireless/realtek/rtw89/core.c
include/linux/netdevice.h
net/core/dev.c

index dfe4e01029609a5b4f60d520b79e36645353eae4..6268f96cb4aaed3fece7c515e48e1f0802e801ad 100644 (file)
@@ -2501,14 +2501,6 @@ static int napi_rx_handler(struct napi_struct *napi, int budget)
        return work_done;
 }
 
-/*
- * Returns true if the device is already scheduled for polling.
- */
-static inline int napi_is_scheduled(struct napi_struct *napi)
-{
-       return test_bit(NAPI_STATE_SCHED, &napi->state);
-}
-
 /**
  *     process_pure_responses - process pure responses from a response queue
  *     @adap: the adapter
index 4bfb4188de72334668f7314e925be639e1a806a3..3d75165e48bea883de72992bcaf89a3d5c89ccff 100644 (file)
@@ -2005,7 +2005,7 @@ static void rtw89_core_rx_to_mac80211(struct rtw89_dev *rtwdev,
        struct napi_struct *napi = &rtwdev->napi;
 
        /* In low power mode, napi isn't scheduled. Receive it to netif. */
-       if (unlikely(!test_bit(NAPI_STATE_SCHED, &napi->state)))
+       if (unlikely(!napi_is_scheduled(napi)))
                napi = NULL;
 
        rtw89_core_hw_to_sband_rate(rx_status);
index 1c7681263d30262a9147511a575d551d3c38469e..b8bf669212cce0152c02b12eb489433b0276b43e 100644 (file)
@@ -482,6 +482,29 @@ static inline bool napi_prefer_busy_poll(struct napi_struct *n)
        return test_bit(NAPI_STATE_PREFER_BUSY_POLL, &n->state);
 }
 
+/**
+ * napi_is_scheduled - test if NAPI is scheduled
+ * @n: NAPI context
+ *
+ * This check is "best-effort". With no locking implemented,
+ * a NAPI can be scheduled or terminate right after this check
+ * and produce not precise results.
+ *
+ * NAPI_STATE_SCHED is an internal state, napi_is_scheduled
+ * should not be used normally and napi_schedule should be
+ * used instead.
+ *
+ * Use only if the driver really needs to check if a NAPI
+ * is scheduled for example in the context of delayed timer
+ * that can be skipped if a NAPI is already scheduled.
+ *
+ * Return True if NAPI is scheduled, False otherwise.
+ */
+static inline bool napi_is_scheduled(struct napi_struct *n)
+{
+       return test_bit(NAPI_STATE_SCHED, &n->state);
+}
+
 bool napi_schedule_prep(struct napi_struct *n);
 
 /**
index 97e7b9833db9ffb003c142ed2b1c53a47e39f0e1..e7f61f5a53228eaf2a7a9b221264cfa49f425879 100644 (file)
@@ -6532,7 +6532,7 @@ static int __napi_poll(struct napi_struct *n, bool *repoll)
         * accidentally calling ->poll() when NAPI is not scheduled.
         */
        work = 0;
-       if (test_bit(NAPI_STATE_SCHED, &n->state)) {
+       if (napi_is_scheduled(n)) {
                work = n->poll(n, weight);
                trace_napi_poll(n, work, weight);
        }