All of lore.kernel.org
 help / color / mirror / Atom feed
From: Nicola Vetrini <nicola.vetrini@bugseng.com>
To: Stefano Stabellini <sstabellini@kernel.org>
Cc: Jan Beulich <jbeulich@suse.com>,
	michal.orzel@amd.com, xenia.ragiadakou@amd.com,
	ayan.kumar.halder@amd.com, consulting@bugseng.com,
	Andrew Cooper <andrew.cooper3@citrix.com>,
	George Dunlap <george.dunlap@citrix.com>,
	Julien Grall <julien@xen.org>, Wei Liu <wl@xen.org>,
	xen-devel@lists.xenproject.org
Subject: Re: [XEN PATCH] xen/types: address Rule 10.1 for macro BITS_TO_LONGS
Date: Fri, 08 Sep 2023 10:48:36 +0200	[thread overview]
Message-ID: <6818e3ac47edca6ba107b8eeff95955a@bugseng.com> (raw)
In-Reply-To: <alpine.DEB.2.22.394.2309061828360.6458@ubuntu-linux-20-04-desktop>

On 07/09/2023 03:33, Stefano Stabellini wrote:
> On Wed, 6 Sep 2023, Jan Beulich wrote:
>> On 06.09.2023 17:57, Nicola Vetrini wrote:
>> > On 05/09/2023 10:33, Jan Beulich wrote:
>> >> On 05.09.2023 10:20, Nicola Vetrini wrote:
>> >>> On 05/09/2023 09:46, Jan Beulich wrote:
>> >>>> On 05.09.2023 09:31, Nicola Vetrini wrote:
>> >>>>> Given its use in the declaration
>> >>>>> 'DECLARE_BITMAP(features, IOMMU_FEAT_count)' the argument
>> >>>>> 'bits' has essential type 'enum iommu_feature', which is not
>> >>>>> allowed by the Rule as an operand to the addition operator.
>> >>>>> Given that its value can be represented by a signed integer,
>> >>>>> the explicit cast resolves the violation.
>> >>>>
>> >>>> Wait - why would this lead to a change to BITS_TO_LONGS()? And if
>> >>>> that
>> >>>> was to be changed, why plain int? I don't think negative input makes
>> >>>> sense there, and in principle I'd expect values beyond 4 billion to
>> >>>> also be permissible (even if likely no such use will ever appear in a
>> >>>> DECLARE_BITMAP(), but elsewhere it may make sense). Even going to
>> >>>> "unsigned long" may be too limiting ...
>> >>>>
>> >>>
>> >>> You have a point. I can think of doing it like this:
>> >>> DECLARE_BITMAP(features, (int)IOMMU_FEAT_count)
> 
> I think this is a good solution for this case (even more so if we can't
> find a better implementation of BITS_TO_LONGS)
> 
> 
>> >>> on the grounds that the enum constant is representable in an int, and
>> >>> it
>> >>> does not seem likely
>> >>> to get much bigger.
>> >>> Having an unsigned cast requires making the whole expression
>> >>> essentially unsigned, otherwise Rule 10.4 is violated because
>> >>> BITS_PER_LONG is
>> >>> essentially signed. This can be done, but it depends on how
>> >>> BITS_TO_LONGS will be/is used.
>> >>
>> >> It'll need looking closely, yes, but I expect that actually wants to be
>> >> an
>> >> unsigned constant. I wouldn't be surprised if some use of
>> >> DECLARE_BITMAP()
>> >> appeared (or already existed) where the 2nd argument involves sizeof()
>> >> in
>> >> some way.
>> >>
>> >
>> > I think there's one with ARRAY_SIZE. In my opinion this can be resolved
>> > as follows:
>> >
>> > #define BYTES_PER_LONG (1U << LONG_BYTEORDER) // the essential type gets
>> > from signed to unsigned
>> >
>> > #define BITS_TO_LONGS(bits) \
>> >          (((unsigned long long)(bits)+BITS_PER_LONG-1U)/BITS_PER_LONG) //
>> > same here
>> 
>> Except, as said before, I consider any kind of cast on "bits" latently
>> problematic.
> 
> Can't we just do this (same but without the cast):
> 
> #define BYTES_PER_LONG (1U << LONG_BYTEORDER)
> #define BITS_TO_LONGS(bits) \
>          (((bits)+BITS_PER_LONG-1U)/BITS_PER_LONG)
> 
> Then we just need to make sure to pass an unsigned to BITS_TO_LONGS. In
> the case above we would do:
> 
> DECLARE_BITMAP(features, (unsigned int)IOMMU_FEAT_count)

There is a build error due to -Werror because of a pointer comparison at 
line 469 of common/numa.c:
i = min(PADDR_BITS, BITS_PER_LONG - 1);
where
#define PADDR_BITS              52

I guess PADDR_BITS can become unsigned or gain a cast

-- 
Nicola Vetrini, BSc
Software Engineer, BUGSENG srl (https://bugseng.com)


  parent reply	other threads:[~2023-09-08  8:49 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-05  7:31 [XEN PATCH] xen/types: address Rule 10.1 for macro BITS_TO_LONGS Nicola Vetrini
2023-09-05  7:46 ` Jan Beulich
2023-09-05  8:20   ` Nicola Vetrini
2023-09-05  8:33     ` Jan Beulich
2023-09-06 15:57       ` Nicola Vetrini
2023-09-06 16:02         ` Jan Beulich
2023-09-07  1:33           ` Stefano Stabellini
2023-09-07  6:42             ` Jan Beulich
2023-09-08  8:48             ` Nicola Vetrini [this message]
2023-09-08 11:57               ` Jan Beulich
2023-09-08 11:59                 ` Jan Beulich
2023-09-08 14:53                   ` Nicola Vetrini
2023-09-08 15:09                     ` Nicola Vetrini
2023-09-11  6:43                       ` Jan Beulich
2023-09-08 19:37                     ` Stefano Stabellini
2023-09-19  9:19                       ` Nicola Vetrini
2023-09-19  9:33                         ` Jan Beulich
2023-09-19  9:54                           ` Nicola Vetrini
2023-09-19  9:59                             ` Jan Beulich
2023-10-04 13:23 ` Nicola Vetrini
2023-10-16 10:31   ` Jan Beulich

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=6818e3ac47edca6ba107b8eeff95955a@bugseng.com \
    --to=nicola.vetrini@bugseng.com \
    --cc=andrew.cooper3@citrix.com \
    --cc=ayan.kumar.halder@amd.com \
    --cc=consulting@bugseng.com \
    --cc=george.dunlap@citrix.com \
    --cc=jbeulich@suse.com \
    --cc=julien@xen.org \
    --cc=michal.orzel@amd.com \
    --cc=sstabellini@kernel.org \
    --cc=wl@xen.org \
    --cc=xen-devel@lists.xenproject.org \
    --cc=xenia.ragiadakou@amd.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.