ARC: Improve cmpxchng syscall implementation
Subsystems (2)
PATCH 1/1 ARC: Improve cmpxchng syscall implementation
arc_usr_cmpxchg syscall is supposed to be used on platforms that lack support of Load-Locked/Store-Conditional instructions in hardware. And in that case we mimic missing hardware features with help of kernel's sycall that "atomically" checks current value in memory and then if it matches caller expectation new value is written to that same location.
What's important in the description above:
- Check-and-exchange must be "atomical" which means preemption must be disabled during entire "transaction"
- Data accessed is from user-space, i.e. we're dealing with virtual addresses
And in current implementation we have a couple of problems:
1. We do disable preemprion around __get_user() & __put_user() but that in its turn disables page fault handler. That means if a pointer to user's data has no mapping in the TLB we won't be able to access required data. Instead software "exception handling" code from __get_user_fn() will return -EFAULT.
2. What's worse if we're dealing with data from not yet allocated page (think of pre-copy-on-write state) we'll successfully read data but on write we'll silently return to user-space with correct result (which we really read just before). That leads to very strange problems in user-space app further down the line because new value was never written to the destination.
3. Regardless of what went wrong we'll return from syscall and user-space application will continue to execute. Even if user's pointer was completely bogus.
In case of hardware LL/SC that app would have been killed by the kernel.
With that change we attempt to imrove on all 3 items above:
1. We still disable preemption around read-and-write of user's data but if we happen to fail with either of them we're enabling preemption and try to force page fault so that we have a correct mapping in the TLB. Then re-try again in "atomic" context.
2. If real page fault fails or even access_ok() returns false we send SIGSEGV to the user-space process so if something goes seriously wrong we'll know about it much earlier.
--- arch/arc/kernel/process.c | 47 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 10 deletions(-)
arch/arc/kernel/process.c +37 -10
@@ -47,7 +47,9 @@ SYSCALL_DEFINE0(arc_gettls) SYSCALL_DEFINE3(arc_usr_cmpxchg, int *, uaddr, int, expected, int, new) {struct pt_regs *regs =current_pt_regs (); -int uval = -EFAULT ; +struct page *page; +u32 val; +int ret;/* * This is only for old cores lacking LLOCK/SCOND, which by defintion
@@ -60,23 +62,48 @@ SYSCALL_DEFINE3(arc_usr_cmpxchg, int *, uaddr, int, expected, int, new)/* Z indicates to userspace if operation succeded */ regs->status32 &= ~STATUS_Z_MASK ; -if (!access_ok (VERIFY_WRITE , uaddr,sizeof (int ))) -return -EFAULT ; + ret =access_ok (VERIFY_WRITE , uaddr,sizeof (*uaddr)); +if (!ret) + goto fail; +again:preempt_disable (); -if (__get_user (uval, uaddr)) - goto done; - -if (uval == expected) { -if (!__put_user (new, uaddr)) + ret =__get_user (val, uaddr); +if (ret == -EFAULT ) { +preempt_enable (); + ret =get_user_pages_fast ((unsigned long )uaddr,1 ,1 , &page); +if (ret <0 ) + goto fail; + +put_page (page); + goto again; + }else if (ret) + goto fail; + +if (val == expected) { + ret =__put_user (new, uaddr); +if (!ret) regs->status32 |=STATUS_Z_MASK ; } -done:preempt_enable (); -return uval; +if (ret == -EFAULT ) { + ret =get_user_pages_fast ((unsigned long )uaddr,1 ,1 , &page); +if (ret <0 ) + goto fail; + +put_page (page); + goto again; + }else if (ret) + goto fail; + +return val; + +fail: +force_sig (SIGSEGV , current); +return ret; }#ifdef CONFIG_ISA_ARCV2
Signature · 1 line
2.11.0
Quoted 7 lines · Alexey Brodkin
arc_usr_cmpxchgsyscall is supposed to be used on platforms that lack support of Load-Locked/Store-Conditional instructions in hardware. And in that case we mimic missing hardware features with help of kernel's sycall that "atomically" checks current value in memory and then if it matches caller expectation new value is written to that same location.
...
...
Quoted 5 lines
2. What's worse if we're dealing with data from not yet allocated page (think of pre-copy-on-write state) we'll successfully read data but on write we'll silently return to user-space with correct result
This is technically incorrect, even for reading, you need a page, which could be common zero page in certain cases.
(which we really read just before). That leads
Quoted 6 lines
to very strange problems in user-space app further down the line because new value was never written to the destination.
3. Regardless of what went wrong we'll return from syscall and user-space application will continue to execute. Even if user's pointer was completely bogus.
Again we are exaggerating (from technical correctness POV) - if user pointer was bogs, the read would not have worked in first place etc. So lets tone down the rhetoric.
Quoted 30 lines
In case of hardware LL/SC that app would have been killed by the kernel.
With that change we attempt to imrove on all 3 items above:
1. We still disable preemption around read-and-write of user's data but if we happen to fail with either of them we're enabling preemption and try to force page fault so that we have a correct mapping in the TLB. Then re-try again in "atomic" context.
2. If real page fault fails or even
access_ok()returns false we send SIGSEGV to the user-space process so if something goes seriously wrong we'll know about it much earlier./*
* This is only for old cores lacking LLOCK/SCOND, which by defintionarch/arc/kernel/process.c +3
-2@@ -60,23 +62,48 @@ SYSCALL_DEFINE3(arc_usr_cmpxchg, int *, uaddr, int, expected, int, new)/* Z indicates to userspace if operation succeded */ regs->status32 &= ~STATUS_Z_MASK ; -if (!access_ok (VERIFY_WRITE , uaddr,sizeof (int ))) -return -EFAULT ; + ret =access_ok (VERIFY_WRITE , uaddr,sizeof (*uaddr)); +if (!ret) + goto fail;
arch/arc/kernel/process.c SYSCALL_DEFINE3(arc_usr_cmpxchg, int *, uaddr, int, expected, int, new) · line 73
Quoted 10 lines
arch/arc/kernel/process.c +3
-5+again:preempt_disable (); -if (__get_user (uval, uaddr)) - goto done; - -if (uval == expected) { -if (!__put_user (new, uaddr)) + ret =__get_user (val, uaddr); +if (ret == -EFAULT ) {
Lets see if this warrants adding complexity ! This implies that TLB entry with Read permissions didn't exist for reading the var and page fault handler could not wire up even a zero page due to preempt_disable, meaning it was something not touched by userspace already - sort of uninitialized variable in user code. Otherwise it is extremely unlikely to start with a TLB entry with Read permissions, followed by syscall Trap only to find the entry missing, unless a global TLB flush came from other cores, right in the middle. But this syscall is not guaranteed to work with SMP anyways, so lets ignore any SMP misdoings here.
Now in case it was *an* uninitialized var, do we have to guarantee any well defined semantics for the kernel emulation of cmpxchg ? IMO it should be fine to return 0 or -EFAULT etc. Infact -EFAULT is better as it will force a retry loop on user side, given the typical cmpxchg usage pattern.
Quoted 39 lines
arch/arc/kernel/process.c +28
-2+preempt_enable (); + ret =get_user_pages_fast ((unsigned long )uaddr,1 ,1 , &page); +if (ret <0 ) + goto fail; + +put_page (page); + goto again; + }else if (ret) + goto fail; + +if (val == expected) { + ret =__put_user (new, uaddr); +if (!ret) regs->status32 |=STATUS_Z_MASK ; } -done:preempt_enable (); -return uval; +if (ret == -EFAULT ) { + ret =get_user_pages_fast ((unsigned long )uaddr,1 ,1 , &page); +if (ret <0 ) + goto fail; + +put_page (page); + goto again; + }else if (ret) + goto fail; + +return val; + +fail: +force_sig (SIGSEGV , current); +return ret; }#ifdef CONFIG_ISA_ARCV2
Hi Vineet,
Quoted 13 lines · Vineet Gupta
Quoted 7 lines · Alexey Brodkin
arc_usr_cmpxchgsyscall is supposed to be used on platforms that lack support of Load-Locked/Store-Conditional instructions in hardware. And in that case we mimic missing hardware features with help of kernel's sycall that "atomically" checks current value in memory and then if it matches caller expectation new value is written to that same location....
...Quoted 1 line
Quoted 7 lines
Quoted 4 lines
2. What's worse if we're dealing with data from not yet allocated page (think of pre-copy-on-write state) we'll successfully read data but on write we'll silently return to user-space with correct result
This is technically incorrect, even for reading, you need a page, which could be common zero page in certain cases.
Ok I'll reword it like.
Quoted 5 lines
(which we really read just before). That leads
Quoted 3 lines
to very strange problems in user-space app further down the line because new value was never written to the destination.
Quoted 7 lines
Quoted 3 lines
3. Regardless of what went wrong we'll return from syscall and user-space application will continue to execute. Even if user's pointer was completely bogus.
Again we are exaggerating (from technical correctness POV) - if user pointer was bogs, the read would not have worked in first place etc. So lets tone down the rhetoric.
Ok here I may rephrase it like that:
------------------------------->8-----------------------------
3. Regardless of what went wrong we'll return from syscall and user-space application will continue to execute.
------------------------------->8-----------------------------
Quoted 34 lines
Quoted 34 lines
In case of hardware LL/SC that app would have been killed by the kernel.
With that change we attempt to imrove on all 3 items above:
1. We still disable preemption around read-and-write of user's data but if we happen to fail with either of them we're enabling preemption and try to force page fault so that we have a correct mapping in the TLB. Then re-try again in "atomic" context.
2. If real page fault fails or even
access_ok()returns false we send SIGSEGV to the user-space process so if something goes seriously wrong we'll know about it much earlier./*
* This is only for old cores lacking LLOCK/SCOND, which by defintionarch/arc/kernel/process.c +4
-2@@ -60,23 +62,48 @@ SYSCALL_DEFINE3(arc_usr_cmpxchg, int *, uaddr, int, expected, int, new)/* Z indicates to userspace if operation succeded */ regs->status32 &= ~STATUS_Z_MASK ; -if (!access_ok (VERIFY_WRITE , uaddr,sizeof (int ))) -return -EFAULT ; + ret =access_ok (VERIFY_WRITE , uaddr,sizeof (*uaddr)); +if (!ret) + goto fail; +again:preempt_disable ();
Quoted 13 lines
arch/arc/kernel/process.c SYSCALL_DEFINE3(arc_usr_cmpxchg, int *, uaddr, int, expected, int, new) · line 73
Quoted 7 lines
arch/arc/kernel/process.c +2
-5-if (__get_user (uval, uaddr)) - goto done; - -if (uval == expected) { -if (!__put_user (new, uaddr)) + ret =__get_user (val, uaddr); +if (ret == -EFAULT ) {Lets see if this warrants adding complexity ! This implies that TLB entry with Read permissions didn't exist for reading the var and page fault handler could not wire up even a zero page due to
preempt_disable, meaning it was something not touched by userspace already - sort of uninitialized variable in user code.
Ok I completely missed the fact that fast path TLB miss handler is being executed even if we have preemption disabled. So given the mapping exist we do not need to retry with enabled preemption.
Still maybe I'm a bit paranoid here but IMHO it's good to be ready for a corner-case when the pointer is completely bogus and there's no mapping for him.
I understand that today we only expect this syscall to be used from libc's internals but as long as syscall exists nobody stops anybody from using it directly without libc. So maybe instead of doing get_user_pages_fast() just send a SIGSEGV to the process? At least user will realize there's some problem at earlier stage.
Quoted 4 lines
Otherwise it is extremely unlikely to start with a TLB entry with Read permissions, followed by syscall Trap only to find the entry missing, unless a global TLB flush came from other cores, right in the middle. But this syscall is not guaranteed to work with SMP anyways, so lets ignore any SMP misdoings here.
Well but that's exactly the situation I was debugging: we start from data from read-only page and on attempt to write back modified value COW machinery gets involved.
That was on UP platform.
Quoted 4 lines
Now in case it was *an* uninitialized var, do we have to guarantee any well defined semantics for the kernel emulation of cmpxchg ? IMO it should be fine to return 0 or
-EFAULTetc. Infact-EFAULTis better as it will force a retry loop on user side, given the typical cmpxchg usage pattern.
The problem is libc only expects to get a value read from memory. And in theory expected value might be -14 which is basically -EFAULT. I'm not talking about 0 at all because in some cases that's exactly what user-space expects.
So if we read unexpected value then we'll just return it without even attempting to write.
If we read expected data but fail to write then we'll send a SIGSEGV and return whatever... let it be -EFAULT - anyways the app will be killed on exit from this syscall.
-Alexey
Hi Vineet, Peter,
Quoted 125 lines · Alexey Brodkin
Hi Vineet,
Quoted 13 lines · Vineet Gupta
Quoted 7 lines · Alexey Brodkin
arc_usr_cmpxchgsyscall is supposed to be used on platforms that lack support of Load-Locked/Store-Conditional instructions in hardware. And in that case we mimic missing hardware features with help of kernel's sycall that "atomically" checks current value in memory and then if it matches caller expectation new value is written to that same location....
...Quoted 1 line
Quoted 7 lines
Quoted 4 lines
2. What's worse if we're dealing with data from not yet allocated page (think of pre-copy-on-write state) we'll successfully read data but on write we'll silently return to user-space with correct result
This is technically incorrect, even for reading, you need a page, which could be common zero page in certain cases.
Ok I'll reword it like.
Quoted 5 lines
(which we really read just before). That leads
Quoted 3 lines
to very strange problems in user-space app further down the line because new value was never written to the destination.
Quoted 7 lines
Quoted 3 lines
3. Regardless of what went wrong we'll return from syscall and user-space application will continue to execute. Even if user's pointer was completely bogus.
Again we are exaggerating (from technical correctness POV) - if user pointer was bogs, the read would not have worked in first place etc. So lets tone down the rhetoric.
Ok here I may rephrase it like that:
------------------------------->8-----------------------------
3. Regardless of what went wrong we'll return from syscall and user-space application will continue to execute.
------------------------------->8-----------------------------Quoted 34 lines
Quoted 34 lines
In case of hardware LL/SC that app would have been killed by the kernel.
With that change we attempt to imrove on all 3 items above:
1. We still disable preemption around read-and-write of user's data but if we happen to fail with either of them we're enabling preemption and try to force page fault so that we have a correct mapping in the TLB. Then re-try again in "atomic" context.
2. If real page fault fails or even
access_ok()returns false we send SIGSEGV to the user-space process so if something goes seriously wrong we'll know about it much earlier./*
* This is only for old cores lacking LLOCK/SCOND, which by defintionarch/arc/kernel/process.c +4
-2@@ -60,23 +62,48 @@ SYSCALL_DEFINE3(arc_usr_cmpxchg, int *, uaddr, int, expected, int, new)/* Z indicates to userspace if operation succeded */ regs->status32 &= ~STATUS_Z_MASK ; -if (!access_ok (VERIFY_WRITE , uaddr,sizeof (int ))) -return -EFAULT ; + ret =access_ok (VERIFY_WRITE , uaddr,sizeof (*uaddr)); +if (!ret) + goto fail; +again:preempt_disable ();Quoted 13 lines
arch/arc/kernel/process.c SYSCALL_DEFINE3(arc_usr_cmpxchg, int *, uaddr, int, expected, int, new) · line 73
Quoted 7 lines
arch/arc/kernel/process.c +2
-5-if (__get_user (uval, uaddr)) - goto done; - -if (uval == expected) { -if (!__put_user (new, uaddr)) + ret =__get_user (val, uaddr); +if (ret == -EFAULT ) {Lets see if this warrants adding complexity ! This implies that TLB entry with Read permissions didn't exist for reading the var and page fault handler could not wire up even a zero page due to
preempt_disable, meaning it was something not touched by userspace already - sort of uninitialized variable in user code.Ok I completely missed the fact that fast path TLB miss handler is being executed even if we have preemption disabled. So given the mapping exist we do not need to retry with enabled preemption.
Still maybe I'm a bit paranoid here but IMHO it's good to be ready for a corner-case when the pointer is completely bogus and there's no mapping for him.
I understand that today we only expect this syscall to be used from libc's internals but as long as syscall exists nobody stops anybody from using it directly without libc. So maybe instead of doingget_user_pages_fast()just send a SIGSEGV to the process? At least user will realize there's some problem at earlier stage.Quoted 4 lines
Otherwise it is extremely unlikely to start with a TLB entry with Read permissions, followed by syscall Trap only to find the entry missing, unless a global TLB flush came from other cores, right in the middle. But this syscall is not guaranteed to work with SMP anyways, so lets ignore any SMP misdoings here.
Well but that's exactly the situation I was debugging: we start from data from read-only page and on attempt to write back modified value COW machinery gets involved.
That was on UP platform.
Quoted 4 lines
Now in case it was *an* uninitialized var, do we have to guarantee any well defined semantics for the kernel emulation of cmpxchg ? IMO it should be fine to return 0 or
-EFAULTetc. Infact-EFAULTis better as it will force a retry loop on user side, given the typical cmpxchg usage pattern.The problem is libc only expects to get a value read from memory. And in theory expected value might be -14 which is basically
-EFAULT. I'm not talking about 0 at all because in some cases that's exactly what user-space expects.
Quoted 6 lines
So if we read unexpected value then we'll just return it without even attempting to write.
If we read expected data but fail to write then we'll send a SIGSEGV and return whatever... let it be
-EFAULT- anyways the app will be killed on exit from this syscall.
Any comments on my comments above?
-Alexey
Quoted 31 lines · Alexey Brodkin
/*
Quoted 14 lines
Quoted 14 lines
* This is only for old cores lacking LLOCK/SCOND, which by defintion
arch/arc/kernel/process.c +4
-2@@ -60,23 +62,48 @@ SYSCALL_DEFINE3(arc_usr_cmpxchg, int *, uaddr, int, expected, int, new)/* Z indicates to userspace if operation succeded */ regs->status32 &= ~STATUS_Z_MASK ; -if (!access_ok (VERIFY_WRITE , uaddr,sizeof (int ))) -return -EFAULT ; + ret =access_ok (VERIFY_WRITE , uaddr,sizeof (*uaddr)); +if (!ret) + goto fail; +again:preempt_disable ();Quoted 12 lines
arch/arc/kernel/process.c SYSCALL_DEFINE3(arc_usr_cmpxchg, int *, uaddr, int, expected, int, new) · line 73
Quoted 7 lines
arch/arc/kernel/process.c +2
-5-if (__get_user (uval, uaddr)) - goto done; - -if (uval == expected) { -if (!__put_user (new, uaddr)) + ret =__get_user (val, uaddr); +if (ret == -EFAULT ) {Lets see if this warrants adding complexity ! This implies that TLB entry with Read permissions didn't exist for reading the var and page fault handler could not wire up even a zero page due to
preempt_disable, meaning it was something not touched by userspace already - sort of uninitialized variable in user code.Ok I completely missed the fact that fast path TLB miss handler is being executed even if we have preemption disabled. So given the mapping exist we do not need to retry with enabled preemption.
Quoted 7 lines
Still maybe I'm a bit paranoid here but IMHO it's good to be ready for a corner-case when the pointer is completely bogus and there's no mapping for him.
I understand that today we only expect this syscall to be used from libc's internals but as long as syscall exists nobody stops anybody from using it directly without libc. So maybe instead of doingget_user_pages_fast()just send a SIGSEGV to the process? At least user will realize there's some problem at earlier stage.
if the pointer is bogus, we currently return -EFAULT, is that not enough ! I'm fine if u want to change that to segv.
Quoted 6 lines
Quoted 4 lines
Otherwise it is extremely unlikely to start with a TLB entry with Read permissions, followed by syscall Trap only to find the entry missing, unless a global TLB flush came from other cores, right in the middle. But this syscall is not guaranteed to work with SMP anyways, so lets ignore any SMP misdoings here.
Well but that's exactly the situation I was debugging: we start from data from read-only page and on attempt to write back modified value COW machinery gets involved.
No exactly your situation. In your case the TLB entry *did* exist with Read permission. What I was pointing to is that case where it woudl vanish between user reading the backing page and making a syscall !
Quoted 10 lines
Quoted 5 lines
Now in case it was *an* uninitialized var, do we have to guarantee any well defined semantics for the kernel emulation of cmpxchg ? IMO it should be fine to return 0 or
-EFAULTetc. Infact-EFAULTis better as it will force a retry loop on user side, given the typical cmpxchg usage pattern.The problem is libc only expects to get a value read from memory. And in theory expected value might be -14 which is basically
-EFAULT. I'm not talking about 0 at all because in some cases that's exactly what user-space expects.
Quoted 6 lines
So if we read unexpected value then we'll just return it without even attempting to write.
If we read expected data but fail to write then we'll send a SIGSEGV and return whatever... let it be
-EFAULT- anyways the app will be killed on exit from this syscall.
I'm not sure what you mean here. I'm fine with adding segv kill semantics, but don't think complexity for get_user is worth it !
-Vineet
Hi Vineet,
Quoted 41 lines · Vineet Gupta
Quoted 32 lines · Alexey Brodkin
/*
Quoted 14 lines
Quoted 14 lines
* This is only for old cores lacking LLOCK/SCOND, which by defintion
arch/arc/kernel/process.c +4
-2@@ -60,23 +62,48 @@ SYSCALL_DEFINE3(arc_usr_cmpxchg, int *, uaddr, int, expected, int, new)/* Z indicates to userspace if operation succeded */ regs->status32 &= ~STATUS_Z_MASK ; -if (!access_ok (VERIFY_WRITE , uaddr,sizeof (int ))) -return -EFAULT ; + ret =access_ok (VERIFY_WRITE , uaddr,sizeof (*uaddr)); +if (!ret) + goto fail; +again:preempt_disable ();Quoted 12 lines
arch/arc/kernel/process.c SYSCALL_DEFINE3(arc_usr_cmpxchg, int *, uaddr, int, expected, int, new) · line 73
Quoted 7 lines
arch/arc/kernel/process.c +2
-5-if (__get_user (uval, uaddr)) - goto done; - -if (uval == expected) { -if (!__put_user (new, uaddr)) + ret =__get_user (val, uaddr); +if (ret == -EFAULT ) {Lets see if this warrants adding complexity ! This implies that TLB entry with Read permissions didn't exist for reading the var and page fault handler could not wire up even a zero page due to
preempt_disable, meaning it was something not touched by userspace already - sort of uninitialized variable in user code.Ok I completely missed the fact that fast path TLB miss handler is being executed even if we have preemption disabled. So given the mapping exist we do not need to retry with enabled preemption.
Quoted 7 lines
Still maybe I'm a bit paranoid here but IMHO it's good to be ready for a corner-case when the pointer is completely bogus and there's no mapping for him.
I understand that today we only expect this syscall to be used from libc's internals but as long as syscall exists nobody stops anybody from using it directly without libc. So maybe instead of doingget_user_pages_fast()just send a SIGSEGV to the process? At least user will realize there's some problem at earlier stage.
Quoted 2 lines
if the pointer is bogus, we currently return
-EFAULT, is that not enough ! I'm fine if u want to change that to segv.
Ok good.
Quoted 11 lines
Quoted 7 lines
Quoted 4 lines
Otherwise it is extremely unlikely to start with a TLB entry with Read permissions, followed by syscall Trap only to find the entry missing, unless a global TLB flush came from other cores, right in the middle. But this syscall is not guaranteed to work with SMP anyways, so lets ignore any SMP misdoings here.
Well but that's exactly the situation I was debugging: we start from data from read-only page and on attempt to write back modified value COW machinery gets involved.
No exactly your situation. In your case the TLB entry *did* exist with Read permission. What I was pointing to is that case where it woudl vanish between user reading the backing page and making a syscall !
Probably I'm missing something here. Indeed there's already TLB entry with READ permission and we need ProtV exception to happen to update this entry such that it becomes READ-WRITE enabled. And in its turn for ProtV exception to happen we need to enable preemption and execute __put_user().
Quoted 13 lines
Quoted 13 lines
Quoted 4 lines
Now in case it was *an* uninitialized var, do we have to guarantee any well defined semantics for the kernel emulation of cmpxchg ? IMO it should be fine to return 0 or
-EFAULTetc. Infact-EFAULTis better as it will force a retry loop on user side, given the typical cmpxchg usage pattern.The problem is libc only expects to get a value read from memory. And in theory expected value might be -14 which is basically
-EFAULT. I'm not talking about 0 at all because in some cases that's exactly what user-space expects.So if we read unexpected value then we'll just return it without even attempting to write.
Quoted 6 lines
Quoted 3 lines
If we read expected data but fail to write then we'll send a SIGSEGV and return whatever... let it be
-EFAULT- anyways the app will be killed on exit from this syscall.I'm not sure what you mean here. I'm fine with adding segv kill semantics, but don't think complexity for
get_useris worth it !
This complexity adds predictability - if we cannot read or write data we kill the app as if our normal LD/ST fails fro whatever reason.
-Alexey
Quoted 26 lines · Alexey Brodkin
arch/arc/kernel/process.c +8
-0@@ -47,7 +47,9 @@ SYSCALL_DEFINE0(arc_gettls) SYSCALL_DEFINE3(arc_usr_cmpxchg, int *, uaddr, int, expected, int, new) {struct pt_regs *regs =current_pt_regs (); +struct page *page; +u32 val; +int ret;/* * This is only for old cores lacking LLOCK/SCOND, which by defintion @@ -60,23 +62,48 @@ SYSCALL_DEFINE3(arc_usr_cmpxchg, int *, uaddr, int, expected, int, new)/* Z indicates to userspace if operation succeded */ regs->status32 &= ~STATUS_Z_MASK ; + ret =access_ok (VERIFY_WRITE , uaddr,sizeof (*uaddr)); +if (!ret) + goto fail; +again:preempt_disable (); + ret =__get_user (val, uaddr);
arch/arc/kernel/process.c SYSCALL_DEFINE3(arc_usr_cmpxchg, int *, uaddr, int, expected, int, new) · line 82
Quoted 10 lines
arch/arc/kernel/process.c +10
-0+if (ret == -EFAULT ) { +preempt_enable (); + ret =get_user_pages_fast ((unsigned long )uaddr,1 ,1 , &page); +if (ret <0 ) + goto fail; + +put_page (page); + goto again; + }else if (ret) + goto fail;
goto fail with preempt disabled.
Quoted 17 lines
arch/arc/kernel/process.c +10
-2+ +if (val == expected) { + ret =__put_user (new, uaddr); +if (!ret) regs->status32 |=STATUS_Z_MASK ; } -done:preempt_enable (); -return uval; +if (ret == -EFAULT ) { + ret =get_user_pages_fast ((unsigned long )uaddr,1 ,1 , &page); +if (ret <0 ) + goto fail; + +put_page (page);
arch/arc/kernel/process.c SYSCALL_DEFINE3(arc_usr_cmpxchg, int *, uaddr, int, expected, int, new) · line 107
Quoted 10 lines
arch/arc/kernel/process.c +9
-0+ goto again; + }else if (ret) + goto fail; + +return val; + +fail: +force_sig (SIGSEGV , current); +return ret; }
Sorry for the delay, but I would write it like:
arch/arc/kernel/process.c +36 -11
@@ -47,7 +47,9 @@ SYSCALL_DEFINE0(arc_gettls) SYSCALL_DEFINE3(arc_usr_cmpxchg, int *, uaddr, int, expected, int, new) {struct pt_regs *regs =current_pt_regs (); -int uval = -EFAULT ; +struct page *page; +u32 val; +int ret;/* * This is only for old cores lacking LLOCK/SCOND, which by defintion
@@ -60,23 +62,46 @@ SYSCALL_DEFINE3(arc_usr_cmpxchg, int *,/* Z indicates to userspace if operation succeded */ regs->status32 &= ~STATUS_Z_MASK ; -if (!access_ok (VERIFY_WRITE , uaddr,sizeof (int ))) -return -EFAULT ; + ret =access_ok (VERIFY_WRITE , uaddr,sizeof (*uaddr)); +if (!ret) + goto fail; +again:preempt_disable (); -if (__get_user (uval, uaddr)) - goto done; + ret =__get_user (val, uaddr); +if (ret) + goto fault; -if (uval == expected) { -if (!__put_user (new, uaddr)) - regs->status32 |=STATUS_Z_MASK ; - } +if (val != expected) + goto out; -done: + ret =__put_user (new, uaddr); +if (ret) + goto fault; + + regs->status32 |=STATUS_Z_MASK ; + +out:preempt_enable (); +return val; + +fault: +preempt_enable (); + +if (unlikely (ret != -EFAULT )) + goto fail; -return uval; +down_read (¤t->mm->mmap_sem); + ret =fixup_user_fault (current, current->mm, uaddr,FAULT_FLAG_WRITE ,NULL ); +up_read (¤t->mm->mmap_sem); + +if (likely (!ret)) + goto again; + +fail: +force_sig (SIGSEGV , current); +return ret; }#ifdef CONFIG_ISA_ARCV2