2015-06-11 21:31:40

by Waiman Long

[permalink] [raw]
Subject: [PATCH v2] selinux: reduce locking overhead in inode_free_security()

The inode_free_security() function just took the superblock's isec_lock
before checking and trying to remove the inode security struct from the
linked list. In many cases, the list was empty and so the lock taking
is wasteful as no useful work is done. On multi-socket systems with
a large number of CPUs, there can also be a fair amount of spinlock
contention on the isec_lock if many tasks are exiting at the same time.

This patch changes the code to check the state of the list first
before taking the lock and attempting to dequeue it. As this function
is called indirectly from __destroy_inode(), there can't be another
instance of inode_free_security() running on the same inode.

Signed-off-by: Waiman Long <[email protected]>
---
security/selinux/hooks.c | 15 ++++++++++++---
1 files changed, 12 insertions(+), 3 deletions(-)

v1->v2:
- Take out the second list_empty() test inside the lock.

diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
index 7dade28..e5cdad7 100644
--- a/security/selinux/hooks.c
+++ b/security/selinux/hooks.c
@@ -254,10 +254,19 @@ static void inode_free_security(struct inode *inode)
struct inode_security_struct *isec = inode->i_security;
struct superblock_security_struct *sbsec = inode->i_sb->s_security;

- spin_lock(&sbsec->isec_lock);
- if (!list_empty(&isec->list))
+ /*
+ * As not all inode security structures are in a list, we check for
+ * empty list outside of the lock to make sure that we won't waste
+ * time taking a lock doing nothing. As inode_free_security() is
+ * being called indirectly from __destroy_inode(), there is no way
+ * there can be two or more concurrent calls. So doing the list_empty()
+ * test outside the loop should be safe.
+ */
+ if (!list_empty(&isec->list)) {
+ spin_lock(&sbsec->isec_lock);
list_del_init(&isec->list);
- spin_unlock(&sbsec->isec_lock);
+ spin_unlock(&sbsec->isec_lock);
+ }

/*
* The inode may still be referenced in a path walk and
--
1.7.1


2015-06-12 06:45:23

by Raghavendra K T

[permalink] [raw]
Subject: Re: [PATCH v2] selinux: reduce locking overhead in inode_free_security()

On 06/12/2015 03:01 AM, Waiman Long wrote:
> The inode_free_security() function just took the superblock's isec_lock
> before checking and trying to remove the inode security struct from the
> linked list. In many cases, the list was empty and so the lock taking
> is wasteful as no useful work is done. On multi-socket systems with
> a large number of CPUs, there can also be a fair amount of spinlock
> contention on the isec_lock if many tasks are exiting at the same time.
>
> This patch changes the code to check the state of the list first
> before taking the lock and attempting to dequeue it. As this function
> is called indirectly from __destroy_inode(), there can't be another
> instance of inode_free_security() running on the same inode.
>
> Signed-off-by: Waiman Long <[email protected]>
> ---
> security/selinux/hooks.c | 15 ++++++++++++---
> 1 files changed, 12 insertions(+), 3 deletions(-)
>
> v1->v2:
> - Take out the second list_empty() test inside the lock.
>
> diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
> index 7dade28..e5cdad7 100644
> --- a/security/selinux/hooks.c
> +++ b/security/selinux/hooks.c
> @@ -254,10 +254,19 @@ static void inode_free_security(struct inode *inode)
> struct inode_security_struct *isec = inode->i_security;
> struct superblock_security_struct *sbsec = inode->i_sb->s_security;
>
> - spin_lock(&sbsec->isec_lock);
> - if (!list_empty(&isec->list))
> + /*
> + * As not all inode security structures are in a list, we check for
> + * empty list outside of the lock to make sure that we won't waste
> + * time taking a lock doing nothing. As inode_free_security() is
> + * being called indirectly from __destroy_inode(), there is no way
> + * there can be two or more concurrent calls. So doing the list_empty()
> + * test outside the loop should be safe.
> + */
> + if (!list_empty(&isec->list)) {
> + spin_lock(&sbsec->isec_lock);
> list_del_init(&isec->list);

Stupid question,

I need to take a look at list_del_init() code, but it can so happen that
if !list_empty() check could happen simultaneously, then serially two
list_del_init() can happen.

is that not a problem()?

> - spin_unlock(&sbsec->isec_lock);
> + spin_unlock(&sbsec->isec_lock);
> + }
>
> /*
> * The inode may still be referenced in a path walk and
>

2015-06-12 12:32:18

by Stephen Smalley

[permalink] [raw]
Subject: Re: [PATCH v2] selinux: reduce locking overhead in inode_free_security()

On 06/12/2015 02:26 AM, Raghavendra K T wrote:
> On 06/12/2015 03:01 AM, Waiman Long wrote:
>> The inode_free_security() function just took the superblock's isec_lock
>> before checking and trying to remove the inode security struct from the
>> linked list. In many cases, the list was empty and so the lock taking
>> is wasteful as no useful work is done. On multi-socket systems with
>> a large number of CPUs, there can also be a fair amount of spinlock
>> contention on the isec_lock if many tasks are exiting at the same time.
>>
>> This patch changes the code to check the state of the list first
>> before taking the lock and attempting to dequeue it. As this function
>> is called indirectly from __destroy_inode(), there can't be another
>> instance of inode_free_security() running on the same inode.
>>
>> Signed-off-by: Waiman Long <[email protected]>
>> ---
>> security/selinux/hooks.c | 15 ++++++++++++---
>> 1 files changed, 12 insertions(+), 3 deletions(-)
>>
>> v1->v2:
>> - Take out the second list_empty() test inside the lock.
>>
>> diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
>> index 7dade28..e5cdad7 100644
>> --- a/security/selinux/hooks.c
>> +++ b/security/selinux/hooks.c
>> @@ -254,10 +254,19 @@ static void inode_free_security(struct inode
>> *inode)
>> struct inode_security_struct *isec = inode->i_security;
>> struct superblock_security_struct *sbsec = inode->i_sb->s_security;
>>
>> - spin_lock(&sbsec->isec_lock);
>> - if (!list_empty(&isec->list))
>> + /*
>> + * As not all inode security structures are in a list, we check for
>> + * empty list outside of the lock to make sure that we won't waste
>> + * time taking a lock doing nothing. As inode_free_security() is
>> + * being called indirectly from __destroy_inode(), there is no way
>> + * there can be two or more concurrent calls. So doing the
>> list_empty()
>> + * test outside the loop should be safe.
>> + */
>> + if (!list_empty(&isec->list)) {
>> + spin_lock(&sbsec->isec_lock);
>> list_del_init(&isec->list);
>
> Stupid question,
>
> I need to take a look at list_del_init() code, but it can so happen that
> if !list_empty() check could happen simultaneously, then serially two
> list_del_init() can happen.
>
> is that not a problem()?

Hmm...I suppose that's possible (sb_finish_set_opts and
inode_free_security could both perform the list_del_init). Ok, we'll
stay with the first version.

2015-06-12 14:01:47

by Eric Paris

[permalink] [raw]
Subject: Re: [PATCH v2] selinux: reduce locking overhead in inode_free_security()

On Fri, 2015-06-12 at 08:31 -0400, Stephen Smalley wrote:
> On 06/12/2015 02:26 AM, Raghavendra K T wrote:
> > On 06/12/2015 03:01 AM, Waiman Long wrote:
> > > The inode_free_security() function just took the superblock's
> > > isec_lock
> > > before checking and trying to remove the inode security struct
> > > from the
> > > linked list. In many cases, the list was empty and so the lock
> > > taking
> > > is wasteful as no useful work is done. On multi-socket systems
> > > with
> > > a large number of CPUs, there can also be a fair amount of
> > > spinlock
> > > contention on the isec_lock if many tasks are exiting at the same
> > > time.
> > >
> > > This patch changes the code to check the state of the list first
> > > before taking the lock and attempting to dequeue it. As this
> > > function
> > > is called indirectly from __destroy_inode(), there can't be
> > > another
> > > instance of inode_free_security() running on the same inode.
> > >
> > > Signed-off-by: Waiman Long <[email protected]>
> > > ---
> > > security/selinux/hooks.c | 15 ++++++++++++---
> > > 1 files changed, 12 insertions(+), 3 deletions(-)
> > >
> > > v1->v2:
> > > - Take out the second list_empty() test inside the lock.
> > >
> > > diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
> > > index 7dade28..e5cdad7 100644
> > > --- a/security/selinux/hooks.c
> > > +++ b/security/selinux/hooks.c
> > > @@ -254,10 +254,19 @@ static void inode_free_security(struct
> > > inode
> > > *inode)
> > > struct inode_security_struct *isec = inode->i_security;
> > > struct superblock_security_struct *sbsec = inode->i_sb
> > > ->s_security;
> > >
> > > - spin_lock(&sbsec->isec_lock);
> > > - if (!list_empty(&isec->list))
> > > + /*
> > > + * As not all inode security structures are in a list, we
> > > check for
> > > + * empty list outside of the lock to make sure that we won't
> > > waste
> > > + * time taking a lock doing nothing. As
> > > inode_free_security() is
> > > + * being called indirectly from __destroy_inode(), there is
> > > no way
> > > + * there can be two or more concurrent calls. So doing the
> > > list_empty()
> > > + * test outside the loop should be safe.
> > > + */
> > > + if (!list_empty(&isec->list)) {
> > > + spin_lock(&sbsec->isec_lock);
> > > list_del_init(&isec->list);
> >
> > Stupid question,
> >
> > I need to take a look at list_del_init() code, but it can so happen
> > that
> > if !list_empty() check could happen simultaneously, then serially
> > two
> > list_del_init() can happen.
> >
> > is that not a problem()?
>
> Hmm...I suppose that's possible (sb_finish_set_opts and
> inode_free_security could both perform the list_del_init). Ok, we'll
> stay with the first version.

Wait, can't you list_del_init() an already list_del_init'd object.
Isn't that a big difference between list_del() and list_del_init() ?

2015-06-12 22:35:52

by Waiman Long

[permalink] [raw]
Subject: Re: [PATCH v2] selinux: reduce locking overhead in inode_free_security()

On 06/12/2015 08:31 AM, Stephen Smalley wrote:
> On 06/12/2015 02:26 AM, Raghavendra K T wrote:
>> On 06/12/2015 03:01 AM, Waiman Long wrote:
>>> The inode_free_security() function just took the superblock's isec_lock
>>> before checking and trying to remove the inode security struct from the
>>> linked list. In many cases, the list was empty and so the lock taking
>>> is wasteful as no useful work is done. On multi-socket systems with
>>> a large number of CPUs, there can also be a fair amount of spinlock
>>> contention on the isec_lock if many tasks are exiting at the same time.
>>>
>>> This patch changes the code to check the state of the list first
>>> before taking the lock and attempting to dequeue it. As this function
>>> is called indirectly from __destroy_inode(), there can't be another
>>> instance of inode_free_security() running on the same inode.
>>>
>>> Signed-off-by: Waiman Long<[email protected]>
>>> ---
>>> security/selinux/hooks.c | 15 ++++++++++++---
>>> 1 files changed, 12 insertions(+), 3 deletions(-)
>>>
>>> v1->v2:
>>> - Take out the second list_empty() test inside the lock.
>>>
>>> diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
>>> index 7dade28..e5cdad7 100644
>>> --- a/security/selinux/hooks.c
>>> +++ b/security/selinux/hooks.c
>>> @@ -254,10 +254,19 @@ static void inode_free_security(struct inode
>>> *inode)
>>> struct inode_security_struct *isec = inode->i_security;
>>> struct superblock_security_struct *sbsec = inode->i_sb->s_security;
>>>
>>> - spin_lock(&sbsec->isec_lock);
>>> - if (!list_empty(&isec->list))
>>> + /*
>>> + * As not all inode security structures are in a list, we check for
>>> + * empty list outside of the lock to make sure that we won't waste
>>> + * time taking a lock doing nothing. As inode_free_security() is
>>> + * being called indirectly from __destroy_inode(), there is no way
>>> + * there can be two or more concurrent calls. So doing the
>>> list_empty()
>>> + * test outside the loop should be safe.
>>> + */
>>> + if (!list_empty(&isec->list)) {
>>> + spin_lock(&sbsec->isec_lock);
>>> list_del_init(&isec->list);
>> Stupid question,
>>
>> I need to take a look at list_del_init() code, but it can so happen that
>> if !list_empty() check could happen simultaneously, then serially two
>> list_del_init() can happen.
>>
>> is that not a problem()?
> Hmm...I suppose that's possible (sb_finish_set_opts and
> inode_free_security could both perform the list_del_init). Ok, we'll
> stay with the first version.
>

