Skip to contentSkip to conversation

xen/arm: gnttab: cast unused macro arguments to void

PATCH In review Michal Orzel 3 participants 6 messages Apr 28, 2022 – May 4, 2022

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 xen/arm: gnttab: cast unused macro arguments to void

Function unmap_common_complete (common/grant_table.c) defines and sets a variable ld that is later on passed to a macro:
gnttab_host_mapping_get_page_type().
On Arm this macro does not make use of any arguments causing a compiler to warn about unused-but-set variable (when -Wunused-but-set-variable is enabled). Fix it by casting the arguments to void in macro's body.

While there, take the opportunity to modify other macros in this file that do not make use of all the arguments to prevent similar issues in the future.

---
Changes since v1:
-standalone patch carved out from a series (other patches already merged) -v1 was ([3/8] gnttab: Remove unused-but-set variable) -modify macro on Arm instead of removing ld variable

---
 xen/arch/arm/include/asm/grant_table.h | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
xen/arch/arm/include/asm/grant_table.h +8 -5
index d31a4d6805..5bcd1ec528 100644
@@ -31,10 +31,11 @@ static inline void gnttab_mark_dirty(struct domain *d, mfn_t mfn)
 
 int create_grant_host_mapping(unsigned long gpaddr, mfn_t mfn,
                               unsigned int flags, unsigned int cache_flags);
-#define gnttab_host_mapping_get_page_type(ro, ld, rd) (0)
+#define gnttab_host_mapping_get_page_type(ro, ld, rd) \
+    ((void)(ro), (void)(ld), (void)(rd), 0)
 int replace_grant_host_mapping(unsigned long gpaddr, mfn_t mfn,
                                unsigned long new_gpaddr, unsigned int flags);
