Skip to contentSkip to conversation

x86, fpu: wrap get_xsave_addr() to make it safer

PATCH Andy Lutomirski 5 participants 13 messages Mar 24, 2015 – Mar 25, 2015 2 messages not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

#2 lore raw
Quoted 76 lines · Dave Hansen

From: Dave Hansen <[email protected]>

The MPX code appears to be saving off the FPU in an unsafe
way. It does not disable preemption or ensure that the
FPU state has been allocated.

This patch introduces a new helper which will do both of those things internally to a helper.

Cc Rik van Riel <[email protected]>
Cc Suresh Siddha <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Fenghua Yu <[email protected]>
Cc the arch/x86 maintainers <[email protected]>
---
 arch/x86/include/asm/xsave.h |  1 +
 arch/x86/kernel/xsave.c      | 32 ++++++++++++++++++++++++++++++++
 2 files changed, 33 insertions(+)
arch/x86/include/asm/xsave.h +1 -0
index c9a6d68..86b58fb 100644
@@ -252,6 +252,7 @@ static inline int xrestore_user(struct xsave_struct __user *buf, u64 mask)
 }
 
 void *get_xsave_addr(struct xsave_struct *xsave, int xstate);
+void *tsk_get_xsave_field(struct task_struct *tsk, int xstate_field);
 void setup_xstate_comp(void);
 
 #endif
arch/x86/kernel/xsave.c +32 -1
index 34f66e5..9919e7e 100644
@@ -740,3 +740,35 @@ void *get_xsave_addr(struct xsave_struct *xsave, int xstate)
        return (void *)xsave + xstate_comp_offsets[feature];
 }
 EXPORT_SYMBOL_GPL(get_xsave_addr);
+
+/*
+ * This wraps up the common operations that need to occur when retrieving
+ * data from an xsave struct.  It first ensures that the task was actually
+ * using the FPU and retrieves the data in to a buffer.  It then calculates
+ * the offset of the requested field in the buffer.
+ *
+ * This function is safe to call whether the FPU is in use or not.
+ *
+ * Inputs:
+ *     tsk: the task from which we are fetching xsave state
+ *     xstate: state which is defined in xsave.h (e.g. XSTATE_FP, XSTATE_SSE,
+ *     etc.)
+ * Output:
+ *     address of the state in the xsave area.
+ */
+void *tsk_get_xsave_field(struct task_struct *tsk, int xsave_field)
+{
+       union thread_xstate *xstate;
+
+       unlazy_fpu(tsk);
+       xstate = tsk->thread.fpu.state;
+       /*
+        * This might be unallocated if the FPU
+        * was never in use.
+        */
+       if (!xstate)
+               return NULL;
+
+       return get_xsave_addr(&xstate->xsave, xsave_field);
+}
+EXPORT_SYMBOL_GPL(tsk_get_xsave_field);
--
Quoted 2 lines

1.9.1

I have one objection to get_xsave_addr and tsk_get_xsave_field: I think that get_xsave_addr should be called either get_xsave_addr_for_read or get_xsave_addr_for_write, depending on which of those it does.

