xref: /openbmc/linux/drivers/base/devcoredump.c (revision d0c44de2d8ffd2e4780d360b34ee6614aa4af080)
1989d42e8SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2833c9545SJohannes Berg /*
3833c9545SJohannes Berg  * Copyright(c) 2014 Intel Mobile Communications GmbH
452256637SAviya Erenfeld  * Copyright(c) 2015 Intel Deutschland GmbH
5833c9545SJohannes Berg  *
6833c9545SJohannes Berg  * Author: Johannes Berg <johannes@sipsolutions.net>
7833c9545SJohannes Berg  */
8833c9545SJohannes Berg #include <linux/module.h>
9833c9545SJohannes Berg #include <linux/device.h>
10833c9545SJohannes Berg #include <linux/devcoredump.h>
11833c9545SJohannes Berg #include <linux/list.h>
12833c9545SJohannes Berg #include <linux/slab.h>
13833c9545SJohannes Berg #include <linux/fs.h>
14833c9545SJohannes Berg #include <linux/workqueue.h>
15833c9545SJohannes Berg 
16d4533329SJohannes Berg static struct class devcd_class;
17d4533329SJohannes Berg 
18d4533329SJohannes Berg /* global disable flag, for security purposes */
19d4533329SJohannes Berg static bool devcd_disabled;
20d4533329SJohannes Berg 
21833c9545SJohannes Berg /* if data isn't read by userspace after 5 minutes then delete it */
22833c9545SJohannes Berg #define DEVCD_TIMEOUT	(HZ * 60 * 5)
23833c9545SJohannes Berg 
24833c9545SJohannes Berg struct devcd_entry {
25833c9545SJohannes Berg 	struct device devcd_dev;
2652256637SAviya Erenfeld 	void *data;
27833c9545SJohannes Berg 	size_t datalen;
2801daccf7SMukesh Ojha 	/*
2901daccf7SMukesh Ojha 	 * Here, mutex is required to serialize the calls to del_wk work between
3001daccf7SMukesh Ojha 	 * user/kernel space which happens when devcd is added with device_add()
3101daccf7SMukesh Ojha 	 * and that sends uevent to user space. User space reads the uevents,
3201daccf7SMukesh Ojha 	 * and calls to devcd_data_write() which try to modify the work which is
3301daccf7SMukesh Ojha 	 * not even initialized/queued from devcoredump.
3401daccf7SMukesh Ojha 	 *
3501daccf7SMukesh Ojha 	 *
3601daccf7SMukesh Ojha 	 *
3701daccf7SMukesh Ojha 	 *        cpu0(X)                                 cpu1(Y)
3801daccf7SMukesh Ojha 	 *
3901daccf7SMukesh Ojha 	 *        dev_coredump() uevent sent to user space
4001daccf7SMukesh Ojha 	 *        device_add()  ======================> user space process Y reads the
4101daccf7SMukesh Ojha 	 *                                              uevents writes to devcd fd
4201daccf7SMukesh Ojha 	 *                                              which results into writes to
4301daccf7SMukesh Ojha 	 *
4401daccf7SMukesh Ojha 	 *                                             devcd_data_write()
4501daccf7SMukesh Ojha 	 *                                               mod_delayed_work()
4601daccf7SMukesh Ojha 	 *                                                 try_to_grab_pending()
4701daccf7SMukesh Ojha 	 *                                                   del_timer()
4801daccf7SMukesh Ojha 	 *                                                     debug_assert_init()
4901daccf7SMukesh Ojha 	 *       INIT_DELAYED_WORK()
5001daccf7SMukesh Ojha 	 *       schedule_delayed_work()
5101daccf7SMukesh Ojha 	 *
5201daccf7SMukesh Ojha 	 *
5301daccf7SMukesh Ojha 	 * Also, mutex alone would not be enough to avoid scheduling of
5401daccf7SMukesh Ojha 	 * del_wk work after it get flush from a call to devcd_free()
5501daccf7SMukesh Ojha 	 * mentioned as below.
5601daccf7SMukesh Ojha 	 *
5701daccf7SMukesh Ojha 	 *	disabled_store()
5801daccf7SMukesh Ojha 	 *        devcd_free()
5901daccf7SMukesh Ojha 	 *          mutex_lock()             devcd_data_write()
6001daccf7SMukesh Ojha 	 *          flush_delayed_work()
6101daccf7SMukesh Ojha 	 *          mutex_unlock()
6201daccf7SMukesh Ojha 	 *                                   mutex_lock()
6301daccf7SMukesh Ojha 	 *                                   mod_delayed_work()
6401daccf7SMukesh Ojha 	 *                                   mutex_unlock()
6501daccf7SMukesh Ojha 	 * So, delete_work flag is required.
6601daccf7SMukesh Ojha 	 */
6701daccf7SMukesh Ojha 	struct mutex mutex;
6801daccf7SMukesh Ojha 	bool delete_work;
69833c9545SJohannes Berg 	struct module *owner;
70833c9545SJohannes Berg 	ssize_t (*read)(char *buffer, loff_t offset, size_t count,
7152256637SAviya Erenfeld 			void *data, size_t datalen);
7252256637SAviya Erenfeld 	void (*free)(void *data);
73833c9545SJohannes Berg 	struct delayed_work del_wk;
74833c9545SJohannes Berg 	struct device *failing_dev;
75833c9545SJohannes Berg };
76833c9545SJohannes Berg 
dev_to_devcd(struct device * dev)77833c9545SJohannes Berg static struct devcd_entry *dev_to_devcd(struct device *dev)
78833c9545SJohannes Berg {
79833c9545SJohannes Berg 	return container_of(dev, struct devcd_entry, devcd_dev);
80833c9545SJohannes Berg }
81833c9545SJohannes Berg 
devcd_dev_release(struct device * dev)82833c9545SJohannes Berg static void devcd_dev_release(struct device *dev)
83833c9545SJohannes Berg {
84833c9545SJohannes Berg 	struct devcd_entry *devcd = dev_to_devcd(dev);
85833c9545SJohannes Berg 
86833c9545SJohannes Berg 	devcd->free(devcd->data);
87833c9545SJohannes Berg 	module_put(devcd->owner);
88833c9545SJohannes Berg 
89833c9545SJohannes Berg 	/*
90833c9545SJohannes Berg 	 * this seems racy, but I don't see a notifier or such on
91833c9545SJohannes Berg 	 * a struct device to know when it goes away?
92833c9545SJohannes Berg 	 */
93833c9545SJohannes Berg 	if (devcd->failing_dev->kobj.sd)
94833c9545SJohannes Berg 		sysfs_delete_link(&devcd->failing_dev->kobj, &dev->kobj,
95833c9545SJohannes Berg 				  "devcoredump");
96833c9545SJohannes Berg 
97833c9545SJohannes Berg 	put_device(devcd->failing_dev);
98833c9545SJohannes Berg 	kfree(devcd);
99833c9545SJohannes Berg }
100833c9545SJohannes Berg 
devcd_del(struct work_struct * wk)101833c9545SJohannes Berg static void devcd_del(struct work_struct *wk)
102833c9545SJohannes Berg {
103833c9545SJohannes Berg 	struct devcd_entry *devcd;
104833c9545SJohannes Berg 
105833c9545SJohannes Berg 	devcd = container_of(wk, struct devcd_entry, del_wk.work);
106833c9545SJohannes Berg 
107833c9545SJohannes Berg 	device_del(&devcd->devcd_dev);
108833c9545SJohannes Berg 	put_device(&devcd->devcd_dev);
109833c9545SJohannes Berg }
110833c9545SJohannes Berg 
devcd_data_read(struct file * filp,struct kobject * kobj,struct bin_attribute * bin_attr,char * buffer,loff_t offset,size_t count)111833c9545SJohannes Berg static ssize_t devcd_data_read(struct file *filp, struct kobject *kobj,
112833c9545SJohannes Berg 			       struct bin_attribute *bin_attr,
113833c9545SJohannes Berg 			       char *buffer, loff_t offset, size_t count)
114833c9545SJohannes Berg {
115833c9545SJohannes Berg 	struct device *dev = kobj_to_dev(kobj);
116833c9545SJohannes Berg 	struct devcd_entry *devcd = dev_to_devcd(dev);
117833c9545SJohannes Berg 
118833c9545SJohannes Berg 	return devcd->read(buffer, offset, count, devcd->data, devcd->datalen);
119833c9545SJohannes Berg }
120833c9545SJohannes Berg 
devcd_data_write(struct file * filp,struct kobject * kobj,struct bin_attribute * bin_attr,char * buffer,loff_t offset,size_t count)121833c9545SJohannes Berg static ssize_t devcd_data_write(struct file *filp, struct kobject *kobj,
122833c9545SJohannes Berg 				struct bin_attribute *bin_attr,
123833c9545SJohannes Berg 				char *buffer, loff_t offset, size_t count)
124833c9545SJohannes Berg {
125833c9545SJohannes Berg 	struct device *dev = kobj_to_dev(kobj);
126833c9545SJohannes Berg 	struct devcd_entry *devcd = dev_to_devcd(dev);
127833c9545SJohannes Berg 
12801daccf7SMukesh Ojha 	mutex_lock(&devcd->mutex);
12901daccf7SMukesh Ojha 	if (!devcd->delete_work) {
13001daccf7SMukesh Ojha 		devcd->delete_work = true;
131833c9545SJohannes Berg 		mod_delayed_work(system_wq, &devcd->del_wk, 0);
13201daccf7SMukesh Ojha 	}
13301daccf7SMukesh Ojha 	mutex_unlock(&devcd->mutex);
134833c9545SJohannes Berg 
135833c9545SJohannes Berg 	return count;
136833c9545SJohannes Berg }
137833c9545SJohannes Berg 
138833c9545SJohannes Berg static struct bin_attribute devcd_attr_data = {
139833c9545SJohannes Berg 	.attr = { .name = "data", .mode = S_IRUSR | S_IWUSR, },
140833c9545SJohannes Berg 	.size = 0,
141833c9545SJohannes Berg 	.read = devcd_data_read,
142833c9545SJohannes Berg 	.write = devcd_data_write,
143833c9545SJohannes Berg };
144833c9545SJohannes Berg 
145833c9545SJohannes Berg static struct bin_attribute *devcd_dev_bin_attrs[] = {
146833c9545SJohannes Berg 	&devcd_attr_data, NULL,
147833c9545SJohannes Berg };
148833c9545SJohannes Berg 
149833c9545SJohannes Berg static const struct attribute_group devcd_dev_group = {
150833c9545SJohannes Berg 	.bin_attrs = devcd_dev_bin_attrs,
151833c9545SJohannes Berg };
152833c9545SJohannes Berg 
153833c9545SJohannes Berg static const struct attribute_group *devcd_dev_groups[] = {
154833c9545SJohannes Berg 	&devcd_dev_group, NULL,
155833c9545SJohannes Berg };
156833c9545SJohannes Berg 
devcd_free(struct device * dev,void * data)157d4533329SJohannes Berg static int devcd_free(struct device *dev, void *data)
158d4533329SJohannes Berg {
159d4533329SJohannes Berg 	struct devcd_entry *devcd = dev_to_devcd(dev);
160d4533329SJohannes Berg 
16101daccf7SMukesh Ojha 	mutex_lock(&devcd->mutex);
16201daccf7SMukesh Ojha 	if (!devcd->delete_work)
16301daccf7SMukesh Ojha 		devcd->delete_work = true;
16401daccf7SMukesh Ojha 
165d4533329SJohannes Berg 	flush_delayed_work(&devcd->del_wk);
16601daccf7SMukesh Ojha 	mutex_unlock(&devcd->mutex);
167d4533329SJohannes Berg 	return 0;
168d4533329SJohannes Berg }
169d4533329SJohannes Berg 
disabled_show(const struct class * class,const struct class_attribute * attr,char * buf)17075a2d422SGreg Kroah-Hartman static ssize_t disabled_show(const struct class *class, const struct class_attribute *attr,
171d4533329SJohannes Berg 			     char *buf)
172d4533329SJohannes Berg {
173948b3edbSJoe Perches 	return sysfs_emit(buf, "%d\n", devcd_disabled);
174d4533329SJohannes Berg }
175d4533329SJohannes Berg 
17601daccf7SMukesh Ojha /*
17701daccf7SMukesh Ojha  *
17801daccf7SMukesh Ojha  *	disabled_store()                                	worker()
17901daccf7SMukesh Ojha  *	 class_for_each_device(&devcd_class,
18001daccf7SMukesh Ojha  *		NULL, NULL, devcd_free)
18101daccf7SMukesh Ojha  *         ...
18201daccf7SMukesh Ojha  *         ...
18301daccf7SMukesh Ojha  *	   while ((dev = class_dev_iter_next(&iter))
18401daccf7SMukesh Ojha  *                                                             devcd_del()
18501daccf7SMukesh Ojha  *                                                               device_del()
18601daccf7SMukesh Ojha  *                                                                 put_device() <- last reference
18701daccf7SMukesh Ojha  *             error = fn(dev, data)                           devcd_dev_release()
18801daccf7SMukesh Ojha  *             devcd_free(dev, data)                           kfree(devcd)
18901daccf7SMukesh Ojha  *             mutex_lock(&devcd->mutex);
19001daccf7SMukesh Ojha  *
19101daccf7SMukesh Ojha  *
19201daccf7SMukesh Ojha  * In the above diagram, It looks like disabled_store() would be racing with parallely
19301daccf7SMukesh Ojha  * running devcd_del() and result in memory abort while acquiring devcd->mutex which
19401daccf7SMukesh Ojha  * is called after kfree of devcd memory  after dropping its last reference with
19501daccf7SMukesh Ojha  * put_device(). However, this will not happens as fn(dev, data) runs
19601daccf7SMukesh Ojha  * with its own reference to device via klist_node so it is not its last reference.
19701daccf7SMukesh Ojha  * so, above situation would not occur.
19801daccf7SMukesh Ojha  */
19901daccf7SMukesh Ojha 
disabled_store(const struct class * class,const struct class_attribute * attr,const char * buf,size_t count)20075a2d422SGreg Kroah-Hartman static ssize_t disabled_store(const struct class *class, const struct class_attribute *attr,
201d4533329SJohannes Berg 			      const char *buf, size_t count)
202d4533329SJohannes Berg {
203d4533329SJohannes Berg 	long tmp = simple_strtol(buf, NULL, 10);
204d4533329SJohannes Berg 
205d4533329SJohannes Berg 	/*
206d4533329SJohannes Berg 	 * This essentially makes the attribute write-once, since you can't
207d4533329SJohannes Berg 	 * go back to not having it disabled. This is intentional, it serves
208d4533329SJohannes Berg 	 * as a system lockdown feature.
209d4533329SJohannes Berg 	 */
210d4533329SJohannes Berg 	if (tmp != 1)
211d4533329SJohannes Berg 		return -EINVAL;
212d4533329SJohannes Berg 
213d4533329SJohannes Berg 	devcd_disabled = true;
214d4533329SJohannes Berg 
215d4533329SJohannes Berg 	class_for_each_device(&devcd_class, NULL, NULL, devcd_free);
216d4533329SJohannes Berg 
217d4533329SJohannes Berg 	return count;
218d4533329SJohannes Berg }
219f76d2527SGreg Kroah-Hartman static CLASS_ATTR_RW(disabled);
220d4533329SJohannes Berg 
221f76d2527SGreg Kroah-Hartman static struct attribute *devcd_class_attrs[] = {
222f76d2527SGreg Kroah-Hartman 	&class_attr_disabled.attr,
223f76d2527SGreg Kroah-Hartman 	NULL,
224d4533329SJohannes Berg };
225f76d2527SGreg Kroah-Hartman ATTRIBUTE_GROUPS(devcd_class);
226d4533329SJohannes Berg 
227833c9545SJohannes Berg static struct class devcd_class = {
228833c9545SJohannes Berg 	.name		= "devcoredump",
229833c9545SJohannes Berg 	.dev_release	= devcd_dev_release,
230833c9545SJohannes Berg 	.dev_groups	= devcd_dev_groups,
231f76d2527SGreg Kroah-Hartman 	.class_groups	= devcd_class_groups,
232833c9545SJohannes Berg };
233833c9545SJohannes Berg 
devcd_readv(char * buffer,loff_t offset,size_t count,void * data,size_t datalen)234833c9545SJohannes Berg static ssize_t devcd_readv(char *buffer, loff_t offset, size_t count,
23552256637SAviya Erenfeld 			   void *data, size_t datalen)
236833c9545SJohannes Berg {
237ce684d95SAkinobu Mita 	return memory_read_from_buffer(buffer, count, &offset, data, datalen);
238833c9545SJohannes Berg }
239833c9545SJohannes Berg 
devcd_freev(void * data)24052256637SAviya Erenfeld static void devcd_freev(void *data)
24152256637SAviya Erenfeld {
24252256637SAviya Erenfeld 	vfree(data);
24352256637SAviya Erenfeld }
24452256637SAviya Erenfeld 
245833c9545SJohannes Berg /**
246833c9545SJohannes Berg  * dev_coredumpv - create device coredump with vmalloc data
247833c9545SJohannes Berg  * @dev: the struct device for the crashed device
248833c9545SJohannes Berg  * @data: vmalloc data containing the device coredump
249833c9545SJohannes Berg  * @datalen: length of the data
250833c9545SJohannes Berg  * @gfp: allocation flags
251833c9545SJohannes Berg  *
252833c9545SJohannes Berg  * This function takes ownership of the vmalloc'ed data and will free
253833c9545SJohannes Berg  * it when it is no longer used. See dev_coredumpm() for more information.
254833c9545SJohannes Berg  */
dev_coredumpv(struct device * dev,void * data,size_t datalen,gfp_t gfp)25552256637SAviya Erenfeld void dev_coredumpv(struct device *dev, void *data, size_t datalen,
256833c9545SJohannes Berg 		   gfp_t gfp)
257833c9545SJohannes Berg {
25852256637SAviya Erenfeld 	dev_coredumpm(dev, NULL, data, datalen, gfp, devcd_readv, devcd_freev);
259833c9545SJohannes Berg }
260833c9545SJohannes Berg EXPORT_SYMBOL_GPL(dev_coredumpv);
261833c9545SJohannes Berg 
devcd_match_failing(struct device * dev,const void * failing)262833c9545SJohannes Berg static int devcd_match_failing(struct device *dev, const void *failing)
263833c9545SJohannes Berg {
264833c9545SJohannes Berg 	struct devcd_entry *devcd = dev_to_devcd(dev);
265833c9545SJohannes Berg 
266833c9545SJohannes Berg 	return devcd->failing_dev == failing;
267833c9545SJohannes Berg }
268833c9545SJohannes Berg 
269833c9545SJohannes Berg /**
27052256637SAviya Erenfeld  * devcd_free_sgtable - free all the memory of the given scatterlist table
27152256637SAviya Erenfeld  * (i.e. both pages and scatterlist instances)
27252256637SAviya Erenfeld  * NOTE: if two tables allocated with devcd_alloc_sgtable and then chained
27352256637SAviya Erenfeld  * using the sg_chain function then that function should be called only once
27452256637SAviya Erenfeld  * on the chained table
275cc710790SPierre-Louis Bossart  * @data: pointer to sg_table to free
27652256637SAviya Erenfeld  */
devcd_free_sgtable(void * data)27752256637SAviya Erenfeld static void devcd_free_sgtable(void *data)
27852256637SAviya Erenfeld {
27952256637SAviya Erenfeld 	_devcd_free_sgtable(data);
28052256637SAviya Erenfeld }
28152256637SAviya Erenfeld 
28252256637SAviya Erenfeld /**
283cc710790SPierre-Louis Bossart  * devcd_read_from_sgtable - copy data from sg_table to a given buffer
28452256637SAviya Erenfeld  * and return the number of bytes read
28552256637SAviya Erenfeld  * @buffer: the buffer to copy the data to it
28652256637SAviya Erenfeld  * @buf_len: the length of the buffer
28752256637SAviya Erenfeld  * @data: the scatterlist table to copy from
28852256637SAviya Erenfeld  * @offset: start copy from @offset@ bytes from the head of the data
28952256637SAviya Erenfeld  *	in the given scatterlist
29052256637SAviya Erenfeld  * @data_len: the length of the data in the sg_table
29152256637SAviya Erenfeld  */
devcd_read_from_sgtable(char * buffer,loff_t offset,size_t buf_len,void * data,size_t data_len)29252256637SAviya Erenfeld static ssize_t devcd_read_from_sgtable(char *buffer, loff_t offset,
29352256637SAviya Erenfeld 				       size_t buf_len, void *data,
29452256637SAviya Erenfeld 				       size_t data_len)
29552256637SAviya Erenfeld {
29652256637SAviya Erenfeld 	struct scatterlist *table = data;
29752256637SAviya Erenfeld 
29852256637SAviya Erenfeld 	if (offset > data_len)
29952256637SAviya Erenfeld 		return -EINVAL;
30052256637SAviya Erenfeld 
30152256637SAviya Erenfeld 	if (offset + buf_len > data_len)
30252256637SAviya Erenfeld 		buf_len = data_len - offset;
30352256637SAviya Erenfeld 	return sg_pcopy_to_buffer(table, sg_nents(table), buffer, buf_len,
30452256637SAviya Erenfeld 				  offset);
30552256637SAviya Erenfeld }
30652256637SAviya Erenfeld 
30752256637SAviya Erenfeld /**
308833c9545SJohannes Berg  * dev_coredumpm - create device coredump with read/free methods
309833c9545SJohannes Berg  * @dev: the struct device for the crashed device
310833c9545SJohannes Berg  * @owner: the module that contains the read/free functions, use %THIS_MODULE
311833c9545SJohannes Berg  * @data: data cookie for the @read/@free functions
312833c9545SJohannes Berg  * @datalen: length of the data
313833c9545SJohannes Berg  * @gfp: allocation flags
314833c9545SJohannes Berg  * @read: function to read from the given buffer
315833c9545SJohannes Berg  * @free: function to free the given buffer
316833c9545SJohannes Berg  *
317833c9545SJohannes Berg  * Creates a new device coredump for the given device. If a previous one hasn't
318833c9545SJohannes Berg  * been read yet, the new coredump is discarded. The data lifetime is determined
319833c9545SJohannes Berg  * by the device coredump framework and when it is no longer needed the @free
320833c9545SJohannes Berg  * function will be called to free the data.
321833c9545SJohannes Berg  */
dev_coredumpm(struct device * dev,struct module * owner,void * data,size_t datalen,gfp_t gfp,ssize_t (* read)(char * buffer,loff_t offset,size_t count,void * data,size_t datalen),void (* free)(void * data))322833c9545SJohannes Berg void dev_coredumpm(struct device *dev, struct module *owner,
32352256637SAviya Erenfeld 		   void *data, size_t datalen, gfp_t gfp,
324833c9545SJohannes Berg 		   ssize_t (*read)(char *buffer, loff_t offset, size_t count,
32552256637SAviya Erenfeld 				   void *data, size_t datalen),
32652256637SAviya Erenfeld 		   void (*free)(void *data))
327833c9545SJohannes Berg {
328833c9545SJohannes Berg 	static atomic_t devcd_count = ATOMIC_INIT(0);
329833c9545SJohannes Berg 	struct devcd_entry *devcd;
330833c9545SJohannes Berg 	struct device *existing;
331833c9545SJohannes Berg 
332d4533329SJohannes Berg 	if (devcd_disabled)
333d4533329SJohannes Berg 		goto free;
334d4533329SJohannes Berg 
335833c9545SJohannes Berg 	existing = class_find_device(&devcd_class, NULL, dev,
336833c9545SJohannes Berg 				     devcd_match_failing);
337833c9545SJohannes Berg 	if (existing) {
338833c9545SJohannes Berg 		put_device(existing);
339833c9545SJohannes Berg 		goto free;
340833c9545SJohannes Berg 	}
341833c9545SJohannes Berg 
342833c9545SJohannes Berg 	if (!try_module_get(owner))
343833c9545SJohannes Berg 		goto free;
344833c9545SJohannes Berg 
345833c9545SJohannes Berg 	devcd = kzalloc(sizeof(*devcd), gfp);
346833c9545SJohannes Berg 	if (!devcd)
347833c9545SJohannes Berg 		goto put_module;
348833c9545SJohannes Berg 
349833c9545SJohannes Berg 	devcd->owner = owner;
350833c9545SJohannes Berg 	devcd->data = data;
351833c9545SJohannes Berg 	devcd->datalen = datalen;
352833c9545SJohannes Berg 	devcd->read = read;
353833c9545SJohannes Berg 	devcd->free = free;
354833c9545SJohannes Berg 	devcd->failing_dev = get_device(dev);
35501daccf7SMukesh Ojha 	devcd->delete_work = false;
356833c9545SJohannes Berg 
35701daccf7SMukesh Ojha 	mutex_init(&devcd->mutex);
358833c9545SJohannes Berg 	device_initialize(&devcd->devcd_dev);
359833c9545SJohannes Berg 
360833c9545SJohannes Berg 	dev_set_name(&devcd->devcd_dev, "devcd%d",
361833c9545SJohannes Berg 		     atomic_inc_return(&devcd_count));
362833c9545SJohannes Berg 	devcd->devcd_dev.class = &devcd_class;
363833c9545SJohannes Berg 
36401daccf7SMukesh Ojha 	mutex_lock(&devcd->mutex);
365*7d831c68SMukesh Ojha 	dev_set_uevent_suppress(&devcd->devcd_dev, true);
366833c9545SJohannes Berg 	if (device_add(&devcd->devcd_dev))
367833c9545SJohannes Berg 		goto put_device;
368833c9545SJohannes Berg 
36953f95c55SArnd Bergmann 	/*
37053f95c55SArnd Bergmann 	 * These should normally not fail, but there is no problem
37153f95c55SArnd Bergmann 	 * continuing without the links, so just warn instead of
37253f95c55SArnd Bergmann 	 * failing.
37353f95c55SArnd Bergmann 	 */
374833c9545SJohannes Berg 	if (sysfs_create_link(&devcd->devcd_dev.kobj, &dev->kobj,
37553f95c55SArnd Bergmann 			      "failing_device") ||
37653f95c55SArnd Bergmann 	    sysfs_create_link(&dev->kobj, &devcd->devcd_dev.kobj,
377833c9545SJohannes Berg 		              "devcoredump"))
37853f95c55SArnd Bergmann 		dev_warn(dev, "devcoredump create_link failed\n");
379833c9545SJohannes Berg 
380*7d831c68SMukesh Ojha 	dev_set_uevent_suppress(&devcd->devcd_dev, false);
381*7d831c68SMukesh Ojha 	kobject_uevent(&devcd->devcd_dev.kobj, KOBJ_ADD);
382833c9545SJohannes Berg 	INIT_DELAYED_WORK(&devcd->del_wk, devcd_del);
383833c9545SJohannes Berg 	schedule_delayed_work(&devcd->del_wk, DEVCD_TIMEOUT);
38401daccf7SMukesh Ojha 	mutex_unlock(&devcd->mutex);
385833c9545SJohannes Berg 	return;
386833c9545SJohannes Berg  put_device:
387833c9545SJohannes Berg 	put_device(&devcd->devcd_dev);
38801daccf7SMukesh Ojha 	mutex_unlock(&devcd->mutex);
389833c9545SJohannes Berg  put_module:
390833c9545SJohannes Berg 	module_put(owner);
391833c9545SJohannes Berg  free:
392833c9545SJohannes Berg 	free(data);
393833c9545SJohannes Berg }
394833c9545SJohannes Berg EXPORT_SYMBOL_GPL(dev_coredumpm);
395833c9545SJohannes Berg 
39652256637SAviya Erenfeld /**
3972a77eec0SAkinobu Mita  * dev_coredumpsg - create device coredump that uses scatterlist as data
39852256637SAviya Erenfeld  * parameter
39952256637SAviya Erenfeld  * @dev: the struct device for the crashed device
40052256637SAviya Erenfeld  * @table: the dump data
40152256637SAviya Erenfeld  * @datalen: length of the data
40252256637SAviya Erenfeld  * @gfp: allocation flags
40352256637SAviya Erenfeld  *
40452256637SAviya Erenfeld  * Creates a new device coredump for the given device. If a previous one hasn't
40552256637SAviya Erenfeld  * been read yet, the new coredump is discarded. The data lifetime is determined
40652256637SAviya Erenfeld  * by the device coredump framework and when it is no longer needed
40752256637SAviya Erenfeld  * it will free the data.
40852256637SAviya Erenfeld  */
dev_coredumpsg(struct device * dev,struct scatterlist * table,size_t datalen,gfp_t gfp)40952256637SAviya Erenfeld void dev_coredumpsg(struct device *dev, struct scatterlist *table,
41052256637SAviya Erenfeld 		    size_t datalen, gfp_t gfp)
41152256637SAviya Erenfeld {
41252256637SAviya Erenfeld 	dev_coredumpm(dev, NULL, table, datalen, gfp, devcd_read_from_sgtable,
41352256637SAviya Erenfeld 		      devcd_free_sgtable);
41452256637SAviya Erenfeld }
41552256637SAviya Erenfeld EXPORT_SYMBOL_GPL(dev_coredumpsg);
41652256637SAviya Erenfeld 
devcoredump_init(void)417833c9545SJohannes Berg static int __init devcoredump_init(void)
418833c9545SJohannes Berg {
419833c9545SJohannes Berg 	return class_register(&devcd_class);
420833c9545SJohannes Berg }
421833c9545SJohannes Berg __initcall(devcoredump_init);
422833c9545SJohannes Berg 
devcoredump_exit(void)423833c9545SJohannes Berg static void __exit devcoredump_exit(void)
424833c9545SJohannes Berg {
425833c9545SJohannes Berg 	class_for_each_device(&devcd_class, NULL, NULL, devcd_free);
426833c9545SJohannes Berg 	class_unregister(&devcd_class);
427833c9545SJohannes Berg }
428833c9545SJohannes Berg __exitcall(devcoredump_exit);
429