2015-11-30 09:15:28

by Michael S. Tsirkin

[permalink] [raw]
Subject: [PATCH v2] vhost: replace % with & on data path

We know vring num is a power of 2, so use &
to mask the high bits.

Signed-off-by: Michael S. Tsirkin <[email protected]>
---

Changes from v1: drop an unrelated chunk

drivers/vhost/vhost.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c
index 080422f..ad2146a 100644
--- a/drivers/vhost/vhost.c
+++ b/drivers/vhost/vhost.c
@@ -1369,7 +1369,7 @@ int vhost_get_vq_desc(struct vhost_virtqueue *vq,
/* Grab the next descriptor number they're advertising, and increment
* the index we've seen. */
if (unlikely(__get_user(ring_head,
- &vq->avail->ring[last_avail_idx % vq->num]))) {
+ &vq->avail->ring[last_avail_idx & (vq->num - 1)]))) {
vq_err(vq, "Failed to read head: idx %d address %p\n",
last_avail_idx,
&vq->avail->ring[last_avail_idx % vq->num]);
@@ -1489,7 +1489,7 @@ static int __vhost_add_used_n(struct vhost_virtqueue *vq,
u16 old, new;
int start;

- start = vq->last_used_idx % vq->num;
+ start = vq->last_used_idx & (vq->num - 1);
used = vq->used->ring + start;
if (count == 1) {
if (__put_user(heads[0].id, &used->id)) {
@@ -1531,7 +1531,7 @@ int vhost_add_used_n(struct vhost_virtqueue *vq, struct vring_used_elem *heads,
{
int start, n, r;

- start = vq->last_used_idx % vq->num;
+ start = vq->last_used_idx & (vq->num - 1);
n = vq->num - start;
if (n < count) {
r = __vhost_add_used_n(vq, heads, n);
--
MST


2015-11-30 09:21:58

by Christian Borntraeger

[permalink] [raw]
Subject: Re: [PATCH v2] vhost: replace % with & on data path

On 11/30/2015 10:15 AM, Michael S. Tsirkin wrote:
> We know vring num is a power of 2, so use &
> to mask the high bits.

Makes a lot of sense and virtio_ring.c seems to use the same logic.

Acked-by: Christian Borntraeger <[email protected]>

>
> Signed-off-by: Michael S. Tsirkin <[email protected]>
> ---
>
> Changes from v1: drop an unrelated chunk
>
> drivers/vhost/vhost.c | 6 +++---
> 1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c
> index 080422f..ad2146a 100644
> --- a/drivers/vhost/vhost.c
> +++ b/drivers/vhost/vhost.c
> @@ -1369,7 +1369,7 @@ int vhost_get_vq_desc(struct vhost_virtqueue *vq,
> /* Grab the next descriptor number they're advertising, and increment
> * the index we've seen. */
> if (unlikely(__get_user(ring_head,
> - &vq->avail->ring[last_avail_idx % vq->num]))) {
> + &vq->avail->ring[last_avail_idx & (vq->num - 1)]))) {
> vq_err(vq, "Failed to read head: idx %d address %p\n",
> last_avail_idx,
> &vq->avail->ring[last_avail_idx % vq->num]);
> @@ -1489,7 +1489,7 @@ static int __vhost_add_used_n(struct vhost_virtqueue *vq,
> u16 old, new;
> int start;
>
> - start = vq->last_used_idx % vq->num;
> + start = vq->last_used_idx & (vq->num - 1);
> used = vq->used->ring + start;
> if (count == 1) {
> if (__put_user(heads[0].id, &used->id)) {
> @@ -1531,7 +1531,7 @@ int vhost_add_used_n(struct vhost_virtqueue *vq, struct vring_used_elem *heads,
> {
> int start, n, r;
>
> - start = vq->last_used_idx % vq->num;
> + start = vq->last_used_idx & (vq->num - 1);
> n = vq->num - start;
> if (n < count) {
> r = __vhost_add_used_n(vq, heads, n);
>

2015-11-30 16:17:55

by David Miller

[permalink] [raw]
Subject: Re: [PATCH v2] vhost: replace % with & on data path

From: "Michael S. Tsirkin" <[email protected]>
Date: Mon, 30 Nov 2015 11:15:23 +0200

> We know vring num is a power of 2, so use &
> to mask the high bits.
>
> Signed-off-by: Michael S. Tsirkin <[email protected]>

Acked-by: David S. Miller <[email protected]>

2015-12-04 20:19:07

by Venkatesh Srinivas

[permalink] [raw]
Subject: Re: [PATCH v2] vhost: replace % with & on data path

On Mon, Nov 30, 2015 at 11:15:23AM +0200, Michael S. Tsirkin wrote:
> We know vring num is a power of 2, so use &
> to mask the high bits.
>
> Signed-off-by: Michael S. Tsirkin <[email protected]>
> ---

The generated code switches from DIV -> masking, source is clearer as well.

Tested-by: Venkatesh Srinivas <[email protected]>

-- vs;

2015-12-13 00:01:24

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v2] vhost: replace % with & on data path

On Fri, Dec 4, 2015 at 10:19 PM, Venkatesh Srinivas
<[email protected]> wrote:
> On Mon, Nov 30, 2015 at 11:15:23AM +0200, Michael S. Tsirkin wrote:
>> We know vring num is a power of 2, so use &
>> to mask the high bits.
>>
>> Signed-off-by: Michael S. Tsirkin <[email protected]>
>> ---
>
> The generated code switches from DIV -> masking, source is clearer as well.

First impression was why, now it seems that compiler can't predict
this for variables.
For constants it would be optimized to the same, I suppose.

>
> Tested-by: Venkatesh Srinivas <[email protected]>
>
> -- vs;
> --
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to [email protected]
> More majordomo info at http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at http://www.tux.org/lkml/



--
With Best Regards,
Andy Shevchenko