From: Jacob Keller <jacob.e.keller@intel.com>
To: Masahiro Yamada <masahiroy@kernel.org>
Cc: Vladimir Oltean <olteanv@gmail.com>,
Andrew Morton <akpm@linux-foundation.org>,
Eric Dumazet <edumazet@google.com>,
"Jakub Kicinski" <kuba@kernel.org>,
Paolo Abeni <pabeni@redhat.com>,
Tony Nguyen <anthony.l.nguyen@intel.com>,
Przemek Kitszel <przemyslaw.kitszel@intel.com>,
netdev <netdev@vger.kernel.org>, <linux-kbuild@vger.kernel.org>,
"Vladimir Oltean" <vladimir.oltean@nxp.com>
Subject: Re: [PATCH net-next v5 3/9] lib: packing: add pack_fields() and unpack_fields()
Date: Fri, 15 Nov 2024 15:44:07 -0800 [thread overview]
Message-ID: <0083a475-a573-44bc-8f8d-595b0bd3b675@intel.com> (raw)
In-Reply-To: <CAK7LNARhMDEMZFjC1tU5oHefGocxwBC5=Vyy9Q=bx3VvQyssVQ@mail.gmail.com>
On 11/15/2024 12:48 PM, Masahiro Yamada wrote:
> On Thu, Nov 14, 2024 at 6:04 AM Jacob Keller <jacob.e.keller@intel.com> wrote:
>> With the goal of maintaining compile time checks, we end up either
>> needing to use generated macros which are O(N^2) if we allow arbitrary
>> overlap. If we instead allow only only ascending or descending order,
>> this would drop to O(N) which would avoid needing to have 20k lines of
>> generated code for the case with 50. I think we could implement them
>> without forcing drivers to specifically call the correct macro by using
>> something like __builtin_choose_expr(), tho implementing that macro to
>> select could be quite long.
>
>
> WIth Clang, the following check seems to work,
> but with GCC, it works only when the array size is small.
>
>
> #define PACKED_FIELDS_OUT_OF_ORDER(fields) \
> ({ \
> bool res = false; \
> for (unsigned int i = 1; i < ARRAY_SIZE(fields); i++) \
> res |= fields[i - 1].startbit < fields[i].startbit; \
> res; \
> })
>
> #define PACKED_FIELDS_OVERWRAP(fields) \
> ({ \
> bool res = false; \
> for (unsigned int i = 1; i < ARRAY_SIZE(fields); i++) \
> res |= fields[i - 1].endbit <= fields[i].startbit; \
> res; \
> })
>
> /*
> * Clang cleverly computes this at compile time.
> * Unfortunately, GCC gives it up when the array size becomes large.
> * Turn on this check only when building the kernel with Clang.
> */
> #ifdef CONFIG_CC_IS_CLANG
> #define PACKED_FIELDS_SANITY_CHECKS(fields) \
> BUILD_BUG_ON_MSG(PACKED_FIELDS_OUT_OF_ORDER(fields), \
> #fields ": not sorted decending order"); \
> BUILD_BUG_ON_MSG(PACKED_FIELDS_OVERWRAP(fields), \
> #fields ": contains overwrap")
> #else
> #define PACKED_FIELDS_SANITY_CHECKS(fields)
> #endif
>
This definitely seems compiler specific.
>
>
>
>
>> Otherwise we can fall back to either module load time checks, or go all
>> the way back to only sanity checking at executing of pack_fields or
>> unpack_fields.
>
> Is it a big deal?
> One solution is a run-time check (for GCC), which is a one-time
> for booting or module loading.
>
> Another is to rely on CICD running with Clang to detect overwraps.
>
>
> It is horrible to include kernel-space structures from user-space
> programs that run in a different architecture.
>
> file2alias.c does this because it is only possible at compile-time,
> but it is always the source of troubles.
> I am search for a way to generate MODULE_ALIAS() without
> including mod_devicetable.h from modpost.
>
Yea, I agree modpost is pretty ugly, and I'm happy to drop it.
I think I've managed to get something that works in GCC and Clang with
~3k lines of macro vs the original 20K lines we had for sizes up to 50.
The version I have now works, but does require the 3K lines of macro to
effectively unwind the loops.
Its also slightly brittle because some slight alterations of the checks
no longer get detected by GCC as compile-time constants :(
I am not a huge fan of only testing on CI, since not every developer
will have things go through CI, so we end up with late reports.
I'm going to post the next version which uses the macros again, but
manages to limit things so that the calls are all done from within
<linux/packing.h> without driver intervention, and seem to work reliably
on my test systems.
>
>
>
> --
> Best Regards
> Masahiro Yamada
next prev parent reply other threads:[~2024-11-15 23:44 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-11-11 8:08 [PATCH net-next v5 0/9] lib: packing: introduce and use (un)pack_fields Jacob Keller
2024-11-11 8:08 ` [PATCH net-next v5 1/9] lib: packing: create __pack() and __unpack() variants without error checking Jacob Keller
2024-11-11 8:08 ` [PATCH net-next v5 2/9] lib: packing: demote truncation error in pack() to a warning in __pack() Jacob Keller
2024-11-11 8:08 ` [PATCH net-next v5 3/9] lib: packing: add pack_fields() and unpack_fields() Jacob Keller
2024-11-11 10:25 ` kernel test robot
2024-11-11 10:36 ` kernel test robot
2024-11-13 20:32 ` Masahiro Yamada
2024-11-13 21:04 ` Jacob Keller
2024-11-15 20:48 ` Masahiro Yamada
2024-11-15 23:44 ` Jacob Keller [this message]
2024-11-11 8:08 ` [PATCH net-next v5 4/9] ice: remove int_q_state from ice_tlan_ctx Jacob Keller
2024-11-11 8:08 ` [PATCH net-next v5 5/9] ice: use structures to keep track of queue context size Jacob Keller
2024-11-11 8:08 ` [PATCH net-next v5 6/9] ice: use <linux/packing.h> for Tx and Rx queue context data Jacob Keller
2024-11-11 8:08 ` [PATCH net-next v5 7/9] ice: reduce size of queue context fields Jacob Keller
2024-11-11 8:08 ` [PATCH net-next v5 8/9] ice: move prefetch enable to ice_setup_rx_ctx Jacob Keller
2024-11-11 8:08 ` [PATCH net-next v5 9/9] ice: cleanup Rx queue context programming functions Jacob Keller
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=0083a475-a573-44bc-8f8d-595b0bd3b675@intel.com \
--to=jacob.e.keller@intel.com \
--cc=akpm@linux-foundation.org \
--cc=anthony.l.nguyen@intel.com \
--cc=edumazet@google.com \
--cc=kuba@kernel.org \
--cc=linux-kbuild@vger.kernel.org \
--cc=masahiroy@kernel.org \
--cc=netdev@vger.kernel.org \
--cc=olteanv@gmail.com \
--cc=pabeni@redhat.com \
--cc=przemyslaw.kitszel@intel.com \
--cc=vladimir.oltean@nxp.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