[PATCH] xen/bitmap: Don't open code DIV_ROUND_UP()

Jane Malalane posted 1 patch 2 years, 8 months ago
Test gitlab-ci failed
Patches applied successfully (tree, apply log)
git fetch https://gitlab.com/xen-project/patchew/xen tags/patchew/20210811152055.22962-1-jane.malalane@citrix.com
There is a newer version of this series
xen/common/bitmap.c | 27 +++++++++++++++------------
1 file changed, 15 insertions(+), 12 deletions(-)
[PATCH] xen/bitmap: Don't open code DIV_ROUND_UP()
Posted by Jane Malalane 2 years, 8 months ago
Also, change bitmap_long_to_byte() and bitmap_byte_to_long() to take
'unsigned int' instead of 'int' number of bits, to match the type of
their callers.

Suggested-by: Andrew Cooper <andrew.cooper3@citrix.com>
Signed-off-by: Jane Malalane <jane.malalane@citrix.com>
---
CC: Andrew Cooper <andrew.cooper3@citrix.com>
CC: George Dunlap <george.dunlap@citrix.com>
CC: Ian Jackson <iwj@xenproject.org>
CC: Jan Beulich <jbeulich@suse.com>
CC: Julien Grall <julien@xen.org>
CC: Stefano Stabellini <sstabellini@kernel.org>
CC: Wei Liu <wl@xen.org>
---
 xen/common/bitmap.c | 27 +++++++++++++++------------
 1 file changed, 15 insertions(+), 12 deletions(-)

diff --git a/xen/common/bitmap.c b/xen/common/bitmap.c
index bbc3554ae1..b78cb6029f 100644
--- a/xen/common/bitmap.c
+++ b/xen/common/bitmap.c
@@ -10,6 +10,7 @@
 #include <xen/bitmap.h>
 #include <xen/bitops.h>
 #include <xen/cpumask.h>
+#include <xen/lib.h>
 #include <xen/guest_access.h>
 #include <asm/byteorder.h>
 
@@ -338,7 +339,7 @@ EXPORT_SYMBOL(bitmap_allocate_region);
 
 #ifdef __BIG_ENDIAN
 
-static void bitmap_long_to_byte(uint8_t *bp, const unsigned long *lp, int nbits)
+static void bitmap_long_to_byte(uint8_t *bp, const unsigned long *lp, unsigned int nbits)
 {
 	unsigned long l;
 	int i, j, b;
@@ -354,7 +355,7 @@ static void bitmap_long_to_byte(uint8_t *bp, const unsigned long *lp, int nbits)
 	clamp_last_byte(bp, nbits);
 }
 
-static void bitmap_byte_to_long(unsigned long *lp, const uint8_t *bp, int nbits)
+static void bitmap_byte_to_long(unsigned long *lp, const uint8_t *bp, unsigned int nbits)
 {
 	unsigned long l;
 	int i, j, b;
@@ -371,18 +372,18 @@ static void bitmap_byte_to_long(unsigned long *lp, const uint8_t *bp, int nbits)
 
 #elif defined(__LITTLE_ENDIAN)
 
-static void bitmap_long_to_byte(uint8_t *bp, const unsigned long *lp, int nbits)
+static void bitmap_long_to_byte(uint8_t *bp, const unsigned long *lp, unsigned int nbits)
 {
-	memcpy(bp, lp, (nbits+7)/8);
+	memcpy(bp, lp, DIV_ROUND_UP(nbits, BITS_PER_BYTE));
 	clamp_last_byte(bp, nbits);
 }
 
-static void bitmap_byte_to_long(unsigned long *lp, const uint8_t *bp, int nbits)
+static void bitmap_byte_to_long(unsigned long *lp, const uint8_t *bp, unsigned int nbits)
 {
 	/* We may need to pad the final longword with zeroes. */
 	if (nbits & (BITS_PER_LONG-1))
 		lp[BITS_TO_LONGS(nbits)-1] = 0;
-	memcpy(lp, bp, (nbits+7)/8);
+	memcpy(lp, bp, DIV_ROUND_UP(nbits, BITS_PER_BYTE));
 }
 
 #endif
@@ -393,13 +394,14 @@ int bitmap_to_xenctl_bitmap(struct xenctl_bitmap *xenctl_bitmap,
     unsigned int guest_bytes, copy_bytes, i;
     uint8_t zero = 0;
     int err = 0;
-    uint8_t *bytemap = xmalloc_array(uint8_t, (nbits + 7) / 8);
+    unsigned int xen_bytes = DIV_ROUND_UP(nbits, BITS_PER_BYTE);
+    uint8_t *bytemap = xmalloc_array(uint8_t, xen_bytes);
 
     if ( !bytemap )
         return -ENOMEM;
 
-    guest_bytes = (xenctl_bitmap->nr_bits + 7) / 8;
-    copy_bytes  = min(guest_bytes, (nbits + 7) / 8);
+    guest_bytes = DIV_ROUND_UP(xenctl_bitmap->nr_bits, BITS_PER_BYTE);
+    copy_bytes  = min(guest_bytes, xen_bytes);
 
     bitmap_long_to_byte(bytemap, bitmap, nbits);
 
@@ -422,13 +424,14 @@ int xenctl_bitmap_to_bitmap(unsigned long *bitmap,
 {
     unsigned int guest_bytes, copy_bytes;
     int err = 0;
-    uint8_t *bytemap = xzalloc_array(uint8_t, (nbits + 7) / 8);
+    unsigned int xen_bytes = DIV_ROUND_UP(nbits, BITS_PER_BYTE);
+    uint8_t *bytemap = xzalloc_array(uint8_t, xen_bytes);
 
     if ( !bytemap )
         return -ENOMEM;
 
-    guest_bytes = (xenctl_bitmap->nr_bits + 7) / 8;
-    copy_bytes  = min(guest_bytes, (nbits + 7) / 8);
+    guest_bytes = DIV_ROUND_UP(xenctl_bitmap->nr_bits, BITS_PER_BYTE);
+    copy_bytes  = min(guest_bytes, xen_bytes);
 
     if ( copy_bytes )
     {
-- 
2.11.0


Re: [PATCH] xen/bitmap: Don't open code DIV_ROUND_UP()
Posted by Jan Beulich 2 years, 8 months ago
On 11.08.2021 17:20, Jane Malalane wrote:
> --- a/xen/common/bitmap.c
> +++ b/xen/common/bitmap.c
> @@ -10,6 +10,7 @@
>  #include <xen/bitmap.h>
>  #include <xen/bitops.h>
>  #include <xen/cpumask.h>
> +#include <xen/lib.h>
>  #include <xen/guest_access.h>
>  #include <asm/byteorder.h>

I'm curious how you've chosen this insertion point. While the first
two #include-s (just out of context) are still unsorted, the rest
currently matches our goal of alphabetical sorting (within the
sub-groups, i.e. xen/, asm/, and public/), unless there's an actual
reason to violate this pattern. Hence please don't violate this
partial sorting and move the insertion one line down.

> @@ -338,7 +339,7 @@ EXPORT_SYMBOL(bitmap_allocate_region);
>  
>  #ifdef __BIG_ENDIAN
>  
> -static void bitmap_long_to_byte(uint8_t *bp, const unsigned long *lp, int nbits)
> +static void bitmap_long_to_byte(uint8_t *bp, const unsigned long *lp, unsigned int nbits)

This now exceeds 80 chars, which is the limit per line. Please wrap
accordingly.

With these taken care of
Reviewed-by: Jan Beulich <jbeulich@suse.com>

Jan