Skip to content

Commit c66fdab

Browse files
Eric W. Biedermangregkh
authored andcommitted
uio: Statically allocate uio_class and use class .dev_attrs.
Instead of adding uio class attributes manually after the uio device has been created and we have sent a uevent to userspace, use the class attribute mechanism. This removes races and makes the code simpler. At the same time don't bother to dynamically allocate a struct class for uio, just declare one statically. Less code is needed and it is easier to set the class parameters.tune the class Signed-off-by: Eric W. Biederman <ebiederm@aristanetworks.com> Reviewed-by: Thomas Gleixner <tglx@linutronix.de> Signed-off-by: Hans J. Koch <hjk@linutronix.de> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
1 parent 91960a4 commit c66fdab

1 file changed

Lines changed: 18 additions & 35 deletions

File tree

‎drivers/uio/uio.c‎

Lines changed: 18 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,6 @@ static struct cdev *uio_cdev;
4646
static DEFINE_IDR(uio_idr);
4747
static const struct file_operations uio_fops;
4848

49-
/* UIO class infrastructure */
50-
static struct class *uio_class;
51-
5249
/* Protect idr accesses */
5350
static DEFINE_MUTEX(minor_lock);
5451

@@ -233,33 +230,32 @@ static ssize_t show_name(struct device *dev,
233230
struct uio_device *idev = dev_get_drvdata(dev);
234231
return sprintf(buf, "%s\n", idev->info->name);
235232
}
236-
static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
237233

238234
static ssize_t show_version(struct device *dev,
239235
struct device_attribute *attr, char *buf)
240236
{
241237
struct uio_device *idev = dev_get_drvdata(dev);
242238
return sprintf(buf, "%s\n", idev->info->version);
243239
}
244-
static DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
245240

246241
static ssize_t show_event(struct device *dev,
247242
struct device_attribute *attr, char *buf)
248243
{
249244
struct uio_device *idev = dev_get_drvdata(dev);
250245
return sprintf(buf, "%u\n", (unsigned int)atomic_read(&idev->event));
251246
}
252-
static DEVICE_ATTR(event, S_IRUGO, show_event, NULL);
253247

254-
static struct attribute *uio_attrs[] = {
255-
&dev_attr_name.attr,
256-
&dev_attr_version.attr,
257-
&dev_attr_event.attr,
258-
NULL,
248+
static struct device_attribute uio_class_attributes[] = {
249+
__ATTR(name, S_IRUGO, show_name, NULL),
250+
__ATTR(version, S_IRUGO, show_version, NULL),
251+
__ATTR(event, S_IRUGO, show_event, NULL),
252+
{}
259253
};
260254

261-
static struct attribute_group uio_attr_grp = {
262-
.attrs = uio_attrs,
255+
/* UIO class infrastructure */
256+
static struct class uio_class = {
257+
.name = "uio",
258+
.dev_attrs = uio_class_attributes,
263259
};
264260

265261
/*
@@ -276,10 +272,6 @@ static int uio_dev_add_attributes(struct uio_device *idev)
276272
struct uio_port *port;
277273
struct uio_portio *portio;
278274

279-
ret = sysfs_create_group(&idev->dev->kobj, &uio_attr_grp);
280-
if (ret)
281-
goto err_group;
282-
283275
for (mi = 0; mi < MAX_UIO_MAPS; mi++) {
284276
mem = &idev->info->mem[mi];
285277
if (mem->size == 0)
@@ -347,8 +339,6 @@ static int uio_dev_add_attributes(struct uio_device *idev)
347339
kobject_put(&map->kobj);
348340
}
349341
kobject_put(idev->map_dir);
350-
sysfs_remove_group(&idev->dev->kobj, &uio_attr_grp);
351-
err_group:
352342
dev_err(idev->dev, "error creating sysfs files (%d)\n", ret);
353343
return ret;
354344
}
@@ -374,8 +364,6 @@ static void uio_dev_del_attributes(struct uio_device *idev)
374364
kobject_put(&port->portio->kobj);
375365
}
376366
kobject_put(idev->portio_dir);
377-
378-
sysfs_remove_group(&idev->dev->kobj, &uio_attr_grp);
379367
}
380368

381369
static int uio_get_minor(struct uio_device *idev)
@@ -775,34 +763,29 @@ static void uio_major_cleanup(void)
775763

776764
static int init_uio_class(void)
777765
{
778-
struct class *class;
779766
int ret;
780767

781768
/* This is the first time in here, set everything up properly */
782769
ret = uio_major_init();
783770
if (ret)
784771
goto exit;
785772

786-
class = class_create(THIS_MODULE, "uio");
787-
if (IS_ERR(class)) {
788-
ret = IS_ERR(class);
789-
printk(KERN_ERR "class_create failed for uio\n");
790-
goto err_class_create;
773+
ret = class_register(&uio_class);
774+
if (ret) {
775+
printk(KERN_ERR "class_register failed for uio\n");
776+
goto err_class_register;
791777
}
792-
uio_class = class;
793778
return 0;
794779

795-
err_class_create:
780+
err_class_register:
796781
uio_major_cleanup();
797782
exit:
798783
return ret;
799784
}
800785

801786
static void release_uio_class(void)
802787
{
803-
/* Ok, we cheat as we know we only have one uio_class */
804-
class_destroy(uio_class);
805-
uio_class = NULL;
788+
class_unregister(&uio_class);
806789
uio_major_cleanup();
807790
}
808791

@@ -841,7 +824,7 @@ int __uio_register_device(struct module *owner,
841824
if (ret)
842825
goto err_get_minor;
843826

844-
idev->dev = device_create(uio_class, parent,
827+
idev->dev = device_create(&uio_class, parent,
845828
MKDEV(uio_major, idev->minor), idev,
846829
"uio%d", idev->minor);
847830
if (IS_ERR(idev->dev)) {
@@ -868,7 +851,7 @@ int __uio_register_device(struct module *owner,
868851
err_request_irq:
869852
uio_dev_del_attributes(idev);
870853
err_uio_dev_add_attributes:
871-
device_destroy(uio_class, MKDEV(uio_major, idev->minor));
854+
device_destroy(&uio_class, MKDEV(uio_major, idev->minor));
872855
err_device_create:
873856
uio_free_minor(idev);
874857
err_get_minor:
@@ -899,7 +882,7 @@ void uio_unregister_device(struct uio_info *info)
899882

900883
uio_dev_del_attributes(idev);
901884

902-
device_destroy(uio_class, MKDEV(uio_major, idev->minor));
885+
device_destroy(&uio_class, MKDEV(uio_major, idev->minor));
903886
kfree(idev);
904887

905888
return;

0 commit comments

Comments
 (0)