Skip to content

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

Open
clingfei wants to merge 3 commits into
lkl:masterfrom
clingfei:dma-fix
Open

lkl: pci: Fix freeing DMA allocation with CPU address#637
clingfei wants to merge 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 23s ⏱️ +26s
205 tests +1  194 ✅ +1  11 💤 ±0  0 ❌ ±0 
822 runs  +1  766 ✅ +1  56 💤 ±0  0 ❌ ±0 

Results for commit 623bb9c. ± 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

#define KASAN_CMD_LINE "kunit.filter_glob=kasan* "
#define KASAN_CMD_LINE

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.

if we remove that kasan filter, this macro variable is always empty, no ?
we can remove it if necessary.

(I guess the original intention of this filter is to shorten the test exec time)
Cc: @rodionov

Copy link
Copy Markdown
Author

@clingfei clingfei May 26, 2026

Choose a reason for hiding this comment

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

if we remove that kasan filter, this macro variable is always empty, no ? we can remove it if necessary.

Yes, those command-line macros are always empty now. Removing them does not affect the test. Thus, I removed them in the latest commit.

However, the CI failed. These errors seem to be network-related and unrelated to my recent changes.

Comment thread tools/lkl/tests/boot.c Outdated
#define LKL_MMU_TEST_CMD_LINE "kunit.filter_glob=lkl_mmu "
#define LKL_MMU_TEST_CMD_LINE
#else
#define LKL_MMU_TEST_CMD_LINE
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.

ditto.
Cc: @rodionov

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>
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