Skip to content

Commit ccf73cf

Browse files
htejungregkh
authored andcommitted
sysfs, kernfs: introduce kernfs[_find_and]_get() and kernfs_put()
Introduce kernfs interface for finding, getting and putting sysfs_dirents. * sysfs_find_dirent() is renamed to kernfs_find_ns() and lockdep assertion for sysfs_mutex is added. * sysfs_get_dirent_ns() is renamed to kernfs_find_and_get(). * Macro inline dancing around __sysfs_get/put() are removed and kernfs_get/put() are made proper functions implemented in fs/sysfs/dir.c. While the conversions are mostly equivalent, there's one difference - kernfs_get() doesn't return the input param as its return value. This change is intentional. While passing through the input increases writability in some areas, it is unnecessary and has been shown to cause confusion regarding how the last ref is handled. Signed-off-by: Tejun Heo <tj@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 517e64f commit ccf73cf

9 files changed

Lines changed: 151 additions & 148 deletions

File tree

‎fs/sysfs/dir.c‎

Lines changed: 67 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -240,10 +240,31 @@ static void sysfs_free_ino(unsigned int ino)
240240
spin_unlock(&sysfs_ino_lock);
241241
}
242242

243-
void release_sysfs_dirent(struct sysfs_dirent *sd)
243+
/**
244+
* kernfs_get - get a reference count on a sysfs_dirent
245+
* @sd: the target sysfs_dirent
246+
*/
247+
void kernfs_get(struct sysfs_dirent *sd)
248+
{
249+
if (sd) {
250+
WARN_ON(!atomic_read(&sd->s_count));
251+
atomic_inc(&sd->s_count);
252+
}
253+
}
254+
EXPORT_SYMBOL_GPL(kernfs_get);
255+
256+
/**
257+
* kernfs_put - put a reference count on a sysfs_dirent
258+
* @sd: the target sysfs_dirent
259+
*
260+
* Put a reference count of @sd and destroy it if it reached zero.
261+
*/
262+
void kernfs_put(struct sysfs_dirent *sd)
244263
{
245264
struct sysfs_dirent *parent_sd;
246265

266+
if (!sd || !atomic_dec_and_test(&sd->s_count))
267+
return;
247268
repeat:
248269
/* Moving/renaming is always done while holding reference.
249270
* sd->s_parent won't change beneath us.
@@ -255,7 +276,7 @@ void release_sysfs_dirent(struct sysfs_dirent *sd)
255276
parent_sd ? parent_sd->s_name : "", sd->s_name);
256277

257278
if (sysfs_type(sd) == SYSFS_KOBJ_LINK)
258-
sysfs_put(sd->s_symlink.target_sd);
279+
kernfs_put(sd->s_symlink.target_sd);
259280
if (sysfs_type(sd) & SYSFS_COPY_NAME)
260281
kfree(sd->s_name);
261282
if (sd->s_iattr && sd->s_iattr->ia_secdata)
@@ -269,6 +290,7 @@ void release_sysfs_dirent(struct sysfs_dirent *sd)
269290
if (sd && atomic_dec_and_test(&sd->s_count))
270291
goto repeat;
271292
}
293+
EXPORT_SYMBOL_GPL(kernfs_put);
272294

273295
static int sysfs_dentry_delete(const struct dentry *dentry)
274296
{
@@ -331,7 +353,7 @@ static int sysfs_dentry_revalidate(struct dentry *dentry, unsigned int flags)
331353

332354
static void sysfs_dentry_release(struct dentry *dentry)
333355
{
334-
sysfs_put(dentry->d_fsdata);
356+
kernfs_put(dentry->d_fsdata);
335357
}
336358

337359
const struct dentry_operations sysfs_dentry_ops = {
@@ -433,7 +455,8 @@ int sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd,
433455
return -EINVAL;
434456

435457
sd->s_hash = sysfs_name_hash(sd->s_name, sd->s_ns);
436-
sd->s_parent = sysfs_get(parent_sd);
458+
sd->s_parent = parent_sd;
459+
kernfs_get(parent_sd);
437460

438461
ret = sysfs_link_sibling(sd);
439462
if (ret)
@@ -553,36 +576,33 @@ void sysfs_addrm_finish(struct sysfs_addrm_cxt *acxt)
553576

554577
sysfs_deactivate(sd);
555578
sysfs_unmap_bin_file(sd);
556-
sysfs_put(sd);
579+
kernfs_put(sd);
557580
}
558581
}
559582

560583
/**
561-
* sysfs_find_dirent - find sysfs_dirent with the given name
562-
* @parent_sd: sysfs_dirent to search under
563-
* @name: name to look for
564-
* @ns: the namespace tag to use
565-
*
566-
* Look for sysfs_dirent with name @name under @parent_sd.
567-
*
568-
* LOCKING:
569-
* mutex_lock(sysfs_mutex)
584+
* kernfs_find_ns - find sysfs_dirent with the given name
585+
* @parent: sysfs_dirent to search under
586+
* @name: name to look for
587+
* @ns: the namespace tag to use
570588
*
571-
* RETURNS:
572-
* Pointer to sysfs_dirent if found, NULL if not.
589+
* Look for sysfs_dirent with name @name under @parent. Returns pointer to
590+
* the found sysfs_dirent on success, %NULL on failure.
573591
*/
574-
struct sysfs_dirent *sysfs_find_dirent(struct sysfs_dirent *parent_sd,
575-
const unsigned char *name,
576-
const void *ns)
592+
static struct sysfs_dirent *kernfs_find_ns(struct sysfs_dirent *parent,
593+
const unsigned char *name,
594+
const void *ns)
577595
{
578-
struct rb_node *node = parent_sd->s_dir.children.rb_node;
579-
bool has_ns = parent_sd->s_flags & SYSFS_FLAG_NS;
596+
struct rb_node *node = parent->s_dir.children.rb_node;
597+
bool has_ns = parent->s_flags & SYSFS_FLAG_NS;
580598
unsigned int hash;
581599

600+
lockdep_assert_held(&sysfs_mutex);
601+
582602
if (has_ns != (bool)ns) {
583603
WARN(1, KERN_WARNING "sysfs: ns %s in '%s' for '%s'\n",
584604
has_ns ? "required" : "invalid",
585-
parent_sd->s_name, name);
605+
parent->s_name, name);
586606
return NULL;
587607
}
588608

@@ -604,34 +624,28 @@ struct sysfs_dirent *sysfs_find_dirent(struct sysfs_dirent *parent_sd,
604624
}
605625

606626
/**
607-
* sysfs_get_dirent_ns - find and get sysfs_dirent with the given name
608-
* @parent_sd: sysfs_dirent to search under
609-
* @name: name to look for
610-
* @ns: the namespace tag to use
611-
*
612-
* Look for sysfs_dirent with name @name under @parent_sd and get
613-
* it if found.
614-
*
615-
* LOCKING:
616-
* Kernel thread context (may sleep). Grabs sysfs_mutex.
627+
* kernfs_find_and_get_ns - find and get sysfs_dirent with the given name
628+
* @parent: sysfs_dirent to search under
629+
* @name: name to look for
630+
* @ns: the namespace tag to use
617631
*
618-
* RETURNS:
619-
* Pointer to sysfs_dirent if found, NULL if not.
632+
* Look for sysfs_dirent with name @name under @parent and get a reference
633+
* if found. This function may sleep and returns pointer to the found
634+
* sysfs_dirent on success, %NULL on failure.
620635
*/
621-
struct sysfs_dirent *sysfs_get_dirent_ns(struct sysfs_dirent *parent_sd,
622-
const unsigned char *name,
623-
const void *ns)
636+
struct sysfs_dirent *kernfs_find_and_get_ns(struct sysfs_dirent *parent,
637+
const char *name, const void *ns)
624638
{
625639
struct sysfs_dirent *sd;
626640

627641
mutex_lock(&sysfs_mutex);
628-
sd = sysfs_find_dirent(parent_sd, name, ns);
629-
sysfs_get(sd);
642+
sd = kernfs_find_ns(parent, name, ns);
643+
kernfs_get(sd);
630644
mutex_unlock(&sysfs_mutex);
631645

632646
return sd;
633647
}
634-
EXPORT_SYMBOL_GPL(sysfs_get_dirent_ns);
648+
EXPORT_SYMBOL_GPL(kernfs_find_and_get_ns);
635649

636650
/**
637651
* kernfs_create_dir_ns - create a directory
@@ -667,7 +681,7 @@ struct sysfs_dirent *kernfs_create_dir_ns(struct sysfs_dirent *parent,
667681
if (!rc)
668682
return sd;
669683

670-
sysfs_put(sd);
684+
kernfs_put(sd);
671685
return ERR_PTR(rc);
672686
}
673687

@@ -716,14 +730,15 @@ static struct dentry *sysfs_lookup(struct inode *dir, struct dentry *dentry,
716730
if (parent_sd->s_flags & SYSFS_FLAG_NS)
717731
ns = sysfs_info(dir->i_sb)->ns;
718732

719-
sd = sysfs_find_dirent(parent_sd, dentry->d_name.name, ns);
733+
sd = kernfs_find_ns(parent_sd, dentry->d_name.name, ns);
720734

721735
/* no such entry */
722736
if (!sd) {
723737
ret = ERR_PTR(-ENOENT);
724738
goto out_unlock;
725739
}
726-
dentry->d_fsdata = sysfs_get(sd);
740+
kernfs_get(sd);
741+
dentry->d_fsdata = sd;
727742

728743
/* attach dentry and inode */
729744
inode = sysfs_get_inode(dir->i_sb, sd);
@@ -859,7 +874,7 @@ int kernfs_remove_by_name_ns(struct sysfs_dirent *dir_sd, const char *name,
859874

860875
sysfs_addrm_start(&acxt);
861876

862-
sd = sysfs_find_dirent(dir_sd, name, ns);
877+
sd = kernfs_find_ns(dir_sd, name, ns);
863878
if (sd)
864879
__kernfs_remove(&acxt, sd);
865880

@@ -925,7 +940,7 @@ int kernfs_rename_ns(struct sysfs_dirent *sd, struct sysfs_dirent *new_parent,
925940
goto out; /* nothing to rename */
926941

927942
error = -EEXIST;
928-
if (sysfs_find_dirent(new_parent, new_name, new_ns))
943+
if (kernfs_find_ns(new_parent, new_name, new_ns))
929944
goto out;
930945

931946
/* rename sysfs_dirent */
@@ -943,8 +958,8 @@ int kernfs_rename_ns(struct sysfs_dirent *sd, struct sysfs_dirent *new_parent,
943958
* Move to the appropriate place in the appropriate directories rbtree.
944959
*/
945960
sysfs_unlink_sibling(sd);
946-
sysfs_get(new_parent);
947-
sysfs_put(sd->s_parent);
961+
kernfs_get(new_parent);
962+
kernfs_put(sd->s_parent);
948963
sd->s_ns = new_ns;
949964
sd->s_hash = sysfs_name_hash(sd->s_name, sd->s_ns);
950965
sd->s_parent = new_parent;
@@ -1000,7 +1015,7 @@ static inline unsigned char dt_type(struct sysfs_dirent *sd)
10001015

10011016
static int sysfs_dir_release(struct inode *inode, struct file *filp)
10021017
{
1003-
sysfs_put(filp->private_data);
1018+
kernfs_put(filp->private_data);
10041019
return 0;
10051020
}
10061021

@@ -1011,7 +1026,7 @@ static struct sysfs_dirent *sysfs_dir_pos(const void *ns,
10111026
int valid = !(pos->s_flags & SYSFS_FLAG_REMOVED) &&
10121027
pos->s_parent == parent_sd &&
10131028
hash == pos->s_hash;
1014-
sysfs_put(pos);
1029+
kernfs_put(pos);
10151030
if (!valid)
10161031
pos = NULL;
10171032
}
@@ -1075,8 +1090,10 @@ static int sysfs_readdir(struct file *file, struct dir_context *ctx)
10751090
unsigned int type = dt_type(pos);
10761091
int len = strlen(name);
10771092
ino_t ino = pos->s_ino;
1093+
10781094
ctx->pos = pos->s_hash;
1079-
file->private_data = sysfs_get(pos);
1095+
file->private_data = pos;
1096+
kernfs_get(pos);
10801097

10811098
mutex_unlock(&sysfs_mutex);
10821099
if (!dir_emit(ctx, name, len, ino, type))

‎fs/sysfs/file.c‎

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -881,19 +881,19 @@ void sysfs_notify(struct kobject *k, const char *dir, const char *attr)
881881
struct sysfs_dirent *sd = k->sd, *tmp;
882882

883883
if (sd && dir)
884-
sd = sysfs_get_dirent(sd, dir);
884+
sd = kernfs_find_and_get(sd, dir);
885885
else
886-
sysfs_get(sd);
886+
kernfs_get(sd);
887887

888888
if (sd && attr) {
889-
tmp = sysfs_get_dirent(sd, attr);
890-
sysfs_put(sd);
889+
tmp = kernfs_find_and_get(sd, attr);
890+
kernfs_put(sd);
891891
sd = tmp;
892892
}
893893

894894
if (sd) {
895895
kernfs_notify(sd);
896-
sysfs_put(sd);
896+
kernfs_put(sd);
897897
}
898898
}
899899
EXPORT_SYMBOL_GPL(sysfs_notify);
@@ -1052,7 +1052,7 @@ struct sysfs_dirent *kernfs_create_file_ns_key(struct sysfs_dirent *parent,
10521052
sysfs_addrm_finish(&acxt);
10531053

10541054
if (rc) {
1055-
sysfs_put(sd);
1055+
kernfs_put(sd);
10561056
return ERR_PTR(rc);
10571057
}
10581058
return sd;
@@ -1106,16 +1106,18 @@ int sysfs_add_file_to_group(struct kobject *kobj,
11061106
struct sysfs_dirent *dir_sd;
11071107
int error;
11081108

1109-
if (group)
1110-
dir_sd = sysfs_get_dirent(kobj->sd, group);
1111-
else
1112-
dir_sd = sysfs_get(kobj->sd);
1109+
if (group) {
1110+
dir_sd = kernfs_find_and_get(kobj->sd, group);
1111+
} else {
1112+
dir_sd = kobj->sd;
1113+
kernfs_get(dir_sd);
1114+
}
11131115

11141116
if (!dir_sd)
11151117
return -ENOENT;
11161118

11171119
error = sysfs_add_file(dir_sd, attr, false);
1118-
sysfs_put(dir_sd);
1120+
kernfs_put(dir_sd);
11191121

11201122
return error;
11211123
}
@@ -1135,7 +1137,7 @@ int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr,
11351137
struct iattr newattrs;
11361138
int rc;
11371139

1138-
sd = sysfs_get_dirent(kobj->sd, attr->name);
1140+
sd = kernfs_find_and_get(kobj->sd, attr->name);
11391141
if (!sd)
11401142
return -ENOENT;
11411143

@@ -1144,7 +1146,7 @@ int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr,
11441146

11451147
rc = kernfs_setattr(sd, &newattrs);
11461148

1147-
sysfs_put(sd);
1149+
kernfs_put(sd);
11481150
return rc;
11491151
}
11501152
EXPORT_SYMBOL_GPL(sysfs_chmod_file);
@@ -1185,13 +1187,16 @@ void sysfs_remove_file_from_group(struct kobject *kobj,
11851187
{
11861188
struct sysfs_dirent *dir_sd;
11871189

1188-
if (group)
1189-
dir_sd = sysfs_get_dirent(kobj->sd, group);
1190-
else
1191-
dir_sd = sysfs_get(kobj->sd);
1190+
if (group) {
1191+
dir_sd = kernfs_find_and_get(kobj->sd, group);
1192+
} else {
1193+
dir_sd = kobj->sd;
1194+
kernfs_get(dir_sd);
1195+
}
1196+
11921197
if (dir_sd) {
11931198
kernfs_remove_by_name(dir_sd, attr->name);
1194-
sysfs_put(dir_sd);
1199+
kernfs_put(dir_sd);
11951200
}
11961201
}
11971202
EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);

0 commit comments

Comments
 (0)