Actually, list_del_init() can be applied twice with no harm being done.
The first list_del_init() will set list-> next = list->prev = list. The
second one will do the same thing and so it should be safe.

Cheers,
Longman

2015-06-13 07:35:47

by Yury Norov

[permalink] [raw]
Subject: Re: [PATCH v2] selinux: reduce locking overhead in inode_free_security()



On 13.06.2015 01:35, Waiman Long wrote:
> On 06/12/2015 08:31 AM, Stephen Smalley wrote:
>> On 06/12/2015 02:26 AM, Raghavendra K T wrote:
>>> On 06/12/2015 03:01 AM, Waiman Long wrote:
>>>> The inode_free_security() function just took the superblock's
>>>> isec_lock
>>>> before checking and trying to remove the inode security struct from
>>>> the
>>>> linked list. In many cases, the list was empty and so the lock taking
>>>> is wasteful as no useful work is done. On multi-socket systems with
>>>> a large number of CPUs, there can also be a fair amount of spinlock
>>>> contention on the isec_lock if many tasks are exiting at the same
>>>> time.
>>>>
>>>> This patch changes the code to check the state of the list first
>>>> before taking the lock and attempting to dequeue it. As this function
>>>> is called indirectly from __destroy_inode(), there can't be another
>>>> instance of inode_free_security() running on the same inode.
>>>>
>>>> Signed-off-by: Waiman Long<[email protected]>
>>>> ---
>>>> security/selinux/hooks.c | 15 ++++++++++++---
>>>> 1 files changed, 12 insertions(+), 3 deletions(-)
>>>>
>>>> v1->v2:
>>>> - Take out the second list_empty() test inside the lock.
>>>>
>>>> diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
>>>> index 7dade28..e5cdad7 100644
>>>> --- a/security/selinux/hooks.c
>>>> +++ b/security/selinux/hooks.c
>>>> @@ -254,10 +254,19 @@ static void inode_free_security(struct inode
>>>> *inode)
>>>> struct inode_security_struct *isec = inode->i_security;
>>>> struct superblock_security_struct *sbsec =
>>>> inode->i_sb->s_security;
>>>>
>>>> - spin_lock(&sbsec->isec_lock);
>>>> - if (!list_empty(&isec->list))
>>>> + /*
>>>> + * As not all inode security structures are in a list, we
>>>> check for
>>>> + * empty list outside of the lock to make sure that we won't
>>>> waste
>>>> + * time taking a lock doing nothing. As inode_free_security() is
>>>> + * being called indirectly from __destroy_inode(), there is no
>>>> way
>>>> + * there can be two or more concurrent calls. So doing the
>>>> list_empty()
>>>> + * test outside the loop should be safe.
>>>> + */
>>>> + if (!list_empty(&isec->list)) {
>>>> + spin_lock(&sbsec->isec_lock);
>>>> list_del_init(&isec->list);
>>> Stupid question,
>>>
>>> I need to take a look at list_del_init() code, but it can so happen
>>> that
>>> if !list_empty() check could happen simultaneously, then serially two
>>> list_del_init() can happen.
>>>
>>> is that not a problem()?
>> Hmm...I suppose that's possible (sb_finish_set_opts and
>> inode_free_security could both perform the list_del_init). Ok, we'll
>> stay with the first version.
>>
>
> Actually, list_del_init() can be applied twice with no harm being
> done. The first list_del_init() will set list-> next = list->prev =
> list. The second one will do the same thing and so it should be safe.
>
> Cheers,
> Longman
>

Hello, Waiman!

At first, minor.
For me, moving the line 'if (!list_empty(&isec->list))' out of lock is
not possible just because 'inode_free_security' is called from
'__destroy_inode' only. You cannot rely on it in future. It's rather
possible because empty list is invariant under 'list_del_init', as you
noted here. In fact, you can call 'list_del_init' unconditionally here,
and condition is the only optimization to decrease lock contention. So,
I'd like to ask you reflect it in your comment.

At second, less minor.
Now that you access list element outside of the lock, why don't you use
'list_empty_careful' instead of 'list_empty'? It may eliminate possible
race between, say, 'list_add' and 'list_empty', and costs you virtually
nothing.

Best regards,
Yury


2015-06-13 15:48:50

by Eric Paris

[permalink] [raw]
Subject: Re: [PATCH v2] selinux: reduce locking overhead in inode_free_security()

On Sat, 2015-06-13 at 10:35 +0300, Yury wrote:
>
> On 13.06.2015 01:35, Waiman Long wrote:
> > On 06/12/2015 08:31 AM, Stephen Smalley wrote:
> > > On 06/12/2015 02:26 AM, Raghavendra K T wrote:
> > > > On 06/12/2015 03:01 AM, Waiman Long wrote:
> > > > > The inode_free_security() function just took the superblock's
> > > > >
> > > > > isec_lock
> > > > > before checking and trying to remove the inode security
> > > > > struct from
> > > > > the
> > > > > linked list. In many cases, the list was empty and so the
> > > > > lock taking
> > > > > is wasteful as no useful work is done. On multi-socket
> > > > > systems with
> > > > > a large number of CPUs, there can also be a fair amount of
> > > > > spinlock
> > > > > contention on the isec_lock if many tasks are exiting at the
> > > > > same
> > > > > time.
> > > > >
> > > > > This patch changes the code to check the state of the list
> > > > > first
> > > > > before taking the lock and attempting to dequeue it. As this
> > > > > function
> > > > > is called indirectly from __destroy_inode(), there can't be
> > > > > another
> > > > > instance of inode_free_security() running on the same inode.
> > > > >
> > > > > Signed-off-by: Waiman Long<[email protected]>
> > > > > ---
> > > > > security/selinux/hooks.c | 15 ++++++++++++---
> > > > > 1 files changed, 12 insertions(+), 3 deletions(-)
> > > > >
> > > > > v1->v2:
> > > > > - Take out the second list_empty() test inside the lock.
> > > > >
> > > > > diff --git a/security/selinux/hooks.c
> > > > > b/security/selinux/hooks.c
> > > > > index 7dade28..e5cdad7 100644
> > > > > --- a/security/selinux/hooks.c
> > > > > +++ b/security/selinux/hooks.c
> > > > > @@ -254,10 +254,19 @@ static void inode_free_security(struct
> > > > > inode
> > > > > *inode)
> > > > > struct inode_security_struct *isec = inode
> > > > > ->i_security;
> > > > > struct superblock_security_struct *sbsec =
> > > > > inode->i_sb->s_security;
> > > > >
> > > > > - spin_lock(&sbsec->isec_lock);
> > > > > - if (!list_empty(&isec->list))
> > > > > + /*
> > > > > + * As not all inode security structures are in a list,
> > > > > we
> > > > > check for
> > > > > + * empty list outside of the lock to make sure that we
> > > > > won't
> > > > > waste
> > > > > + * time taking a lock doing nothing. As
> > > > > inode_free_security() is
> > > > > + * being called indirectly from __destroy_inode(), there
> > > > > is no
> > > > > way
> > > > > + * there can be two or more concurrent calls. So doing
> > > > > the
> > > > > list_empty()
> > > > > + * test outside the loop should be safe.
> > > > > + */
> > > > > + if (!list_empty(&isec->list)) {
> > > > > + spin_lock(&sbsec->isec_lock);
> > > > > list_del_init(&isec->list);
> > > > Stupid question,
> > > >
> > > > I need to take a look at list_del_init() code, but it can so
> > > > happen
> > > > that
> > > > if !list_empty() check could happen simultaneously, then
> > > > serially two
> > > > list_del_init() can happen.
> > > >
> > > > is that not a problem()?
> > > Hmm...I suppose that's possible (sb_finish_set_opts and
> > > inode_free_security could both perform the list_del_init). Ok,
> > > we'll
> > > stay with the first version.
> > >
> >
> > Actually, list_del_init() can be applied twice with no harm being
> > done. The first list_del_init() will set list-> next = list->prev =
> >
> > list. The second one will do the same thing and so it should be
> > safe.
> >
> > Cheers,
> > Longman
> >
>
> Hello, Waiman!
>
> At first, minor.
> For me, moving the line 'if (!list_empty(&isec->list))' out of lock
> is
> not possible just because 'inode_free_security' is called from
> '__destroy_inode' only. You cannot rely on it in future. It's rather
> possible because empty list is invariant under 'list_del_init', as
> you
> noted here. In fact, you can call 'list_del_init' unconditionally
> here,
> and condition is the only optimization to decrease lock contention.
> So,
> I'd like to ask you reflect it in your comment.
>
> At second, less minor.
> Now that you access list element outside of the lock, why don't you
> use
> 'list_empty_careful' instead of 'list_empty'? It may eliminate
> possible
> race between, say, 'list_add' and 'list_empty', and costs you
> virtually
> nothing.

