1

I want to build an out-of-tree driver using a kernel built from the latest mainline source code. I use localmodconfig when building, which reduces the number of modules and kernel symbols exported to match devices available on the system, per my understanding. The out-of-tree driver requires symbols that aren't used by other components of the kernel, so I think I need to specify the symbols to be exported manually according to this conversation about TRIM_UNUSED_KSYMS and the whitelist.

My .config looks like this:

...
CONFIG_TRIM_UNUSED_KSYMS=y
CONFIG_UNUSED_KSYMS_WHITELIST="ksyms-whitelist"
...

And I created ksyms-whitelist file in the root of the kernel source directory:

vb2_queue_init
vb2_dma_sg_memops
video_ioctl2
...

The driver builds when using a generic kernel with no problem (and the device works), but what do we need to do to build out-of-tree driver using symbols from a kernel built from source?

Edit: my whole .config

0

1 Answer 1

1

I suspect there’s some misunderstanding of what TRIM_UNUSED_KSYMS does. Its Kconfig description is as follows:

The kernel and some modules make many symbols available for other modules to use via EXPORT_SYMBOL() and variants. Depending on the set of modules being selected in your kernel configuration, many of those exported symbols might never be used.

This option allows for unused exported symbols to be dropped from the build. In turn, this provides the compiler more opportunities (especially when using LTO) for optimizing the code and reducing binary size. This might have some security advantages as well.

If unsure, or if you need to build out-of-tree modules, say N.

Note in particular the last sentence: in your scenario, you should disable TRIM_UNUSED_KSYMS, unless you have a really good reason to enable it (and deal with the fallout).

The overall set of available symbols in the kernel depends on the build configuration, before any symbol trimming occurs: a kernel whose configuration excludes a given feature will never have the corresponding symbols. In your case, your .config doesn’t include VIDEO_V4L2, which means kernels you build won’t ever have video_ioctl2, nor does it include VIDEOBUF_V4L2, which you need for vb2_init_queue, and VIDEOBUF2_DMA_SG, which you need for vb2_dma_sg_memops. Adding those symbols to TRIM_UNUSED_KSYMS’s whitelist won’t help: a symbol that isn’t present to start with can’t be added by not removing it.

To support building out-of-tree modules, you need to start by determining the symbols those modules will need, and enabling the necessary features in the kernels you build so that those symbols are available. If you then decide you still want to enable symbol trimming, you can do so, adding the necessary symbols to the whiltelist; but the latter is useless unless you get the first part right.

Generic distribution kernels enable most (if not all) subsystems, which allows out-of-tree modules to be built on top of them without foreknowledge of the features they need.

1
  • Thank you for this answer - it filled in some missing knowledge and I was able to identify some configuration options so that the symbols are included in my build :) Aug 17, 2021 at 2:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .