All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] vhost: use bvec_kmap_local in {get,put}u16_iotlb
@ 2022-02-22 15:48 ` Christoph Hellwig
  0 siblings, 0 replies; 4+ messages in thread
From: Christoph Hellwig @ 2022-02-22 15:48 UTC (permalink / raw)
  To: jasowang; +Cc: kvm, virtualization

Using local kmaps slightly reduces the chances to stray writes, and
the bvec interface cleans up the code a little bit.

Signed-off-by: Christoph Hellwig <hch@lst.de>
---
 drivers/vhost/vringh.c | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)

diff --git a/drivers/vhost/vringh.c b/drivers/vhost/vringh.c
index 14e2043d76852..0f22a83fd09af 100644
--- a/drivers/vhost/vringh.c
+++ b/drivers/vhost/vringh.c
@@ -1173,7 +1173,7 @@ static inline int getu16_iotlb(const struct vringh *vrh,
 			       u16 *val, const __virtio16 *p)
 {
 	struct bio_vec iov;
-	void *kaddr, *from;
+	void *kaddr;
 	int ret;
 
 	/* Atomic read is needed for getu16 */
@@ -1182,10 +1182,9 @@ static inline int getu16_iotlb(const struct vringh *vrh,
 	if (ret < 0)
 		return ret;
 
-	kaddr = kmap_atomic(iov.bv_page);
-	from = kaddr + iov.bv_offset;
-	*val = vringh16_to_cpu(vrh, READ_ONCE(*(__virtio16 *)from));
-	kunmap_atomic(kaddr);
+	kaddr = bvec_kmap_local(&iov);
+	*val = vringh16_to_cpu(vrh, READ_ONCE(*(__virtio16 *)kaddr));
+	kunmap_local(kaddr);
 
 	return 0;
 }
@@ -1194,7 +1193,7 @@ static inline int putu16_iotlb(const struct vringh *vrh,
 			       __virtio16 *p, u16 val)
 {
 	struct bio_vec iov;
-	void *kaddr, *to;
+	void *kaddr;
 	int ret;
 
 	/* Atomic write is needed for putu16 */
@@ -1203,10 +1202,9 @@ static inline int putu16_iotlb(const struct vringh *vrh,
 	if (ret < 0)
 		return ret;
 
-	kaddr = kmap_atomic(iov.bv_page);
-	to = kaddr + iov.bv_offset;
-	WRITE_ONCE(*(__virtio16 *)to, cpu_to_vringh16(vrh, val));
-	kunmap_atomic(kaddr);
+	kaddr = bvec_kmap_local(&iov);
+	WRITE_ONCE(*(__virtio16 *)kaddr, cpu_to_vringh16(vrh, val));
+	kunmap_local(kaddr);
 
 	return 0;
 }
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH] vhost: use bvec_kmap_local in {get,put}u16_iotlb
@ 2022-02-22 15:48 ` Christoph Hellwig
  0 siblings, 0 replies; 4+ messages in thread
From: Christoph Hellwig @ 2022-02-22 15:48 UTC (permalink / raw)
  To: jasowang; +Cc: kvm, virtualization

Using local kmaps slightly reduces the chances to stray writes, and
the bvec interface cleans up the code a little bit.

Signed-off-by: Christoph Hellwig <hch@lst.de>
---
 drivers/vhost/vringh.c | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)

