diff options
author | kib <kib@FreeBSD.org> | 2014-06-23 07:37:54 +0000 |
---|---|---|
committer | kib <kib@FreeBSD.org> | 2014-06-23 07:37:54 +0000 |
commit | fe547198b1973809825a6d6e48d12cf6f1c93741 (patch) | |
tree | a51002de5281f8a64709f9e315f0f0337d66108c | |
parent | c2a4e94982dc22b2ff3eb85b284839f96f217faf (diff) | |
download | FreeBSD-src-fe547198b1973809825a6d6e48d12cf6f1c93741.zip FreeBSD-src-fe547198b1973809825a6d6e48d12cf6f1c93741.tar.gz |
Add FPU_KERN_KTHR flag to fpu_kern_enter(9), which avoids saving FPU
context into memory for the kernel threads which called
fpu_kern_thread(9). This allows the fpu_kern_enter() callers to not
check for is_fpu_kern_thread() to get the optimization.
Apply the flag to padlock(4) and aesni(4). In aesni_cipher_process(),
do not leak FPU context state on error.
Sponsored by: The FreeBSD Foundation
MFC after: 1 week
-rw-r--r-- | sys/amd64/amd64/fpu.c | 8 | ||||
-rw-r--r-- | sys/amd64/include/fpu.h | 1 | ||||
-rw-r--r-- | sys/crypto/aesni/aesni_wrap.c | 43 | ||||
-rw-r--r-- | sys/crypto/via/padlock.c | 15 | ||||
-rw-r--r-- | sys/crypto/via/padlock_cipher.c | 16 | ||||
-rw-r--r-- | sys/crypto/via/padlock_hash.c | 14 | ||||
-rw-r--r-- | sys/i386/include/npx.h | 1 | ||||
-rw-r--r-- | sys/i386/isa/npx.c | 7 |
8 files changed, 45 insertions, 60 deletions
diff --git a/sys/amd64/amd64/fpu.c b/sys/amd64/amd64/fpu.c index d2d506a..0f4b2b5 100644 --- a/sys/amd64/amd64/fpu.c +++ b/sys/amd64/amd64/fpu.c @@ -890,6 +890,7 @@ static MALLOC_DEFINE(M_FPUKERN_CTX, "fpukern_ctx", "Kernel contexts for FPU state"); #define FPU_KERN_CTX_FPUINITDONE 0x01 +#define FPU_KERN_CTX_DUMMY 0x02 /* avoided save for the kern thread */ struct fpu_kern_ctx { struct savefpu *prev; @@ -933,6 +934,10 @@ fpu_kern_enter(struct thread *td, struct fpu_kern_ctx *ctx, u_int flags) { struct pcb *pcb; + if ((flags & FPU_KERN_KTHR) != 0 && is_fpu_kern_thread(0)) { + ctx->flags = FPU_KERN_CTX_DUMMY; + return (0); + } pcb = td->td_pcb; KASSERT(!PCB_USER_FPU(pcb) || pcb->pcb_save == get_pcb_user_save_pcb(pcb), ("mangled pcb_save")); @@ -952,6 +957,9 @@ fpu_kern_leave(struct thread *td, struct fpu_kern_ctx *ctx) { struct pcb *pcb; + if (is_fpu_kern_thread(0) && (ctx->flags & FPU_KERN_CTX_DUMMY) != 0) + return (0); + KASSERT((ctx->flags & FPU_KERN_CTX_DUMMY) == 0, ("dummy ctx")); pcb = td->td_pcb; critical_enter(); if (curthread == PCPU_GET(fpcurthread)) diff --git a/sys/amd64/include/fpu.h b/sys/amd64/include/fpu.h index ef5d623..be1bdc6 100644 --- a/sys/amd64/include/fpu.h +++ b/sys/amd64/include/fpu.h @@ -84,6 +84,7 @@ void fpu_save_area_reset(struct savefpu *fsa); */ #define FPU_KERN_NORMAL 0x0000 #define FPU_KERN_NOWAIT 0x0001 +#define FPU_KERN_KTHR 0x0002 #endif diff --git a/sys/crypto/aesni/aesni_wrap.c b/sys/crypto/aesni/aesni_wrap.c index 4f5acf3..0a6193b 100644 --- a/sys/crypto/aesni/aesni_wrap.c +++ b/sys/crypto/aesni/aesni_wrap.c @@ -382,22 +382,16 @@ int aesni_cipher_setup(struct aesni_session *ses, struct cryptoini *encini) { struct thread *td; - int error, saved_ctx; + int error; td = curthread; - if (!is_fpu_kern_thread(0)) { - error = fpu_kern_enter(td, ses->fpu_ctx, FPU_KERN_NORMAL); - saved_ctx = 1; - } else { - error = 0; - saved_ctx = 0; - } - if (error == 0) { - error = aesni_cipher_setup_common(ses, encini->cri_key, - encini->cri_klen); - if (saved_ctx) - fpu_kern_leave(td, ses->fpu_ctx); - } + error = fpu_kern_enter(td, ses->fpu_ctx, FPU_KERN_NORMAL | + FPU_KERN_KTHR); + if (error != 0) + return (error); + error = aesni_cipher_setup_common(ses, encini->cri_key, + encini->cri_klen); + fpu_kern_leave(td, ses->fpu_ctx); return (error); } @@ -407,22 +401,17 @@ aesni_cipher_process(struct aesni_session *ses, struct cryptodesc *enccrd, { struct thread *td; uint8_t *buf; - int error, allocated, saved_ctx; + int error, allocated; buf = aesni_cipher_alloc(enccrd, crp, &allocated); if (buf == NULL) return (ENOMEM); td = curthread; - if (!is_fpu_kern_thread(0)) { - error = fpu_kern_enter(td, ses->fpu_ctx, FPU_KERN_NORMAL); - if (error != 0) - goto out; - saved_ctx = 1; - } else { - saved_ctx = 0; - error = 0; - } + error = fpu_kern_enter(td, ses->fpu_ctx, FPU_KERN_NORMAL | + FPU_KERN_KTHR); + if (error != 0) + goto out1; if ((enccrd->crd_flags & CRD_F_KEY_EXPLICIT) != 0) { error = aesni_cipher_setup_common(ses, enccrd->crd_key, @@ -460,8 +449,6 @@ aesni_cipher_process(struct aesni_session *ses, struct cryptodesc *enccrd, ses->iv); } } - if (saved_ctx) - fpu_kern_leave(td, ses->fpu_ctx); if (allocated) crypto_copyback(crp->crp_flags, crp->crp_buf, enccrd->crd_skip, enccrd->crd_len, buf); @@ -469,7 +456,9 @@ aesni_cipher_process(struct aesni_session *ses, struct cryptodesc *enccrd, crypto_copydata(crp->crp_flags, crp->crp_buf, enccrd->crd_skip + enccrd->crd_len - AES_BLOCK_LEN, AES_BLOCK_LEN, ses->iv); - out: +out: + fpu_kern_leave(td, ses->fpu_ctx); +out1: if (allocated) { bzero(buf, enccrd->crd_len); free(buf, M_AESNI); diff --git a/sys/crypto/via/padlock.c b/sys/crypto/via/padlock.c index f601d2a..65a7b5d 100644 --- a/sys/crypto/via/padlock.c +++ b/sys/crypto/via/padlock.c @@ -171,7 +171,7 @@ padlock_newsession(device_t dev, uint32_t *sidp, struct cryptoini *cri) struct padlock_session *ses = NULL; struct cryptoini *encini, *macini; struct thread *td; - int error, saved_ctx; + int error; if (sidp == NULL || cri == NULL) return (EINVAL); @@ -246,18 +246,11 @@ padlock_newsession(device_t dev, uint32_t *sidp, struct cryptoini *cri) if (macini != NULL) { td = curthread; - if (!is_fpu_kern_thread(0)) { - error = fpu_kern_enter(td, ses->ses_fpu_ctx, - FPU_KERN_NORMAL); - saved_ctx = 1; - } else { - error = 0; - saved_ctx = 0; - } + error = fpu_kern_enter(td, ses->ses_fpu_ctx, FPU_KERN_NORMAL | + FPU_KERN_KTHR); if (error == 0) { error = padlock_hash_setup(ses, macini); - if (saved_ctx) - fpu_kern_leave(td, ses->ses_fpu_ctx); + fpu_kern_leave(td, ses->ses_fpu_ctx); } if (error != 0) { padlock_freesession_one(sc, ses, 0); diff --git a/sys/crypto/via/padlock_cipher.c b/sys/crypto/via/padlock_cipher.c index 7170211..0e4beb8 100644 --- a/sys/crypto/via/padlock_cipher.c +++ b/sys/crypto/via/padlock_cipher.c @@ -205,7 +205,7 @@ padlock_cipher_process(struct padlock_session *ses, struct cryptodesc *enccrd, struct thread *td; u_char *buf, *abuf; uint32_t *key; - int allocated, error, saved_ctx; + int allocated, error; buf = padlock_cipher_alloc(enccrd, crp, &allocated); if (buf == NULL) @@ -250,21 +250,13 @@ padlock_cipher_process(struct padlock_session *ses, struct cryptodesc *enccrd, } td = curthread; - if (!is_fpu_kern_thread(0)) { - error = fpu_kern_enter(td, ses->ses_fpu_ctx, FPU_KERN_NORMAL); - saved_ctx = 1; - } else { - error = 0; - saved_ctx = 0; - } + error = fpu_kern_enter(td, ses->ses_fpu_ctx, FPU_KERN_NORMAL | + FPU_KERN_KTHR); if (error != 0) goto out; - padlock_cbc(abuf, abuf, enccrd->crd_len / AES_BLOCK_LEN, key, cw, ses->ses_iv); - - if (saved_ctx) - fpu_kern_leave(td, ses->ses_fpu_ctx); + fpu_kern_leave(td, ses->ses_fpu_ctx); if (allocated) { crypto_copyback(crp->crp_flags, crp->crp_buf, enccrd->crd_skip, diff --git a/sys/crypto/via/padlock_hash.c b/sys/crypto/via/padlock_hash.c index 924a9ec..9dffc40 100644 --- a/sys/crypto/via/padlock_hash.c +++ b/sys/crypto/via/padlock_hash.c @@ -366,24 +366,18 @@ padlock_hash_process(struct padlock_session *ses, struct cryptodesc *maccrd, struct cryptop *crp) { struct thread *td; - int error, saved_ctx; + int error; td = curthread; - if (!is_fpu_kern_thread(0)) { - error = fpu_kern_enter(td, ses->ses_fpu_ctx, FPU_KERN_NORMAL); - saved_ctx = 1; - } else { - error = 0; - saved_ctx = 0; - } + error = fpu_kern_enter(td, ses->ses_fpu_ctx, FPU_KERN_NORMAL | + FPU_KERN_KTHR); if (error != 0) return (error); if ((maccrd->crd_flags & CRD_F_KEY_EXPLICIT) != 0) padlock_hash_key_setup(ses, maccrd->crd_key, maccrd->crd_klen); error = padlock_authcompute(ses, maccrd, crp->crp_buf, crp->crp_flags); - if (saved_ctx) - fpu_kern_leave(td, ses->ses_fpu_ctx); + fpu_kern_leave(td, ses->ses_fpu_ctx); return (error); } diff --git a/sys/i386/include/npx.h b/sys/i386/include/npx.h index 33a47b3..19e9b31 100644 --- a/sys/i386/include/npx.h +++ b/sys/i386/include/npx.h @@ -71,6 +71,7 @@ int is_fpu_kern_thread(u_int flags); */ #define FPU_KERN_NORMAL 0x0000 #define FPU_KERN_NOWAIT 0x0001 +#define FPU_KERN_KTHR 0x0002 #endif diff --git a/sys/i386/isa/npx.c b/sys/i386/isa/npx.c index 622e5b7..dec7366 100644 --- a/sys/i386/isa/npx.c +++ b/sys/i386/isa/npx.c @@ -1008,6 +1008,7 @@ static MALLOC_DEFINE(M_FPUKERN_CTX, "fpukern_ctx", #define XSAVE_AREA_ALIGN 64 #define FPU_KERN_CTX_NPXINITDONE 0x01 +#define FPU_KERN_CTX_DUMMY 0x02 struct fpu_kern_ctx { union savefpu *prev; @@ -1051,6 +1052,10 @@ fpu_kern_enter(struct thread *td, struct fpu_kern_ctx *ctx, u_int flags) { struct pcb *pcb; + if ((flags & FPU_KERN_KTHR) != 0 && is_fpu_kern_thread(0)) { + ctx->flags = FPU_KERN_CTX_DUMMY; + return (0); + } pcb = td->td_pcb; KASSERT(!PCB_USER_FPU(pcb) || pcb->pcb_save == &pcb->pcb_user_save, ("mangled pcb_save")); @@ -1070,6 +1075,8 @@ fpu_kern_leave(struct thread *td, struct fpu_kern_ctx *ctx) { struct pcb *pcb; + if (is_fpu_kern_thread(0) && (ctx->flags & FPU_KERN_CTX_DUMMY) != 0) + return (0); pcb = td->td_pcb; critical_enter(); if (curthread == PCPU_GET(fpcurthread)) |