Page MenuHomeFreeBSD

libkern: add ilog2 helpers
ClosedPublic

Authored by dougm on Sun, May 12, 6:47 PM.
Tags
None
Referenced Files
Unknown Object (File)
Mon, Jun 3, 5:06 PM
Unknown Object (File)
Wed, May 22, 4:55 AM
Unknown Object (File)
Wed, May 22, 3:28 AM
Unknown Object (File)
Tue, May 21, 8:55 PM
Unknown Object (File)
Tue, May 21, 4:49 AM
Unknown Object (File)
Mon, May 20, 1:16 PM
Unknown Object (File)
Sun, May 19, 5:32 AM
Unknown Object (File)
Sat, May 18, 8:34 PM

Details

Summary

The fls* bit-finding functions have to include zero-checks. In a case where the caller knows that the argument is not zero, these new functions provide a way to avoid those zero-checks.

Test Plan

Built kernels for several architectures.

Diff Detail

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

Event Timeline

dougm requested review of this revision.Sun, May 12, 6:47 PM
dougm created this revision.

Others have used the ilog2 name for the same function. Drop those other definitions.

sys/powerpc/booke/pmap_64.c
753

There's another ilog2 in pmap_32.c, too, and looks like yet another declaration in pmap.c. Also, now you'll need to change all uses in pmap.c to ilog2l() instead of ilog2(), since ilog2() now operates on an int, and we need a long here (vm_size_t).

dougm edited the test plan for this revision. (Show Details)

Change ilog2 to ilog2l where appropriate. Drop bslr, since it's just another name for ilog2l.

PPC LGTM. If you haven't, test a buildkernel with MPC85XX (powerpc) and QORIQ64 (powerpc64) just to be safe.

This revision is now accepted and ready to land.Sun, May 12, 8:35 PM

PPC LGTM. If you haven't, test a buildkernel with MPC85XX (powerpc) and QORIQ64 (powerpc64) just to be safe.

With or without these changes in place
make -j 24 buildkernel TARGET=powerpc KERNCONF=QORIQ64
fails:
./machine/reg.h:80:41: error: declaration of 'struct reg32' will not be visible outside of this function [-Werror,-Wvisibility]

80 | int     fill_regs32(struct thread *, struct reg32 *);

begins a stream of errors.

With these changes in place
make -j 24 buildkernel TARGET=powerpc KERNCONF=MPC85XX
completes successfully.

PPC LGTM. If you haven't, test a buildkernel with MPC85XX (powerpc) and QORIQ64 (powerpc64) just to be safe.

With or without these changes in place
make -j 24 buildkernel TARGET=powerpc KERNCONF=QORIQ64
fails:
./machine/reg.h:80:41: error: declaration of 'struct reg32' will not be visible outside of this function [-Werror,-Wvisibility]

80 | int     fill_regs32(struct thread *, struct reg32 *);

begins a stream of errors.

powerpc64 is a trickier beast. You need to 'make TARGET=powerpc TARGET_ARCH=powerpc64 ...' (explicitly specify TARGET_ARCH)

powerpc64 is a trickier beast. You need to 'make TARGET=powerpc TARGET_ARCH=powerpc64 ...' (explicitly specify TARGET_ARCH)

Then it builds fine with these changes.

@whu: This change would drop the definition of ilog2 that you included in sys/dev/mana/gdma_util.h. However, that function, called ilog2(x), actually returns x + log2(x), because it initializes 'log' to x, and not to 0.

Is this change, as currently formulated, fixing an error in the dev/mana code, or introducing one?

dougm marked an inline comment as done.
sys/powerpc/booke/pmap_32.c
933–944

I would think the consumers within this file should be changed to use ilog2l(), no?

sys/powerpc/booke/pmap_64.c
753

Same question as pmap_32.c above.

dougm added inline comments.
sys/powerpc/booke/pmap_32.c
933–944

Yes, but there are none.

sys/powerpc/booke/pmap_64.c
753

Same answer as above.

markj added inline comments.
sys/arm64/iommu/smmu.c
938

I think these uses can be ilog2(), even though the removed function implemented ilog2l().

sys/sys/libkern.h
206

I wonder if we want to start using _Generic to reduce the need for callers to be careful about picking the right variant. That is, provide a macro ilog2() which uses _Generic to dispatch to a specific variant. We've been using _Generic in atomic_common.h for some time now without problems.

dougm added inline comments.
sys/sys/libkern.h
206

It is used conditionally in atomic_common.h. I believe I would need to use Generic unconditionally, if I used it at all. Is unconditional use going to be a problem?

sys/sys/libkern.h
206

Additionally, defining ilog2 as a _Generic clashes with the #define of ilog2 in sys/compat/linuxkpi/common/include/linux/log2.h, so a #define of ilog2 in libkern will have to include that functionality as well.

sys/sys/libkern.h
206

I think unconditional use is ok so long as the uses are restricted to the kernel. atomic_common.h is also used by userspace, where we can't control the compiler.

This header (libkern.h) is exposed to userspace (there are no _KERNEL guards), but I think it's most likely ok to assume that it's only going to be used by the kernel.

This revision was automatically updated to reflect the committed changes.