Agree, the comment isn't really accurate. list_empty() outside of the
lock is safe because there is only one place one can ever get onto the
list. If you are already off (as most inodes will be!) the lock and
remove would be completely useless.

list_empty_careful() is not safe against list_add().

http://marc.info/?l=git-commits-head&m=107277005829348

I'm not even really sure what it is safe/useful for, but the comment
does seem like it would be fine for our case. I guess it might be
appropriate with the other task calling list_del_init(). In this case,
I don't believe we care to sync at all (especially since there can't be
another task, but whatever)

In any case, I agree with v2. But if people want to be 'extra safe' v1
is fine as well, the useless conditional branch will rarely ever happen
and if it does, wouldn't be in an area i'd care about one branch
performance hit.

-Eric

2015-06-14 04:20:03

by Raghavendra K T

[permalink] [raw]
Subject: Re: [PATCH v2] selinux: reduce locking overhead in inode_free_security()

On 06/13/2015 04:05 AM, Waiman Long wrote:
> On 06/12/2015 08:31 AM, Stephen Smalley wrote:
>> On 06/12/2015 02:26 AM, Raghavendra K T wrote:
>>> On 06/12/2015 03:01 AM, Waiman Long wrote:
>>>> The inode_free_security() function just took the superblock's isec_lock
>>>> before checking and trying to remove the inode security struct from the
>>>> linked list. In many cases, the list was empty and so the lock taking
>>>> is wasteful as no useful work is done. On multi-socket systems with
>>>> a large number of CPUs, there can also be a fair amount of spinlock
>>>> contention on the isec_lock if many tasks are exiting at the same time.
>>>>
>>>> This patch changes the code to check the state of the list first
>>>> before taking the lock and attempting to dequeue it. As this function
>>>> is called indirectly from __destroy_inode(), there can't be another
>>>> instance of inode_free_security() running on the same inode.
>>>>
>>>> Signed-off-by: Waiman Long<[email protected]>
>>>> ---
>>>> security/selinux/hooks.c | 15 ++++++++++++---
>>>> 1 files changed, 12 insertions(+), 3 deletions(-)
>>>>
>>>> v1->v2:
>>>> - Take out the second list_empty() test inside the lock.
>>>>
>>>> diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
>>>> index 7dade28..e5cdad7 100644
>>>> --- a/security/selinux/hooks.c
>>>> +++ b/security/selinux/hooks.c
>>>> @@ -254,10 +254,19 @@ static void inode_free_security(struct inode
>>>> *inode)
>>>> struct inode_security_struct *isec = inode->i_security;
>>>> struct superblock_security_struct *sbsec =
>>>> inode->i_sb->s_security;
>>>>
>>>> - spin_lock(&sbsec->isec_lock);
>>>> - if (!list_empty(&isec->list))
>>>> + /*
>>>> + * As not all inode security structures are in a list, we check
>>>> for
>>>> + * empty list outside of the lock to make sure that we won't waste
>>>> + * time taking a lock doing nothing. As inode_free_security() is
>>>> + * being called indirectly from __destroy_inode(), there is no way
>>>> + * there can be two or more concurrent calls. So doing the
>>>> list_empty()
>>>> + * test outside the loop should be safe.
>>>> + */
>>>> + if (!list_empty(&isec->list)) {
>>>> + spin_lock(&sbsec->isec_lock);
>>>> list_del_init(&isec->list);
>>> Stupid question,
>>>
>>> I need to take a look at list_del_init() code, but it can so happen that
>>> if !list_empty() check could happen simultaneously, then serially two
>>> list_del_init() can happen.
>>>
>>> is that not a problem()?
>> Hmm...I suppose that's possible (sb_finish_set_opts and
>> inode_free_security could both perform the list_del_init). Ok, we'll
>> stay with the first version.
>>
>
> Actually, list_del_init() can be applied twice with no harm being done.
> The first list_del_init() will set list-> next = list->prev = list. The
> second one will do the same thing and so it should be safe.
>

Waiman,
I do not think it is just about list_del_init() twice

what if


CPU1 CPU2 CPU3

!list_empty() !list_empty()

lock
list_del_init()
unlock

list_add()
lock
list_del_init
unlock

