Re: [PATCH 02/12] kasan, mm: optimize kmalloc poisoning
From: Marco Elver <elver@google.com>
Date: 2021-02-02 17:43:50
Also in:
linux-arm-kernel, lkml
On Tue, 2 Feb 2021 at 18:16, Andrey Konovalov [off-list ref] wrote:
On Tue, Feb 2, 2021 at 5:25 PM Marco Elver [off-list ref] wrote:quoted
quoted
+#ifdef CONFIG_KASAN_GENERIC + +/** + * kasan_poison_last_granule - mark the last granule of the memory range as + * unaccessible + * @addr - range start address, must be aligned to KASAN_GRANULE_SIZE + * @size - range size + * + * This function is only available for the generic mode, as it's the only mode + * that has partially poisoned memory granules. + */ +void kasan_poison_last_granule(const void *address, size_t size); + +#else /* CONFIG_KASAN_GENERIC */ + +static inline void kasan_poison_last_granule(const void *address, size_t size) { }^quoted
quoted
+ +#endif /* CONFIG_KASAN_GENERIC */ + /* * Exported functions for interfaces called from assembly or from generated * code. Declarations here to avoid warning about missing declarations.quoted
quoted
@@ -96,6 +92,16 @@ void kasan_poison(const void *address, size_t size, u8 value) } EXPORT_SYMBOL(kasan_poison); +#ifdef CONFIG_KASAN_GENERIC +void kasan_poison_last_granule(const void *address, size_t size) +{ + if (size & KASAN_GRANULE_MASK) { + u8 *shadow = (u8 *)kasan_mem_to_shadow(address + size); + *shadow = size & KASAN_GRANULE_MASK; + } +} +#endifThe function declaration still needs to exist in the dead branch if !IS_ENABLED(CONFIG_KASAN_GENERIC). It appears in that case it's declared (in kasan.h), but not defined. We shouldn't get linker errors because the optimizer should remove the dead branch. Nevertheless, is this code generally acceptable?The function is defined as empty when !CONFIG_KASAN_GENERIC, see above.
I missed that, thanks. Reviewed-by: Marco Elver <elver@google.com>