Keys: Improve usage of memory barriers and remove IRQ disablement
Subsystems (2)
David Howells
maintainerPATCH 1/1 Keys: Improve usage of memory barriers and remove IRQ disablement
The attached patch adds a missing memory barrier into the key_put() and removes an unnecessary barrier from install_session_keyring().
install_session_keyring() is also rearranged a little to make it slightly more efficient.
As install_*_keyring() may schedule (in synchronize_rcu() or keyring_alloc()), they may not be entered with interrupts disabled - and so there's no point saving the interrupt disablement state over the critical section.
exec_keys() will also be invoked with interrupts enabled, and so that doesn't need to save the interrupt state either.
---
security/keys/key.c | 1 + security/keys/process_keys.c | 41 ++++++++++++++++++++--------------------- 2 files changed, 21 insertions(+), 21 deletions(-)
security/keys/key.c +1 -0
@@ -619,6 +619,7 @@ void key_put(struct key *key)if (key) {key_check (key); +smp_mb__before_atomic_dec ();if (atomic_dec_and_test (&key->usage))schedule_work (&key_cleanup_task); }
security/keys/process_keys.c +20 -21
98 lines not on this page · raw message
David Howells
maintainerQuoted 3 lines · David Howells
The attached patch adds a missing memory barrier into the
key_put()and removes an unnecessary barrier frominstall_session_keyring().
...
security/keys/key.c void key_put(struct key *key) · line 624
Quoted 11 lines · David Howells
security/keys/key.c +1
-0@@ -619,6 +619,7 @@ void key_put(struct key *key)if (key) {key_check (key); +smp_mb__before_atomic_dec ();if (atomic_dec_and_test (&key->usage))schedule_work (&key_cleanup_task);
Shouldn't be needed: Documentation/atomic_ops.txt specifies that any atomic_ which both modifies its atomic operand and returns something is to be a full barrier before and after the operation.
This misuse occurs a few times in core code, which makes it a bit confusing.
However, I think it is nice to add a comment if these implicit barriers are used, if the purpose is not really obvious.
Signature · 2 lines
SUSE Labs, Novell Inc. Send instant messages to your online friends http://au.messenger.yahoo.com
David Howells
maintainerQuoted 3 lines · Nick Piggin
Shouldn't be needed:
Documentation/atomic_ops.txtspecifies that anyatomic_which both modifies its atomic operand and returns something is to be a full barrier before and after the operation.
Hmmm... It's possible that I've misunderstood what atomic_ops.txt actually says. For instance:
| int atomic_inc_and_test(atomic_t *v); | int atomic_dec_and_test(atomic_t *v); | | These two routines increment and decrement by 1, respectively, the | given atomic counter. They return a boolean indicating whether the | resulting counter value was zero or not. | | It requires explicit memory barrier semantics around the operation as | above.
Note the last paragraph. "It requires" should be "They require", but the sense would seem to be obvious. However, it's not clear on a second reading as to whether this is an instruction to the _caller_ or an instruction to the arch _implementer_.
I suppose from reading the abstract at the top:
| This document is intended to serve as a guide to Linux port maintainers on | how to implement atomic counter, bitops, and spinlock interfaces properly.
that it is meant to be read by the implementor and not the user/caller, in which case, Nick is correct.
It seems I need to adjust my memory barrier doc, and perhaps I should adjust atomic_ops.txt too to make it clearer.
David
Quoted 10 lines · David Howells
Nick Piggin <[email protected]> wrote:
| int atomic_inc_and_test(atomic_t *v); | int atomic_dec_and_test(atomic_t *v); | | These two routines increment and decrement by 1, respectively, the | given atomic counter. They return a boolean indicating whether the | resulting counter value was zero or not. | | It requires explicit memory barrier semantics around the operation as | above.
Quoted 5 lines
Note the last paragraph. "It requires" should be "They require", but the sense would seem to be obvious. However, it's not clear on a second reading as to whether this is an instruction to the _caller_ or an instruction to the arch _implementer_.
Yes, I remember Dave M clarified this sometime ago (on lkml I guess). It is a little confusing, but I think the wording is for the implementer's point of view. Dave will pull me up if I'm wrong...
Quoted 11 lines
I suppose from reading the abstract at the top:
| This document is intended to serve as a guide to Linux port maintainers on | how to implement atomic counter, bitops, and spinlock interfaces properly.
that it is meant to be read by the implementor and not the user/caller, in which case, Nick is correct.
It seems I need to adjust my memory barrier doc, and perhaps I should adjust atomic_ops.txt too to make it clearer.
I think that would be good. atomic_ops.txt is very useful for API users as well, so if it can be made more general without becoming ambiguous, I'm sure that would be appreciated.
Thanks,
Nick
Signature · 2 lines
SUSE Labs, Novell Inc. Send instant messages to your online friends http://au.messenger.yahoo.com
From: Nick Piggin <[email protected]>
Date: Wed, 05 Apr 2006 19:23:58 +1000
Quoted 19 lines
Quoted 12 lines · David Howells
Nick Piggin <[email protected]> wrote:
| int atomic_inc_and_test(atomic_t *v); | int atomic_dec_and_test(atomic_t *v); | | These two routines increment and decrement by 1, respectively, the | given atomic counter. They return a boolean indicating whether the | resulting counter value was zero or not. | | It requires explicit memory barrier semantics around the operation as | above.Quoted 5 lines
Note the last paragraph. "It requires" should be "They require", but the sense would seem to be obvious. However, it's not clear on a second reading as to whether this is an instruction to the _caller_ or an instruction to the arch _implementer_.
Quoted 3 lines
Yes, I remember Dave M clarified this sometime ago (on lkml I guess). It is a little confusing, but I think the wording is for the implementer's point of view. Dave will pull me up if I'm wrong...
Any routine which returns state must have the barriers in the arch implementation. These two routines returns state.