#include <linux/kobject.h>
#include <linux/namei.h>
#include <linux/idr.h>
+#include <linux/completion.h>
#include <asm/semaphore.h>
#include "sysfs.h"
*/
struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd)
{
- if (sd) {
- if (unlikely(!down_read_trylock(&sd->s_active)))
- sd = NULL;
+ if (unlikely(!sd))
+ return NULL;
+
+ while (1) {
+ int v, t;
+
+ v = atomic_read(&sd->s_active);
+ if (unlikely(v < 0))
+ return NULL;
+
+ t = atomic_cmpxchg(&sd->s_active, v, v + 1);
+ if (likely(t == v))
+ return sd;
+ if (t < 0)
+ return NULL;
+
+ cpu_relax();
}
- return sd;
}
/**
*/
void sysfs_put_active(struct sysfs_dirent *sd)
{
- if (sd)
- up_read(&sd->s_active);
+ struct completion *cmpl;
+ int v;
+
+ if (unlikely(!sd))
+ return;
+
+ v = atomic_dec_return(&sd->s_active);
+ if (likely(v != SD_DEACTIVATED_BIAS))
+ return;
+
+ /* atomic_dec_return() is a mb(), we'll always see the updated
+ * sd->s_sibling.next.
+ */
+ cmpl = (void *)sd->s_sibling.next;
+ complete(cmpl);
}
/**
* sysfs_deactivate - deactivate sysfs_dirent
* @sd: sysfs_dirent to deactivate
*
- * Deny new active references and drain existing ones. s_active
- * will be unlocked when the sysfs_dirent is released.
+ * Deny new active references and drain existing ones.
*/
void sysfs_deactivate(struct sysfs_dirent *sd)
{
- down_write_nested(&sd->s_active, SYSFS_S_ACTIVE_DEACTIVATE);
+ DECLARE_COMPLETION_ONSTACK(wait);
+ int v;
+
+ BUG_ON(!list_empty(&sd->s_sibling));
+ sd->s_sibling.next = (void *)&wait;
- /* s_active will be unlocked by the thread doing the final put
- * on @sd. Lie to lockdep.
+ /* atomic_add_return() is a mb(), put_active() will always see
+ * the updated sd->s_sibling.next.
*/
- rwsem_release(&sd->s_active.dep_map, 1, _RET_IP_);
+ v = atomic_add_return(SD_DEACTIVATED_BIAS, &sd->s_active);
+
+ if (v != SD_DEACTIVATED_BIAS)
+ wait_for_completion(&wait);
+
+ INIT_LIST_HEAD(&sd->s_sibling);
}
static int sysfs_alloc_ino(ino_t *pino)
repeat:
parent_sd = sd->s_parent;
- /* If @sd is being released after deletion, s_active is write
- * locked. If @sd is cursor for directory walk or being
- * released prematurely, s_active has no reader or writer.
- *
- * sysfs_deactivate() lies to lockdep that s_active is
- * unlocked immediately. Lie one more time to cover the
- * previous lie.
- */
- if (!down_write_trylock(&sd->s_active))
- rwsem_acquire(&sd->s_active.dep_map,
- SYSFS_S_ACTIVE_DEACTIVATE, 0, _RET_IP_);
- up_write(&sd->s_active);
-
if (sd->s_type & SYSFS_KOBJ_LINK)
sysfs_put(sd->s_elem.symlink.target_sd);
if (sd->s_type & SYSFS_COPY_NAME)
goto err_out;
atomic_set(&sd->s_count, 1);
+ atomic_set(&sd->s_active, 0);
atomic_set(&sd->s_event, 1);
- init_rwsem(&sd->s_active);
INIT_LIST_HEAD(&sd->s_children);
INIT_LIST_HEAD(&sd->s_sibling);
*/
struct sysfs_dirent {
atomic_t s_count;
- struct rw_semaphore s_active;
+ atomic_t s_active;
struct sysfs_dirent * s_parent;
struct list_head s_sibling;
struct list_head s_children;
atomic_t s_event;
};
-/*
- * A sysfs file which deletes another file when written to need to
- * write lock the s_active of the victim while its s_active is read
- * locked for the write operation. Tell lockdep that this is okay.
- */
-enum sysfs_s_active_class
-{
- SYSFS_S_ACTIVE_NORMAL, /* file r/w access, etc - default */
- SYSFS_S_ACTIVE_DEACTIVATE, /* file deactivation */
-};
+#define SD_DEACTIVATED_BIAS INT_MIN
extern struct vfsmount * sysfs_mount;
extern struct kmem_cache *sysfs_dir_cachep;