[PATCH] kobj_map semaphore to mutex conversion

Convert the kobj_map code to use a mutex instead of a semaphore.  It
converts the single two users as well, genhd.c and char_dev.c.

Signed-off-by: Jes Sorensen <jes@sgi.com>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
This commit is contained in:
Jes Sorensen 2006-02-06 14:12:43 -08:00 committed by Greg Kroah-Hartman
parent 8b5536bbee
commit 58383af629
4 changed files with 38 additions and 35 deletions

View file

@ -15,12 +15,13 @@
#include <linux/kmod.h> #include <linux/kmod.h>
#include <linux/kobj_map.h> #include <linux/kobj_map.h>
#include <linux/buffer_head.h> #include <linux/buffer_head.h>
#include <linux/mutex.h>
#define MAX_PROBE_HASH 255 /* random */ #define MAX_PROBE_HASH 255 /* random */
static struct subsystem block_subsys; static struct subsystem block_subsys;
static DECLARE_MUTEX(block_subsys_sem); static DEFINE_MUTEX(block_subsys_lock);
/* /*
* Can be deleted altogether. Later. * Can be deleted altogether. Later.
@ -46,7 +47,7 @@ struct blkdev_info {
/* /*
* iterate over a list of blkdev_info structures. allows * iterate over a list of blkdev_info structures. allows
* the major_names array to be iterated over from outside this file * the major_names array to be iterated over from outside this file
* must be called with the block_subsys_sem held * must be called with the block_subsys_lock held
*/ */
void *get_next_blkdev(void *dev) void *get_next_blkdev(void *dev)
{ {
@ -85,20 +86,20 @@ out:
void *acquire_blkdev_list(void) void *acquire_blkdev_list(void)
{ {
down(&block_subsys_sem); mutex_lock(&block_subsys_lock);
return get_next_blkdev(NULL); return get_next_blkdev(NULL);
} }
void release_blkdev_list(void *dev) void release_blkdev_list(void *dev)
{ {
up(&block_subsys_sem); mutex_unlock(&block_subsys_lock);
kfree(dev); kfree(dev);
} }
/* /*
* Count the number of records in the blkdev_list. * Count the number of records in the blkdev_list.
* must be called with the block_subsys_sem held * must be called with the block_subsys_lock held
*/ */
int count_blkdev_list(void) int count_blkdev_list(void)
{ {
@ -118,7 +119,7 @@ int count_blkdev_list(void)
/* /*
* extract the major and name values from a blkdev_info struct * extract the major and name values from a blkdev_info struct
* passed in as a void to *dev. Must be called with * passed in as a void to *dev. Must be called with
* block_subsys_sem held * block_subsys_lock held
*/ */
int get_blkdev_info(void *dev, int *major, char **name) int get_blkdev_info(void *dev, int *major, char **name)
{ {
@ -138,7 +139,7 @@ int register_blkdev(unsigned int major, const char *name)
struct blk_major_name **n, *p; struct blk_major_name **n, *p;
int index, ret = 0; int index, ret = 0;
down(&block_subsys_sem); mutex_lock(&block_subsys_lock);
/* temporary */ /* temporary */
if (major == 0) { if (major == 0) {
@ -183,7 +184,7 @@ int register_blkdev(unsigned int major, const char *name)
kfree(p); kfree(p);
} }
out: out:
up(&block_subsys_sem); mutex_unlock(&block_subsys_lock);
return ret; return ret;
} }
@ -197,7 +198,7 @@ int unregister_blkdev(unsigned int major, const char *name)
int index = major_to_index(major); int index = major_to_index(major);
int ret = 0; int ret = 0;
down(&block_subsys_sem); mutex_lock(&block_subsys_lock);
for (n = &major_names[index]; *n; n = &(*n)->next) for (n = &major_names[index]; *n; n = &(*n)->next)
if ((*n)->major == major) if ((*n)->major == major)
break; break;
@ -207,7 +208,7 @@ int unregister_blkdev(unsigned int major, const char *name)
p = *n; p = *n;
*n = p->next; *n = p->next;
} }
up(&block_subsys_sem); mutex_unlock(&block_subsys_lock);
kfree(p); kfree(p);
return ret; return ret;
@ -301,7 +302,7 @@ static void *part_start(struct seq_file *part, loff_t *pos)
struct list_head *p; struct list_head *p;
loff_t l = *pos; loff_t l = *pos;
down(&block_subsys_sem); mutex_lock(&block_subsys_lock);
list_for_each(p, &block_subsys.kset.list) list_for_each(p, &block_subsys.kset.list)
if (!l--) if (!l--)
return list_entry(p, struct gendisk, kobj.entry); return list_entry(p, struct gendisk, kobj.entry);
@ -318,7 +319,7 @@ static void *part_next(struct seq_file *part, void *v, loff_t *pos)
static void part_stop(struct seq_file *part, void *v) static void part_stop(struct seq_file *part, void *v)
{ {
up(&block_subsys_sem); mutex_unlock(&block_subsys_lock);
} }
static int show_partition(struct seq_file *part, void *v) static int show_partition(struct seq_file *part, void *v)
@ -377,7 +378,7 @@ static struct kobject *base_probe(dev_t dev, int *part, void *data)
static int __init genhd_device_init(void) static int __init genhd_device_init(void)
{ {
bdev_map = kobj_map_init(base_probe, &block_subsys_sem); bdev_map = kobj_map_init(base_probe, &block_subsys_lock);
blk_dev_init(); blk_dev_init();
subsystem_register(&block_subsys); subsystem_register(&block_subsys);
return 0; return 0;
@ -611,7 +612,7 @@ static void *diskstats_start(struct seq_file *part, loff_t *pos)
loff_t k = *pos; loff_t k = *pos;
struct list_head *p; struct list_head *p;
down(&block_subsys_sem); mutex_lock(&block_subsys_lock);
list_for_each(p, &block_subsys.kset.list) list_for_each(p, &block_subsys.kset.list)
if (!k--) if (!k--)
return list_entry(p, struct gendisk, kobj.entry); return list_entry(p, struct gendisk, kobj.entry);
@ -628,7 +629,7 @@ static void *diskstats_next(struct seq_file *part, void *v, loff_t *pos)
static void diskstats_stop(struct seq_file *part, void *v) static void diskstats_stop(struct seq_file *part, void *v)
{ {
up(&block_subsys_sem); mutex_unlock(&block_subsys_lock);
} }
static int diskstats_show(struct seq_file *s, void *v) static int diskstats_show(struct seq_file *s, void *v)

View file

@ -11,6 +11,7 @@
#include <linux/module.h> #include <linux/module.h>
#include <linux/slab.h> #include <linux/slab.h>
#include <linux/mutex.h>
#include <linux/kdev_t.h> #include <linux/kdev_t.h>
#include <linux/kobject.h> #include <linux/kobject.h>
#include <linux/kobj_map.h> #include <linux/kobj_map.h>
@ -25,7 +26,7 @@ struct kobj_map {
int (*lock)(dev_t, void *); int (*lock)(dev_t, void *);
void *data; void *data;
} *probes[255]; } *probes[255];
struct semaphore *sem; struct mutex *lock;
}; };
int kobj_map(struct kobj_map *domain, dev_t dev, unsigned long range, int kobj_map(struct kobj_map *domain, dev_t dev, unsigned long range,
@ -53,7 +54,7 @@ int kobj_map(struct kobj_map *domain, dev_t dev, unsigned long range,
p->range = range; p->range = range;
p->data = data; p->data = data;
} }
down(domain->sem); mutex_lock(domain->lock);
for (i = 0, p -= n; i < n; i++, p++, index++) { for (i = 0, p -= n; i < n; i++, p++, index++) {
struct probe **s = &domain->probes[index % 255]; struct probe **s = &domain->probes[index % 255];
while (*s && (*s)->range < range) while (*s && (*s)->range < range)
@ -61,7 +62,7 @@ int kobj_map(struct kobj_map *domain, dev_t dev, unsigned long range,
p->next = *s; p->next = *s;
*s = p; *s = p;
} }
up(domain->sem); mutex_unlock(domain->lock);
return 0; return 0;
} }
@ -75,7 +76,7 @@ void kobj_unmap(struct kobj_map *domain, dev_t dev, unsigned long range)
if (n > 255) if (n > 255)
n = 255; n = 255;
down(domain->sem); mutex_lock(domain->lock);
for (i = 0; i < n; i++, index++) { for (i = 0; i < n; i++, index++) {
struct probe **s; struct probe **s;
for (s = &domain->probes[index % 255]; *s; s = &(*s)->next) { for (s = &domain->probes[index % 255]; *s; s = &(*s)->next) {
@ -88,7 +89,7 @@ void kobj_unmap(struct kobj_map *domain, dev_t dev, unsigned long range)
} }
} }
} }
up(domain->sem); mutex_unlock(domain->lock);
kfree(found); kfree(found);
} }
@ -99,7 +100,7 @@ struct kobject *kobj_lookup(struct kobj_map *domain, dev_t dev, int *index)
unsigned long best = ~0UL; unsigned long best = ~0UL;
retry: retry:
down(domain->sem); mutex_lock(domain->lock);
for (p = domain->probes[MAJOR(dev) % 255]; p; p = p->next) { for (p = domain->probes[MAJOR(dev) % 255]; p; p = p->next) {
struct kobject *(*probe)(dev_t, int *, void *); struct kobject *(*probe)(dev_t, int *, void *);
struct module *owner; struct module *owner;
@ -120,7 +121,7 @@ retry:
module_put(owner); module_put(owner);
continue; continue;
} }
up(domain->sem); mutex_unlock(domain->lock);
kobj = probe(dev, index, data); kobj = probe(dev, index, data);
/* Currently ->owner protects _only_ ->probe() itself. */ /* Currently ->owner protects _only_ ->probe() itself. */
module_put(owner); module_put(owner);
@ -128,11 +129,11 @@ retry:
return kobj; return kobj;
goto retry; goto retry;
} }
up(domain->sem); mutex_unlock(domain->lock);
return NULL; return NULL;
} }
struct kobj_map *kobj_map_init(kobj_probe_t *base_probe, struct semaphore *sem) struct kobj_map *kobj_map_init(kobj_probe_t *base_probe, struct mutex *lock)
{ {
struct kobj_map *p = kmalloc(sizeof(struct kobj_map), GFP_KERNEL); struct kobj_map *p = kmalloc(sizeof(struct kobj_map), GFP_KERNEL);
struct probe *base = kzalloc(sizeof(*base), GFP_KERNEL); struct probe *base = kzalloc(sizeof(*base), GFP_KERNEL);
@ -149,6 +150,6 @@ struct kobj_map *kobj_map_init(kobj_probe_t *base_probe, struct semaphore *sem)
base->get = base_probe; base->get = base_probe;
for (i = 0; i < 255; i++) for (i = 0; i < 255; i++)
p->probes[i] = base; p->probes[i] = base;
p->sem = sem; p->lock = lock;
return p; return p;
} }

View file

@ -19,6 +19,7 @@
#include <linux/kobject.h> #include <linux/kobject.h>
#include <linux/kobj_map.h> #include <linux/kobj_map.h>
#include <linux/cdev.h> #include <linux/cdev.h>
#include <linux/mutex.h>
#ifdef CONFIG_KMOD #ifdef CONFIG_KMOD
#include <linux/kmod.h> #include <linux/kmod.h>
@ -28,7 +29,7 @@ static struct kobj_map *cdev_map;
#define MAX_PROBE_HASH 255 /* random */ #define MAX_PROBE_HASH 255 /* random */
static DECLARE_MUTEX(chrdevs_lock); static DEFINE_MUTEX(chrdevs_lock);
static struct char_device_struct { static struct char_device_struct {
struct char_device_struct *next; struct char_device_struct *next;
@ -88,13 +89,13 @@ out:
void *acquire_chrdev_list(void) void *acquire_chrdev_list(void)
{ {
down(&chrdevs_lock); mutex_lock(&chrdevs_lock);
return get_next_chrdev(NULL); return get_next_chrdev(NULL);
} }
void release_chrdev_list(void *dev) void release_chrdev_list(void *dev)
{ {
up(&chrdevs_lock); mutex_unlock(&chrdevs_lock);
kfree(dev); kfree(dev);
} }
@ -151,7 +152,7 @@ __register_chrdev_region(unsigned int major, unsigned int baseminor,
memset(cd, 0, sizeof(struct char_device_struct)); memset(cd, 0, sizeof(struct char_device_struct));
down(&chrdevs_lock); mutex_lock(&chrdevs_lock);
/* temporary */ /* temporary */
if (major == 0) { if (major == 0) {
@ -186,10 +187,10 @@ __register_chrdev_region(unsigned int major, unsigned int baseminor,
} }
cd->next = *cp; cd->next = *cp;
*cp = cd; *cp = cd;
up(&chrdevs_lock); mutex_unlock(&chrdevs_lock);
return cd; return cd;
out: out:
up(&chrdevs_lock); mutex_unlock(&chrdevs_lock);
kfree(cd); kfree(cd);
return ERR_PTR(ret); return ERR_PTR(ret);
} }
@ -200,7 +201,7 @@ __unregister_chrdev_region(unsigned major, unsigned baseminor, int minorct)
struct char_device_struct *cd = NULL, **cp; struct char_device_struct *cd = NULL, **cp;
int i = major_to_index(major); int i = major_to_index(major);
down(&chrdevs_lock); mutex_lock(&chrdevs_lock);
for (cp = &chrdevs[i]; *cp; cp = &(*cp)->next) for (cp = &chrdevs[i]; *cp; cp = &(*cp)->next)
if ((*cp)->major == major && if ((*cp)->major == major &&
(*cp)->baseminor == baseminor && (*cp)->baseminor == baseminor &&
@ -210,7 +211,7 @@ __unregister_chrdev_region(unsigned major, unsigned baseminor, int minorct)
cd = *cp; cd = *cp;
*cp = cd->next; *cp = cd->next;
} }
up(&chrdevs_lock); mutex_unlock(&chrdevs_lock);
return cd; return cd;
} }

View file

@ -1,6 +1,6 @@
#ifdef __KERNEL__ #ifdef __KERNEL__
#include <asm/semaphore.h> #include <linux/mutex.h>
typedef struct kobject *kobj_probe_t(dev_t, int *, void *); typedef struct kobject *kobj_probe_t(dev_t, int *, void *);
struct kobj_map; struct kobj_map;
@ -9,6 +9,6 @@ int kobj_map(struct kobj_map *, dev_t, unsigned long, struct module *,
kobj_probe_t *, int (*)(dev_t, void *), void *); kobj_probe_t *, int (*)(dev_t, void *), void *);
void kobj_unmap(struct kobj_map *, dev_t, unsigned long); void kobj_unmap(struct kobj_map *, dev_t, unsigned long);
struct kobject *kobj_lookup(struct kobj_map *, dev_t, int *); struct kobject *kobj_lookup(struct kobj_map *, dev_t, int *);
struct kobj_map *kobj_map_init(kobj_probe_t *, struct semaphore *); struct kobj_map *kobj_map_init(kobj_probe_t *, struct mutex *);
#endif #endif