diff mbox series

virtio_net: reduce raw_smp_processor_id() calling in virtnet_xdp_get_sq

Message ID 1629966095-16341-1-git-send-email-lirongqing@baidu.com (mailing list archive)
State Changes Requested
Delegated to: Netdev Maintainers
Headers show
Series virtio_net: reduce raw_smp_processor_id() calling in virtnet_xdp_get_sq | expand

Checks

Context Check Description
netdev/cover_letter success Link
netdev/fixes_present success Link
netdev/patch_count success Link
netdev/tree_selection success Guessed tree name to be net-next
netdev/subject_prefix warning Target tree name not specified in the subject
netdev/cc_maintainers warning 13 maintainers not CCed: andrii@kernel.org kafai@fb.com yhs@fb.com davem@davemloft.net daniel@iogearbox.net songliubraving@fb.com hawk@kernel.org kuba@kernel.org mst@redhat.com kpsingh@kernel.org john.fastabend@gmail.com ast@kernel.org jasowang@redhat.com
netdev/source_inline success Was 0 now: 0
netdev/verify_signedoff success Link
netdev/module_param success Was 0 now: 0
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/verify_fixes success Link
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 23 lines checked
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/header_inline success Link

Commit Message

Li RongQing Aug. 26, 2021, 8:21 a.m. UTC
smp_processor_id()/raw* will be called once each when not
more queues in virtnet_xdp_get_sq() which is called in
non-preemptible context, so it's safe to call the function
smp_processor_id() once.

Signed-off-by: Li RongQing <lirongqing@baidu.com>
---
 drivers/net/virtio_net.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

Comments

Michael S. Tsirkin Aug. 30, 2021, 9:10 p.m. UTC | #1
On Thu, Aug 26, 2021 at 04:21:35PM +0800, Li RongQing wrote:
> smp_processor_id()/raw* will be called once each when not
> more queues in virtnet_xdp_get_sq() which is called in
> non-preemptible context, so it's safe to call the function
> smp_processor_id() once.
> 
> Signed-off-by: Li RongQing <lirongqing@baidu.com>

commit log should probably explain why it's a good idea
to replace raw_smp_processor_id with smp_processor_id
in the case of curr_queue_pairs <= nr_cpu_ids.

> ---
>  drivers/net/virtio_net.c | 7 ++++---
>  1 file changed, 4 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
> index 2e42210a6503..2a7b368c1da2 100644
> --- a/drivers/net/virtio_net.c
> +++ b/drivers/net/virtio_net.c
> @@ -528,19 +528,20 @@ static int __virtnet_xdp_xmit_one(struct virtnet_info *vi,
>   * functions to perfectly solve these three problems at the same time.
>   */
>  #define virtnet_xdp_get_sq(vi) ({                                       \
> +	int cpu = smp_processor_id();                                   \
>  	struct netdev_queue *txq;                                       \
>  	typeof(vi) v = (vi);                                            \
>  	unsigned int qp;                                                \
>  									\
>  	if (v->curr_queue_pairs > nr_cpu_ids) {                         \
>  		qp = v->curr_queue_pairs - v->xdp_queue_pairs;          \
> -		qp += smp_processor_id();                               \
> +		qp += cpu;                                              \
>  		txq = netdev_get_tx_queue(v->dev, qp);                  \
>  		__netif_tx_acquire(txq);                                \
>  	} else {                                                        \
> -		qp = smp_processor_id() % v->curr_queue_pairs;          \
> +		qp = cpu % v->curr_queue_pairs;                         \
>  		txq = netdev_get_tx_queue(v->dev, qp);                  \
> -		__netif_tx_lock(txq, raw_smp_processor_id());           \
> +		__netif_tx_lock(txq, cpu);                              \
>  	}                                                               \
>  	v->sq + qp;                                                     \
>  })
> -- 
> 2.33.0.69.gc420321.dirty
> 
>
Li RongQing Aug. 31, 2021, 2:09 a.m. UTC | #2
> -----邮件原件-----
> 发件人: Michael S. Tsirkin <mst@redhat.com>
> 发送时间: 2021年8月31日 5:10
> 收件人: Li,Rongqing <lirongqing@baidu.com>
> 抄送: netdev@vger.kernel.org; bpf@vger.kernel.org;
> virtualization@lists.linux-foundation.org
> 主题: Re: [PATCH] virtio_net: reduce raw_smp_processor_id() calling in
> virtnet_xdp_get_sq
> 
> On Thu, Aug 26, 2021 at 04:21:35PM +0800, Li RongQing wrote:
> > smp_processor_id()/raw* will be called once each when not more queues
> > in virtnet_xdp_get_sq() which is called in non-preemptible context, so
> > it's safe to call the function
> > smp_processor_id() once.
> >
> > Signed-off-by: Li RongQing <lirongqing@baidu.com>
> 
> commit log should probably explain why it's a good idea to replace
> raw_smp_processor_id with smp_processor_id in the case of curr_queue_pairs
> <= nr_cpu_ids.
> 


