On Mon, 31 Mar 2025, Jan Beulich wrote:
> On 28.03.2025 00:34, Stefano Stabellini wrote:
> > --- a/xen/common/bitmap.c
> > +++ b/xen/common/bitmap.c
> > @@ -52,7 +52,7 @@ static void clamp_last_byte(uint8_t *bp, unsigned int
> > nbits)
> > unsigned int remainder = nbits % 8;
> >
> > if (remainder)
> > - bp[nbits/8] &= (1U << remainder) - 1;
> > + *bp &= (1U << remainder) - 1;
> > }
>
> Technically there's nothing wrong with dropping the if(), I think. Even more
> so then than now, ...
>
> > @@ -338,7 +338,6 @@ static void bitmap_long_to_byte(uint8_t *bp, const
> > unsigned long *lp,
> > nbits -= 8;
> > }
> > }
> > - clamp_last_byte(bp, nbits);
> > }
> >
> > static void bitmap_byte_to_long(unsigned long *lp, const uint8_t *bp,
> > @@ -363,7 +362,6 @@ static void bitmap_long_to_byte(uint8_t *bp, const
> > unsigned long *lp,
> > unsigned int nbits)
> > {
> > memcpy(bp, lp, DIV_ROUND_UP(nbits, BITS_PER_BYTE));
> > - clamp_last_byte(bp, nbits);
> > }
>
> ... with the two prior call sites now shrunk to ...
>
> > @@ -384,21 +382,41 @@ int bitmap_to_xenctl_bitmap(struct xenctl_bitmap
> > *xenctl_bitmap,
> > uint8_t zero = 0;
> > int err = 0;
> > unsigned int xen_bytes = DIV_ROUND_UP(nbits, BITS_PER_BYTE);
> > - uint8_t *bytemap = xmalloc_array(uint8_t, xen_bytes);
> > -
> > - if ( !bytemap )
> > - return -ENOMEM;
> > + uint8_t last;
> >
> > 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);
> > + if ( IS_ENABLED(__BIG_ENDIAN) )
> > + {
> > + uint8_t *bytemap = xmalloc_array(uint8_t, xen_bytes);
> >
> > - if ( copy_bytes &&
> > - copy_to_guest(xenctl_bitmap->bitmap, bytemap, copy_bytes) )
> > - err = -EFAULT;
> > + if ( !bytemap )
> > + return -ENOMEM;
> >
> > - xfree(bytemap);
> > + bitmap_long_to_byte(bytemap, bitmap, nbits);
> > + last = bytemap[nbits / 8];
> > +
> > + if ( copy_bytes > 1 &&
> > + copy_to_guest(xenctl_bitmap->bitmap, bytemap, copy_bytes - 1)
> > )
> > + err = -EFAULT;
> > +
> > + xfree(bytemap);
> > + }
> > + else
> > + {
> > + const uint8_t *bytemap = (const uint8_t *)bitmap;
> > + last = bytemap[nbits / 8];
> > +
> > + if ( copy_bytes > 1 &&
> > + copy_to_guest(xenctl_bitmap->bitmap, bytemap, copy_bytes - 1)
> > )
> > + err = -EFAULT;
> > + }
> > +
> > + clamp_last_byte(&last, nbits);
>
> ... just one, I wonder if that being a separate function is actually still
> necessary.
>
> As indicated before, I think it would be nice if the two identical copy-out
> operations could also be folded.
Please see: https://marc.info/?l=xen-devel&m=174319650100975
Other than that, I addressed all the other points