But this is valid only if list_add() is possible after first
list_del_init. I need to see code though.
OR am I missing something?











2015-06-15 13:39:50

by Stephen Smalley

[permalink] [raw]
Subject: Re: [PATCH v2] selinux: reduce locking overhead in inode_free_security()

On 06/14/2015 12:01 AM, Raghavendra K T wrote:
> On 06/13/2015 04:05 AM, Waiman Long wrote:
>> On 06/12/2015 08:31 AM, Stephen Smalley wrote:
>>> On 06/12/2015 02:26 AM, Raghavendra K T wrote:
>>>> On 06/12/2015 03:01 AM, Waiman Long wrote:
>>>>> The inode_free_security() function just took the superblock's
>>>>> isec_lock
>>>>> before checking and trying to remove the inode security struct from
>>>>> the
>>>>> linked list. In many cases, the list was empty and so the lock taking
>>>>> is wasteful as no useful work is done. On multi-socket systems with
>>>>> a large number of CPUs, there can also be a fair amount of spinlock
>>>>> contention on the isec_lock if many tasks are exiting at the same
>>>>> time.
>>>>>
>>>>> This patch changes the code to check the state of the list first
>>>>> before taking the lock and attempting to dequeue it. As this function
>>>>> is called indirectly from __destroy_inode(), there can't be another
>>>>> instance of inode_free_security() running on the same inode.
>>>>>
>>>>> Signed-off-by: Waiman Long<[email protected]>
>>>>> ---
>>>>> security/selinux/hooks.c | 15 ++++++++++++---
>>>>> 1 files changed, 12 insertions(+), 3 deletions(-)
>>>>>
>>>>> v1->v2:
>>>>> - Take out the second list_empty() test inside the lock.
>>>>>
>>>>> diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
>>>>> index 7dade28..e5cdad7 100644
>>>>> --- a/security/selinux/hooks.c
>>>>> +++ b/security/selinux/hooks.c
>>>>> @@ -254,10 +254,19 @@ static void inode_free_security(struct inode
>>>>> *inode)
>>>>> struct inode_security_struct *isec = inode->i_security;
>>>>> struct superblock_security_struct *sbsec =
>>>>> inode->i_sb->s_security;
>>>>>
>>>>> - spin_lock(&sbsec->isec_lock);
>>>>> - if (!list_empty(&isec->list))
>>>>> + /*
>>>>> + * As not all inode security structures are in a list, we check
>>>>> for
>>>>> + * empty list outside of the lock to make sure that we won't
>>>>> waste
>>>>> + * time taking a lock doing nothing. As inode_free_security() is
>>>>> + * being called indirectly from __destroy_inode(), there is no
>>>>> way
>>>>> + * there can be two or more concurrent calls. So doing the
>>>>> list_empty()
>>>>> + * test outside the loop should be safe.
>>>>> + */
>>>>> + if (!list_empty(&isec->list)) {
>>>>> + spin_lock(&sbsec->isec_lock);
>>>>> list_del_init(&isec->list);
>>>> Stupid question,
>>>>
>>>> I need to take a look at list_del_init() code, but it can so happen
>>>> that
>>>> if !list_empty() check could happen simultaneously, then serially two
>>>> list_del_init() can happen.
>>>>
>>>> is that not a problem()?
>>> Hmm...I suppose that's possible (sb_finish_set_opts and
>>> inode_free_security could both perform the list_del_init). Ok, we'll
>>> stay with the first version.
>>>
>>
>> Actually, list_del_init() can be applied twice with no harm being done.
>> The first list_del_init() will set list-> next = list->prev = list. The
>> second one will do the same thing and so it should be safe.
>>
>
> Waiman,
> I do not think it is just about list_del_init() twice
>
> what if
>
>
> CPU1 CPU2 CPU3
>
> !list_empty() !list_empty()
>
> lock
> list_del_init()
> unlock
>
> list_add()
> lock
> list_del_init
> unlock
>
> But this is valid only if list_add() is possible after first
> list_del_init. I need to see code though.
> OR am I missing something?

That should never be possible AFAICS. So I guess the second version is
also safe.

2015-06-15 16:57:40

by Waiman Long

[permalink] [raw]
Subject: Re: [PATCH v2] selinux: reduce locking overhead in inode_free_security()

