Skip to contentSkip to conversation

staging: vboxvideo: Remove chekpatch issues

PATCH RESEND In review Shayenne da Luz Moura 6 participants 15 messages Oct 26, 2018 – Oct 30, 2018 Staging

lore
Share & tools
lore
#0 lore raw

cover staging: vboxvideo: Remove chekpatch issues

This series cleans the following checkpatch.pl issues:

CHECK: Prefer kernel type 'u32' over 'uint32_t'
CHECK: Avoid using bool structure members because of possible alignment issues

Shayenne da Luz Moura (2):
staging: vboxvideo: Change uint32_t to u32
staging: vboxvideo: Use unsigned int instead bool

 drivers/staging/vboxvideo/vbox_drv.h        | 14 +++++++-------
 drivers/staging/vboxvideo/vbox_mode.c       |  2 +-
 drivers/staging/vboxvideo/vboxvideo_guest.h |  2 +-
 3 files changed, 9 insertions(+), 9 deletions(-)
Signature · 1 line
2.19.1
#1 lore raw

PATCH RESEND 1/2 staging: vboxvideo: Change uint32_t to u32

This change was suggested by checkpath.pl.

CHECK: Prefer kernel type 'u32' over 'uint32_t'

---
 drivers/staging/vboxvideo/vbox_mode.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/vboxvideo/vbox_mode.c +1 -1
index 79836c8fb909..8a1b117990b8 100644
@@ -311,7 +311,7 @@ static int vbox_crtc_mode_set(struct drm_crtc *crtc,
 static int vbox_crtc_page_flip(struct drm_crtc *crtc,
 			       struct drm_framebuffer *fb,
 			       struct drm_pending_vblank_event *event,
-			       uint32_t page_flip_flags,
+			       u32 page_flip_flags,
 			       struct drm_modeset_acquire_ctx *ctx)
 {
 	struct vbox_private *vbox = crtc->dev->dev_private;
Signature · 1 line
2.19.1
#2 lore raw

Re: [RESEND PATCH 1/2] staging: vboxvideo: Change uint32_t to u32

Hi,

Quoted 5 lines · Shayenne da Luz Moura

This change was suggested by checkpath.pl.

CHECK: Prefer kernel type 'u32' over 'uint32_t'

So as already mentioned in response to the coverletter of the first posting of this series.

The drm headers use uint32_t in the prototype definition of the callback functions we are defining, so the vboxvideo code should use the same even if the compiler does not warn about the callback implementation having different parameter types in this case.

To be precise, the page_flip member of struct drm_crtc_funcs is defined as:

int (*page_flip)(struct drm_crtc *crtc,
struct drm_framebuffer *fb, struct drm_pending_vblank_event *event, uint32_t flags,
struct drm_modeset_acquire_ctx *ctx);

So our implementation before your patch exactly matches the prototype which IMHO is the right thing to do.

Regards,

Hans

Quoted 18 lines
---
  drivers/staging/vboxvideo/vbox_mode.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/vboxvideo/vbox_mode.c +1 -1
index 79836c8fb909..8a1b117990b8 100644
@@ -311,7 +311,7 @@ static int vbox_crtc_mode_set(struct drm_crtc *crtc,
  static int vbox_crtc_page_flip(struct drm_crtc *crtc,
  			       struct drm_framebuffer *fb,
  			       struct drm_pending_vblank_event *event,
-			       uint32_t page_flip_flags,
+			       u32 page_flip_flags,
  			       struct drm_modeset_acquire_ctx *ctx)
  {
  	struct vbox_private *vbox = crtc->dev->dev_private;
#3 lore raw

PATCH RESEND 2/2 staging: vboxvideo: Use unsigned int instead bool

In reply to Shayenne da Luz Moura

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

---
 drivers/staging/vboxvideo/vbox_drv.h        | 14 +++++++-------
 drivers/staging/vboxvideo/vboxvideo_guest.h |  2 +-
 2 files changed, 8 insertions(+), 8 deletions(-)
drivers/staging/vboxvideo/vbox_drv.h +7 -7
index 594f84272957..7d3e329a6b1c 100644
@@ -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;
@@ -106,7 +106,7 @@ struct vbox_private {
 	 * depending on whether they react to a hot-plug event after the initial
 	 * mode query.
 	 */
-	bool initial_mode_queried;
+	unsigned int initial_mode_queried:1;
 	struct work_struct hotplug_work;
 	u32 input_mapping_width;
 	u32 input_mapping_height;
@@ -114,7 +114,7 @@ struct vbox_private {
 	 * Is user-space using an X.Org-style layout of one large frame-buffer
 	 * encompassing all screen ones or is the fbdev console active?
 	 */
-	bool single_framebuffer;
+	unsigned int single_framebuffer:1;
 	u32 cursor_width;
 	u32 cursor_height;
 	u32 cursor_hot_x;
@@ -139,17 +139,17 @@ struct vbox_connector {
 	struct {
 		u32 width;
 		u32 height;
-		bool disconnected;
+		unsigned int disconnected:1;
 	} mode_hint;
 };
 
 struct vbox_crtc {
 	struct drm_crtc base;
-	bool blanked;
-	bool disconnected;
+	unsigned int blanked:1;
+	unsigned int disconnected:1;
 	unsigned int crtc_id;
 	u32 fb_offset;
-	bool cursor_enabled;
+	unsigned int cursor_enabled:1;
 	u32 x_hint;
 	u32 y_hint;
 };
drivers/staging/vboxvideo/vboxvideo_guest.h +1 -1
index d09da841711a..7a98fb4fb108 100644
@@ -36,7 +36,7 @@ struct vbva_buf_ctx {
 	/** Length of the buffer in bytes */
 	u32 buffer_length;
 	/** Set if we wrote to the buffer faster than the host could read it */
-	bool buffer_overflow;
+	unsigned int buffer_overflow:1;
 	/** VBVA record that we are currently preparing for the host, or NULL */
 	struct vbva_record *record;
 	/**
Signature · 1 line
2.19.1
#4 lore raw

Re: [Outreachy kernel] [RESEND PATCH 2/2] staging: vboxvideo: Use unsigned int instead bool

Quoted 17 lines · Shayenne da Luz Moura

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

---
drivers/staging/vboxvideo/vbox_drv.h | 14 +++++++-------
drivers/staging/vboxvideo/vboxvideo_guest.h | 2 +-

2 files changed, 8 insertions(+), 8 deletions(-)
drivers/staging/vboxvideo/vbox_drv.h +0 -0
index 594f84272957..7d3e329a6b1c 100644

drivers/staging/vboxvideo/vbox_drv.h struct vbox_private { · line 87

Quoted 8 lines
drivers/staging/vboxvideo/vbox_drv.h +1 -1
@@ -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.

2. This is actually less efficient (slower) for the compiler to work with. The smallest granularity we have to access memory is 1 byte; we can't set individual bits directly in memory. For the original code, the assembly for 'vbox_private.any_pitch = true' would look something like this:

	movl   $0x1,-0x10(%rsp)

As you can see, the compiler can directly write into the variable. However, when we switch to using bitfields, the compiler must preserve the original value of the other 7 bits, so it must first read them from memory, manipulate the value and write it back. The assembly would look something like this:

	movzbl -0x10(%rsp),%eax
	or     $0x1,%eax
	mov    %al,-0x10(%rsp)

Which is less efficient than what was previously happening.

--
Thanks,
Sasha

#5 lore raw

Re: [Outreachy kernel] [RESEND PATCH 2/2] staging: vboxvideo: Use unsigned int instead bool

[Adding Joe Perches]

Quoted 50 lines · Sasha Levin
Quoted 18 lines · Shayenne da Luz Moura

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

---
drivers/staging/vboxvideo/vbox_drv.h | 14 +++++++-------
drivers/staging/vboxvideo/vboxvideo_guest.h | 2 +-

2 files changed, 8 insertions(+), 8 deletions(-)
drivers/staging/vboxvideo/vbox_drv.h +0 -0
index 594f84272957..7d3e329a6b1c 100644

drivers/staging/vboxvideo/vbox_drv.h struct vbox_private { · line 87

Quoted 8 lines
drivers/staging/vboxvideo/vbox_drv.h +1 -1
@@ -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.

2. This is actually less efficient (slower) for the compiler to work with. The smallest granularity we have to access memory is 1 byte; we can't set individual bits directly in memory. For the original code, the assembly for 'vbox_private.any_pitch = true' would look something like this:

	movl   $0x1,-0x10(%rsp)

As you can see, the compiler can directly write into the variable. However, when we switch to using bitfields, the compiler must preserve the original value of the other 7 bits, so it must first read them from memory, manipulate the value and write it back. The assembly would look something like this:

Quoted 5 lines
	movzbl -0x10(%rsp),%eax
	or     $0x1,%eax
	mov    %al,-0x10(%rsp)

Which is less efficient than what was previously happening.

Maybe checkpatch could be more precise about what kind of bools should be changed?

julia

#6 lore raw

Re: [Outreachy kernel] [RESEND PATCH 2/2] staging: vboxvideo: Use unsigned int instead bool

Quoted 54 lines · Julia Lawall

[Adding Joe Perches]

Quoted 50 lines · Sasha Levin
Quoted 18 lines · Shayenne da Luz Moura

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

---
drivers/staging/vboxvideo/vbox_drv.h | 14 +++++++-------
drivers/staging/vboxvideo/vboxvideo_guest.h | 2 +-

2 files changed, 8 insertions(+), 8 deletions(-)
drivers/staging/vboxvideo/vbox_drv.h +0 -0
index 594f84272957..7d3e329a6b1c 100644

drivers/staging/vboxvideo/vbox_drv.h struct vbox_private { · line 87

Quoted 8 lines
drivers/staging/vboxvideo/vbox_drv.h +1 -1
@@ -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.

2. This is actually less efficient (slower) for the compiler to work with. The smallest granularity we have to access memory is 1 byte; we can't set individual bits directly in memory. For the original code, the assembly for 'vbox_private.any_pitch = true' would look something like this:

	movl   $0x1,-0x10(%rsp)

As you can see, the compiler can directly write into the variable. However, when we switch to using bitfields, the compiler must preserve the original value of the other 7 bits, so it must first read them from memory, manipulate the value and write it back. The assembly would look something like this:

Quoted 8 lines
Quoted 5 lines
	movzbl -0x10(%rsp),%eax
	or     $0x1,%eax
	mov    %al,-0x10(%rsp)

Which is less efficient than what was previously happening.

Maybe checkpatch could be more precise about what kind of bools should be changed?

Probably so, what verbiage would you suggest?

Also, any conversion from bool to int would
have to take care than any assigment uses !!
where appropriate.

#7 lore raw

Re: [Outreachy kernel] [RESEND PATCH 2/2] staging: vboxvideo: Use unsigned int instead bool

Quoted 36 lines · Joe Perches
Quoted 35 lines · Julia Lawall

[Adding Joe Perches]

Quoted 31 lines · Sasha Levin
Quoted 18 lines · Shayenne da Luz Moura

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

---
drivers/staging/vboxvideo/vbox_drv.h | 14 +++++++-------
drivers/staging/vboxvideo/vboxvideo_guest.h | 2 +-

2 files changed, 8 insertions(+), 8 deletions(-)
drivers/staging/vboxvideo/vbox_drv.h +0 -0
index 594f84272957..7d3e329a6b1c 100644

drivers/staging/vboxvideo/vbox_drv.h struct vbox_private { · line 87

Quoted 8 lines
drivers/staging/vboxvideo/vbox_drv.h +1 -1
@@ -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:

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

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.

Since this is between a pointer and a u32, won't the compiler put a lot more padding, in both cases?

Quoted 19 lines
Quoted 19 lines
Quoted 19 lines

2. This is actually less efficient (slower) for the compiler to work with. The smallest granularity we have to access memory is 1 byte; we can't set individual bits directly in memory. For the original code, the assembly for 'vbox_private.any_pitch = true' would look something like this:

	movl   $0x1,-0x10(%rsp)

As you can see, the compiler can directly write into the variable. However, when we switch to using bitfields, the compiler must preserve the original value of the other 7 bits, so it must first read them from memory, manipulate the value and write it back. The assembly would look something like this:

	movzbl -0x10(%rsp),%eax
	or     $0x1,%eax
	mov    %al,-0x10(%rsp)
Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Which is less efficient than what was previously happening.

Maybe checkpatch could be more precise about what kind of bools should be changed?

Probably so, what verbiage would you suggest?

I don't know what are the conditions. Sasha?

julia

Quoted 6 lines

Also, any conversion from bool to int would
have to take care than any assigment uses !!
where appropriate.

#8 lore raw

Re: [Outreachy kernel] [RESEND PATCH 2/2] staging: vboxvideo: Use unsigned int instead bool

In reply to Sasha Levin

Hi Sasha,

Quoted 30 lines · Sasha Levin
Quoted 17 lines · Shayenne da Luz Moura

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

---
drivers/staging/vboxvideo/vbox_drv.h | 14 +++++++-------
drivers/staging/vboxvideo/vboxvideo_guest.h | 2 +-

2 files changed, 8 insertions(+), 8 deletions(-)
drivers/staging/vboxvideo/vbox_drv.h +0 -0
index 594f84272957..7d3e329a6b1c 100644

drivers/staging/vboxvideo/vbox_drv.h struct vbox_private { · line 87

Quoted 8 lines
drivers/staging/vboxvideo/vbox_drv.h +1 -1
@@ -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:

Quoted 4 lines

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/

Signature · 4 lines
Himanshu Jha
Undergraduate Student
Department of Electronics & Communication
Guru Tegh Bahadur Institute of Technology
#9 lore raw

Re: [Outreachy kernel] [RESEND PATCH 2/2] staging: vboxvideo: Use unsigned int instead bool

Quoted 5 lines

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.

If bool x:1 has the size of bool, then wouldn't int x:1 have the size of int? But my little experiments suggest that the size is the smallest that fits the requested bits and alignment chosen by the compiler, regardless of the type.

bool x:1 has the advantage that anything that is not 0 is considered true. So for bool x:1, x = 4 is true, while for int x:1, x = 4 is false.

But the :1 adds instructions, so at least for only one bool, where little space is saved, it is probably not worth it.

julia

#10 lore raw

Re: [Outreachy kernel] [RESEND PATCH 2/2] staging: vboxvideo: Use unsigned int instead bool

Quoted 10 lines · Julia Lawall
Quoted 5 lines

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.

If bool x:1 has the size of bool, then wouldn't int x:1 have the size of int? But my little experiments suggest that the size is the smallest that fits the requested bits and alignment chosen by the compiler, regardless of the type.

Yes, correct!
And we can't use sizeof on bitfields *directly*, nor reference it using a pointer.

It can be applied only when these bitfields are wrapped in a structure.

Testing:

#include <stdio.h>
#include <stdbool.h>

struct S {
bool a:1;
bool b:1;
bool c:1;
bool d:1;
};

int main(void)

{
    printf("%zu\n", sizeof(struct S));
}

Output: 1

If I change all bool to unsigned int, output is: *4*.

So, conclusion is compiler doesn't squeeze the size less than native size of the datatype i.e., if we changed all members to unsigned int:1,
total width = 4 bits
padding = 4 bits

Therefore, total size should have been = 1 byte! But since sizeof(unsigned int) == 4, it can't be squeezed to less than it.

Quoted 1 line

bool x:1 has the advantage that anything that is not 0 is considered true.

Yes, implicit conversion rules for boolean.

Quoted 1 line

So for bool x:1, x = 4 is true, while for int x:1, x = 4 is false.

Well, int x:1 can either have 0..1 or -1..0 range due implementation defined behavior as I said in the previous reply.

If you really want to consider negative values, then make it explicit using signed int x:1 which make range guaranteed to be -1..0

Regardless, integer conversion rules will kick in.

Quoted 2 lines

But the :1 adds instructions, so at least for only one bool, where little space is saved, it is probably not worth it.

True, we should reply on a promised guideline rather than possibility.

Signature · 4 lines
Himanshu Jha
Undergraduate Student
Department of Electronics & Communication
Guru Tegh Bahadur Institute of Technology
#11 lore raw

Re: [Outreachy kernel] [RESEND PATCH 2/2] staging: vboxvideo: Use unsigned int instead bool

Quoted 45 lines · Himanshu Jha
Quoted 10 lines · Julia Lawall
Quoted 5 lines

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.

If bool x:1 has the size of bool, then wouldn't int x:1 have the size of int? But my little experiments suggest that the size is the smallest that fits the requested bits and alignment chosen by the compiler, regardless of the type.

Yes, correct!
And we can't use sizeof on bitfields *directly*, nor reference it using a pointer.

It can be applied only when these bitfields are wrapped in a structure.

Testing:

#include <stdio.h>
#include <stdbool.h>

struct S {
bool a:1;
bool b:1;
bool c:1;
bool d:1;
};

int main(void)

{
    printf("%zu\n", sizeof(struct S));
}

Output: 1

If I change all bool to unsigned int, output is: *4*.

So, conclusion is compiler doesn't squeeze the size less than native size of the datatype i.e., if we changed all members to unsigned int:1,
total width = 4 bits
padding = 4 bits

Quoted 3 lines

Therefore, total size should have been = 1 byte! But since sizeof(unsigned int) == 4, it can't be squeezed to less than it.

This conclusion does not seem to be correct, if you try the following program. I get 4 for everything, meaning that the four unsigned int bits are getting squeezed into one byte when it is convenient.

#include <stdio.h>
#include <stdbool.h>

struct S1 {
bool a:1;
bool b:1;
bool c:1;
bool d:1;
char a1;
char a2;
char a3;
};

struct S2 {
unsigned int a:1;
unsigned int b:1;
unsigned int c:1;
unsigned int d:1;
char a1;
char a2;
char a3;
};

int main(void)

{
    printf("%zu\n", sizeof(struct S1));
    printf("%zu\n", sizeof(struct S2));
    printf("%zu\n", sizeof(unsigned int));
}
Quoted 5 lines

Well, int x:1 can either have 0..1 or -1..0 range due implementation defined behavior as I said in the previous reply.

If you really want to consider negative values, then make it explicit using signed int x:1 which make range guaranteed to be -1..0

The code wants booleans, not negative values.

julia

#12 lore raw

Re: [Outreachy kernel] [RESEND PATCH 2/2] staging: vboxvideo: Use unsigned int instead bool

Hi,

Quoted 88 lines · Himanshu Jha
Quoted 46 lines · Julia Lawall
Quoted 10 lines
Quoted 5 lines

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.

If bool x:1 has the size of bool, then wouldn't int x:1 have the size of int? But my little experiments suggest that the size is the smallest that fits the requested bits and alignment chosen by the compiler, regardless of the type.

Yes, correct!
And we can't use sizeof on bitfields *directly*, nor reference it using a pointer.

It can be applied only when these bitfields are wrapped in a structure.

Testing:

#include <stdio.h>
#include <stdbool.h>

struct S {
bool a:1;
bool b:1;
bool c:1;
bool d:1;
};

int main(void)

{
    printf("%zu\n", sizeof(struct S));
}

Output: 1

If I change all bool to unsigned int, output is: *4*.

So, conclusion is compiler doesn't squeeze the size less than native size of the datatype i.e., if we changed all members to unsigned int:1,
total width = 4 bits
padding = 4 bits

Quoted 3 lines

Therefore, total size should have been = 1 byte! But since sizeof(unsigned int) == 4, it can't be squeezed to less than it.

This conclusion does not seem to be correct, if you try the following program. I get 4 for everything, meaning that the four unsigned int bits are getting squeezed into one byte when it is convenient.

#include <stdio.h>
#include <stdbool.h>

struct S1 {
bool a:1;
bool b:1;
bool c:1;
bool d:1;
char a1;
char a2;
char a3;
};

struct S2 {
unsigned int a:1;
unsigned int b:1;
unsigned int c:1;
unsigned int d:1;
char a1;
char a2;
char a3;
};

int main(void)

{
    printf("%zu\n", sizeof(struct S1));
    printf("%zu\n", sizeof(struct S2));
    printf("%zu\n", sizeof(unsigned int));
}
Quoted 3 lines

Well, int x:1 can either have 0..1 or -1..0 range due implementation defined behavior as I said in the previous reply.

Quoted 6 lines
Quoted 2 lines

If you really want to consider negative values, then make it explicit using signed int x:1 which make range guaranteed to be -1..0

The code wants booleans, not negative values.

julia

Thank you all for the discussion!

However, I think I do not understand the conclusion.

It means that the best way is to use only boolean instead of use unsigned int with bitfield? I mean specifically in the case of my patch, where there are some boolean variables are mixed with other variables types.

Best,

Shayenne

#13 lore raw

Re: [Outreachy kernel] [RESEND PATCH 2/2] staging: vboxvideo: Use unsigned int instead bool

Quoted 97 lines · Shayenne Moura

Hi,

Quoted 88 lines · Himanshu Jha
Quoted 46 lines · Julia Lawall
Quoted 10 lines
Quoted 5 lines

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.

If bool x:1 has the size of bool, then wouldn't int x:1 have the size of int? But my little experiments suggest that the size is the smallest that fits the requested bits and alignment chosen by the compiler, regardless of the type.

Yes, correct!
And we can't use sizeof on bitfields *directly*, nor reference it using a pointer.

It can be applied only when these bitfields are wrapped in a structure.

Testing:

#include <stdio.h>
#include <stdbool.h>

struct S {
bool a:1;
bool b:1;
bool c:1;
bool d:1;
};

int main(void)

{
    printf("%zu\n", sizeof(struct S));
}

Output: 1

If I change all bool to unsigned int, output is: *4*.

So, conclusion is compiler doesn't squeeze the size less than native size of the datatype i.e., if we changed all members to unsigned int:1,
total width = 4 bits
padding = 4 bits

Quoted 3 lines

Therefore, total size should have been = 1 byte! But since sizeof(unsigned int) == 4, it can't be squeezed to less than it.

This conclusion does not seem to be correct, if you try the following program. I get 4 for everything, meaning that the four unsigned int bits are getting squeezed into one byte when it is convenient.

#include <stdio.h>
#include <stdbool.h>

struct S1 {
bool a:1;
bool b:1;
bool c:1;
bool d:1;
char a1;
char a2;
char a3;
};

struct S2 {
unsigned int a:1;
unsigned int b:1;
unsigned int c:1;
unsigned int d:1;
char a1;
char a2;
char a3;
};

int main(void)

{
    printf("%zu\n", sizeof(struct S1));
    printf("%zu\n", sizeof(struct S2));
    printf("%zu\n", sizeof(unsigned int));
}
Quoted 3 lines

Well, int x:1 can either have 0..1 or -1..0 range due implementation defined behavior as I said in the previous reply.

Quoted 6 lines
Quoted 2 lines

If you really want to consider negative values, then make it explicit using signed int x:1 which make range guaranteed to be -1..0

The code wants booleans, not negative values.

julia

Quoted 7 lines

Thank you all for the discussion!

However, I think I do not understand the conclusion.

It means that the best way is to use only boolean instead of use unsigned int with bitfield? I mean specifically in the case of my patch, where there are some boolean variables are mixed with other variables types.

To my recollection, your code had a bool with larger types on either side. In that case, I think bool is fine. The compiler it likely to align those larger typed values such that the field with the bool type will get more than one byte no matter what type you use. If there are several fields with very small types adjacent, there might be some benefit to thinking about what the type should be.

julia

#14 lore raw

Re: [Outreachy kernel] [RESEND PATCH 2/2] staging: vboxvideo: Use unsigned int instead bool

Quoted 108 lines · Julia Lawall
Quoted 97 lines · Shayenne Moura

Hi,

Quoted 88 lines · Himanshu Jha
Quoted 46 lines · Julia Lawall
Quoted 10 lines
Quoted 5 lines

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.

If bool x:1 has the size of bool, then wouldn't int x:1 have the size of int? But my little experiments suggest that the size is the smallest that fits the requested bits and alignment chosen by the compiler, regardless of the type.

Yes, correct!
And we can't use sizeof on bitfields *directly*, nor reference it using a pointer.

It can be applied only when these bitfields are wrapped in a structure.

Testing:

#include <stdio.h>
#include <stdbool.h>

struct S {
bool a:1;
bool b:1;
bool c:1;
bool d:1;
};

int main(void)

{
    printf("%zu\n", sizeof(struct S));
}

Output: 1

If I change all bool to unsigned int, output is: *4*.

So, conclusion is compiler doesn't squeeze the size less than native size of the datatype i.e., if we changed all members to unsigned int:1,
total width = 4 bits
padding = 4 bits

Quoted 3 lines

Therefore, total size should have been = 1 byte! But since sizeof(unsigned int) == 4, it can't be squeezed to less than it.

This conclusion does not seem to be correct, if you try the following program. I get 4 for everything, meaning that the four unsigned int bits are getting squeezed into one byte when it is convenient.

#include <stdio.h>
#include <stdbool.h>

struct S1 {
bool a:1;
bool b:1;
bool c:1;
bool d:1;
char a1;
char a2;
char a3;
};

struct S2 {
unsigned int a:1;
unsigned int b:1;
unsigned int c:1;
unsigned int d:1;
char a1;
char a2;
char a3;
};

int main(void)

{
    printf("%zu\n", sizeof(struct S1));
    printf("%zu\n", sizeof(struct S2));
    printf("%zu\n", sizeof(unsigned int));
}
Quoted 3 lines

Well, int x:1 can either have 0..1 or -1..0 range due implementation defined behavior as I said in the previous reply.

Quoted 6 lines
Quoted 2 lines

If you really want to consider negative values, then make it explicit using signed int x:1 which make range guaranteed to be -1..0

The code wants booleans, not negative values.

julia

Quoted 7 lines

Thank you all for the discussion!

However, I think I do not understand the conclusion.

It means that the best way is to use only boolean instead of use unsigned int with bitfield? I mean specifically in the case of my patch, where there are some boolean variables are mixed with other variables types.

Quoted 8 lines

To my recollection, your code had a bool with larger types on either side. In that case, I think bool is fine. The compiler it likely to align those larger typed values such that the field with the bool type will get more than one byte no matter what type you use. If there are several fields with very small types adjacent, there might be some benefit to thinking about what the type should be.

julia

Got it! Thank you!