[PATCH v7 2/4] can: rx-offload: move skb_queue and napi into struct can_rx_offload_queue

From: Ciprian Costea

Date: Fri Oct 02 2026 - 03:15:02 EST


From: Ciprian Marian Costea <ciprianmarian.costea@xxxxxxxxxxx>

Currently, struct can_rx_offload holds both the state filled by the
driver's IRQ handler (skb_irq_queue, mailbox range) and the state it
feeds (skb_queue, napi). To let more than one source of skbs feed the
same skb_queue and NAPI, the latter has to be separate from the
former.

Move skb_queue and napi into a new struct can_rx_offload_queue. For now
every struct can_rx_offload uses its own embedded instance, reached
through the queue pointer.

The NAPI poll function gets the netdev from napi->dev.

No functional change. The rx-offload API and its users are unchanged.

Suggested-by: Haibo Chen <haibo.chen@xxxxxxx>
Assisted-by: LLM
Signed-off-by: Ciprian Marian Costea <ciprianmarian.costea@xxxxxxxxxxx>
---
drivers/net/can/dev/rx-offload.c | 58 +++++++++++++++++---------------
include/linux/can/rx-offload.h | 14 +++++---
2 files changed, 41 insertions(+), 31 deletions(-)

diff --git a/drivers/net/can/dev/rx-offload.c b/drivers/net/can/dev/rx-offload.c
index 46e7b6db4a1e..92309ed47bac 100644
--- a/drivers/net/can/dev/rx-offload.c
+++ b/drivers/net/can/dev/rx-offload.c
@@ -41,16 +41,15 @@ can_rx_offload_inc(struct can_rx_offload *offload, unsigned int *val)

