Skip to content

kasan: support backing vmalloc space with real shadow memory

Daniel Axtens linux-mm linux-xfs linuxppc-dev lkml 11 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v11 Latest4 patches 18 repliesIn review
  2. v105 patches 10 repliesSuperseded
  3. v95 patches 0 repliesSuperseded
Earlier versions · 8
VersionPostedPatchesRepliesStatus
v85 patches18 repliesSuperseded
v75 patches5 repliesSuperseded
v65 patches3 repliesSuperseded
v55 patches1 replySuperseded
v43 patches8 repliesSuperseded
v33 patches6 repliesSuperseded
v23 patches3 repliesSuperseded
v13 patches16 repliesSuperseded

Patches in v11 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/4kasan: support backing vmalloc space with real shadow memoryDocumentation/dev-tools/kasan.rst include/linux/kasan.h include/linux/moduleloader.h +6 more+403 -917 replies
2/4kasan: add test for vmalloclib/test_kasan.c+26 -00 replies
3/4fork: support VMAP_STACK with KASAN_VMALLOCarch/Kconfig kernel/fork.c+9 -40 replies
4/4x86/kasan: support KASAN_VMALLOCarch/x86/Kconfig arch/x86/mm/kasan_init_64.c+62 -00 replies

Reviews trailers per patch

All 4 patches have a Reviewed-by or Acked-by.

PatchVasily GorbikAndrey RyabininDmitry Vyukov
1/4A-b R-b
2/4R-b
3/4R-b R-b
4/4R-b A-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes. Scroll the table sideways for all 3 reviewers.

Read the v11 thread

Cover letter [PATCH v11 0/4] kasan: support backing vmalloc space with real shadow memory

Currently, vmalloc space is backed by the early shadow page. This means that kasan is incompatible with VMAP_STACK.

This series provides a mechanism to back vmalloc space with real, dynamically allocated memory. I have only wired up x86, because that's the only currently supported arch I can work with easily, but it's very easy to wire up other architectures, and it appears that there is some work-in-progress code to do this on arm64 and s390.

This has been discussed before in the context of VMAP_STACK:
- https://bugzilla.kernel.org/show_bug.cgi?id=202009
- https://lkml.org/lkml/2018/7/22/198
- https://lkml.org/lkml/2019/7/19/822

In terms of implementation details:

Most mappings in vmalloc space are small, requiring less than a full page of shadow space. Allocating a full shadow page per mapping would therefore be wasteful. Furthermore, to ensure that different mappings use different shadow pages, mappings would have to be aligned to KASAN_SHADOW_SCALE_SIZE * PAGE_SIZE.

Instead, share backing space across multiple mappings. Allocate a backing page when a mapping in vmalloc space uses a particular page of the shadow region. This page can be shared by other vmalloc mappings later on.

We hook in to the vmap infrastructure to lazily clean up unused shadow memory.

Testing with test_vmalloc.sh on an x86 VM with 2 vCPUs shows that:

- Turning on KASAN, inline instrumentation, without vmalloc, introuduces a 4.1x-4.2x slowdown in vmalloc operations.

- Turning this on introduces the following slowdowns over KASAN:
* ~1.76x slower single-threaded (test_vmalloc.sh performance)
* ~2.18x slower when both cpus are performing operations simultaneously (test_vmalloc.sh sequential_test_order=1)

This is unfortunate but given that this is a debug feature only, not the end of the world. The benchmarks are also a stress-test for the vmalloc subsystem: they're not indicative of an overall 2x slowdown!

v1: https://lore.kernel.org/linux-mm/[email protected]/
v2: https://lore.kernel.org/linux-mm/[email protected]/
Address review comments:
- Patch 1: use kasan_unpoison_shadow's built-in handling of
ranges that do not align to a full shadow byte
- Patch 3: prepopulate pgds rather than faulting things in
v3: https://lore.kernel.org/linux-mm/[email protected]/
Address comments from Mark Rutland:
- kasan_populate_vmalloc is a better name
- handle concurrency correctly
- various nits and cleanups
- relax module alignment in KASAN_VMALLOC case
v4: https://lore.kernel.org/linux-mm/[email protected]/
Changes to patch 1 only:
- Integrate Mark's rework, thanks Mark!
- handle the case where kasan_populate_shadow might fail
- poision shadow on free, allowing the alloc path to just
unpoision memory that it uses
v5: https://lore.kernel.org/linux-mm/[email protected]/
Address comments from Christophe Leroy:
- Fix some issues with my descriptions in commit messages and docs
- Dynamically free unused shadow pages by hooking into the vmap book-keeping
- Split out the test into a separate patch
- Optional patch to track the number of pages allocated
- minor checkpatch cleanups
v6: https://lore.kernel.org/linux-mm/[email protected]/
Properly guard freeing pages in patch 1, drop debugging code.
v7: https://lore.kernel.org/linux-mm/[email protected]/
Add a TLB flush on freeing, thanks Mark Rutland.
Explain more clearly how I think freeing is concurrency-safe.
v8: https://lore.kernel.org/linux-mm/[email protected]/
rename kasan_vmalloc/shadow_pages to kasan/vmalloc_shadow_pages
v9: https://lore.kernel.org/linux-mm/[email protected]/
(attempt to) address a number of review comments for patch 1.
v10: https://lore.kernel.org/linux-mm/[email protected]/
- rebase on linux-next, pulling in Vlad's new work on splitting the vmalloc locks.
- after much discussion of barriers, document where I think they are needed and why. Thanks Mark and Andrey.
- clean up some TLB flushing and checkpatch bits
v11: Address review comments from Andrey and Vlad, drop patch 5, add benchmarking
results.

Daniel Axtens (4):
kasan: support backing vmalloc space with real shadow memory
kasan: add test for vmalloc
fork: support VMAP_STACK with KASAN_VMALLOC x86/kasan: support KASAN_VMALLOC

 Documentation/dev-tools/kasan.rst |  63 ++++++++
 arch/Kconfig                      |   9 +-
 arch/x86/Kconfig                  |   1 +
 arch/x86/mm/kasan_init_64.c       |  61 ++++++++
 include/linux/kasan.h             |  31 ++++
 include/linux/moduleloader.h      |   2 +-
 include/linux/vmalloc.h           |  12 ++
 kernel/fork.c                     |   4 +
 lib/Kconfig.kasan                 |  16 +++
 lib/test_kasan.c                  |  26 ++++
 mm/kasan/common.c                 | 231 ++++++++++++++++++++++++++++++
 mm/kasan/generic_report.c         |   3 +
 mm/kasan/kasan.h                  |   1 +
 mm/vmalloc.c                      |  53 +++++--
 14 files changed, 500 insertions(+), 13 deletions(-)
Signature · 1 line
2.20.1