From c74a3041f019d6492ff0cd4d46d694fc0262e65f Mon Sep 17 00:00:00 2001 From: Conrad Meyer Date: Fri, 3 Jul 2020 14:54:46 +0000 Subject: [PATCH] Add domain policy allocation for amd64 fpu_kern_ctx Like other types of allocation, fpu_kern_ctx are frequently allocated per-cpu. Provide the API and sketch some example consumers. fpu_kern_alloc_ctx_domain() preferentially allocates memory from the provided domain, and falls back to other domains if that one is empty (DOMAINSET_PREF(domain) policy). Maybe it makes more sense to just shove one of these in the DPCPU area sooner or later -- left for future work. Reviewed by: markj Differential Revision: https://reviews.freebsd.org/D22053 --- sys/amd64/amd64/fpu.c | 31 +++++++++++++++++++++------- sys/amd64/include/fpu.h | 1 + sys/crypto/aesni/aesni.c | 7 ++++++- sys/crypto/blake2/blake2_cryptodev.c | 7 ++++++- 4 files changed, 36 insertions(+), 10 deletions(-) diff --git a/sys/amd64/amd64/fpu.c b/sys/amd64/amd64/fpu.c index b554394a17b..53819131e5b 100644 --- a/sys/amd64/amd64/fpu.c +++ b/sys/amd64/amd64/fpu.c @@ -38,6 +38,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -1030,17 +1031,31 @@ struct fpu_kern_ctx { char hwstate1[]; }; +static inline size_t __pure2 +fpu_kern_alloc_sz(u_int max_est) +{ + return (sizeof(struct fpu_kern_ctx) + XSAVE_AREA_ALIGN + max_est); +} + +static inline int __pure2 +fpu_kern_malloc_flags(u_int fpflags) +{ + return (((fpflags & FPU_KERN_NOWAIT) ? M_NOWAIT : M_WAITOK) | M_ZERO); +} + +struct fpu_kern_ctx * +fpu_kern_alloc_ctx_domain(int domain, u_int flags) +{ + return (malloc_domainset(fpu_kern_alloc_sz(cpu_max_ext_state_size), + M_FPUKERN_CTX, DOMAINSET_PREF(domain), + fpu_kern_malloc_flags(flags))); +} + struct fpu_kern_ctx * fpu_kern_alloc_ctx(u_int flags) { - struct fpu_kern_ctx *res; - size_t sz; - - sz = sizeof(struct fpu_kern_ctx) + XSAVE_AREA_ALIGN + - cpu_max_ext_state_size; - res = malloc(sz, M_FPUKERN_CTX, ((flags & FPU_KERN_NOWAIT) ? - M_NOWAIT : M_WAITOK) | M_ZERO); - return (res); + return (malloc(fpu_kern_alloc_sz(cpu_max_ext_state_size), + M_FPUKERN_CTX, fpu_kern_malloc_flags(flags))); } void diff --git a/sys/amd64/include/fpu.h b/sys/amd64/include/fpu.h index 6b9782e7207..8f957eede39 100644 --- a/sys/amd64/include/fpu.h +++ b/sys/amd64/include/fpu.h @@ -71,6 +71,7 @@ int fputrap_sse(void); int fputrap_x87(void); void fpuuserinited(struct thread *td); struct fpu_kern_ctx *fpu_kern_alloc_ctx(u_int flags); +struct fpu_kern_ctx *fpu_kern_alloc_ctx_domain(int domain, u_int flags); void fpu_kern_free_ctx(struct fpu_kern_ctx *ctx); void fpu_kern_enter(struct thread *td, struct fpu_kern_ctx *ctx, u_int flags); diff --git a/sys/crypto/aesni/aesni.c b/sys/crypto/aesni/aesni.c index 42973b50718..3a5a9e0fb1f 100644 --- a/sys/crypto/aesni/aesni.c +++ b/sys/crypto/aesni/aesni.c @@ -180,7 +180,12 @@ aesni_attach(device_t dev) M_WAITOK|M_ZERO); CPU_FOREACH(i) { - ctx_fpu[i] = fpu_kern_alloc_ctx(0); +#ifdef __amd64__ + ctx_fpu[i] = fpu_kern_alloc_ctx_domain( + pcpu_find(i)->pc_domain, FPU_KERN_NORMAL); +#else + ctx_fpu[i] = fpu_kern_alloc_ctx(FPU_KERN_NORMAL); +#endif mtx_init(&ctx_mtx[i], "anifpumtx", NULL, MTX_DEF|MTX_NEW); } diff --git a/sys/crypto/blake2/blake2_cryptodev.c b/sys/crypto/blake2/blake2_cryptodev.c index c374be28ea1..fcb3ec56059 100644 --- a/sys/crypto/blake2/blake2_cryptodev.c +++ b/sys/crypto/blake2/blake2_cryptodev.c @@ -142,7 +142,12 @@ blake2_attach(device_t dev) M_WAITOK | M_ZERO); CPU_FOREACH(i) { - ctx_fpu[i] = fpu_kern_alloc_ctx(0); +#ifdef __amd64__ + ctx_fpu[i] = fpu_kern_alloc_ctx_domain( + pcpu_find(i)->pc_domain, FPU_KERN_NORMAL); +#else + ctx_fpu[i] = fpu_kern_alloc_ctx(FPU_KERN_NORMAL); +#endif mtx_init(&ctx_mtx[i], "bl2fpumtx", NULL, MTX_DEF | MTX_NEW); }