Page MenuHomeFreeBSD

linuxkpi: Fix the definition of BITS_PER_LONG
ClosedPublic

Authored by markj on Oct 16 2025, 1:14 PM.
Tags
None
Referenced Files
Unknown Object (File)
Thu, Nov 13, 9:26 PM
Unknown Object (File)
Wed, Nov 12, 3:49 AM
Unknown Object (File)
Sun, Nov 9, 6:03 PM
Unknown Object (File)
Thu, Oct 30, 2:17 AM
Unknown Object (File)
Wed, Oct 29, 10:13 PM
Unknown Object (File)
Wed, Oct 29, 7:04 AM
Unknown Object (File)
Tue, Oct 21, 2:30 PM
Unknown Object (File)
Tue, Oct 21, 2:30 PM
Subscribers

Details

Summary

Don't use LP64 to decide the value, as it gives the wrong result on
CHERI platforms. Just define it in terms of sizeof(long).

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

markj requested review of this revision.Oct 16 2025, 1:14 PM
This revision is now accepted and ready to land.Oct 16 2025, 1:18 PM

Note, there are quite a few other definitions of BITS_PER_LONG that have similar problems, but they're mostly in obsolete or unmaintained drivers. I'm not sure whether to sweep through and fix them too.

I'm not sure whether to sweep through and fix them too.

I'd be inclined to just leave them unless they come up in a CHERI context.

Use SIZEOF_LONG instead. Fix up BITS_PER_LONG_LONG too.

This revision now requires review to proceed.Oct 16 2025, 3:50 PM

This is fine too. We do have some existing cases using sizeof, e.g.

sys/dev/drm2/drm_atomic.h:#define       NB_BITS_PER_LONG                (sizeof(long) * NBBY)
sys/dev/axgbe/xgbe_osdep.h:#define      BITS_PER_LONG                   (sizeof(long) * CHAR_BIT)
This revision is now accepted and ready to land.Thu, Oct 16, 4:14 PM
bz added inline comments.
sys/compat/linuxkpi/common/include/linux/bitops.h
41

I have a rather bad feeling about CHAR_BITS but maybe that stems from the fact that I used to write code for a 7bbit system before. I think the general FreeBSD usage was * NBBY.
The question is if we should fix that to be defined as well to CHAR_BITS then? If they start to disagree, we'll be in trouble.

This revision now requires review to proceed.Thu, Oct 16, 10:49 PM

This is fine too. We do have some existing cases using sizeof, e.g.

sys/dev/drm2/drm_atomic.h:#define       NB_BITS_PER_LONG                (sizeof(long) * NBBY)
sys/dev/axgbe/xgbe_osdep.h:#define      BITS_PER_LONG                   (sizeof(long) * CHAR_BIT)

The point was made elsewhere that we might want the value to be an int rather than a size_t, which would have been the case with the old version.

sys/compat/linuxkpi/common/include/linux/bitops.h
41

I think it's safe to assume that CHAR_BIT (I made a typo before) and NBBY agree with each other. Probably NBBY should be defined in terms of the former, but that's not within the scope of this change.

bz added inline comments.
sys/compat/linuxkpi/common/include/linux/bitops.h
41

We have one use of CHAR_BIT in a tree from a few days ago in sys/ ; more than 240 of NBBY (incl. in cddl); I am happy here either way and if NBBY is a FreeBSD-ism (I don't know) maybe we should clean it up. I just prefer to have one way and not multiple.

This revision is now accepted and ready to land.Thu, Oct 16, 11:00 PM
This revision was automatically updated to reflect the committed changes.