Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mszeredi...
[firefly-linux-kernel-4.4.55.git] / fs / sysfs / dir.c
index e14512678c9b7b8042f5ba55a329a0101849f606..e8e0e71b29d54228bd3bd9fe95cd44e39422b2aa 100644 (file)
@@ -165,21 +165,8 @@ struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd)
        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))
-                       break;
-               if (t < 0)
-                       return NULL;
-
-               cpu_relax();
-       }
+       if (!atomic_inc_unless_negative(&sd->s_active))
+               return NULL;
 
        if (likely(!ignore_lockdep(sd)))
                rwsem_acquire_read(&sd->dep_map, 0, 1, _RET_IP_);
@@ -281,6 +268,10 @@ void release_sysfs_dirent(struct sysfs_dirent * sd)
         */
        parent_sd = sd->s_parent;
 
+       WARN(!(sd->s_flags & SYSFS_FLAG_REMOVED),
+               "sysfs: free using entry: %s/%s\n",
+               parent_sd ? parent_sd->s_name : "", sd->s_name);
+
        if (sysfs_type(sd) == SYSFS_KOBJ_LINK)
                sysfs_put(sd->s_symlink.target_sd);
        if (sysfs_type(sd) & SYSFS_COPY_NAME)
@@ -399,7 +390,7 @@ struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type)
 
        sd->s_name = name;
        sd->s_mode = mode;
-       sd->s_flags = type;
+       sd->s_flags = type | SYSFS_FLAG_REMOVED;
 
        return sd;
 
@@ -479,6 +470,9 @@ int __sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
                ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME;
        }
 
+       /* Mark the entry added into directory tree */
+       sd->s_flags &= ~SYSFS_FLAG_REMOVED;
+
        return 0;
 }
 
@@ -1012,6 +1006,7 @@ static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
        enum kobj_ns_type type;
        const void *ns;
        ino_t ino;
+       loff_t off;
 
        type = sysfs_ns_type(parent_sd);
        ns = sysfs_info(dentry->d_sb)->ns[type];
@@ -1034,6 +1029,7 @@ static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
                        return 0;
        }
        mutex_lock(&sysfs_mutex);
+       off = filp->f_pos;
        for (pos = sysfs_dir_pos(ns, parent_sd, filp->f_pos, pos);
             pos;
             pos = sysfs_dir_next_pos(ns, parent_sd, filp->f_pos, pos)) {
@@ -1045,19 +1041,24 @@ static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
                len = strlen(name);
                ino = pos->s_ino;
                type = dt_type(pos);
-               filp->f_pos = pos->s_hash;
+               off = filp->f_pos = pos->s_hash;
                filp->private_data = sysfs_get(pos);
 
                mutex_unlock(&sysfs_mutex);
-               ret = filldir(dirent, name, len, filp->f_pos, ino, type);
+               ret = filldir(dirent, name, len, off, ino, type);
                mutex_lock(&sysfs_mutex);
                if (ret < 0)
                        break;
        }
        mutex_unlock(&sysfs_mutex);
-       if ((filp->f_pos > 1) && !pos) { /* EOF */
-               filp->f_pos = INT_MAX;
+
+       /* don't reference last entry if its refcount is dropped */
+       if (!pos) {
                filp->private_data = NULL;
+
+               /* EOF and not changed as 0 or 1 in read/write path */
+               if (off == filp->f_pos && off > 1)
+                       filp->f_pos = INT_MAX;
        }
        return 0;
 }