mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Dave Hansen <dave.hansen@intel.com>
To: liangjs <liangjs@pku.edu.cn>
Cc: linux-kernel@vger.kernel.org,
	Thomas Gleixner <tglx@linutronix.de>,
	Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
	x86@kernel.org, "H. Peter Anvin" <hpa@zytor.com>
Subject: Re: arch_set_user_pkey_access only works on the current task_struct
Date: Tue, 8 Jun 2021 07:55:29 -0700	[thread overview]
Message-ID: <3b6f8899-2a11-31da-f67c-57e786661ef3@intel.com> (raw)
In-Reply-To: <774b6e6c8cb1be4923048bb1bb18753a6854758f.camel@pku.edu.cn>

On 6/7/21 8:16 PM, liangjs wrote:
> On Mon, 2021-06-07 at 10:52 -0700, Dave Hansen wrote:
>> On 6/5/21 6:10 AM, Jiashuo Liang wrote:
>>> I am learning the kernel implementation of the x86 PKU feature. I find the
>>> arch_set_user_pkey_access function in arch/x86/kernel/fpu/xstate.c does not
>>> use its first parameter. So it is perhaps a bug?
>> I wouldn't really call it a bug.  But, yes, it is something we should
>> clean up.
> Should we remove the tsk parameter, or allow it to change the PKRU of tsk?

Probably just remove the parameter.

By the way, there's a big PKRU rework in progress.  It might be best to
wait until the dust settles to poke at this.

> By the way, we are calling write_pkru, which changes both the CPU's PKRU
> and the xsave one. Why is this necessary?

PKRU affects kernel accesses to user memory.  That means that you can't
run the *kernel* with an out-of-date PKRU, thus the write_pkru().

Returning to userspace blindly restores the *WHOLE* XSAVE buffer to the
regsisters.  If you don't update the XSAVE buffer, the write_pkru() will
be overwritten before returning to userspace.

> If I want to change PKRU of a task_struct other than current, do I still
> need to call __write_pkru?

No.  You can't do that.  Seriously.

The protection keys architecture really doesn't support off-thread
manipulation of PKRU.  Imagine you want to mask a bit out of PKRU, you
do the following to make key 2 memory accessible and writable:

	reg = read_pkru();
	reg &= 0x30;
	write_pkru(reg);

Now, imagine that you tried to interrupt this poor task in the middle of
that operation.  Let's say you try to *set* the bits for key 4, effectively:

	pkru |= 0x300;

Now you try to do that key-4 business with an IPI.

	reg = read_pkru(); // PKRU=0x30
	reg &= 0x30;
		-> IPI
			ipireg = read_pkru(); // PKRU=0x0
			ipireg |= 0x300;
			write_pkru(ipireg);   // PKRU=0x300
	write_pkru(reg); // PKRU=0x0

You *LOST* the update from the IPI.

  reply	other threads:[~2021-06-08 14:55 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-05 13:10 Jiashuo Liang
2021-06-07 17:52 ` Dave Hansen
2021-06-08  3:16   ` liangjs
2021-06-08 14:55     ` Dave Hansen [this message]
2021-06-08 19:05     ` Thomas Gleixner

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=3b6f8899-2a11-31da-f67c-57e786661ef3@intel.com \
    --to=dave.hansen@intel.com \
    --cc=bp@alien8.de \
    --cc=hpa@zytor.com \
    --cc=liangjs@pku.edu.cn \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=tglx@linutronix.de \
    --cc=x86@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

all inboxes | Powered by JetHome®