linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Himanshu Jha <himanshujha199640@gmail.com>
To: Sasha Levin <sashal@kernel.org>
Cc: Shayenne da Luz Moura <shayenneluzmoura@gmail.com>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Hans de Goede <hdegoede@redhat.com>,
	Michael Thayer <michael.thayer@oracle.com>,
	devel@driverdev.osuosl.org, linux-kernel@vger.kernel.org,
	outreachy-kernel@googlegroups.com
Subject: Re: [Outreachy kernel] [RESEND PATCH 2/2] staging: vboxvideo: Use unsigned int instead bool
Date: Sun, 28 Oct 2018 13:22:10 +0530	[thread overview]
Message-ID: <20181028075209.GA1938@himanshu-Vostro-3559> (raw)
In-Reply-To: <20181026204225.GH2015@sasha-vm>

Hi Sasha,

On Fri, Oct 26, 2018 at 04:42:25PM -0400, Sasha Levin wrote:
> On Fri, Oct 26, 2018 at 04:04:45PM -0300, Shayenne da Luz Moura wrote:
> > This change was suggested by checkpath.pl. Use unsigned int with bitfield
> > allocate only one bit to the boolean variable.
> > 
> > CHECK: Avoid using bool structure members because of possible alignment
> > issues
> > 
> > Signed-off-by: Shayenne da Luz Moura <shayenneluzmoura@gmail.com>
> > ---
> > drivers/staging/vboxvideo/vbox_drv.h        | 14 +++++++-------
> > drivers/staging/vboxvideo/vboxvideo_guest.h |  2 +-
> > 2 files changed, 8 insertions(+), 8 deletions(-)
> > 
> > diff --git a/drivers/staging/vboxvideo/vbox_drv.h b/drivers/staging/vboxvideo/vbox_drv.h
> > index 594f84272957..7d3e329a6b1c 100644
> > --- a/drivers/staging/vboxvideo/vbox_drv.h
> > +++ b/drivers/staging/vboxvideo/vbox_drv.h
> > @@ -81,7 +81,7 @@ struct vbox_private {
> > 	u8 __iomem *vbva_buffers;
> > 	struct gen_pool *guest_pool;
> > 	struct vbva_buf_ctx *vbva_info;
> > -	bool any_pitch;
> > +	unsigned int any_pitch:1;
> > 	u32 num_crtcs;
> > 	/** Amount of available VRAM, including space used for buffers. */
> > 	u32 full_vram_size;
> 
> Using bitfields for booleans in these cases is less efficient than just
> using "regular" booleans for two reasons:
> 
> 1. It will use the same amount of space. Due to alignment requirements,
> the compiler can't squeeze in anything into the 7 bits that are now
> "free". Each member, unless it's another bitfield, must start at a whole
> byte.

Agreed!

FYI original thread of discussion: https://lkml.org/lkml/2017/11/21/207

As Steve says:

"Thus, changing:

 int  a : 1;
 int  b : 1;
 int  c : 1;
 int  d : 1;

to

 bool a;
 bool b;
 bool c;
 bool d;

at best increases the size required from 1 byte to 4 bytes, and at
worse, it increases it from one byte to 16 bytes."

In the above cases, we have all bitfields members and no non-bitfields
members.

But before playing with these bitfields there are some points to be
noted:
https://port70.net/~nsz/c/c11/n1570.html#J.3.9

Implementation Defined
----------------------

* Whether a ''plain'' int bit-field is treated as a signed int 
  bit-field or as an unsigned int bit-field.

eg: `int foo:3` may have a range from 0..7 or -4..3

So, changing `int foo:3` to `unsigned int foo:3` might be a sane
change to remove the ambiguity and make sure range is 0..7.

Also, such an change can also handle unsigned overflow
or better said "wrapping".

* Whether a bit-field can straddle a storage-unit boundary.

So, you can't guess what could be the possible unless you're familiar
or tested the change for different arch.

* The alignment of non-bit-field members of structures.

...

The "possible alignement issues" in CHECK report is difficult to figure
out by just doing a glance analysis. :)

Linus also suggested to use bool as the base type i.e., `bool x:1` but
again sizeof(_Bool) is implementation defined ranging from 1-4 bytes.

And since this issue is added to checkpatch now, very likely there would
be blast of patches sent on the same.

Not everyone who sends checkpatch would be able to disassemble or test
on different implementations.

But if anyone is interested check this:
https://godbolt.org/


-- 
Himanshu Jha
Undergraduate Student
Department of Electronics & Communication
Guru Tegh Bahadur Institute of Technology

  parent reply	other threads:[~2018-10-28  7:52 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-10-26 19:03 [RESEND PATCH 0/2] staging: vboxvideo: Remove chekpatch issues Shayenne da Luz Moura
2018-10-26 19:04 ` [RESEND PATCH 1/2] staging: vboxvideo: Change uint32_t to u32 Shayenne da Luz Moura
2018-10-28 11:02   ` Hans de Goede
2018-10-26 19:04 ` [RESEND PATCH 2/2] staging: vboxvideo: Use unsigned int instead bool Shayenne da Luz Moura
2018-10-26 20:42   ` [Outreachy kernel] " Sasha Levin
2018-10-26 20:54     ` Julia Lawall
2018-10-27 21:59       ` Joe Perches
2018-10-28  5:41         ` Julia Lawall
2018-10-28  7:52     ` Himanshu Jha [this message]
2018-10-28  8:47       ` Julia Lawall
2018-10-28 11:20         ` Himanshu Jha
2018-10-28 11:46           ` Julia Lawall
2018-10-30 20:18             ` Shayenne Moura
2018-10-30 20:25               ` Julia Lawall
2018-10-30 20:33                 ` Shayenne Moura

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20181028075209.GA1938@himanshu-Vostro-3559 \
    --to=himanshujha199640@gmail.com \
    --cc=devel@driverdev.osuosl.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=hdegoede@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=michael.thayer@oracle.com \
    --cc=outreachy-kernel@googlegroups.com \
    --cc=sashal@kernel.org \
    --cc=shayenneluzmoura@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).