Skip to content

mm/kasan: make kasan=on|off work for all three modes

Baoquan He linux-arm-kernel linux-mm linux-riscv linux-s390 linux-um linuxppc-dev lkml loongarch 5 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v5 Latest15 patches 7 repliesIn review
  2. v412 patches 19 repliesSuperseded
  3. v312 patches 17 repliesSuperseded
Earlier versions · 2
VersionPostedPatchesRepliesStatus
v212 patches10 repliesSuperseded
v14 patches16 repliesSuperseded

Changes in v5 from the cover letter

- Add helper __kasan_cache_shrink() in mm/kasan/generic.c so that the kasan_enabled() checking done in kasan_cache_shrink() which is in include/linux/kasan.h. This change is made in patch 1.
- Carve out the change of renaming 'kasan_arg' to 'kasan_arg_disabled' into a separate patch from the old patch 2.
- put the old patch 12 to earlier place as patch 4 in this sereis so that the ifdeffery scope embracing kasan_flag_enabled definition is meaningful and understandable.
- Remove the stale and incorrect comment above kasan_enabled() in the old patch 12.
- Add comment 'If KASAN is disabled via command line, don't initialize it.' to all places where kasan is initialized and kasan_arg_disabled is checked.
- Add document in kernel-parameters.txt to note kasan=on|off.
- Remove unneeded ARCH_DEFER_KASAN and kasan_arch_is_ready().
- All these changes are made according to reviewers' suggestion in v4, thanks to Andrey Konovalov, Andrey Ryabinin and Alexander Potapenko.

Patches in v5 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/15mm/kasan: add conditional checks in functions to return directly if kasan is disabledinclude/linux/kasan.h mm/kasan/generic.c mm/kasan/init.c +4 more+45 -50 replies
2/15mm/kasan: rename 'kasan_arg' to 'kasan_arg_disabled'mm/kasan/hw_tags.c+5 -110 replies
3/15mm/kasan: mm/kasan: move kasan= code to common placeinclude/linux/kasan-enabled.h mm/kasan/common.c mm/kasan/hw_tags.c+23 -180 replies
4/15mm/kasan: make kasan=on|off take effect for all three modesinclude/linux/kasan-enabled.h mm/kasan/common.c+5 -110 replies
5/15mm/kasan/sw_tags: don't initialize kasan if it's disabledmm/kasan/sw_tags.c+4 -00 replies
6/15arch/arm: don't initialize kasan if it's disabledarch/arm/kernel/setup.c arch/arm/mm/kasan_init.c+9 -00 replies
7/15arch/arm64: don't initialize kasan if it's disabledarch/arm64/mm/kasan_init.c+7 -00 replies
8/15arch/loongarch: don't initialize kasan if it's disabledarch/loongarch/mm/kasan_init.c+3 -00 replies
9/15arch/powerpc: don't initialize kasan if it's disabledarch/powerpc/mm/kasan/init_32.c arch/powerpc/mm/kasan/init_book3e_64.c arch/powerpc/mm/kasan/init_book3s_64.c+13 -14 replies
10/15arch/riscv: don't initialize kasan if it's disabledarch/riscv/mm/kasan_init.c+4 -00 replies
11/15arch/x86: don't initialize kasan if it's disabledarch/x86/mm/kasan_init_64.c+4 -00 replies
12/15arch/xtensa: don't initialize kasan if it's disabledarch/xtensa/kernel/setup.c arch/xtensa/mm/kasan_init.c+5 -00 replies
13/15arch/um: don't initialize kasan if it's disabledarch/um/kernel/mem.c+4 -10 replies
14/15mm/kasan: add document into kernel-parameters.txtDocumentation/admin-guide/kernel-parameters.txt Documentation/dev-tools/kasan.rst+4 -23 replies
15/15mm/kasan: clean up unneeded ARCH_DEFER_KASAN and kasan_arch_is_readyarch/loongarch/Kconfig arch/powerpc/Kconfig arch/um/Kconfig +2 more+0 -210 replies

Reviews trailers per patch

1 of 15 patches has a Reviewed-by or Acked-by.

PatchSourabh JainSamuel Holland
1/15
2/15
3/15
4/15
5/15
6/15
7/15
8/15
9/15T-b
10/15
11/15
12/15
13/15
14/15R-b
15/15

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v5 thread

Cover letter [PATCH v5 00/15] mm/kasan: make kasan=on|off work for all three modes

Currently only hw_tags mode of kasan can be enabled or disabled with kernel parameter kasan=on|off for built kernel. For kasan generic and sw_tags mode, there's no way to disable them once kernel is built.

This is not convenient sometime, e.g in system kdump is configured. When the 1st kernel has KASAN enabled and crash triggered to switch to kdump kernel, the generic or sw_tags mode will cost much extra memory while in fact it's meaningless to have kasan in kdump kernel

There are two parts of big amount of memory requiring for kasan enabed kernel. One is the direct memory mapping shadow of kasan, which is 1/8 of system RAM in generic mode and 1/16 of system RAM in sw_tags mode; the other is the shadow meomry for vmalloc which causes big meomry usage in kdump kernel because of lazy vmap freeing. By introducing "kasan=off|on", if we specify 'kasan=off', the former is avoided by skipping the kasan_init(), and the latter is avoided by not building the vmalloc shadow for vmalloc.

So this patchset moves the kasan=on|off out of hw_tags scope and into common code to make it visible in generic and sw_tags mode too. Then we can add kasan=off in kdump kernel to reduce the unneeded meomry cost for kasan.

Note that this hasn't been supported on s390 since I am not familiar with s390 code. Hope s390 developer will make it work.