Your function appears to be getting it for write (I assume that's what the unlazy_fpu is for), so I'd rather have it called tsk_get_xsave_field_for_write or something like that.

--Andy

Signature · 2 lines
Andy Lutomirski
AMA Capital Management, LLC
#3 lore raw
Quoted 3 lines · Andy Lutomirski

Your function appears to be getting it for write (I assume that's what the unlazy_fpu is for), so I'd rather have it called tsk_get_xsave_field_for_write or something like that.

It should be entirely read-only.

For MPX (the only user of get_xsave_addr() iirc), we are only worried about getting the status codes (and addresses) out of the bndstatus register and making sure that the kernel-recorded bounds directory address matches the bndcfgu (configuration) register.

We don't ever write to the registers.

#4 lore raw
Quoted 5 lines · Dave Hansen
Quoted 3 lines · Andy Lutomirski

Your function appears to be getting it for write (I assume that's what the unlazy_fpu is for), so I'd rather have it called tsk_get_xsave_field_for_write or something like that.

Quoted 8 lines

It should be entirely read-only.

For MPX (the only user of get_xsave_addr() iirc), we are only worried about getting the status codes (and addresses) out of the bndstatus register and making sure that the kernel-recorded bounds directory address matches the bndcfgu (configuration) register.

We don't ever write to the registers.

So why are you unlazying it?

IIUC, the xstae for current can be in one of three logical states:

1. Live in CPU regs. The in-memory copy is garbage and the state is in CPU regs.
2. Lazy. The in-memory copy and the CPU regs match. Writing to either copy is illegal.
3. In memory only. Writing to the in-memory copy is safe.

IIUC, you want to read the xstate, do you're okay with #2 or #3. This would be tsk_get_xsave_field_for_read in my terminology.

If you want to write the xstate, you'd need to be in state #3, which would be tsk_get_xsave_field_for_write.

IIUC, unlazy_fpu just moves from from state 2 to 3.

I could be totally wrong for any number of reasons, though.

--Andy

Signature · 2 lines
Andy Lutomirski
AMA Capital Management, LLC
#5 lore raw
Quoted 8 lines · Andy Lutomirski
Quoted 7 lines · Dave Hansen
Quoted 3 lines · Andy Lutomirski

Your function appears to be getting it for write (I assume that's what the unlazy_fpu is for), so I'd rather have it called tsk_get_xsave_field_for_write or something like that.

It should be entirely read-only.

Quoted 8 lines
Quoted 6 lines

For MPX (the only user of get_xsave_addr() iirc), we are only worried about getting the status codes (and addresses) out of the bndstatus register and making sure that the kernel-recorded bounds directory address matches the bndcfgu (configuration) register.

We don't ever write to the registers.

So why are you unlazying it?

Oleg actually suggested it.

Quoted 8 lines

IIUC, the xstae for current can be in one of three logical states:

1. Live in CPU regs. The in-memory copy is garbage and the state is in CPU regs.
2. Lazy. The in-memory copy and the CPU regs match. Writing to either copy is illegal.
3. In memory only. Writing to the in-memory copy is safe.

Quoted 7 lines

IIUC, you want to read the xstate, do you're okay with #2 or #3. This would be tsk_get_xsave_field_for_read in my terminology.

If you want to write the xstate, you'd need to be in state #3, which would be tsk_get_xsave_field_for_write.

IIUC, unlazy_fpu just moves from from state 2 to 3.

I won't completely claim to understand what's going on with the FPU code, but I think your analysis is a bit off.

unlazy_fpu() does __save_init_fpu() which (among other things) calls xsave to dump the CPU registers to memory. That doesn't make any sense to do if "The in-memory copy and the CPU regs match."

IOW, unlazy_fpu() is called when the in-memory copy is garbage and takes us to a state where we can look at the in-memory copy.

#6 lore raw
Quoted 39 lines · Dave Hansen
Quoted 8 lines · Andy Lutomirski
Quoted 7 lines · Dave Hansen
Quoted 3 lines · Andy Lutomirski

Your function appears to be getting it for write (I assume that's what the unlazy_fpu is for), so I'd rather have it called tsk_get_xsave_field_for_write or something like that.

It should be entirely read-only.

Quoted 8 lines
Quoted 6 lines

For MPX (the only user of get_xsave_addr() iirc), we are only worried about getting the status codes (and addresses) out of the bndstatus register and making sure that the kernel-recorded bounds directory address matches the bndcfgu (configuration) register.

We don't ever write to the registers.

So why are you unlazying it?

Oleg actually suggested it.

Quoted 8 lines

IIUC, the xstae for current can be in one of three logical states:

1. Live in CPU regs. The in-memory copy is garbage and the state is in CPU regs.
2. Lazy. The in-memory copy and the CPU regs match. Writing to either copy is illegal.
3. In memory only. Writing to the in-memory copy is safe.

Quoted 7 lines

IIUC, you want to read the xstate, do you're okay with #2 or #3. This would be tsk_get_xsave_field_for_read in my terminology.

If you want to write the xstate, you'd need to be in state #3, which would be tsk_get_xsave_field_for_write.

IIUC, unlazy_fpu just moves from from state 2 to 3.

I won't completely claim to understand what's going on with the FPU code, but I think your analysis is a bit off.

Quoted 6 lines

unlazy_fpu() does __save_init_fpu() which (among other things) calls xsave to dump the CPU registers to memory. That doesn't make any sense to do if "The in-memory copy and the CPU regs match."

IOW, unlazy_fpu() is called when the in-memory copy is garbage and takes us to a state where we can look at the in-memory copy.

I think that __save_init_fpu (called by unlazy_fpu) does that, but __thread_fpu_end calls __thread_clear_has_fpu, which, in turn, zaps fpu_owner_task, which will force an unnecessary xrstor. Or maybe not if we have further bugs.

Holy crap these functions are poorly named. Also, what, if anything, guarantees that fpu_owner_task is set on entry to userspace? Do we even need it to be set? Oleg, help?

--Andy

Signature · 2 lines
Andy Lutomirski
AMA Capital Management, LLC
#7 lore raw

[add Borislav]

I swear it would actually be an improvement if we just randomized the function names. fpu_817, fpu_717, etc. At least no one would think they understand them...

Quoted 58 lines · Andy Lutomirski
Quoted 39 lines · Dave Hansen
Quoted 8 lines · Andy Lutomirski
Quoted 7 lines · Dave Hansen
Quoted 3 lines · Andy Lutomirski

Your function appears to be getting it for write (I assume that's what the unlazy_fpu is for), so I'd rather have it called tsk_get_xsave_field_for_write or something like that.

It should be entirely read-only.

Quoted 8 lines
Quoted 6 lines

For MPX (the only user of get_xsave_addr() iirc), we are only worried about getting the status codes (and addresses) out of the bndstatus register and making sure that the kernel-recorded bounds directory address matches the bndcfgu (configuration) register.

We don't ever write to the registers.

So why are you unlazying it?

Oleg actually suggested it.

Quoted 8 lines

IIUC, the xstae for current can be in one of three logical states:

1. Live in CPU regs. The in-memory copy is garbage and the state is in CPU regs.
2. Lazy. The in-memory copy and the CPU regs match. Writing to either copy is illegal.
3. In memory only. Writing to the in-memory copy is safe.

Quoted 7 lines

IIUC, you want to read the xstate, do you're okay with #2 or #3. This would be tsk_get_xsave_field_for_read in my terminology.

If you want to write the xstate, you'd need to be in state #3, which would be tsk_get_xsave_field_for_write.

IIUC, unlazy_fpu just moves from from state 2 to 3.

I won't completely claim to understand what's going on with the FPU code, but I think your analysis is a bit off.

Quoted 6 lines

unlazy_fpu() does __save_init_fpu() which (among other things) calls xsave to dump the CPU registers to memory. That doesn't make any sense to do if "The in-memory copy and the CPU regs match."

IOW, unlazy_fpu() is called when the in-memory copy is garbage and takes us to a state where we can look at the in-memory copy.

I think that __save_init_fpu (called by unlazy_fpu) does that, but __thread_fpu_end calls __thread_clear_has_fpu, which, in turn, zaps fpu_owner_task, which will force an unnecessary xrstor. Or maybe not if we have further bugs.

Holy crap these functions are poorly named. Also, what, if anything, guarantees that fpu_owner_task is set on entry to userspace? Do we even need it to be set? Oleg, help?

--Andy

#8 lore raw
In reply to Andy Lutomirski
Quoted 9 lines · Andy Lutomirski
Quoted 7 lines · Dave Hansen

I won't completely claim to understand what's going on with the FPU code, but I think your analysis is a bit off.

unlazy_fpu() does __save_init_fpu() which (among other things) calls xsave to dump the CPU registers to memory. That doesn't make any sense to do if "The in-memory copy and the CPU regs match."

Quoted 7 lines
Quoted 2 lines

IOW, unlazy_fpu() is called when the in-memory copy is garbage and takes us to a state where we can look at the in-memory copy.

I think that __save_init_fpu (called by unlazy_fpu) does that, but __thread_fpu_end calls __thread_clear_has_fpu, which, in turn, zaps fpu_owner_task, which will force an unnecessary xrstor. Or maybe not if we have further bugs.

Indeed, __save_init_fpu (yeah, terrible name) will save the in-register state to memory for you, so you can inspect it.

Is there any reason not to rename __save_init_fpu to save_fpu_state, or just save_fpu?

Signature · 1 line
All rights reversed
#9 lore raw
Quoted 6 lines · Rik van Riel

Indeed, __save_init_fpu (yeah, terrible name) will save the in-register state to memory for you, so you can inspect it.

Is there any reason not to rename __save_init_fpu to save_fpu_state, or just save_fpu?

That whole place there needs more rubbing.

So the way I see it, the "init" thing also says that the FPU is intact.

__save_fpu() does *SAVE too but it misses the FNSAVE fallback for old machines. Why TF is that so?

It also doesn't do FNCLEX and IMHO the clearing of exceptions is what the "init" aspect in the name tries to imply.

I think we should simply merge those into a main one called

save_fpu_state() (what Rik suggests)

which does all that __save_init_fpu() does currently and a low-level helper __save_fpu() which does only the *SAVE saving of context work.

Meh, not ecstatic about it either but it is the cleanup worth and maybe later more stuff will collapse during further cleaning...

Signature · 5 lines
Regards/Gruss,
    Boris.

ECO tip #101: Trim your mails when you reply.
--
#10 lore raw
Quoted 6 lines · Borislav Petkov
Quoted 4 lines · Rik van Riel

Indeed, __save_init_fpu (yeah, terrible name) will save the in-register state to memory for you, so you can inspect it.

Quoted 6 lines
Quoted 2 lines

Is there any reason not to rename __save_init_fpu to save_fpu_state, or just save_fpu?

That whole place there needs more rubbing.

So the way I see it, the "init" thing also says that the FPU is intact.

Yes, this is my understanding too.

And note that nobody actually wants this "init" part, so it actually means "destroy".

I agree we should rename it later (at least). Plus unlazy_fpu() looks confusing too. Nobody actually wants to "unlazy", the callers want to save FPU state. So it could be named save_fpu_state() too ;)

Oleg.

#11 lore raw
In reply to Rik van Riel
Quoted 4 lines · Rik van Riel

On 03/24/2015 08:18 PM, Andy Lutomirski wrote:
Is there any reason not to rename __save_init_fpu to save_fpu_state, or just save_fpu?

Please see another email. __save_init_fpu() saves the FPU state but it can also change the FPU registers. At least I think so ;)

Otherwise, why switch_fpu_prepare() resets .last_cpu if it returns zero? And unlazy_fpu() should do the same.

This all needs the cleanups, yes.

Oleg.

#12 lore raw
In reply to Andy Lutomirski

So far I do not understand this discussion ;) I didn't see the patches and other emails...

Quoted 6 lines · Andy Lutomirski
Quoted 4 lines · Dave Hansen
Quoted 2 lines

So why are you unlazying it?

Oleg actually suggested it.

perhaps you are talking about math_error() ? It uses save_init_fpu() in Linus's tree, but this is wrong. It was changed in tip/x86/fpu to use unlazy_fpu() and save_init_fpu() was killed. Plus, just in case, unlazy_fpu() was changed too and now it is

	void unlazy_fpu(struct task_struct *tsk)
	{
		preempt_disable();
		if (__thread_has_fpu(tsk)) {
			if (use_eager_fpu()) {
				__save_fpu(tsk);
			} else {
				__save_init_fpu(tsk);
				__thread_fpu_end(tsk);
			}
		}
		preempt_enable();
	}

and in fact (I think) it needs another change later, __thread_fpu_end() should depend on __save_init_fpu().

Quoted 19 lines
Quoted 19 lines
Quoted 8 lines

IIUC, the xstae for current can be in one of three logical states:

1. Live in CPU regs. The in-memory copy is garbage and the state is in CPU regs.
2. Lazy. The in-memory copy and the CPU regs match. Writing to either copy is illegal.
3. In memory only. Writing to the in-memory copy is safe.

Quoted 7 lines

IIUC, you want to read the xstate, do you're okay with #2 or #3. This would be tsk_get_xsave_field_for_read in my terminology.

If you want to write the xstate, you'd need to be in state #3, which would be tsk_get_xsave_field_for_write.

IIUC, unlazy_fpu just moves from from state 2 to 3.

I won't completely claim to understand what's going on with the FPU code, but I think your analysis is a bit off.

Quoted 6 lines
Quoted 6 lines

unlazy_fpu() does __save_init_fpu() which (among other things) calls xsave to dump the CPU registers to memory. That doesn't make any sense to do if "The in-memory copy and the CPU regs match."

IOW, unlazy_fpu() is called when the in-memory copy is garbage and takes us to a state where we can look at the in-memory copy.

Not necessarily, but most probably I misunderstood... If __thread_has_fpu() we don't and can't know whether in-memory copy and the CPU regs match, so we simply write FPU state to memory.

Quoted 4 lines

I think that __save_init_fpu (called by unlazy_fpu) does that, but __thread_fpu_end calls __thread_clear_has_fpu, which, in turn, zaps fpu_owner_task, which will force an unnecessary xrstor. Or maybe not if we have further bugs.

Yes, see above. We do not really need unconditional __thread_fpu_end().

Quoted 1 line

Holy crap these functions are poorly named.

Yes... let me quote another email from me:

Perhaps you can also find a beter name for __save_init_fpu/etc ;) The name clearly suggests that it does "save + init" while in fact it does "save and may be destroy FPU state". At least for the callers, the fact that "destroy" is actually "init" doesn't really matter.

But lets not rename it right now. This can conflict with the fixes we need to do first.

So please do not rename it now ;) I had to switch to other tasks, but I hope I will continue the FPU cleanups "soon".

Quoted 2 lines

Also, what, if anything,
guarantees that fpu_owner_task is set on entry to userspace?

Well, this probaly needs other cleanups. I am not sure __thread_set_has_fpu() actually should set fpu_owner_task = current. But this is offtopic.

Again, I am not sure I understand your concern... but fpu_owner_task should be set if __thread_has_fpu(). Otherwise we do not care.

But __thread_fpu_end() must _clear_ fpu_owner_task, this is what we do care.

Oleg.

#13 lore raw
Quoted 2 lines · Oleg Nesterov

So far I do not understand this discussion ;) I didn't see the patches and other emails...

