Skip to content

lkl: pci: Fix freeing DMA allocation with CPU address#637

Merged
thehajime merged 3 commits into
lkl:masterfrom
clingfei:dma-fix
May 27, 2026
Merged

lkl: pci: Fix freeing DMA allocation with CPU address#637
thehajime merged 3 commits into
lkl:masterfrom
clingfei:dma-fix

Conversation

@clingfei
Copy link
Copy Markdown

lkl_dma_alloc() returns a kernel virtual address derived from the allocated page. The DMA ops free callback receives that same CPU address, but lkl_dma_free() passed it directly to __free_pages(), which expects a struct page pointer.

Convert the CPU address with virt_to_page() before freeing it.

Add an LKL PCI KUnit test that exercises dma_alloc_attrs() and dma_free_attrs() through stub PCI host ops, and wire it into the LKL test configuration via PCI_KUNIT.

lkl_dma_alloc() returns a kernel virtual address derived from the
allocated page. The DMA ops free callback receives that same CPU address,
but lkl_dma_free() passed it directly to __free_pages(), which expects a
struct page pointer.

Convert the CPU address with virt_to_page() before freeing it.

Add an LKL PCI KUnit test that exercises dma_alloc_attrs() and
dma_free_attrs() through stub PCI host ops, and wire it into the LKL test
configuration via PCI_KUNIT.

Signed-off-by: Cheng Lingfei <chenglingfei@foxmail.com>
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 25, 2026

Test Results

106 files  ±0  106 suites  ±0   7m 37s ⏱️ +40s
205 tests +1  194 ✅ +1  11 💤 ±0  0 ❌ ±0 
822 runs  +1  766 ✅ +1  56 💤 ±0  0 ❌ ±0 

Results for commit 43bd9f8. ± Comparison against base commit fa134a0.

♻️ This comment has been updated with latest results.

Copy link
Copy Markdown
Member

@tavip tavip left a comment

Choose a reason for hiding this comment

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

Looks great, thank you @clingfei ! I have left a comment inline, it would be great if you can refactor a bit to make adding kunit tests more scalable.

Comment thread tools/lkl/Makefile.autoconf Outdated
We also enable kunit test in CI, and remove kunit.filter_glob=
in CONFIG_BUILTIN_CMDLINE to avoid the kasan and pci test be
filtered out.

Signed-off-by: Cheng Lingfei <chenglingfei@foxmail.com>
@thehajime
Copy link
Copy Markdown
Member

thanks @clingfei !
Just curious how you detected this issue. Are you using CONFIG_MMU=y case ?

@clingfei
Copy link
Copy Markdown
Author

thanks @clingfei ! Just curious how you detected this issue. Are you using CONFIG_MMU=y case ?

Yes, I am using CONFIG_MMU=y. I wanted to see how LKL supports DMA. Then I realized that this might be a confusion between __free_pages and free_pages.

Comment thread tools/lkl/tests/boot.c Outdated
Comment thread tools/lkl/tests/boot.c Outdated
As we have already removed the filter, the KASAN_CMD_LINE and other
macros are always empty. Thus we remove them.

Signed-off-by: Cheng Lingfei <chenglingfei@foxmail.com>
Copy link
Copy Markdown
Member

@tavip tavip left a comment

Choose a reason for hiding this comment

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

LGTM, thanks @clingfei !

@thehajime thehajime merged commit b6379a8 into lkl:master May 27, 2026
13 of 20 checks passed
@thehajime
Copy link
Copy Markdown
Member

thanks !

@clingfei clingfei deleted the dma-fix branch May 27, 2026 05:41
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.

3 participants