Re: [PATCH v2 01/12] regmap-irq: Convert bool bitfields to unsigned int

From: Aidan MacDonald
Date: Fri Jun 24 2022 - 10:30:15 EST



Joe Perches <joe@xxxxxxxxxxx> writes:

> On Fri, 2022-06-24 at 14:05 +0100, Aidan MacDonald wrote:
>> Joe Perches <joe@xxxxxxxxxxx> writes:
>>
>> > On Fri, 2022-06-24 at 13:11 +0100, Mark Brown wrote:
>> > > On Thu, Jun 23, 2022 at 11:26:10PM +0200, Andy Shevchenko wrote:
>> > > > On Thu, Jun 23, 2022 at 11:13 PM Aidan MacDonald
>> > >
>> > > > > Use 'unsigned int' for bitfields for consistency with most other
>> > > > > kernel code.
>> > >
>> > > > There is no point to convert the fields you are about to remove.
>> > >
>> > > > So, either don't touch them or make this patch closer to the end of the series.
>> > >
>> > > It costs us nothing to convert them, this isn't a difficult or hard to
>> > > understand refactoring - the patch is fine the way it is.
>> >
>> > Modulo the defects that might be introduced if an overflow occurs.
>> >
>> > struct foo {
>> > unsigned int a:1;
>> > bool b:1;
>> > }
>> >
>> > Assign a non-zero int without bit 0 set to each and see if
>> > a and b differ.
>>
>> Bool permits implicit pointer-to-bool conversions, so it isn't free
>> of pitfalls either.
>
> Care to describe some of those pitfalls?
> I can't think of any off the top of my head.
>

I just listed the pitfall. I don't consider silently converting a
pointer to a bool value desirable, outside of contexts where that
is made obvious, ie: while(...), if(...), and so on.

>> Overflow is probably more dangerous in general,
>> but here there's little chance of pointers or overflow getting involved.
>
> I don't know _this_ code at all, nor have I read it.
>
> If all the conversions are just deleted later, then of course
> it should not be converted at all.

Only _some_ of the flags are being removed, not all of them.

>
> I'm just commenting on the proposed refactoring.
>
> I'm trying to show that conversions of bool:1->unsigned int:1
> as being trivial are not so trivial after all.
>
> It's fairly common to have code like:
>
> [bool] foo.bar = some_value & SETTING;
>
> where some value is tested for a mask/bit and a non-zero is true.
>
> So conversions of foo.bar from bool:1 to unsigned int:1 are not
> wise unless all possible side effects are known.

Good point. I didn't take that into account, because I expect all
users are using literal true/false values.

Anyhow, Andy asked for the flags to be converted to unsigned int since
he thought bool was strange for a bitfield, and grepping showed it was
much less common than unsigned int. I personally don't mind either way,
so maybe it's better to leave them as bools.