Hi Oleg,

My patch set apparently didn't make it to LKML, but here are the two relevant ones. We're essentially replacing the MPX use of fpu_save_init(). CPUs with MPX should entirely have eager FPU mode on.
But, the edges of the MPX code (do_bounds()) will call this to
distinguish a plain #BR exception from a #BR caused by MPX. It may get called on CPUs without eager FPU mode on.

Quoted 2 lines

http://git.kernel.org/cgit/linux/kernel/git/daveh/x86-mpx.git/commit/?h=mpx-v16&id=92d3e7c1664f766142904904e27e126888adb8a7
http://git.kernel.org/cgit/linux/kernel/git/daveh/x86-mpx.git/commit/?h=mpx-v16&id=18049953ae43a7ffa084a01613c1684bdf24dd2e

All that the MPX code wants here is to read the in-memory copy of the MPX registers, or error out.

So, for the purposes of this series:

With the (so far unmerged to Linus's tree) changes to unlazy_fpu(), does tsk_get_xsave_field()'s use of unlazy_fpu() look correct?

Should we also be renaming tsk_get_xsave_field() to something more appropriate?

#14 lore raw

Hi Dave,

Quoted 9 lines · Dave Hansen

It may get
called on CPUs without eager FPU mode on.

Quoted 2 lines

http://git.kernel.org/cgit/linux/kernel/git/daveh/x86-mpx.git/commit/?h=mpx-v16&id=92d3e7c1664f766142904904e27e126888adb8a7
http://git.kernel.org/cgit/linux/kernel/git/daveh/x86-mpx.git/commit/?h=mpx-v16&id=18049953ae43a7ffa084a01613c1684bdf24dd2e

All that the MPX code wants here is to read the in-memory copy of the MPX registers, or error out.

Yes, iirc we alredy discussed these fixes ?

I still think that the "if (!xstate)" check at the start of tsk_get_xsave_field() will look better, but this is cosmetic.

Quoted 4 lines

So, for the purposes of this series:

With the (so far unmerged to Linus's tree) changes to unlazy_fpu(), does tsk_get_xsave_field()'s use of unlazy_fpu() look correct?

I think yes. But let me remind just in case that this depends on "x86, fpu: unlazy_fpu: don't do __thread_fpu_end() if use_eager_fpu()".

Quoted 2 lines

Should we also be renaming tsk_get_xsave_field() to something more appropriate?

Oh, don't ask me ;) To me it looks fine.

Oleg.