On 06/13/2015 03:35 AM, Yury wrote:
>
>
> On 13.06.2015 01:35, Waiman Long wrote:
>> On 06/12/2015 08:31 AM, Stephen Smalley wrote:
>>> On 06/12/2015 02:26 AM, Raghavendra K T wrote:
>>>> On 06/12/2015 03:01 AM, Waiman Long wrote:
>>>>> The inode_free_security() function just took the superblock's
>>>>> isec_lock
>>>>> before checking and trying to remove the inode security struct
>>>>> from the
>>>>> linked list. In many cases, the list was empty and so the lock taking
>>>>> is wasteful as no useful work is done. On multi-socket systems with
>>>>> a large number of CPUs, there can also be a fair amount of spinlock
>>>>> contention on the isec_lock if many tasks are exiting at the same
>>>>> time.
>>>>>
>>>>> This patch changes the code to check the state of the list first
>>>>> before taking the lock and attempting to dequeue it. As this function
>>>>> is called indirectly from __destroy_inode(), there can't be another
>>>>> instance of inode_free_security() running on the same inode.
>>>>>
>>>>> Signed-off-by: Waiman Long<[email protected]>
>>>>> ---
>>>>> security/selinux/hooks.c | 15 ++++++++++++---
>>>>> 1 files changed, 12 insertions(+), 3 deletions(-)
>>>>>
>>>>> v1->v2:
>>>>> - Take out the second list_empty() test inside the lock.
>>>>>
>>>>> diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
>>>>> index 7dade28..e5cdad7 100644
>>>>> --- a/security/selinux/hooks.c
>>>>> +++ b/security/selinux/hooks.c
>>>>> @@ -254,10 +254,19 @@ static void inode_free_security(struct inode
>>>>> *inode)
>>>>> struct inode_security_struct *isec = inode->i_security;
>>>>> struct superblock_security_struct *sbsec =
>>>>> inode->i_sb->s_security;
>>>>>
>>>>> - spin_lock(&sbsec->isec_lock);
>>>>> - if (!list_empty(&isec->list))
>>>>> + /*
>>>>> + * As not all inode security structures are in a list, we
>>>>> check for
>>>>> + * empty list outside of the lock to make sure that we won't
>>>>> waste
>>>>> + * time taking a lock doing nothing. As inode_free_security() is
>>>>> + * being called indirectly from __destroy_inode(), there is
>>>>> no way
>>>>> + * there can be two or more concurrent calls. So doing the
>>>>> list_empty()
>>>>> + * test outside the loop should be safe.
>>>>> + */
>>>>> + if (!list_empty(&isec->list)) {
>>>>> + spin_lock(&sbsec->isec_lock);
>>>>> list_del_init(&isec->list);
>>>> Stupid question,
>>>>
>>>> I need to take a look at list_del_init() code, but it can so happen
>>>> that
>>>> if !list_empty() check could happen simultaneously, then serially two
>>>> list_del_init() can happen.
>>>>
>>>> is that not a problem()?
>>> Hmm...I suppose that's possible (sb_finish_set_opts and
>>> inode_free_security could both perform the list_del_init). Ok, we'll
>>> stay with the first version.
>>>
>>
>> Actually, list_del_init() can be applied twice with no harm being
>> done. The first list_del_init() will set list-> next = list->prev =
>> list. The second one will do the same thing and so it should be safe.
>>
>> Cheers,
>> Longman
>>
>
> Hello, Waiman!
>
> At first, minor.
> For me, moving the line 'if (!list_empty(&isec->list))' out of lock is
> not possible just because 'inode_free_security' is called from
> '__destroy_inode' only. You cannot rely on it in future. It's rather
> possible because empty list is invariant under 'list_del_init', as you
> noted here. In fact, you can call 'list_del_init' unconditionally
> here, and condition is the only optimization to decrease lock
> contention. So, I'd like to ask you reflect it in your comment.
>

I will send out an updated patch with the correct comment and commit log.

> At second, less minor.
> Now that you access list element outside of the lock, why don't you
> use 'list_empty_careful' instead of 'list_empty'? It may eliminate
> possible race between, say, 'list_add' and 'list_empty', and costs you
> virtually nothing.
>
> Best regards,
> Yury


I don't think it is possible to have concurrent list_add() and
list_empty() for this particular case. However, I also don't see any
downside of using list_empty_careful() neither. So I can make the change.

Cheers,
Longman