Re: [PATCH 1/2] net: Assert proper context while calling napi_schedule()

Breno Leitao
2025-02-17T16:46:57Z

On Fri, Feb 14, 2025 at 02:10:11PM -0800, Jakub Kicinski wrote:
> On Fri, 14 Feb 2025 08:43:28 -0800 Breno Leitao wrote:
> > diff --git a/drivers/net/netdevsim/netdev.c b/drivers/net/netdevsim/netdev.c
> > index 42f247cbdceec..cd56904a39049 100644
> > --- a/drivers/net/netdevsim/netdev.c
> > +++ b/drivers/net/netdevsim/netdev.c
> > @@ -87,7 +87,7 @@ static netdev_tx_t nsim_start_xmit(struct sk_buff *skb, struct net_device *dev)
> > if (unlikely(nsim_forward_skb(peer_dev, skb, rq) == NET_RX_DROP))
> > goto out_drop_cnt;
> >
> > - napi_schedule(&rq->napi);
> > + hrtimer_start(&rq->napi_timer, ns_to_ktime(5), HRTIMER_MODE_REL);
>
> ns -> us
>
> We want to leave the timer be in case it's already scheduled.
> Otherwise we'll keep postponing forever under load.
> Double check that hrtime_start() does not reset the time if already
> pending. Maybe hrtimer_start_range_ns(..., 0, us_to_ktime(5), ...)
> would work?

Reading the code, I got the impression that
hrtimer_start_range_ns(..., 0, us_to_ktime(5), ...) will reprogram the
timer anyway.

hrtimer_start_range_ns() {
__hrtimer_start_range_ns() {
remove_hrtimer(timer, base, true, force_local);
enqueue_hrtimer(timer, new_base, mode);
...
}
}

I think a better solution is to do something as:

if (!hrtimer_active(&rq->napi_timer))
hrtimer_start(&rq->napi_timer, us_to_ktime(5), HRTIMER_MODE_REL);