SIMD: Use alloc_pages_node to force alignment

fxsave and xsave require the target address to be 16-/64-byte aligned.

kmalloc(_node) does not (yet) offer such fine-grained control over
alignment[0,1], even though it does "the right thing" most of the time
for power-of-2 sizes. unfortunately, alignment is completely off when
using certain debugging or hardening features/configs, such as KASAN,
slub_debug=Z or the not-yet-upstream SLAB_CANARY.

Use alloc_pages_node() instead which allows us to allocate page-aligned
memory. Since fpregs_state is padded to a full page anyway, and this
code is only relevant for x86 which has 4k pages, this approach should
not allocate any unnecessary memory but still guarantee the needed
alignment.

0: https://lwn.net/Articles/787740/
1: https://lore.kernel.org/linux-block/20190826111627.7505-1-vbabka@suse.cz/

Reviewed-by: Tony Hutter <hutter2@llnl.gov>
Signed-off-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Closes #9608 
Closes #9674
This commit is contained in:
Fabian-Gruenbichler 2019-12-10 21:53:25 +01:00 committed by Brian Behlendorf
parent 362ae8d11f
commit b119e2c6f1

View File

@ -135,6 +135,8 @@
*/ */
#if defined(HAVE_KERNEL_FPU_INTERNAL) #if defined(HAVE_KERNEL_FPU_INTERNAL)
#include <linux/mm.h>
extern union fpregs_state **zfs_kfpu_fpregs; extern union fpregs_state **zfs_kfpu_fpregs;
/* /*
@ -147,7 +149,8 @@ kfpu_fini(void)
for_each_possible_cpu(cpu) { for_each_possible_cpu(cpu) {
if (zfs_kfpu_fpregs[cpu] != NULL) { if (zfs_kfpu_fpregs[cpu] != NULL) {
kfree(zfs_kfpu_fpregs[cpu]); free_pages((unsigned long)zfs_kfpu_fpregs[cpu],
get_order(sizeof (union fpregs_state)));
} }
} }
@ -157,20 +160,28 @@ kfpu_fini(void)
static inline int static inline int
kfpu_init(void) kfpu_init(void)
{ {
int cpu;
zfs_kfpu_fpregs = kzalloc(num_possible_cpus() * zfs_kfpu_fpregs = kzalloc(num_possible_cpus() *
sizeof (union fpregs_state *), GFP_KERNEL); sizeof (union fpregs_state *), GFP_KERNEL);
if (zfs_kfpu_fpregs == NULL) if (zfs_kfpu_fpregs == NULL)
return (-ENOMEM); return (-ENOMEM);
/*
* The fxsave and xsave operations require 16-/64-byte alignment of
* the target memory. Since kmalloc() provides no alignment
* guarantee instead use alloc_pages_node().
*/
unsigned int order = get_order(sizeof (union fpregs_state));
int cpu;
for_each_possible_cpu(cpu) { for_each_possible_cpu(cpu) {
zfs_kfpu_fpregs[cpu] = kmalloc_node(sizeof (union fpregs_state), struct page *page = alloc_pages_node(cpu_to_node(cpu),
GFP_KERNEL | __GFP_ZERO, cpu_to_node(cpu)); GFP_KERNEL | __GFP_ZERO, order);
if (zfs_kfpu_fpregs[cpu] == NULL) { if (page == NULL) {
kfpu_fini(); kfpu_fini();
return (-ENOMEM); return (-ENOMEM);
} }
zfs_kfpu_fpregs[cpu] = page_address(page);
} }
return (0); return (0);