I change it as below, is it ok?

    virtio_net: reduce raw_smp_processor_id() calling in virtnet_xdp_get_sq

    smp_processor_id() and raw_smp_processor_id() are called once
    each in virtnet_xdp_get_sq(), when curr_queue_pairs <= nr_cpu_ids,
    should be merged

    virtnet_xdp_get_sq() is called in non-preemptible context, so
    it's safe to call the function smp_processor_id(), and keep
    smp_processor_id(), and remove the calling of raw_smp_processor_id(),
    avoid the wrong use virtnet_xdp_get_sq to preemptible context
    in the future

-Li
Michael S. Tsirkin Aug. 31, 2021, 2:33 p.m. UTC | #3
On Tue, Aug 31, 2021 at 02:09:36AM +0000, Li,Rongqing wrote:
> > -----邮件原件-----
> > 发件人: Michael S. Tsirkin <mst@redhat.com>
> > 发送时间: 2021年8月31日 5:10
> > 收件人: Li,Rongqing <lirongqing@baidu.com>
> > 抄送: netdev@vger.kernel.org; bpf@vger.kernel.org;
> > virtualization@lists.linux-foundation.org
> > 主题: Re: [PATCH] virtio_net: reduce raw_smp_processor_id() calling in
> > virtnet_xdp_get_sq
> > 
> > On Thu, Aug 26, 2021 at 04:21:35PM +0800, Li RongQing wrote:
> > > smp_processor_id()/raw* will be called once each when not more queues
> > > in virtnet_xdp_get_sq() which is called in non-preemptible context, so
> > > it's safe to call the function
> > > smp_processor_id() once.
> > >
> > > Signed-off-by: Li RongQing <lirongqing@baidu.com>
> > 
> > commit log should probably explain why it's a good idea to replace
> > raw_smp_processor_id with smp_processor_id in the case of curr_queue_pairs
> > <= nr_cpu_ids.
> > 
> 
> 
> I change it as below, is it ok?
> 
>     virtio_net: reduce raw_smp_processor_id() calling in virtnet_xdp_get_sq

shorter:

virtio_net: s/raw_smp_processor_id/smp_processor_id/ in virtnet_xdp_get_sq


> 
>     smp_processor_id() and raw_smp_processor_id() are called once
>     each in virtnet_xdp_get_sq(), when curr_queue_pairs <= nr_cpu_ids,
>     should be merged

I'd just drop this part.

> 
>     virtnet_xdp_get_sq() is called in non-preemptible context, so
>     it's safe to call the function smp_processor_id(), and keep
>     smp_processor_id(), and remove the calling of raw_smp_processor_id(),
>     avoid the wrong use virtnet_xdp_get_sq to preemptible context
>     in the future

s/avoid.*/this way we'll get a warning if this is ever called in a preemptible context/