diff --git a/drivers/vhost/vringh.c b/drivers/vhost/vringh.c
index 14e2043d76852..0f22a83fd09af 100644
--- a/drivers/vhost/vringh.c
+++ b/drivers/vhost/vringh.c
@@ -1173,7 +1173,7 @@ static inline int getu16_iotlb(const struct vringh *vrh,
 			       u16 *val, const __virtio16 *p)
 {
 	struct bio_vec iov;
-	void *kaddr, *from;
+	void *kaddr;
 	int ret;
 
 	/* Atomic read is needed for getu16 */
@@ -1182,10 +1182,9 @@ static inline int getu16_iotlb(const struct vringh *vrh,
 	if (ret < 0)
 		return ret;
 
-	kaddr = kmap_atomic(iov.bv_page);
-	from = kaddr + iov.bv_offset;
-	*val = vringh16_to_cpu(vrh, READ_ONCE(*(__virtio16 *)from));
-	kunmap_atomic(kaddr);
+	kaddr = bvec_kmap_local(&iov);
+	*val = vringh16_to_cpu(vrh, READ_ONCE(*(__virtio16 *)kaddr));
+	kunmap_local(kaddr);
 
 	return 0;
 }
@@ -1194,7 +1193,7 @@ static inline int putu16_iotlb(const struct vringh *vrh,
 			       __virtio16 *p, u16 val)
 {
 	struct bio_vec iov;
-	void *kaddr, *to;
+	void *kaddr;
 	int ret;
 
 	/* Atomic write is needed for putu16 */
@@ -1203,10 +1202,9 @@ static inline int putu16_iotlb(const struct vringh *vrh,
 	if (ret < 0)
 		return ret;
 
-	kaddr = kmap_atomic(iov.bv_page);
-	to = kaddr + iov.bv_offset;
-	WRITE_ONCE(*(__virtio16 *)to, cpu_to_vringh16(vrh, val));
-	kunmap_atomic(kaddr);
+	kaddr = bvec_kmap_local(&iov);
+	WRITE_ONCE(*(__virtio16 *)kaddr, cpu_to_vringh16(vrh, val));
+	kunmap_local(kaddr);
 
 	return 0;
 }
-- 
2.30.2

_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] vhost: use bvec_kmap_local in {get,put}u16_iotlb
  2022-02-22 15:48 ` Christoph Hellwig
@ 2022-02-23  2:18   ` Jason Wang
  -1 siblings, 0 replies; 4+ messages in thread
From: Jason Wang @ 2022-02-23  2:18 UTC (permalink / raw)
  To: Christoph Hellwig; +Cc: kvm, virtualization

On Tue, Feb 22, 2022 at 11:49 PM Christoph Hellwig <hch@lst.de> wrote:
>
> Using local kmaps slightly reduces the chances to stray writes, and
> the bvec interface cleans up the code a little bit.
>
> Signed-off-by: Christoph Hellwig <hch@lst.de>

Acked-by: Jason Wang <jasowang@redhat.com>

> ---
>  drivers/vhost/vringh.c | 18 ++++++++----------
>  1 file changed, 8 insertions(+), 10 deletions(-)
>
> diff --git a/drivers/vhost/vringh.c b/drivers/vhost/vringh.c
> index 14e2043d76852..0f22a83fd09af 100644
> --- a/drivers/vhost/vringh.c
> +++ b/drivers/vhost/vringh.c
> @@ -1173,7 +1173,7 @@ static inline int getu16_iotlb(const struct vringh *vrh,
>                                u16 *val, const __virtio16 *p)
>  {
>         struct bio_vec iov;
> -       void *kaddr, *from;
> +       void *kaddr;
>         int ret;
>
>         /* Atomic read is needed for getu16 */
> @@ -1182,10 +1182,9 @@ static inline int getu16_iotlb(const struct vringh *vrh,
>         if (ret < 0)
>                 return ret;
>
> -       kaddr = kmap_atomic(iov.bv_page);
> -       from = kaddr + iov.bv_offset;
> -       *val = vringh16_to_cpu(vrh, READ_ONCE(*(__virtio16 *)from));
> -       kunmap_atomic(kaddr);
> +       kaddr = bvec_kmap_local(&iov);
> +       *val = vringh16_to_cpu(vrh, READ_ONCE(*(__virtio16 *)kaddr));
> +       kunmap_local(kaddr);
>
>         return 0;
>  }
> @@ -1194,7 +1193,7 @@ static inline int putu16_iotlb(const struct vringh *vrh,
>                                __virtio16 *p, u16 val)
>  {
>         struct bio_vec iov;
> -       void *kaddr, *to;
> +       void *kaddr;
>         int ret;
>
>         /* Atomic write is needed for putu16 */
> @@ -1203,10 +1202,9 @@ static inline int putu16_iotlb(const struct vringh *vrh,
>         if (ret < 0)
>                 return ret;
>
> -       kaddr = kmap_atomic(iov.bv_page);
> -       to = kaddr + iov.bv_offset;
> -       WRITE_ONCE(*(__virtio16 *)to, cpu_to_vringh16(vrh, val));
> -       kunmap_atomic(kaddr);
> +       kaddr = bvec_kmap_local(&iov);
> +       WRITE_ONCE(*(__virtio16 *)kaddr, cpu_to_vringh16(vrh, val));
> +       kunmap_local(kaddr);
>
>         return 0;
>  }
> --
> 2.30.2
>


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] vhost: use bvec_kmap_local in {get,put}u16_iotlb
@ 2022-02-23  2:18   ` Jason Wang
  0 siblings, 0 replies; 4+ messages in thread
From: Jason Wang @ 2022-02-23  2:18 UTC (permalink / raw)
  To: Christoph Hellwig; +Cc: kvm, virtualization

On Tue, Feb 22, 2022 at 11:49 PM Christoph Hellwig <hch@lst.de> wrote:
>
> Using local kmaps slightly reduces the chances to stray writes, and
> the bvec interface cleans up the code a little bit.
>
> Signed-off-by: Christoph Hellwig <hch@lst.de>

Acked-by: Jason Wang <jasowang@redhat.com>

> ---
>  drivers/vhost/vringh.c | 18 ++++++++----------
>  1 file changed, 8 insertions(+), 10 deletions(-)
>
> diff --git a/drivers/vhost/vringh.c b/drivers/vhost/vringh.c
> index 14e2043d76852..0f22a83fd09af 100644
> --- a/drivers/vhost/vringh.c
> +++ b/drivers/vhost/vringh.c
> @@ -1173,7 +1173,7 @@ static inline int getu16_iotlb(const struct vringh *vrh,
>                                u16 *val, const __virtio16 *p)
>  {
>         struct bio_vec iov;
> -       void *kaddr, *from;
> +       void *kaddr;
>         int ret;
>
>         /* Atomic read is needed for getu16 */
> @@ -1182,10 +1182,9 @@ static inline int getu16_iotlb(const struct vringh *vrh,
>         if (ret < 0)
>                 return ret;
>
> -       kaddr = kmap_atomic(iov.bv_page);
> -       from = kaddr + iov.bv_offset;
> -       *val = vringh16_to_cpu(vrh, READ_ONCE(*(__virtio16 *)from));
> -       kunmap_atomic(kaddr);
> +       kaddr = bvec_kmap_local(&iov);
> +       *val = vringh16_to_cpu(vrh, READ_ONCE(*(__virtio16 *)kaddr));
> +       kunmap_local(kaddr);
>
>         return 0;
>  }
> @@ -1194,7 +1193,7 @@ static inline int putu16_iotlb(const struct vringh *vrh,
>                                __virtio16 *p, u16 val)
>  {
>         struct bio_vec iov;
> -       void *kaddr, *to;
> +       void *kaddr;
>         int ret;
>
>         /* Atomic write is needed for putu16 */
> @@ -1203,10 +1202,9 @@ static inline int putu16_iotlb(const struct vringh *vrh,
>         if (ret < 0)
>                 return ret;
>
> -       kaddr = kmap_atomic(iov.bv_page);
> -       to = kaddr + iov.bv_offset;
> -       WRITE_ONCE(*(__virtio16 *)to, cpu_to_vringh16(vrh, val));
> -       kunmap_atomic(kaddr);
> +       kaddr = bvec_kmap_local(&iov);
> +       WRITE_ONCE(*(__virtio16 *)kaddr, cpu_to_vringh16(vrh, val));
> +       kunmap_local(kaddr);
>
>         return 0;
>  }
> --
> 2.30.2
>

_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-02-23  2:18 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-22 15:48 [PATCH] vhost: use bvec_kmap_local in {get,put}u16_iotlb Christoph Hellwig
2022-02-22 15:48 ` Christoph Hellwig
2022-02-23  2:18 ` Jason Wang
2022-02-23  2:18   ` Jason Wang

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.