static int can_rx_offload_napi_poll(struct napi_struct *napi, int quota)
{
- struct can_rx_offload *offload = container_of(napi,
- struct can_rx_offload,
- napi);
- struct net_device *dev = offload->dev;
- struct net_device_stats *stats = &dev->stats;
+ struct can_rx_offload_queue *queue = container_of(napi,
+ struct can_rx_offload_queue,
+ napi);
+ struct net_device_stats *stats = &napi->dev->stats;
struct sk_buff *skb;
int work_done = 0;

while ((work_done < quota) &&
- (skb = skb_dequeue(&offload->skb_queue))) {
+ (skb = skb_dequeue(&queue->skb_queue))) {
struct can_frame *cf = (struct can_frame *)skb->data;

work_done++;
@@ -66,8 +65,8 @@ static int can_rx_offload_napi_poll(struct napi_struct *napi, int quota)
napi_complete_done(napi, work_done);

/* Check if there was another interrupt */
- if (!skb_queue_empty(&offload->skb_queue))
- napi_schedule(&offload->napi);
+ if (!skb_queue_empty(&queue->skb_queue))
+ napi_schedule(&queue->napi);
}

return work_done;
@@ -125,7 +124,7 @@ static int can_rx_offload_compare(struct sk_buff *a, struct sk_buff *b)
* from the device and return the mailbox's content as a struct
* sk_buff.
*
- * If the struct can_rx_offload::skb_queue exceeds the maximal queue
+ * If the struct can_rx_offload_queue::skb_queue exceeds the maximal queue
* length (struct can_rx_offload::skb_queue_len_max) or no skb can be
* allocated, the mailbox contents is discarded by reading it into an
* overflow buffer. This way the mailbox is marked as free by the
@@ -146,7 +145,7 @@ can_rx_offload_offload_one(struct can_rx_offload *offload, unsigned int n)
u32 timestamp;

/* If queue is full drop frame */
- if (unlikely(skb_queue_len(&offload->skb_queue) >
+ if (unlikely(skb_queue_len(&offload->queue->skb_queue) >
offload->skb_queue_len_max))
drop = true;

@@ -224,7 +223,7 @@ int can_rx_offload_queue_timestamp(struct can_rx_offload *offload,
{
struct can_rx_offload_cb *cb;

- if (skb_queue_len(&offload->skb_queue) >
+ if (skb_queue_len(&offload->queue->skb_queue) >
offload->skb_queue_len_max) {
dev_kfree_skb_any(skb);
return -ENOBUFS;
@@ -268,7 +267,7 @@ EXPORT_SYMBOL_GPL(can_rx_offload_get_echo_skb_queue_timestamp);
int can_rx_offload_queue_tail(struct can_rx_offload *offload,
struct sk_buff *skb)
{
- if (skb_queue_len(&offload->skb_queue) >
+ if (skb_queue_len(&offload->queue->skb_queue) >
offload->skb_queue_len_max) {
dev_kfree_skb_any(skb);
return -ENOBUFS;
@@ -307,44 +306,46 @@ EXPORT_SYMBOL_GPL(can_rx_offload_get_echo_skb_queue_tail);

void can_rx_offload_irq_finish(struct can_rx_offload *offload)
{
+ struct can_rx_offload_queue *queue = offload->queue;
unsigned long flags;
int queue_len;

if (skb_queue_empty_lockless(&offload->skb_irq_queue))
return;

- spin_lock_irqsave(&offload->skb_queue.lock, flags);
- skb_queue_splice_tail_init(&offload->skb_irq_queue, &offload->skb_queue);
- spin_unlock_irqrestore(&offload->skb_queue.lock, flags);
+ spin_lock_irqsave(&queue->skb_queue.lock, flags);
+ skb_queue_splice_tail_init(&offload->skb_irq_queue, &queue->skb_queue);
+ spin_unlock_irqrestore(&queue->skb_queue.lock, flags);

- queue_len = skb_queue_len(&offload->skb_queue);
+ queue_len = skb_queue_len(&queue->skb_queue);
if (queue_len > offload->skb_queue_len_max / 8)
netdev_dbg(offload->dev, "%s: queue_len=%d\n",
__func__, queue_len);

- napi_schedule(&offload->napi);
+ napi_schedule(&queue->napi);
}
EXPORT_SYMBOL_GPL(can_rx_offload_irq_finish);

void can_rx_offload_threaded_irq_finish(struct can_rx_offload *offload)
{
+ struct can_rx_offload_queue *queue = offload->queue;
unsigned long flags;
int queue_len;

if (skb_queue_empty_lockless(&offload->skb_irq_queue))
return;

- spin_lock_irqsave(&offload->skb_queue.lock, flags);
- skb_queue_splice_tail_init(&offload->skb_irq_queue, &offload->skb_queue);
- spin_unlock_irqrestore(&offload->skb_queue.lock, flags);
+ spin_lock_irqsave(&queue->skb_queue.lock, flags);
+ skb_queue_splice_tail_init(&offload->skb_irq_queue, &queue->skb_queue);
+ spin_unlock_irqrestore(&queue->skb_queue.lock, flags);

- queue_len = skb_queue_len(&offload->skb_queue);
+ queue_len = skb_queue_len(&queue->skb_queue);
if (queue_len > offload->skb_queue_len_max / 8)
netdev_dbg(offload->dev, "%s: queue_len=%d\n",
__func__, queue_len);

local_bh_disable();
- napi_schedule(&offload->napi);
+ napi_schedule(&queue->napi);
local_bh_enable();
}
EXPORT_SYMBOL_GPL(can_rx_offload_threaded_irq_finish);
@@ -353,15 +354,18 @@ static int can_rx_offload_init_queue(struct net_device *dev,
struct can_rx_offload *offload,
unsigned int weight)
{
+ struct can_rx_offload_queue *queue = &offload->own_queue;
+
offload->dev = dev;
+ offload->queue = queue;

/* Limit queue len to 4x the weight (rounded to next power of two) */
offload->skb_queue_len_max = 2 << fls(weight);
offload->skb_queue_len_max *= 4;
- skb_queue_head_init(&offload->skb_queue);
+ skb_queue_head_init(&queue->skb_queue);
__skb_queue_head_init(&offload->skb_irq_queue);

- netif_napi_add_weight(dev, &offload->napi, can_rx_offload_napi_poll,
+ netif_napi_add_weight(dev, &queue->napi, can_rx_offload_napi_poll,
weight);

dev_dbg(dev->dev.parent, "%s: skb_queue_len_max=%d\n",
@@ -414,14 +418,14 @@ EXPORT_SYMBOL_GPL(can_rx_offload_add_manual);

void can_rx_offload_enable(struct can_rx_offload *offload)
{
- napi_enable(&offload->napi);
+ napi_enable(&offload->queue->napi);
}
EXPORT_SYMBOL_GPL(can_rx_offload_enable);

void can_rx_offload_del(struct can_rx_offload *offload)
{
- netif_napi_del(&offload->napi);
- skb_queue_purge(&offload->skb_queue);
+ netif_napi_del(&offload->queue->napi);
+ skb_queue_purge(&offload->queue->skb_queue);
__skb_queue_purge(&offload->skb_irq_queue);
}
EXPORT_SYMBOL_GPL(can_rx_offload_del);
diff --git a/include/linux/can/rx-offload.h b/include/linux/can/rx-offload.h
index d29bb4521947..fafc00fc3700 100644
--- a/include/linux/can/rx-offload.h
+++ b/include/linux/can/rx-offload.h
@@ -12,6 +12,11 @@
#include <linux/netdevice.h>
#include <linux/can.h>

+struct can_rx_offload_queue {
+ struct sk_buff_head skb_queue;
+ struct napi_struct napi;
+};
+
struct can_rx_offload {
struct net_device *dev;

@@ -19,16 +24,17 @@ struct can_rx_offload {
unsigned int mb, u32 *timestamp,
bool drop);

- struct sk_buff_head skb_queue;
struct sk_buff_head skb_irq_queue;
u32 skb_queue_len_max;

unsigned int mb_first;
unsigned int mb_last;

- struct napi_struct napi;
-
bool inc;
+
+ /* Points to own_queue. */
+ struct can_rx_offload_queue *queue;
+ struct can_rx_offload_queue own_queue;
};

int can_rx_offload_add_timestamp(struct net_device *dev,
@@ -59,7 +65,7 @@ void can_rx_offload_enable(struct can_rx_offload *offload);

static inline void can_rx_offload_disable(struct can_rx_offload *offload)
{
- napi_disable(&offload->napi);
+ napi_disable(&offload->queue->napi);
}

#endif /* !_CAN_RX_OFFLOAD_H */
--
2.43.0