Re: [PATCH] sysfs: move assignment to be under lock insysfs_remove_dir()

From: Tejun Heo
Date: Wed Oct 30 2013 - 09:14:37 EST

Hey, guys.

On Tue, Oct 29, 2013 at 03:09:39PM -0700, Greg KH wrote:
> From: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx>
> Linus noticed that the assignment of sd isn't protected by the lock in
> sysfs_remove_dir(), so move the assignment of the variable under the
> lock to be safe.
> Reported-by: Linus Torvalds <torvalds@xxxxxxxxxxxxxxxxxxxx>
> Signed-off-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx>
> ---
> Tejun, any objection to this patch? You consolidated the locks back in
> 2007 on this function, and nothing has changed there since then, so odds
> are it's not a problem, but nice to be safe, right?

IIRC, it was a lock added to specifically address race between kobject
removal and someone else symlinking to it. In all other cases, the
rule is that the kobject owner is responsible for ensuring that
removal doesn't race against other sysfs operations on the kobject.
However, someone else linking to it didn't have any way to ensure that
kobj->sd won't be removed underneath it - it holds the target kobject
but that doesn't do anything to prevent the target's owner from
disassociating the kobject from its sysfs_dirent.

So, sysfs_assoc_lock is protecting against that specific scenario - it
guarantees that either kobj->sd is disassociated and symlinking sees
that or symlinking grabs sysfs_dirent's reference before
disassociation happens. It addresses the one exception case of the
general "kobject owner handles exclusion among operations" rule.

Moving "sd = kobj->sd" inside the spinlock doesn't make any difference
but what we really need is a comment explaining what that odd piece of
locking is about.


To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at
Please read the FAQ at