Testing:
========
Testing is done on upstream kernel 6.19.0+:

- For generic mode, testing is taken on below systems and passed.
- x86_64 baremetal system
- aarch64 baremetal system
- ppc64le baremetal system (Model 9183-22X)
- risc-v kvm guest

- For sw_tags mode, testing is taken on below system and passed.
- aarch64 baremetal system

- For hw_tags mode, testing is taken on below system and passed.
- aarch64 kvm guest with "-machine virt,mte=on -cpu max" qemu command.

Changelog:
====
v4->v5:
- Add helper __kasan_cache_shrink() in mm/kasan/generic.c so that the kasan_enabled() checking done in kasan_cache_shrink() which is in include/linux/kasan.h. This change is made in patch 1.
- Carve out the change of renaming 'kasan_arg' to 'kasan_arg_disabled' into a separate patch from the old patch 2.
- put the old patch 12 to earlier place as patch 4 in this sereis so that the ifdeffery scope embracing kasan_flag_enabled definition is meaningful and understandable.
- Remove the stale and incorrect comment above kasan_enabled() in the old patch 12.
- Add comment 'If KASAN is disabled via command line, don't initialize it.' to all places where kasan is initialized and kasan_arg_disabled is checked.
- Add document in kernel-parameters.txt to note kasan=on|off.
- Remove unneeded ARCH_DEFER_KASAN and kasan_arch_is_ready().
- All these changes are made according to reviewers' suggestion in v4, thanks to Andrey Konovalov, Andrey Ryabinin and Alexander Potapenko.

v3->v4:
- Rebase code to the latest linux-next/master to make the whole patchset set on top of
[PATCH 0/2] kasan: cleanups for kasan_enabled() checks
[PATCH v6 0/2] kasan: unify kasan_enabled() and remove arch-specific implementations

v2->v3:
- Fix a building error on UML ARCH when CONFIG_KASAN is not set. The change of fixing is appended into patch patch 11. This is reported by LKP, thanks to them.

v1->v2:
- Add __ro_after_init for kasan_arg_disabled, and remove redundant blank lines in mm/kasan/common.c. Thanks to Marco.
- Fix a code bug in <linux/kasan-enabled.h> when CONFIG_KASAN is unset, this is found out by SeongJae and Lorenzo, and also reported by LKP report, thanks to them.
- Add a missing kasan_enabled() checking in kasan_report(). This will cause a KASAN report info even though kasan=off is set:
- Add jump_label_init() calling before kasan_init() in setup_arch() in these
architectures: xtensa, arm. Because they currenly rely on jump_label_init() in main() which is a little late. Then the early static key kasan_flag_enabled in kasan_init() won't work.
- In UML architecture, change to enable kasan_flag_enabled in arch_mm_preinit() because kasan_init() is enabled before main(), there's no chance to operate on static key in kasan_init().

Baoquan He (15):
mm/kasan: add conditional checks in functions to return directly if
kasan is disabled
mm/kasan: rename 'kasan_arg' to 'kasan_arg_disabled' mm/kasan: mm/kasan: move kasan= code to common place mm/kasan: make kasan=on|off take effect for all three modes mm/kasan/sw_tags: don't initialize kasan if it's disabled arch/arm: don't initialize kasan if it's disabled arch/arm64: don't initialize kasan if it's disabled arch/loongarch: don't initialize kasan if it's disabled arch/powerpc: don't initialize kasan if it's disabled arch/riscv: don't initialize kasan if it's disabled arch/x86: don't initialize kasan if it's disabled arch/xtensa: don't initialize kasan if it's disabled arch/um: don't initialize kasan if it's disabled mm/kasan: add document into kernel-parameters.txt mm/kasan: clean up unneeded ARCH_DEFER_KASAN and kasan_arch_is_ready

 .../admin-guide/kernel-parameters.txt         |  4 +++
 Documentation/dev-tools/kasan.rst             |  2 --
 arch/arm/kernel/setup.c                       |  6 ++++
 arch/arm/mm/kasan_init.c                      |  3 ++
 arch/arm64/mm/kasan_init.c                    |  7 +++++
 arch/loongarch/Kconfig                        |  1 -
 arch/loongarch/mm/kasan_init.c                |  3 ++
 arch/powerpc/Kconfig                          |  1 -
 arch/powerpc/mm/kasan/init_32.c               |  6 +++-
 arch/powerpc/mm/kasan/init_book3e_64.c        |  4 +++
 arch/powerpc/mm/kasan/init_book3s_64.c        |  4 +++
 arch/riscv/mm/kasan_init.c                    |  4 +++
 arch/um/Kconfig                               |  1 -
 arch/um/kernel/mem.c                          |  5 +++-
 arch/x86/mm/kasan_init_64.c                   |  4 +++
 arch/xtensa/kernel/setup.c                    |  1 +
 arch/xtensa/mm/kasan_init.c                   |  4 +++
 include/linux/kasan-enabled.h                 | 10 +++----
 include/linux/kasan.h                         |  7 ++++-
 lib/Kconfig.kasan                             | 12 --------
 mm/kasan/common.c                             | 21 ++++++++++++--
 mm/kasan/generic.c                            | 16 +++++++++--
 mm/kasan/hw_tags.c                            | 28 ++-----------------
 mm/kasan/init.c                               |  6 ++++
 mm/kasan/kasan.h                              |  6 ----
 mm/kasan/quarantine.c                         |  3 ++
 mm/kasan/report.c                             |  4 ++-
 mm/kasan/shadow.c                             | 11 +++++++-
 mm/kasan/sw_tags.c                            |  7 +++++
 29 files changed, 128 insertions(+), 63 deletions(-)
Signature · 1 line
2.52.0