> 
> -Li
Li RongQing Sept. 3, 2021, 4 a.m. UTC | #4
> -----邮件原件-----
> 发件人: Michael S. Tsirkin <mst@redhat.com>
> 发送时间: 2021年8月31日 22:34
> 收件人: Li,Rongqing <lirongqing@baidu.com>
> 抄送: netdev@vger.kernel.org; bpf@vger.kernel.org;
> virtualization@lists.linux-foundation.org
> 主题: Re: 答复: [PATCH] virtio_net: reduce raw_smp_processor_id() calling in
> virtnet_xdp_get_sq
> 
> On Tue, Aug 31, 2021 at 02:09:36AM +0000, Li,Rongqing wrote:
> > > -----邮件原件-----
> > > 发件人: Michael S. Tsirkin <mst@redhat.com>
> > > 发送时间: 2021年8月31日 5:10
> > > 收件人: Li,Rongqing <lirongqing@baidu.com>
> > > 抄送: netdev@vger.kernel.org; bpf@vger.kernel.org;
> > > virtualization@lists.linux-foundation.org
> > > 主题: Re: [PATCH] virtio_net: reduce raw_smp_processor_id() calling in
> > > virtnet_xdp_get_sq
> > >
> > > On Thu, Aug 26, 2021 at 04:21:35PM +0800, Li RongQing wrote:
> > > > smp_processor_id()/raw* will be called once each when not more
> > > > queues in virtnet_xdp_get_sq() which is called in non-preemptible
> > > > context, so it's safe to call the function
> > > > smp_processor_id() once.
> > > >
> > > > Signed-off-by: Li RongQing <lirongqing@baidu.com>
> > >
> > > commit log should probably explain why it's a good idea to replace
> > > raw_smp_processor_id with smp_processor_id in the case of
> > > curr_queue_pairs <= nr_cpu_ids.
> > >
> >
> >
> > I change it as below, is it ok?
> >
> >     virtio_net: reduce raw_smp_processor_id() calling in
> > virtnet_xdp_get_sq
> 
> shorter:
> 
> virtio_net: s/raw_smp_processor_id/smp_processor_id/ in virtnet_xdp_get_sq
> 
> 
> >
> >     smp_processor_id() and raw_smp_processor_id() are called once
> >     each in virtnet_xdp_get_sq(), when curr_queue_pairs <= nr_cpu_ids,
> >     should be merged
> 
> I'd just drop this part.
> 
> >
> >     virtnet_xdp_get_sq() is called in non-preemptible context, so
> >     it's safe to call the function smp_processor_id(), and keep
> >     smp_processor_id(), and remove the calling of raw_smp_processor_id(),
> >     avoid the wrong use virtnet_xdp_get_sq to preemptible context
> >     in the future
> 
> s/avoid.*/this way we'll get a warning if this is ever called in a preemptible
> context/
> 


Thanks, should I resend it

-Li


> 
> >
> > -Li
diff mbox series

Patch

diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
index 2e42210a6503..2a7b368c1da2 100644
--- a/drivers/net/virtio_net.c
+++ b/drivers/net/virtio_net.c
@@ -528,19 +528,20 @@  static int __virtnet_xdp_xmit_one(struct virtnet_info *vi,
  * functions to perfectly solve these three problems at the same time.
  */
 #define virtnet_xdp_get_sq(vi) ({                                       \
+	int cpu = smp_processor_id();                                   \
 	struct netdev_queue *txq;                                       \
 	typeof(vi) v = (vi);                                            \
 	unsigned int qp;                                                \
 									\
 	if (v->curr_queue_pairs > nr_cpu_ids) {                         \
 		qp = v->curr_queue_pairs - v->xdp_queue_pairs;          \
-		qp += smp_processor_id();                               \
+		qp += cpu;                                              \
 		txq = netdev_get_tx_queue(v->dev, qp);                  \
 		__netif_tx_acquire(txq);                                \
 	} else {                                                        \
-		qp = smp_processor_id() % v->curr_queue_pairs;          \
+		qp = cpu % v->curr_queue_pairs;                         \
 		txq = netdev_get_tx_queue(v->dev, qp);                  \
-		__netif_tx_lock(txq, raw_smp_processor_id());           \
+		__netif_tx_lock(txq, cpu);                              \
 	}                                                               \
 	v->sq + qp;                                                     \
 })