-#define gnttab_release_host_mappings(domain) 1
+#define gnttab_release_host_mappings(domain) ((void)(domain), 1)
 
 /*
  * The region used by Xen on the memory will never be mapped in DOM0
@@ -89,10 +90,12 @@ int replace_grant_host_mapping(unsigned long gpaddr, mfn_t mfn,
 })
 
 #define gnttab_shared_gfn(d, t, i)                                       \
-    (((i) >= nr_grant_frames(t)) ? INVALID_GFN : (t)->arch.shared_gfn[i])
+    ((void)(d),                                                          \
+     ((i) >= nr_grant_frames(t)) ? INVALID_GFN : (t)->arch.shared_gfn[i])
 
-#define gnttab_status_gfn(d, t, i)                                       \
-    (((i) >= nr_status_frames(t)) ? INVALID_GFN : (t)->arch.status_gfn[i])
+#define gnttab_status_gfn(d, t, i)                                        \
+    ((void)(d),                                                           \
+     ((i) >= nr_status_frames(t)) ? INVALID_GFN : (t)->arch.status_gfn[i])
 
 #define gnttab_need_iommu_mapping(d)                    \
     (is_domain_direct_mapped(d) && is_iommu_enabled(d))
Signature · 1 line
2.25.1
#1 lore raw

xen/arch/arm/include/asm/grant_table.h int replace_grant_host_mapping(unsigned long gpaddr, mfn_t mfn, · line 98

Quoted 13 lines · Michal Orzel
xen/arch/arm/include/asm/grant_table.h +5 -3
@@ -89,10 +90,12 @@ int replace_grant_host_mapping(unsigned long gpaddr, mfn_t mfn,
 })
 
 #define gnttab_shared_gfn(d, t, i)                                       \
-    (((i) >= nr_grant_frames(t)) ? INVALID_GFN : (t)->arch.shared_gfn[i])
+    ((void)(d),                                                          \
+     ((i) >= nr_grant_frames(t)) ? INVALID_GFN : (t)->arch.shared_gfn[i])
 
-#define gnttab_status_gfn(d, t, i)                                       \
-    (((i) >= nr_status_frames(t)) ? INVALID_GFN : (t)->arch.status_gfn[i])
+#define gnttab_status_gfn(d, t, i)                                        \
+    ((void)(d),                                                           \
+     ((i) >= nr_status_frames(t)) ? INVALID_GFN : (t)->arch.status_gfn[i])

Just as a note (I don't mind changing these too): If a macro cares to evaluate all its arguments, I think it should also care to evaluate all of them exactly once.

Jan

#2 lore raw
In reply to Michal Orzel

Hi,

Quoted 26 lines · Michal Orzel

Function unmap_common_complete (common/grant_table.c) defines and sets a variable ld that is later on passed to a macro:
gnttab_host_mapping_get_page_type().
On Arm this macro does not make use of any arguments causing a compiler to warn about unused-but-set variable (when -Wunused-but-set-variable is enabled). Fix it by casting the arguments to void in macro's body.

While there, take the opportunity to modify other macros in this file that do not make use of all the arguments to prevent similar issues in the future.

---
Changes since v1:

(quoted diff) +0 -4
-standalone patch carved out from a series (other patches already merged)
-v1 was ([3/8] gnttab: Remove unused-but-set variable)
-modify macro on Arm instead of removing ld variable
---
  xen/arch/arm/include/asm/grant_table.h | 13 ++++++++-----
  1 file changed, 8 insertions(+), 5 deletions(-)
xen/arch/arm/include/asm/grant_table.h +0 -0
index d31a4d6805..5bcd1ec528 100644

xen/arch/arm/include/asm/grant_table.h static inline void gnttab_mark_dirty(struct domain *d, mfn_t mfn) · line 35

Quoted 6 lines
xen/arch/arm/include/asm/grant_table.h +2 -1
@@ -31,10 +31,11 @@ static inline void gnttab_mark_dirty(struct domain *d, mfn_t mfn)
 
  int create_grant_host_mapping(unsigned long gpaddr, mfn_t mfn,
                                unsigned int flags, unsigned int cache_flags);
-#define gnttab_host_mapping_get_page_type(ro, ld, rd) (0)
+#define gnttab_host_mapping_get_page_type(ro, ld, rd) \
+    ((void)(ro), (void)(ld), (void)(rd), 0)

I would switch to a static inline helper:

static inline bool gnttab_host_mapping_get_page_type(bool ro, struct domain *ld,
struct domian *rd)

{
	return false;
}

Note the switch from 0 to false as the function is technically returning a boolean (see the x86 implementation).

xen/arch/arm/include/asm/grant_table.h static inline void gnttab_mark_dirty(struct domain *d, mfn_t mfn) · line 38

Quoted 4 lines
xen/arch/arm/include/asm/grant_table.h +1 -1
  int replace_grant_host_mapping(unsigned long gpaddr, mfn_t mfn,
                                 unsigned long new_gpaddr, unsigned int flags);
-#define gnttab_release_host_mappings(domain) 1
+#define gnttab_release_host_mappings(domain) ((void)(domain), 1)

Same here.

Quoted 6 lines
xen/arch/arm/include/asm/grant_table.h +0 -0
  /*
   * The region used by Xen on the memory will never be mapped in DOM0
@@ -89,10 +90,12 @@ int replace_grant_host_mapping(unsigned long gpaddr, mfn_t mfn,
  })
 

xen/arch/arm/include/asm/grant_table.h int replace_grant_host_mapping(unsigned long gpaddr, mfn_t mfn, · line 98

Quoted 10 lines
xen/arch/arm/include/asm/grant_table.h +5 -3
  #define gnttab_shared_gfn(d, t, i)                                       \
-    (((i) >= nr_grant_frames(t)) ? INVALID_GFN : (t)->arch.shared_gfn[i])
+    ((void)(d),                                                          \
+     ((i) >= nr_grant_frames(t)) ? INVALID_GFN : (t)->arch.shared_gfn[i])
 
-#define gnttab_status_gfn(d, t, i)                                       \
-    (((i) >= nr_status_frames(t)) ? INVALID_GFN : (t)->arch.status_gfn[i])
+#define gnttab_status_gfn(d, t, i)                                        \
+    ((void)(d),                                                           \
+     ((i) >= nr_status_frames(t)) ? INVALID_GFN : (t)->arch.status_gfn[i])

I share Jan's opinion here. If we want to evaluate d, then we should make sure t and i should be also evaluated once. However, IIRC, they can't be turned to static inline because the type of t (struct grant_table) is not fully defined yet.

Cheers

Signature · 1 line
Julien Grall
#3 lore raw

Hi Julien,

Quoted 44 lines · Julien Grall

Hi,

Quoted 26 lines · Michal Orzel

Function unmap_common_complete (common/grant_table.c) defines and sets a variable ld that is later on passed to a macro:
gnttab_host_mapping_get_page_type().
On Arm this macro does not make use of any arguments causing a compiler to warn about unused-but-set variable (when -Wunused-but-set-variable is enabled). Fix it by casting the arguments to void in macro's body.

While there, take the opportunity to modify other macros in this file that do not make use of all the arguments to prevent similar issues in the future.

---
Changes since v1:

(quoted diff) +0 -4
-standalone patch carved out from a series (other patches already merged)
-v1 was ([3/8] gnttab: Remove unused-but-set variable)
-modify macro on Arm instead of removing ld variable
---

  xen/arch/arm/include/asm/grant_table.h | 13 ++++++++-----

  1 file changed, 8 insertions(+), 5 deletions(-)
xen/arch/arm/include/asm/grant_table.h +0 -0
index d31a4d6805..5bcd1ec528 100644

xen/arch/arm/include/asm/grant_table.h static inline void gnttab_mark_dirty(struct domain *d, mfn_t mfn) · line 34

Quoted 5 lines
xen/arch/arm/include/asm/grant_table.h +2 -1
@@ -31,10 +31,11 @@ static inline void gnttab_mark_dirty(struct domain *d, mfn_t mfn)
     int create_grant_host_mapping(unsigned long gpaddr, mfn_t mfn,
                                 unsigned int flags, unsigned int cache_flags);
-#define gnttab_host_mapping_get_page_type(ro, ld, rd) (0)
+#define gnttab_host_mapping_get_page_type(ro, ld, rd) \
+    ((void)(ro), (void)(ld), (void)(rd), 0)

I would switch to a static inline helper:

static inline bool gnttab_host_mapping_get_page_type(bool ro, struct domain *ld, struct domian *rd)
{
    return false;
}

Quoted 9 lines

Note the switch from 0 to false as the function is technically returning a boolean (see the x86 implementation).

xen/arch/arm/include/asm/grant_table.h static inline void gnttab_mark_dirty(struct domain *d, mfn_t mfn) · line 38

Quoted 4 lines
xen/arch/arm/include/asm/grant_table.h +1 -1
   int replace_grant_host_mapping(unsigned long gpaddr, mfn_t mfn,
                                  unsigned long new_gpaddr, unsigned int flags);
-#define gnttab_release_host_mappings(domain) 1
+#define gnttab_release_host_mappings(domain) ((void)(domain), 1)

Same here.

Ok, sounds right.

Quoted 7 lines
Quoted 7 lines
xen/arch/arm/include/asm/grant_table.h +1 -1
     /*
    * The region used by Xen on the memory will never be mapped in DOM0
@@ -89,10 +90,12 @@ int replace_grant_host_mapping(unsigned long gpaddr, mfn_t mfn,
   })
     #define gnttab_shared_gfn(d, t, i)                                       \
-    (((i) >= nr_grant_frames(t)) ? INVALID_GFN : (t)->arch.shared_gfn[i])
+    ((void)(d),                                                          \
Quoted 9 lines

xen/arch/arm/include/asm/grant_table.h int replace_grant_host_mapping(unsigned long gpaddr, mfn_t mfn, · line 98

Quoted 6 lines
xen/arch/arm/include/asm/grant_table.h +4 -1
+     ((i) >= nr_grant_frames(t)) ? INVALID_GFN : (t)->arch.shared_gfn[i])
   -#define gnttab_status_gfn(d, t, i)                                       \
-    (((i) >= nr_status_frames(t)) ? INVALID_GFN : (t)->arch.status_gfn[i])
+#define gnttab_status_gfn(d, t, i)                                        \
+    ((void)(d),                                                           \
+     ((i) >= nr_status_frames(t)) ? INVALID_GFN : (t)->arch.status_gfn[i])

I share Jan's opinion here. If we want to evaluate d, then we should make sure t and i should be also evaluated once. However, IIRC, they can't be turned to static inline because the type of t (struct grant_table) is not fully defined yet.

Then, we could do like this:

#define gnttab_shared_gfn(d, t, i)                                       \
    ({                                                                   \
        const unsigned int _i = (i);                                     \
        const struct grant_table *_t = (t);                              \
        (void)(d);                                                       \
        (_i >= nr_grant_frames(_t)) ? INVALID_GFN                        \
                                    : _t->arch.shared_gfn[_i];           \
    })

However, if we start modifying the macros to evaluate args only once, shouldn't we also take care of the following macros in this file?:
gnttab_set_frame_gfn
gnttab_init_arch

I'm ok to do these changes but I'm afriad we are losing the origin of this patch as we are focusing on macros not related to the issue.

Quoted 2 lines

Cheers

Cheers,
Michal

#4 lore raw
Quoted 18 lines · Michal Orzel
Quoted 15 lines · Julien Grall

xen/arch/arm/include/asm/grant_table.h int replace_grant_host_mapping(unsigned long gpaddr, mfn_t mfn, · line 97

Quoted 11 lines · Michal Orzel
xen/arch/arm/include/asm/grant_table.h +5 -2
@@ -89,10 +90,12 @@ int replace_grant_host_mapping(unsigned long gpaddr, mfn_t mfn,
   })
     #define gnttab_shared_gfn(d, t, i)                                       \
-    (((i) >= nr_grant_frames(t)) ? INVALID_GFN : (t)->arch.shared_gfn[i])
+    ((void)(d),                                                          \
+     ((i) >= nr_grant_frames(t)) ? INVALID_GFN : (t)->arch.shared_gfn[i])
   -#define gnttab_status_gfn(d, t, i)                                       \
-    (((i) >= nr_status_frames(t)) ? INVALID_GFN : (t)->arch.status_gfn[i])
+#define gnttab_status_gfn(d, t, i)                                        \
+    ((void)(d),                                                           \
+     ((i) >= nr_status_frames(t)) ? INVALID_GFN : (t)->arch.status_gfn[i])

I share Jan's opinion here. If we want to evaluate d, then we should make sure t and i should be also evaluated once. However, IIRC, they can't be turned to static inline because the type of t (struct grant_table) is not fully defined yet.

Then, we could do like this:

Quoted 8 lines
#define gnttab_shared_gfn(d, t, i)                                       \
    ({                                                                   \
        const unsigned int _i = (i);                                     \
        const struct grant_table *_t = (t);                              \
        (void)(d);                                                       \
        (_i >= nr_grant_frames(_t)) ? INVALID_GFN                        \
                                    : _t->arch.shared_gfn[_i];           \
    })

Please avoid underscore-prefixed names here; we've started to use underscore-suffixed names in a few macros.

Additionally please consider using typeof() instead of spelling out types. This may help to avoid surprising behavior.

Finally, instead of merely casting d to void, please consider using it in e.g. ASSERT((d)->grant_table == t_), which ought to also take care of the unused variable warning. After all the explicit passing of t is only an (attempted) optimization here.

Quoted 5 lines

However, if we start modifying the macros to evaluate args only once, shouldn't we also take care of the following macros in this file?:
gnttab_set_frame_gfn
gnttab_init_arch

I'm ok to do these changes but I'm afriad we are losing the origin of this patch as we are focusing on macros not related to the issue.

Indeed - I'd leave further ones to a subsequent patch, or make conversion of all of the macros a prereq patch to the one you're after.

Jan

#5 lore raw
Quoted 43 lines · Jan Beulich
Quoted 18 lines · Michal Orzel
Quoted 15 lines · Julien Grall

xen/arch/arm/include/asm/grant_table.h int replace_grant_host_mapping(unsigned long gpaddr, mfn_t mfn, · line 97

Quoted 11 lines · Michal Orzel
xen/arch/arm/include/asm/grant_table.h +5 -2
@@ -89,10 +90,12 @@ int replace_grant_host_mapping(unsigned long gpaddr, mfn_t mfn,
   })
     #define gnttab_shared_gfn(d, t, i)                                       \
-    (((i) >= nr_grant_frames(t)) ? INVALID_GFN : (t)->arch.shared_gfn[i])
+    ((void)(d),                                                          \
+     ((i) >= nr_grant_frames(t)) ? INVALID_GFN : (t)->arch.shared_gfn[i])
   -#define gnttab_status_gfn(d, t, i)                                       \
-    (((i) >= nr_status_frames(t)) ? INVALID_GFN : (t)->arch.status_gfn[i])
+#define gnttab_status_gfn(d, t, i)                                        \
+    ((void)(d),                                                           \
+     ((i) >= nr_status_frames(t)) ? INVALID_GFN : (t)->arch.status_gfn[i])

I share Jan's opinion here. If we want to evaluate d, then we should make sure t and i should be also evaluated once. However, IIRC, they can't be turned to static inline because the type of t (struct grant_table) is not fully defined yet.

Then, we could do like this:

Quoted 8 lines
#define gnttab_shared_gfn(d, t, i)                                       \
    ({                                                                   \
        const unsigned int _i = (i);                                     \
        const struct grant_table *_t = (t);                              \
        (void)(d);                                                       \
        (_i >= nr_grant_frames(_t)) ? INVALID_GFN                        \
                                    : _t->arch.shared_gfn[_i];           \
    })

Please avoid underscore-prefixed names here; we've started to use underscore-suffixed names in a few macros.

Additionally please consider using typeof() instead of spelling out types. This may help to avoid surprising behavior.

Finally, instead of merely casting d to void, please consider using it in e.g. ASSERT((d)->grant_table == t_), which ought to also take care of the unused variable warning. After all the explicit passing of t is only an (attempted) optimization here.

Quoted 4 lines

However, if we start modifying the macros to evaluate args only once, shouldn't we also take care of the following macros in this file?:
gnttab_set_frame_gfn
gnttab_init_arch

Quoted 7 lines
Quoted 1 line

I'm ok to do these changes but I'm afriad we are losing the origin of this patch as we are focusing on macros not related to the issue.

Indeed - I'd leave further ones to a subsequent patch, or make conversion of all of the macros a prereq patch to the one you're after.

Jan

Ok, so I will drop this patch and push a new series containing of 2 patches:
1. xen/arm: gnttab: use static inlines for gnttab_{release_}host_mapping*
2. xen/arm: gnttab: modify macros to evaluate all arguments and only once

Cheers,
Michal