Page MenuHomeFreeBSD

libkern: add ilog2 helpers
AcceptedPublic

Authored by dougm on Fri, May 17, 7:47 PM.
Tags
None
Referenced Files
Unknown Object (File)
Wed, May 22, 5:29 AM
Unknown Object (File)
Wed, May 22, 5:14 AM
Unknown Object (File)
Wed, May 22, 5:13 AM
Unknown Object (File)
Wed, May 22, 5:07 AM
Unknown Object (File)
Tue, May 21, 8:55 PM
Unknown Object (File)
Tue, May 21, 5:54 AM
Unknown Object (File)
Mon, May 20, 6:56 PM
Unknown Object (File)
Sun, May 19, 12:25 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.

This change uses _Generic to define an ilog2 macro that picks the right ilog2 function based on argument type, and to identify constant arguments so that they can be computed at compile time.

This is an alternative to D45170, which does not use _Generic.

Diff Detail

Lint
Lint Skipped
Unit
Tests Skipped

Event Timeline

dougm requested review of this revision.Fri, May 17, 7:47 PM
bz added inline comments.
sys/compat/linuxkpi/common/include/linux/log2.h
54

What is the point of keeping this given libkern.h is included?

sys/sys/libkern.h
190

How does this work with the #define of the same name below?

dougm added inline comments.
sys/compat/linuxkpi/common/include/linux/log2.h
54

None. I'll remove it.

sys/sys/libkern.h
190

The preprocessor can replace 'ilog2' with 'ilog2', or 'ilog2l' or whatever.

dougm marked 2 inline comments as done.

Drop the ilog2 definition from log2.h.

Rename (function) ilog2 so that it differs from (macro) ilog2. Rename the other functions too, and make the parameter name the same across all ilog2 functions/macros.

sys/sys/libkern.h
220

IMO a better formatting here would be

#define ilog2_var(n)          \
        _Generic((n),         \
            default: ...,     \
            long: ...,        \
            ...
225

Indentation here and on following lines should be by four spaces.

Does this microoptimization help in practice? I'd naively expect the compiler's constant folding to be smart enough to make this redundant.

dougm marked 2 inline comments as done.

Address whitespace issues.

sys/sys/libkern.h
225

If ilog2 doesn't offer this special path for constants, then lines that use ilog2 to initialize static variables won't compile:

And there is one:
sys/dev/mthca/mthca_main.c:static int log_mtts_per_seg = ilog2(MTHCA_MTT_SEG_SIZE / 8);

sys/sys/libkern.h
225

I could edit sys/dev/mthca* so that it didn't need a compile-time evaluation of ilog2, and hope that no one ever wants that feature again, and drop the 64 or so cases from the ilog2 macro. Tell me how you want me to proceed.

sys/sys/libkern.h
225

LinuxKPI (and drivers) rely on this I do believe. If you remove it we need to deal with LinuxKPI differently and likely have a duplicate and extra #ifdefs around the libkern version.
Maybe add a comment as-to why it is there pointing at LinuxKPI and just leave it?

Seems fine to me with a comment explaining the compile-time tests.

sys/sys/libkern.h
225

Yes, I think a comment explaining why we do this would be useful.

As an aside, mthca is obsolete at this point and could safely be removed. It was already long in the tooth when I last worked with it ~8 years ago.

This revision is now accepted and ready to land.Tue, May 21, 2:57 PM
sys/dev/mana/gdma_util.h
176

Are you convinced that the code that uses this broken version of ilog2 isn't dependent on the brokenness?

sys/dev/mana/gdma_util.h
176

I can find ilog2 used the same way in our gdma_main.c as in gdma_main.c in linux. I can find an ilog2 definition in linux that does the sensible thing. I cannot prove that there's no broken ilog2 implementation hidden in linux that broken in the same way as this one, and used by their gdma_main.c code. I can say that the gdma_main.c in linux is not in the same directory as a header file like (our) gdma_util.h that defines a bad ilog2.

At the same time, I don't know this code. The committer of this code has not responded to my inquiry. I've just reached out to those who reviewed this code before it was committed, in hope that one of them might provide a more confident assessment.

lwhsu added inline comments.
sys/dev/mana/gdma_util.h
176

@whu @schakrabarti_microsoft.com can you help check if mana(4) can use global ilog2?

sys/dev/mana/gdma_util.h
176

I will check and confirm.

sys/dev/mana/gdma_util.h
176

@schakrabarti_microsoft.com can you let me know when confirmation will be available?

sys/dev/mana/gdma_util.h
176

@dougm I will share it by 3rd June.