diff mbox series

[RFC,11/11] hw/virtio: Make vhost-vdpa.c target-agnostic to build it once

Message ID 20230523163600.83391-12-philmd@linaro.org (mailing list archive)
State New, archived
Headers show
Series hw/virtio: Build various target-agnostic objects just once | expand

Commit Message

Philippe Mathieu-Daudé May 23, 2023, 4:36 p.m. UTC
Replace TARGET_PAGE_MASK -> qemu_target_page_mask() and
TARGET_PAGE_ALIGN() -> qemu_target_page_align() so we don't
need the target-specific "cpu.h" header.

These macros are used in the MemoryListener add/del handlers
(vhost_vdpa_listener_skipped_section is only called by
vhost_vdpa_listener_region_add) which are not hot-path.

Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 hw/virtio/vhost-vdpa.c | 16 ++++++++--------
 hw/virtio/meson.build  |  3 ++-
 2 files changed, 10 insertions(+), 9 deletions(-)

Comments

Richard Henderson May 23, 2023, 11:43 p.m. UTC | #1
On 5/23/23 09:36, Philippe Mathieu-Daudé wrote:
> @@ -321,13 +321,13 @@ static void vhost_vdpa_listener_region_add(MemoryListener *listener,
>           return;
>       }
>   
> -    if (unlikely((section->offset_within_address_space & ~TARGET_PAGE_MASK) !=
> -                 (section->offset_within_region & ~TARGET_PAGE_MASK))) {
> +    if (unlikely((section->offset_within_address_space & ~qemu_target_page_mask()) !=
> +                 (section->offset_within_region & ~qemu_target_page_mask()))) {
>           error_report("%s received unaligned region", __func__);
>           return;
>       }
>   
> -    iova = TARGET_PAGE_ALIGN(section->offset_within_address_space);
> +    iova = qemu_target_page_align(section->offset_within_address_space);
>       llend = vhost_vdpa_section_end(section);
>       if (int128_ge(int128_make64(iova), llend)) {
>           return;

I'm not keen on using 3 function calls to get one constant.
This could be

     int page_size = qemu_target_page_size();
     int page_mask = page_size - 1;

     if (section->foo & page_mask) { ...

     iova = ROUND_UP(section->bar, page_size);

Also in vhost_vdpa_listener_region_del.

This then removes the only uses of qemu_target_page_align, so you don't need to add that 
either.


r~
Thomas Huth May 24, 2023, 7:34 a.m. UTC | #2
On 23/05/2023 18.36, Philippe Mathieu-Daudé wrote:
> Replace TARGET_PAGE_MASK -> qemu_target_page_mask() and
> TARGET_PAGE_ALIGN() -> qemu_target_page_align() so we don't
> need the target-specific "cpu.h" header.
> 
> These macros are used in the MemoryListener add/del handlers
> (vhost_vdpa_listener_skipped_section is only called by
> vhost_vdpa_listener_region_add) which are not hot-path.
> 
> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> ---
>   hw/virtio/vhost-vdpa.c | 16 ++++++++--------
>   hw/virtio/meson.build  |  3 ++-
>   2 files changed, 10 insertions(+), 9 deletions(-)

Reviewed-by: Thomas Huth <thuth@redhat.com>
diff mbox series

Patch

diff --git a/hw/virtio/vhost-vdpa.c b/hw/virtio/vhost-vdpa.c
index 3c575a9a6e..a51497aaf1 100644
--- a/hw/virtio/vhost-vdpa.c
+++ b/hw/virtio/vhost-vdpa.c
@@ -14,6 +14,7 @@ 
 #include <linux/vfio.h>
 #include <sys/eventfd.h>
 #include <sys/ioctl.h>
+#include "exec/target_page.h"
 #include "hw/virtio/vhost.h"
 #include "hw/virtio/vhost-backend.h"
 #include "hw/virtio/virtio-net.h"
@@ -23,7 +24,6 @@ 
 #include "migration/blocker.h"
 #include "qemu/cutils.h"
 #include "qemu/main-loop.h"
-#include "cpu.h"
 #include "trace.h"
 #include "qapi/error.h"
 
@@ -35,7 +35,7 @@  static Int128 vhost_vdpa_section_end(const MemoryRegionSection *section)
 {
     Int128 llend = int128_make64(section->offset_within_address_space);
     llend = int128_add(llend, section->size);
-    llend = int128_and(llend, int128_exts64(TARGET_PAGE_MASK));
+    llend = int128_and(llend, int128_exts64(qemu_target_page_mask()));
 
     return llend;
 }
@@ -321,13 +321,13 @@  static void vhost_vdpa_listener_region_add(MemoryListener *listener,
         return;
     }
 
-    if (unlikely((section->offset_within_address_space & ~TARGET_PAGE_MASK) !=
-                 (section->offset_within_region & ~TARGET_PAGE_MASK))) {
+    if (unlikely((section->offset_within_address_space & ~qemu_target_page_mask()) !=
+                 (section->offset_within_region & ~qemu_target_page_mask()))) {
         error_report("%s received unaligned region", __func__);
         return;
     }
 
-    iova = TARGET_PAGE_ALIGN(section->offset_within_address_space);
+    iova = qemu_target_page_align(section->offset_within_address_space);
     llend = vhost_vdpa_section_end(section);
     if (int128_ge(int128_make64(iova), llend)) {
         return;
@@ -403,13 +403,13 @@  static void vhost_vdpa_listener_region_del(MemoryListener *listener,
         vhost_vdpa_iommu_region_del(listener, section);
     }
 
-    if (unlikely((section->offset_within_address_space & ~TARGET_PAGE_MASK) !=
-                 (section->offset_within_region & ~TARGET_PAGE_MASK))) {
+    if (unlikely((section->offset_within_address_space & ~qemu_target_page_mask()) !=
+                 (section->offset_within_region & ~qemu_target_page_mask()))) {
         error_report("%s received unaligned region", __func__);
         return;
     }
 
-    iova = TARGET_PAGE_ALIGN(section->offset_within_address_space);
+    iova = qemu_target_page_align(section->offset_within_address_space);
     llend = vhost_vdpa_section_end(section);
 
     trace_vhost_vdpa_listener_region_del(v, iova,
diff --git a/hw/virtio/meson.build b/hw/virtio/meson.build
index 16e64e1cf1..c29be98ab0 100644
--- a/hw/virtio/meson.build
+++ b/hw/virtio/meson.build
@@ -18,7 +18,8 @@  if have_vhost
     specific_virtio_ss.add(files('vhost-user.c'))
   endif
   if have_vhost_vdpa
-    specific_virtio_ss.add(files('vhost-vdpa.c', 'vhost-shadow-virtqueue.c'))
+    softmmu_virtio_ss.add(files('vhost-vdpa.c'))
+    specific_virtio_ss.add(files('vhost-shadow-virtqueue.c'))
   endif
 else
   softmmu_virtio_ss.add(files('vhost-stub.c'))