From: Sean Christopherson <seanjc@google.com>
To: ira.weiny@intel.com
Cc: Jarkko Sakkinen <jarkko@kernel.org>,
linux-kernel@vger.kernel.org, linux-sgx@vger.kernel.org
Subject: Re: [PATCH] x86: Remove unnecessary kmap() from sgx_ioc_enclave_init()
Date: Fri, 29 Jan 2021 09:37:30 -0800 [thread overview]
Message-ID: <YBRH2jfPKS8ZofMZ@google.com> (raw)
In-Reply-To: <20210129001459.1538805-1-ira.weiny@intel.com>
On Thu, Jan 28, 2021, ira.weiny@intel.com wrote:
> From: Ira Weiny <ira.weiny@intel.com>
>
> There is no reason to alloc a page and kmap it to store this temporary
> data from the user.
Actually, there is, it's just poorly documented. The sigstruct needs to be
page aligned, and the token needs to be 512-byte aligned. kmcalloc doesn't
guarantee alignment. IIRC things will work until slub_debug is enabled, at
which point the natural alignment behavior goes out the window.
> This is especially true when we are trying to
> remove kmap usages. Also placing the token pointer 1/2 way into the
> page is fragile.
>
> Replace this allocation with two kzalloc()'s which also removes the need
> for the memset().
>
> Signed-off-by: Ira Weiny <ira.weiny@intel.com>
> ---
> arch/x86/kernel/cpu/sgx/ioctl.c | 18 ++++++++++--------
> 1 file changed, 10 insertions(+), 8 deletions(-)
>
> diff --git a/arch/x86/kernel/cpu/sgx/ioctl.c b/arch/x86/kernel/cpu/sgx/ioctl.c
> index 90a5caf76939..9c9019760585 100644
> --- a/arch/x86/kernel/cpu/sgx/ioctl.c
> +++ b/arch/x86/kernel/cpu/sgx/ioctl.c
> @@ -604,7 +604,6 @@ static long sgx_ioc_enclave_init(struct sgx_encl *encl, void __user *arg)
> {
> struct sgx_sigstruct *sigstruct;
> struct sgx_enclave_init init_arg;
> - struct page *initp_page;
> void *token;
> int ret;
>
> @@ -615,13 +614,15 @@ static long sgx_ioc_enclave_init(struct sgx_encl *encl, void __user *arg)
> if (copy_from_user(&init_arg, arg, sizeof(init_arg)))
> return -EFAULT;
>
> - initp_page = alloc_page(GFP_KERNEL);
> - if (!initp_page)
> + sigstruct = kzalloc(sizeof(*sigstruct), GFP_KERNEL);
> + if (!sigstruct)
> return -ENOMEM;
>
> - sigstruct = kmap(initp_page);
> - token = (void *)((unsigned long)sigstruct + PAGE_SIZE / 2);
> - memset(token, 0, SGX_LAUNCH_TOKEN_SIZE);
> + token = kzalloc(SGX_LAUNCH_TOKEN_SIZE, GFP_KERNEL);
> + if (!token) {
> + ret = -ENOMEM;
> + goto free_sigstruct;
> + }
>
> if (copy_from_user(sigstruct, (void __user *)init_arg.sigstruct,
> sizeof(*sigstruct))) {
> @@ -645,8 +646,9 @@ static long sgx_ioc_enclave_init(struct sgx_encl *encl, void __user *arg)
> ret = sgx_encl_init(encl, sigstruct, token);
>
> out:
> - kunmap(initp_page);
> - __free_page(initp_page);
> + kfree(token);
> +free_sigstruct:
> + kfree(sigstruct);
> return ret;
> }
>
> --
> 2.28.0.rc0.12.gb6a658bd00c9
>
next prev parent reply other threads:[~2021-01-29 17:40 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-01-29 0:14 [PATCH] x86: Remove unnecessary kmap() from sgx_ioc_enclave_init() ira.weiny
2021-01-29 17:37 ` Sean Christopherson [this message]
2021-02-01 8:48 ` Christoph Hellwig
2021-02-02 17:37 ` Jarkko Sakkinen
2021-02-02 1:37 ira.weiny
2021-02-02 18:55 ` Dave Hansen
2021-02-02 22:43 ` Jarkko Sakkinen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=YBRH2jfPKS8ZofMZ@google.com \
--to=seanjc@google.com \
--cc=ira.weiny@intel.com \
--cc=jarkko@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-sgx@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).