From: Tejun Heo <[email protected]> Sysfs file poll implementation is scattered over sysfs and kobject. Event numbering is done in sysfs_dirent but wait itself is done on kobject. This not only unecessarily bloats both kobject and sysfs_dirent but is also buggy - if a sysfs_dirent is removed while there still are pollers, the associaton betwen the kobject and sysfs_dirent breaks and kobject may be freed with the pollers still sleeping on it. This patch moves whole poll implementation into sysfs_open_dirent. Each time a sysfs_open_dirent is created, event number restarts from 1 and pollers sleep on sysfs_open_dirent. As event sequence number is meaningless without any open file and pollers should have open file and thus sysfs_open_dirent, this ephemeral event counting works and is a saner implementation. This patch fixes the dnagling sleepers bug and reduces the sizes of kobject and sysfs_dirent by one pointer. Signed-off-by: Tejun Heo <[email protected]> Acked-by: Cornelia Huck <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]> --- fs/sysfs/dir.c | 1 - fs/sysfs/file.c | 25 +++++++++++++++++++------ fs/sysfs/sysfs.h | 1 - include/linux/kobject.h | 1 - lib/kobject.c | 1 - 5 files changed, 19 insertions(+), 10 deletions(-) diff --git a/fs/sysfs/dir.c b/fs/sysfs/dir.c index 4ad9422..e301a12 100644 --- a/fs/sysfs/dir.c +++ b/fs/sysfs/dir.c @@ -318,7 +318,6 @@ struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type) atomic_set(&sd->s_count, 1); atomic_set(&sd->s_active, 0); - atomic_set(&sd->s_event, 1); sd->s_name = name; sd->s_mode = mode; diff --git a/fs/sysfs/file.c b/fs/sysfs/file.c index b13ba94..c05f961 100644 --- a/fs/sysfs/file.c +++ b/fs/sysfs/file.c @@ -62,6 +62,8 @@ static spinlock_t sysfs_open_dirent_lock = SPIN_LOCK_UNLOCKED; struct sysfs_open_dirent { atomic_t refcnt; + atomic_t event; + wait_queue_head_t poll; struct list_head buffers; /* goes through sysfs_buffer.list */ }; @@ -104,7 +106,7 @@ static int fill_read_buffer(struct dentry * dentry, struct sysfs_buffer * buffer if (!sysfs_get_active_two(attr_sd)) return -ENODEV; - buffer->event = atomic_read(&attr_sd->s_event); + buffer->event = atomic_read(&attr_sd->s_attr.open->event); count = ops->show(kobj, attr_sd->s_attr.attr, buffer->page); sysfs_put_active_two(attr_sd); @@ -301,6 +303,8 @@ static int sysfs_get_open_dirent(struct sysfs_dirent *sd, return -ENOMEM; atomic_set(&new_od->refcnt, 0); + atomic_set(&new_od->event, 1); + init_waitqueue_head(&new_od->poll); INIT_LIST_HEAD(&new_od->buffers); goto retry; } @@ -443,17 +447,17 @@ static unsigned int sysfs_poll(struct file *filp, poll_table *wait) { struct sysfs_buffer * buffer = filp->private_data; struct sysfs_dirent *attr_sd = filp->f_path.dentry->d_fsdata; - struct kobject *kobj = attr_sd->s_parent->s_dir.kobj; + struct sysfs_open_dirent *od = attr_sd->s_attr.open; /* need parent for the kobj, grab both */ if (!sysfs_get_active_two(attr_sd)) goto trigger; - poll_wait(filp, &kobj->poll, wait); + poll_wait(filp, &od->poll, wait); sysfs_put_active_two(attr_sd); - if (buffer->event != atomic_read(&attr_sd->s_event)) + if (buffer->event != atomic_read(&od->event)) goto trigger; return 0; @@ -474,8 +478,17 @@ void sysfs_notify(struct kobject *k, char *dir, char *attr) if (sd && attr) sd = sysfs_find_dirent(sd, attr); if (sd) { - atomic_inc(&sd->s_event); - wake_up_interruptible(&k->poll); + struct sysfs_open_dirent *od; + + spin_lock(&sysfs_open_dirent_lock); + + od = sd->s_attr.open; + if (od) { + atomic_inc(&od->event); + wake_up_interruptible(&od->poll); + } + + spin_unlock(&sysfs_open_dirent_lock); } mutex_unlock(&sysfs_mutex); diff --git a/fs/sysfs/sysfs.h b/fs/sysfs/sysfs.h index 3adce7d..269c845 100644 --- a/fs/sysfs/sysfs.h +++ b/fs/sysfs/sysfs.h @@ -46,7 +46,6 @@ struct sysfs_dirent { ino_t s_ino; umode_t s_mode; struct iattr *s_iattr; - atomic_t s_event; }; #define SD_DEACTIVATED_BIAS INT_MIN diff --git a/include/linux/kobject.h b/include/linux/kobject.h index 0777b3f..a8a84fc 100644 --- a/include/linux/kobject.h +++ b/include/linux/kobject.h @@ -66,7 +66,6 @@ struct kobject { struct kset * kset; struct kobj_type * ktype; struct sysfs_dirent * sd; - wait_queue_head_t poll; }; extern int kobject_set_name(struct kobject *, const char *, ...) diff --git a/lib/kobject.c b/lib/kobject.c index e8181d3..fc6db6b 100644 --- a/lib/kobject.c +++ b/lib/kobject.c @@ -131,7 +131,6 @@ void kobject_init(struct kobject * kobj) return; kref_init(&kobj->kref); INIT_LIST_HEAD(&kobj->entry); - init_waitqueue_head(&kobj->poll); kobj->kset = kset_get(kobj->kset); } -- 1.5.3.4 - To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/
- Follow-Ups:
- [PATCH 67/75] driver core: remove subsystem_init()
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 67/75] driver core: remove subsystem_init()
- References:
- [PATCH 01/75] platform: prefix MODALIAS with "platform:"
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 02/75] HOWTO: update ja_JP/HOWTO with latest changes
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 03/75] Driver core: make sysfs uevent-attributes static
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 04/75] Driver core: change add_uevent_var to use a struct
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 05/75] Driver core: add CONFIG_UEVENT_HELPER_PATH
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 06/75] Driver core: remove subsys_set_kset
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 07/75] Driver core: remove kset_set_kset_s
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 08/75] Driver core: remove subsys_put()
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 09/75] Driver core: remove subsys_get()
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 10/75] Driver core: remove put_bus()
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 11/75] Driver core: remove get_bus()
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 12/75] kobjects: fix up improper use of the kobject name field
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 13/75] cdev: remove unneeded setting of cdev names
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 14/75] Drivers: clean up direct setting of the name of a kset
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 15/75] kobject: remove the static array for the name
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 16/75] Driver core: clean up removed functions from the documentation
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 17/75] debugfs: helper for decimal challenged
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 18/75] sysfs/file.c - use mutex instead of semaphore
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 19/75] sysfs: cleanup semaphore.h
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 20/75] sysfs: Remove first pass at shadow directory support
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 21/75] sysfs: cosmetic changes in sysfs_lookup()
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 22/75] sysfs: simplify sysfs_rename_dir()
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 23/75] sysfs: make sysfs_add/remove_one() call link/unlink_sibling() implictly
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 24/75] sysfs: make sysfs_add_one() automatically check for duplicate entry
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 25/75] sysfs: make sysfs_addrm_finish() return void
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 26/75] dmi-id: Use dynamic sysfs attributes
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 27/75] dmi-id: Possible cleanup
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 28/75] Convert from class_device to device for drivers/video
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 29/75] Convert from class_device to device in drivers/char
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 30/75] Driver core: exclude kobject_uevent.c for !CONFIG_HOTPLUG
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 31/75] Driver core: add uevent file for bus and driver
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 32/75] Driver core: kerneldoc - kobject_uevent_env is not "usually KOBJ_MOVE"
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 33/75] Fix Firmware class name collision
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 34/75] drivers/base/power/: make 2 functions static
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 35/75] sysfs: Fix typos in fs/sysfs/file.c
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 36/75] sysdev: remove global sysdev drivers list
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 37/75] Driver core: Make platform_device.id an int
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 38/75] sysfs: fix i_mutex locking in sysfs_get_dentry()
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 39/75] sysfs: Move all of inode initialization into sysfs_init_inode
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 40/75] sysfs: Remove sysfs_instantiate
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 41/75] sysfs: Use kill_anon_super
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 42/75] sysfs: Make sysfs_mount static
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 43/75] sysfs: In sysfs_lookup don't open code sysfs_find_dirent
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 44/75] sysfs: Simplify readdir.
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 45/75] sysfs: Rewrite sysfs_drop_dentry.
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 46/75] sysfs: Introduce sysfs_rename_mutex
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 47/75] sysfs: simply sysfs_get_dentry
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 48/75] sysfs: Remove s_dentry
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 49/75] sysfs: Rewrite rename in terms of sysfs dirents
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 50/75] sysfs: Rewrite sysfs_move_dir in terms of sysfs dirents
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 51/75] PTY: add kernel parameter to overwrite legacy pty count
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 52/75] sysfs: spit a warning to users when they try to create a duplicate sysfs file
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 53/75] sysfs: fix comments of sysfs_add/remove_one()
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 54/75] sysfs: fix sysfs_chmod_file() such that it updates sd->s_mode too
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 55/75] sysfs: clean up header files
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 56/75] sysfs: kill sysfs_update_file()
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 57/75] sysfs: reposition sysfs_dirent->s_mode.
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 58/75] sysfs: kill unnecessary sysfs_get() in open paths
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 59/75] sysfs: kill unnecessary NULL pointer check in sysfs_release()
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 60/75] sysfs: make bin attr open get active reference of parent too
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 61/75] sysfs: make s_elem an anonymous union
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 62/75] sysfs: open code sysfs_attach_dentry()
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 63/75] sysfs: make sysfs_root a regular directory dirent
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 64/75] sysfs: move sysfs_dirent->s_children into sysfs_dirent->s_dir
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 65/75] sysfs: implement sysfs_open_dirent
- From: Greg Kroah-Hartman <[email protected]>
- [PATCH 01/75] platform: prefix MODALIAS with "platform:"
- Prev by Date: [PATCH 65/75] sysfs: implement sysfs_open_dirent
- Next by Date: [PATCH 67/75] driver core: remove subsystem_init()
- Previous by thread: [PATCH 65/75] sysfs: implement sysfs_open_dirent
- Next by thread: [PATCH 67/75] driver core: remove subsystem_init()
- Index(es):