Minor LinuxKPI code cleanup:

- Declare some static functions in linux_compat.c instead if inside
  various header files.
- Prefix FreeBSD local functions in the LinuxKPI with "linux_" to
  avoid symbol name conflicts in the future and to make debugging
  easier.
- Make the "struct kobj_ktype" declaractions constant to shave off a
  few bytes from the data segment.

MFC after:	1 week
Sponsored by:	Mellanox Technologies
This commit is contained in:
Hans Petter Selasky 2015-12-31 12:30:19 +00:00
parent 337cb9f04c
commit 06204f8e25
6 changed files with 192 additions and 182 deletions

View File

@ -41,6 +41,8 @@ struct inode;
struct module;
extern struct cdevsw linuxcdevsw;
extern const struct kobj_type linux_cdev_ktype;
extern const struct kobj_type linux_cdev_static_ktype;
struct linux_cdev {
struct kobject kobj;
@ -50,46 +52,11 @@ struct linux_cdev {
const struct file_operations *ops;
};
static inline void
cdev_release(struct kobject *kobj)
{
struct linux_cdev *cdev;
struct kobject *parent;
cdev = container_of(kobj, struct linux_cdev, kobj);
parent = kobj->parent;
if (cdev->cdev)
destroy_dev(cdev->cdev);
kfree(cdev);
kobject_put(parent);
}
static inline void
cdev_static_release(struct kobject *kobj)
{
struct linux_cdev *cdev;
struct kobject *parent;
cdev = container_of(kobj, struct linux_cdev, kobj);
parent = kobj->parent;
if (cdev->cdev)
destroy_dev(cdev->cdev);
kobject_put(parent);
}
static struct kobj_type cdev_ktype = {
.release = cdev_release,
};
static struct kobj_type cdev_static_ktype = {
.release = cdev_static_release,
};
static inline void
cdev_init(struct linux_cdev *cdev, const struct file_operations *ops)
{
kobject_init(&cdev->kobj, &cdev_static_ktype);
kobject_init(&cdev->kobj, &linux_cdev_static_ktype);
cdev->ops = ops;
}
@ -100,7 +67,7 @@ cdev_alloc(void)
cdev = kzalloc(sizeof(struct linux_cdev), M_WAITOK);
if (cdev)
kobject_init(&cdev->kobj, &cdev_ktype);
kobject_init(&cdev->kobj, &linux_cdev_ktype);
return (cdev);
}

View File

@ -73,8 +73,10 @@ struct device {
unsigned int msix_max;
};
extern struct device linux_rootdev;
extern struct kobject class_root;
extern struct device linux_root_device;
extern struct kobject linux_class_root;
extern const struct kobj_type linux_dev_ktype;
extern const struct kobj_type linux_class_ktype;
struct class_attribute {
struct attribute attr;
@ -170,62 +172,14 @@ put_device(struct device *dev)
kobject_put(&dev->kobj);
}
static inline ssize_t
class_show(struct kobject *kobj, struct attribute *attr, char *buf)
{
struct class_attribute *dattr;
ssize_t error;
dattr = container_of(attr, struct class_attribute, attr);
error = -EIO;
if (dattr->show)
error = dattr->show(container_of(kobj, struct class, kobj),
dattr, buf);
return (error);
}
static inline ssize_t
class_store(struct kobject *kobj, struct attribute *attr, const char *buf,
size_t count)
{
struct class_attribute *dattr;
ssize_t error;
dattr = container_of(attr, struct class_attribute, attr);
error = -EIO;
if (dattr->store)
error = dattr->store(container_of(kobj, struct class, kobj),
dattr, buf, count);
return (error);
}
static inline void
class_release(struct kobject *kobj)
{
struct class *class;
class = container_of(kobj, struct class, kobj);
if (class->class_release)
class->class_release(class);
}
static struct sysfs_ops class_sysfs = {
.show = class_show,
.store = class_store,
};
static struct kobj_type class_ktype = {
.release = class_release,
.sysfs_ops = &class_sysfs
};
static inline int
class_register(struct class *class)
{
class->bsdclass = devclass_create(class->name);
kobject_init(&class->kobj, &class_ktype);
kobject_init(&class->kobj, &linux_class_ktype);
kobject_set_name(&class->kobj, class->name);
kobject_add(&class->kobj, &class_root, class->name);
kobject_add(&class->kobj, &linux_class_root, class->name);
return (0);
}
@ -237,54 +191,6 @@ class_unregister(struct class *class)
kobject_put(&class->kobj);
}
static inline void
device_release(struct kobject *kobj)
{
struct device *dev;
dev = container_of(kobj, struct device, kobj);
/* This is the precedence defined by linux. */
if (dev->release)
dev->release(dev);
else if (dev->class && dev->class->dev_release)
dev->class->dev_release(dev);
}
static inline ssize_t
dev_show(struct kobject *kobj, struct attribute *attr, char *buf)
{
struct device_attribute *dattr;
ssize_t error;
dattr = container_of(attr, struct device_attribute, attr);
error = -EIO;
if (dattr->show)
error = dattr->show(container_of(kobj, struct device, kobj),
dattr, buf);
return (error);
}
static inline ssize_t
dev_store(struct kobject *kobj, struct attribute *attr, const char *buf,
size_t count)
{
struct device_attribute *dattr;
ssize_t error;
dattr = container_of(attr, struct device_attribute, attr);
error = -EIO;
if (dattr->store)
error = dattr->store(container_of(kobj, struct device, kobj),
dattr, buf, count);
return (error);
}
static struct sysfs_ops dev_sysfs = { .show = dev_show, .store = dev_store, };
static struct kobj_type dev_ktype = {
.release = device_release,
.sysfs_ops = &dev_sysfs
};
/*
* Devices are registered and created for exporting to sysfs. create
* implies register and register assumes the device fields have been
@ -311,7 +217,7 @@ device_register(struct device *dev)
device_set_softc(bsddev, dev);
}
dev->bsddev = bsddev;
kobject_init(&dev->kobj, &dev_ktype);
kobject_init(&dev->kobj, &linux_dev_ktype);
kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev));
return (0);
@ -346,7 +252,7 @@ device_destroy(struct class *class, dev_t devt)
}
static inline void
class_kfree(struct class *class)
linux_class_kfree(struct class *class)
{
kfree(class);
@ -361,7 +267,7 @@ class_create(struct module *owner, const char *name)
class = kzalloc(sizeof(*class), M_WAITOK);
class->owner = owner;
class->name= name;
class->class_release = class_kfree;
class->class_release = linux_class_kfree;
error = class_register(class);
if (error) {
kfree(class);
@ -414,7 +320,8 @@ class_remove_file(struct class *class, const struct class_attribute *attr)
sysfs_remove_file(&class->kobj, &attr->attr);
}
static inline int dev_to_node(struct device *dev)
static inline int
dev_to_node(struct device *dev)
{
return -1;
}

View File

@ -46,13 +46,13 @@ struct kobj_type {
struct attribute **default_attrs;
};
extern struct kobj_type kfree_type;
extern const struct kobj_type linux_kfree_type;
struct kobject {
struct kobject *parent;
char *name;
struct kref kref;
struct kobj_type *ktype;
const struct kobj_type *ktype;
struct list_head entry;
struct sysctl_oid *oidp;
};
@ -74,7 +74,7 @@ struct kobj_attribute {
};
static inline void
kobject_init(struct kobject *kobj, struct kobj_type *ktype)
kobject_init(struct kobject *kobj, const struct kobj_type *ktype)
{
kref_init(&kobj->kref);
@ -83,15 +83,14 @@ kobject_init(struct kobject *kobj, struct kobj_type *ktype)
kobj->oidp = NULL;
}
static inline void kobject_put(struct kobject *kobj);
void kobject_release(struct kref *kref);
void linux_kobject_release(struct kref *kref);
static inline void
kobject_put(struct kobject *kobj)
{
if (kobj)
kref_put(&kobj->kref, kobject_release);
kref_put(&kobj->kref, linux_kobject_release);
}
static inline struct kobject *
@ -115,7 +114,7 @@ kobject_create(void)
kobj = kzalloc(sizeof(*kobj), GFP_KERNEL);
if (kobj == NULL)
return (NULL);
kobject_init(kobj, &kfree_type);
kobject_init(kobj, &linux_kfree_type);
return (kobj);
}
@ -135,7 +134,6 @@ kobject_create_and_add(const char *name, struct kobject *parent)
return (NULL);
}
static inline char *
kobject_name(const struct kobject *kobj)
{
@ -144,7 +142,7 @@ kobject_name(const struct kobject *kobj)
}
int kobject_set_name(struct kobject *kobj, const char *fmt, ...);
int kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype,
int kobject_init_and_add(struct kobject *kobj, const struct kobj_type *ktype,
struct kobject *parent, const char *fmt, ...);
#endif /* _LINUX_KOBJECT_H_ */

View File

@ -46,13 +46,13 @@ struct miscdevice {
umode_t mode;
};
extern struct class miscclass;
extern struct class linux_class_misc;
static inline int
misc_register(struct miscdevice *misc)
{
misc->this_device = device_create(&miscclass, &linux_rootdev, 0, misc,
misc->name);
misc->this_device = device_create(&linux_class_misc,
&linux_root_device, 0, misc, misc->name);
misc->cdev = cdev_alloc();
if (misc->cdev == NULL)
return -ENOMEM;
@ -67,7 +67,7 @@ misc_register(struct miscdevice *misc)
static inline int
misc_deregister(struct miscdevice *misc)
{
device_destroy(&miscclass, misc->this_device->devt);
device_destroy(&linux_class_misc, misc->this_device->devt);
cdev_del(misc->cdev);
return (0);

View File

@ -77,9 +77,9 @@ MALLOC_DEFINE(M_KMALLOC, "linux", "Linux kmalloc compat");
#undef cdev
#define RB_ROOT(head) (head)->rbh_root
struct kobject class_root;
struct device linux_rootdev;
struct class miscclass;
struct kobject linux_class_root;
struct device linux_root_device;
struct class linux_class_misc;
struct list_head pci_drivers;
struct list_head pci_devices;
struct net init_net;
@ -151,10 +151,10 @@ kobject_set_name(struct kobject *kobj, const char *fmt, ...)
return (error);
}
static inline int
static int
kobject_add_complete(struct kobject *kobj, struct kobject *parent)
{
struct kobj_type *t;
const struct kobj_type *t;
int error;
kobj->parent = parent;
@ -191,7 +191,7 @@ kobject_add(struct kobject *kobj, struct kobject *parent, const char *fmt, ...)
}
void
kobject_release(struct kref *kref)
linux_kobject_release(struct kref *kref)
{
struct kobject *kobj;
char *name;
@ -205,28 +205,131 @@ kobject_release(struct kref *kref)
}
static void
kobject_kfree(struct kobject *kobj)
linux_kobject_kfree(struct kobject *kobj)
{
kfree(kobj);
}
static void
kobject_kfree_name(struct kobject *kobj)
linux_kobject_kfree_name(struct kobject *kobj)
{
if (kobj) {
kfree(kobj->name);
}
}
struct kobj_type kfree_type = { .release = kobject_kfree };
const struct kobj_type linux_kfree_type = {
.release = linux_kobject_kfree
};
static void
dev_release(struct device *dev)
linux_device_release(struct device *dev)
{
pr_debug("dev_release: %s\n", dev_name(dev));
pr_debug("linux_device_release: %s\n", dev_name(dev));
kfree(dev);
}
static ssize_t
linux_class_show(struct kobject *kobj, struct attribute *attr, char *buf)
{
struct class_attribute *dattr;
ssize_t error;
dattr = container_of(attr, struct class_attribute, attr);
error = -EIO;
if (dattr->show)
error = dattr->show(container_of(kobj, struct class, kobj),
dattr, buf);
return (error);
}
static ssize_t
linux_class_store(struct kobject *kobj, struct attribute *attr, const char *buf,
size_t count)
{
struct class_attribute *dattr;
ssize_t error;
dattr = container_of(attr, struct class_attribute, attr);
error = -EIO;
if (dattr->store)
error = dattr->store(container_of(kobj, struct class, kobj),
dattr, buf, count);
return (error);
}
static void
linux_class_release(struct kobject *kobj)
{
struct class *class;
class = container_of(kobj, struct class, kobj);
if (class->class_release)
class->class_release(class);
}
static const struct sysfs_ops linux_class_sysfs = {
.show = linux_class_show,
.store = linux_class_store,
};
const struct kobj_type linux_class_ktype = {
.release = linux_class_release,
.sysfs_ops = &linux_class_sysfs
};
static void
linux_dev_release(struct kobject *kobj)
{
struct device *dev;
dev = container_of(kobj, struct device, kobj);
/* This is the precedence defined by linux. */
if (dev->release)
dev->release(dev);
else if (dev->class && dev->class->dev_release)
dev->class->dev_release(dev);
}
static ssize_t
linux_dev_show(struct kobject *kobj, struct attribute *attr, char *buf)
{
struct device_attribute *dattr;
ssize_t error;
dattr = container_of(attr, struct device_attribute, attr);
error = -EIO;
if (dattr->show)
error = dattr->show(container_of(kobj, struct device, kobj),
dattr, buf);
return (error);
}
static ssize_t
linux_dev_store(struct kobject *kobj, struct attribute *attr, const char *buf,
size_t count)
{
struct device_attribute *dattr;
ssize_t error;
dattr = container_of(attr, struct device_attribute, attr);
error = -EIO;
if (dattr->store)
error = dattr->store(container_of(kobj, struct device, kobj),
dattr, buf, count);
return (error);
}
static const struct sysfs_ops linux_dev_sysfs = {
.show = linux_dev_show,
.store = linux_dev_store,
};
const struct kobj_type linux_dev_ktype = {
.release = linux_dev_release,
.sysfs_ops = &linux_dev_sysfs
};
struct device *
device_create(struct class *class, struct device *parent, dev_t devt,
void *drvdata, const char *fmt, ...)
@ -239,7 +342,7 @@ device_create(struct class *class, struct device *parent, dev_t devt,
dev->class = class;
dev->devt = devt;
dev->driver_data = drvdata;
dev->release = dev_release;
dev->release = linux_device_release;
va_start(args, fmt);
kobject_set_name_vargs(&dev->kobj, fmt, args);
va_end(args);
@ -249,7 +352,7 @@ device_create(struct class *class, struct device *parent, dev_t devt,
}
int
kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype,
kobject_init_and_add(struct kobject *kobj, const struct kobj_type *ktype,
struct kobject *parent, const char *fmt, ...)
{
va_list args;
@ -998,6 +1101,41 @@ destroy_workqueue(struct workqueue_struct *wq)
kfree(wq);
}
static void
linux_cdev_release(struct kobject *kobj)
{
struct linux_cdev *cdev;
struct kobject *parent;
cdev = container_of(kobj, struct linux_cdev, kobj);
parent = kobj->parent;
if (cdev->cdev)
destroy_dev(cdev->cdev);
kfree(cdev);
kobject_put(parent);
}
static void
linux_cdev_static_release(struct kobject *kobj)
{
struct linux_cdev *cdev;
struct kobject *parent;
cdev = container_of(kobj, struct linux_cdev, kobj);
parent = kobj->parent;
if (cdev->cdev)
destroy_dev(cdev->cdev);
kobject_put(parent);
}
const struct kobj_type linux_cdev_ktype = {
.release = linux_cdev_release,
};
const struct kobj_type linux_cdev_static_ktype = {
.release = linux_cdev_static_release,
};
static void
linux_compat_init(void *arg)
{
@ -1006,18 +1144,18 @@ linux_compat_init(void *arg)
rootoid = SYSCTL_ADD_ROOT_NODE(NULL,
OID_AUTO, "sys", CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, "sys");
kobject_init(&class_root, &class_ktype);
kobject_set_name(&class_root, "class");
class_root.oidp = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(rootoid),
kobject_init(&linux_class_root, &linux_class_ktype);
kobject_set_name(&linux_class_root, "class");
linux_class_root.oidp = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(rootoid),
OID_AUTO, "class", CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, "class");
kobject_init(&linux_rootdev.kobj, &dev_ktype);
kobject_set_name(&linux_rootdev.kobj, "device");
linux_rootdev.kobj.oidp = SYSCTL_ADD_NODE(NULL,
kobject_init(&linux_root_device.kobj, &linux_dev_ktype);
kobject_set_name(&linux_root_device.kobj, "device");
linux_root_device.kobj.oidp = SYSCTL_ADD_NODE(NULL,
SYSCTL_CHILDREN(rootoid), OID_AUTO, "device", CTLFLAG_RD, NULL,
"device");
linux_rootdev.bsddev = root_bus;
miscclass.name = "misc";
class_register(&miscclass);
linux_root_device.bsddev = root_bus;
linux_class_misc.name = "misc";
class_register(&linux_class_misc);
INIT_LIST_HEAD(&pci_drivers);
INIT_LIST_HEAD(&pci_devices);
spin_lock_init(&pci_lock);
@ -1030,9 +1168,9 @@ SYSINIT(linux_compat, SI_SUB_DRIVERS, SI_ORDER_SECOND, linux_compat_init, NULL);
static void
linux_compat_uninit(void *arg)
{
kobject_kfree_name(&class_root);
kobject_kfree_name(&linux_rootdev.kobj);
kobject_kfree_name(&miscclass.kobj);
linux_kobject_kfree_name(&linux_class_root);
linux_kobject_kfree_name(&linux_root_device.kobj);
linux_kobject_kfree_name(&linux_class_misc.kobj);
}
SYSUNINIT(linux_compat, SI_SUB_DRIVERS, SI_ORDER_SECOND, linux_compat_uninit, NULL);

View File

@ -119,16 +119,16 @@ linux_pci_attach(device_t dev)
pdrv = linux_pci_find(dev, &id);
pdev = device_get_softc(dev);
pdev->dev.parent = &linux_rootdev;
pdev->dev.parent = &linux_root_device;
pdev->dev.bsddev = dev;
INIT_LIST_HEAD(&pdev->dev.irqents);
pdev->device = id->device;
pdev->vendor = id->vendor;
pdev->dev.dma_mask = &pdev->dma_mask;
pdev->pdrv = pdrv;
kobject_init(&pdev->dev.kobj, &dev_ktype);
kobject_init(&pdev->dev.kobj, &linux_dev_ktype);
kobject_set_name(&pdev->dev.kobj, device_get_nameunit(dev));
kobject_add(&pdev->dev.kobj, &linux_rootdev.kobj,
kobject_add(&pdev->dev.kobj, &linux_root_device.kobj,
kobject_name(&pdev->dev.kobj));
rle = _pci_get_rle(pdev, SYS_RES_IRQ, 0);
if (rle)