Skip to content

kpatch-build: Add support for module symbol namespaces#1467

Merged
joe-lawrence merged 3 commits intodynup:masterfrom
joe-lawrence:module-namespaces
Jul 8, 2025
Merged

kpatch-build: Add support for module symbol namespaces#1467
joe-lawrence merged 3 commits intodynup:masterfrom
joe-lawrence:module-namespaces

Conversation

@joe-lawrence
Copy link
Copy Markdown
Contributor

Roberto Bergantinos Corpas rbergant@redhat.com reported a problem when building a kpatch for:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/patch/?id=021ba7f1babd029e714d13a6bf2571b08af96d0f

MODPOST /root/.kpatch/tmp/patch/Module.symvers
ERROR: modpost: module livepatch-021ba7f1babd029e714d13a6bf2571b08af96d0f uses symbol dma_buf_export from namespace DMA_BUF, but does not import it.
ERROR: modpost: module livepatch-021ba7f1babd029e714d13a6bf2571b08af96d0f uses symbol dma_buf_put from namespace DMA_BUF, but does not import it.
ERROR: modpost: module livepatch-021ba7f1babd029e714d13a6bf2571b08af96d0f uses symbol dma_buf_fd from namespace DMA_BUF, but does not import it.

The problem is that the livepatch needs to access symbols exported via the "DMA_BUF" symbol namespace. A workaround is to add MODULE_IMPORT_NS("DMA_BUF") to the kpatch, but then kpatch-build complains about unhandled .modinfo changes.

Update kpatch-build to allow .modinfo changes and extract the import_ns=<value> strings from .modinfo for inclusion in the output ELF object file. Coupled with MODULE_IMPORT_NS("DMA_BUF"), this allows kpatch to build a livepatch that accesses a module symbol namespace.

All previous callers needed a corresponding .rela section, but the
following patch just needs to create a .modinfo section.  Split
create_section_pair() into two parts: create the base section
seperately, then add the relocation section for those who request it.

Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
@joe-lawrence
Copy link
Copy Markdown
Contributor Author

(I pushed up an example obj test case here: dynup/kpatch-unit-test-objs#53 It's x86-only, but that's fine as this feature isn't architecture specific. This MR can be updated to move forward the submodule reference when that MR is approved.)

{
struct section *sec;

sec = create_section(kelf_out, ".modinfo",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be skipped if .modinfo doesn't exist in the patched object?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, no need to create an empty section.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I suppose it should only create .modinfo if kelf_patched->modinfo_data is non-NULL?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, that's a better test, as it would skip instances with .modinfo but no imports.

@joe-lawrence
Copy link
Copy Markdown
Contributor Author

v2:

  • Don't add .modinfo section to output ELF if it didn't exist in the patched ELF
  • Bump unit test submodule to include module namespace test

Kernel module namespace imports are saved in the .modinfo section in
key-value strings.  In order for a livepatch to access those symbols in
those namespaces, it needs to provide the same imports else modpost
refuses to process it.  For example:

  ERROR: modpost: module livepatch-test uses symbol dma_buf_export from namespace DMA_BUF, but does not import it.

We don't need the entire .modinfo section, as it carries other key-value
pairs that we are not interested in.  Selectively extract all of the
"import_ns=<value" strings and then restore them in the ELF output for
inclusion in the livepatch .ko.

Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
@joe-lawrence
Copy link
Copy Markdown
Contributor Author

v3:

  • Verify that the modinfo data is non-NULL before creating and copying .modinfo section (Josh)

@joe-lawrence joe-lawrence merged commit 21dad4d into dynup:master Jul 8, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants