···11+22+krefs allow you to add reference counters to your objects. If you33+have objects that are used in multiple places and passed around, and44+you don't have refcounts, your code is almost certainly broken. If55+you want refcounts, krefs are the way to go.66+77+To use a kref, add one to your data structures like:88+99+struct my_data1010+{1111+ .1212+ .1313+ struct kref refcount;1414+ .1515+ .1616+};1717+1818+The kref can occur anywhere within the data structure.1919+2020+You must initialize the kref after you allocate it. To do this, call2121+kref_init as so:2222+2323+ struct my_data *data;2424+2525+ data = kmalloc(sizeof(*data), GFP_KERNEL);2626+ if (!data)2727+ return -ENOMEM;2828+ kref_init(&data->refcount);2929+3030+This sets the refcount in the kref to 1.3131+3232+Once you have an initialized kref, you must follow the following3333+rules:3434+3535+1) If you make a non-temporary copy of a pointer, especially if3636+ it can be passed to another thread of execution, you must3737+ increment the refcount with kref_get() before passing it off:3838+ kref_get(&data->refcount);3939+ If you already have a valid pointer to a kref-ed structure (the4040+ refcount cannot go to zero) you may do this without a lock.4141+4242+2) When you are done with a pointer, you must call kref_put():4343+ kref_put(&data->refcount, data_release);4444+ If this is the last reference to the pointer, the release4545+ routine will be called. If the code never tries to get4646+ a valid pointer to a kref-ed structure without already4747+ holding a valid pointer, it is safe to do this without4848+ a lock.4949+5050+3) If the code attempts to gain a reference to a kref-ed structure5151+ without already holding a valid pointer, it must serialize access5252+ where a kref_put() cannot occur during the kref_get(), and the5353+ structure must remain valid during the kref_get().5454+5555+For example, if you allocate some data and then pass it to another5656+thread to process:5757+5858+void data_release(struct kref *ref)5959+{6060+ struct my_data *data = container_of(ref, struct my_data, refcount);6161+ kfree(data);6262+}6363+6464+void more_data_handling(void *cb_data)6565+{6666+ struct my_data *data = cb_data;6767+ .6868+ . do stuff with data here6969+ .7070+ kref_put(data, data_release);7171+}7272+7373+int my_data_handler(void)7474+{7575+ int rv = 0;7676+ struct my_data *data;7777+ struct task_struct *task;7878+ data = kmalloc(sizeof(*data), GFP_KERNEL);7979+ if (!data)8080+ return -ENOMEM;8181+ kref_init(&data->refcount);8282+8383+ kref_get(&data->refcount);8484+ task = kthread_run(more_data_handling, data, "more_data_handling");8585+ if (task == ERR_PTR(-ENOMEM)) {8686+ rv = -ENOMEM;8787+ kref_put(&data->refcount, data_release);8888+ goto out;8989+ }9090+9191+ .9292+ . do stuff with data here9393+ .9494+ out:9595+ kref_put(&data->refcount, data_release);9696+ return rv;9797+}9898+9999+This way, it doesn't matter what order the two threads handle the100100+data, the kref_put() handles knowing when the data is not referenced101101+any more and releasing it. The kref_get() does not require a lock,102102+since we already have a valid pointer that we own a refcount for. The103103+put needs no lock because nothing tries to get the data without104104+already holding a pointer.105105+106106+Note that the "before" in rule 1 is very important. You should never107107+do something like:108108+109109+ task = kthread_run(more_data_handling, data, "more_data_handling");110110+ if (task == ERR_PTR(-ENOMEM)) {111111+ rv = -ENOMEM;112112+ goto out;113113+ } else114114+ /* BAD BAD BAD - get is after the handoff */115115+ kref_get(&data->refcount);116116+117117+Don't assume you know what you are doing and use the above construct.118118+First of all, you may not know what you are doing. Second, you may119119+know what you are doing (there are some situations where locking is120120+involved where the above may be legal) but someone else who doesn't121121+know what they are doing may change the code or copy the code. It's122122+bad style. Don't do it.123123+124124+There are some situations where you can optimize the gets and puts.125125+For instance, if you are done with an object and enqueuing it for126126+something else or passing it off to something else, there is no reason127127+to do a get then a put:128128+129129+ /* Silly extra get and put */130130+ kref_get(&obj->ref);131131+ enqueue(obj);132132+ kref_put(&obj->ref, obj_cleanup);133133+134134+Just do the enqueue. A comment about this is always welcome:135135+136136+ enqueue(obj);137137+ /* We are done with obj, so we pass our refcount off138138+ to the queue. DON'T TOUCH obj AFTER HERE! */139139+140140+The last rule (rule 3) is the nastiest one to handle. Say, for141141+instance, you have a list of items that are each kref-ed, and you wish142142+to get the first one. You can't just pull the first item off the list143143+and kref_get() it. That violates rule 3 because you are not already144144+holding a valid pointer. You must add locks or semaphores. For145145+instance:146146+147147+static DECLARE_MUTEX(sem);148148+static LIST_HEAD(q);149149+struct my_data150150+{151151+ struct kref refcount;152152+ struct list_head link;153153+};154154+155155+static struct my_data *get_entry()156156+{157157+ struct my_data *entry = NULL;158158+ down(&sem);159159+ if (!list_empty(&q)) {160160+ entry = container_of(q.next, struct my_q_entry, link);161161+ kref_get(&entry->refcount);162162+ }163163+ up(&sem);164164+ return entry;165165+}166166+167167+static void release_entry(struct kref *ref)168168+{169169+ struct my_data *entry = container_of(ref, struct my_data, refcount);170170+171171+ list_del(&entry->link);172172+ kfree(entry);173173+}174174+175175+static void put_entry(struct my_data *entry)176176+{177177+ down(&sem);178178+ kref_put(&entry->refcount, release_entry);179179+ up(&sem);180180+}181181+182182+The kref_put() return value is useful if you do not want to hold the183183+lock during the whole release operation. Say you didn't want to call184184+kfree() with the lock held in the example above (since it is kind of185185+pointless to do so). You could use kref_put() as follows:186186+187187+static void release_entry(struct kref *ref)188188+{189189+ /* All work is done after the return from kref_put(). */190190+}191191+192192+static void put_entry(struct my_data *entry)193193+{194194+ down(&sem);195195+ if (kref_put(&entry->refcount, release_entry)) {196196+ list_del(&entry->link);197197+ up(&sem);198198+ kfree(entry);199199+ } else200200+ up(&sem);201201+}202202+203203+This is really more useful if you have to call other routines as part204204+of the free operations that could take a long time or might claim the205205+same lock. Note that doing everything in the release routine is still206206+preferred as it is a little neater.207207+208208+209209+Corey Minyard <minyard@acm.org>210210+211211+A lot of this was lifted from Greg Kroah-Hartman's 2004 OLS paper and212212+presentation on krefs, which can be found at:213213+ http://www.kroah.com/linux/talks/ols_2004_kref_paper/Reprint-Kroah-Hartman-OLS2004.pdf214214+and:215215+ http://www.kroah.com/linux/talks/ols_2004_kref_talk/216216+
+2
drivers/base/class.c
···430430 sysfs_create_link(&class_dev->kobj,431431 &class_dev->dev->kobj, "device");432432433433+ kobject_hotplug(&class_dev->kobj, KOBJ_ADD);433434 register_done:434435 if (error && parent)435436 class_put(parent);···462461 sysfs_remove_link(&class_dev->kobj, "device");463462 class_device_remove_attrs(class_dev);464463464464+ kobject_hotplug(&class_dev->kobj, KOBJ_REMOVE);465465 kobject_del(&class_dev->kobj);466466467467 if (parent)
+3
drivers/base/core.c
···260260 /* notify platform of device entry */261261 if (platform_notify)262262 platform_notify(dev);263263+264264+ kobject_hotplug(&dev->kobj, KOBJ_ADD);263265 Done:264266 put_device(dev);265267 return error;···351349 platform_notify_remove(dev);352350 bus_remove_device(dev);353351 device_pm_remove(dev);352352+ kobject_hotplug(&dev->kobj, KOBJ_REMOVE);354353 kobject_del(&dev->kobj);355354 if (parent)356355 put_device(parent);
···337337 if ((err = kobject_add(&disk->kobj)))338338 return;339339 disk_sysfs_symlinks(disk);340340+ kobject_hotplug(&disk->kobj, KOBJ_ADD);340341341342 /* No minors to use for partitions */342343 if (disk->minors == 1) {···442441 sysfs_remove_link(&disk->driverfs_dev->kobj, "block");443442 put_device(disk->driverfs_dev);444443 }444444+ kobject_hotplug(&disk->kobj, KOBJ_REMOVE);445445 kobject_del(&disk->kobj);446446}