44 * GPL LICENSE SUMMARY
55 *
66 * Copyright(c) 2014 Intel Mobile Communications GmbH
7+ * Copyright(c) 2015 Intel Deutschland GmbH
78 *
89 * This program is free software; you can redistribute it and/or modify
910 * it under the terms of version 2 of the GNU General Public License as
@@ -41,12 +42,12 @@ static bool devcd_disabled;
4142
4243struct devcd_entry {
4344 struct device devcd_dev ;
44- const void * data ;
45+ void * data ;
4546 size_t datalen ;
4647 struct module * owner ;
4748 ssize_t (* read )(char * buffer , loff_t offset , size_t count ,
48- const void * data , size_t datalen );
49- void (* free )(const void * data );
49+ void * data , size_t datalen );
50+ void (* free )(void * data );
5051 struct delayed_work del_wk ;
5152 struct device * failing_dev ;
5253};
@@ -174,7 +175,7 @@ static struct class devcd_class = {
174175};
175176
176177static ssize_t devcd_readv (char * buffer , loff_t offset , size_t count ,
177- const void * data , size_t datalen )
178+ void * data , size_t datalen )
178179{
179180 if (offset > datalen )
180181 return - EINVAL ;
@@ -188,6 +189,11 @@ static ssize_t devcd_readv(char *buffer, loff_t offset, size_t count,
188189 return count ;
189190}
190191
192+ static void devcd_freev (void * data )
193+ {
194+ vfree (data );
195+ }
196+
191197/**
192198 * dev_coredumpv - create device coredump with vmalloc data
193199 * @dev: the struct device for the crashed device
@@ -198,10 +204,10 @@ static ssize_t devcd_readv(char *buffer, loff_t offset, size_t count,
198204 * This function takes ownership of the vmalloc'ed data and will free
199205 * it when it is no longer used. See dev_coredumpm() for more information.
200206 */
201- void dev_coredumpv (struct device * dev , const void * data , size_t datalen ,
207+ void dev_coredumpv (struct device * dev , void * data , size_t datalen ,
202208 gfp_t gfp )
203209{
204- dev_coredumpm (dev , NULL , data , datalen , gfp , devcd_readv , vfree );
210+ dev_coredumpm (dev , NULL , data , datalen , gfp , devcd_readv , devcd_freev );
205211}
206212EXPORT_SYMBOL_GPL (dev_coredumpv );
207213
@@ -212,6 +218,44 @@ static int devcd_match_failing(struct device *dev, const void *failing)
212218 return devcd -> failing_dev == failing ;
213219}
214220
221+ /**
222+ * devcd_free_sgtable - free all the memory of the given scatterlist table
223+ * (i.e. both pages and scatterlist instances)
224+ * NOTE: if two tables allocated with devcd_alloc_sgtable and then chained
225+ * using the sg_chain function then that function should be called only once
226+ * on the chained table
227+ * @table: pointer to sg_table to free
228+ */
229+ static void devcd_free_sgtable (void * data )
230+ {
231+ _devcd_free_sgtable (data );
232+ }
233+
234+ /**
235+ * devcd_read_from_table - copy data from sg_table to a given buffer
236+ * and return the number of bytes read
237+ * @buffer: the buffer to copy the data to it
238+ * @buf_len: the length of the buffer
239+ * @data: the scatterlist table to copy from
240+ * @offset: start copy from @offset@ bytes from the head of the data
241+ * in the given scatterlist
242+ * @data_len: the length of the data in the sg_table
243+ */
244+ static ssize_t devcd_read_from_sgtable (char * buffer , loff_t offset ,
245+ size_t buf_len , void * data ,
246+ size_t data_len )
247+ {
248+ struct scatterlist * table = data ;
249+
250+ if (offset > data_len )
251+ return - EINVAL ;
252+
253+ if (offset + buf_len > data_len )
254+ buf_len = data_len - offset ;
255+ return sg_pcopy_to_buffer (table , sg_nents (table ), buffer , buf_len ,
256+ offset );
257+ }
258+
215259/**
216260 * dev_coredumpm - create device coredump with read/free methods
217261 * @dev: the struct device for the crashed device
@@ -228,10 +272,10 @@ static int devcd_match_failing(struct device *dev, const void *failing)
228272 * function will be called to free the data.
229273 */
230274void dev_coredumpm (struct device * dev , struct module * owner ,
231- const void * data , size_t datalen , gfp_t gfp ,
275+ void * data , size_t datalen , gfp_t gfp ,
232276 ssize_t (* read )(char * buffer , loff_t offset , size_t count ,
233- const void * data , size_t datalen ),
234- void (* free )(const void * data ))
277+ void * data , size_t datalen ),
278+ void (* free )(void * data ))
235279{
236280 static atomic_t devcd_count = ATOMIC_INIT (0 );
237281 struct devcd_entry * devcd ;
@@ -291,6 +335,27 @@ void dev_coredumpm(struct device *dev, struct module *owner,
291335}
292336EXPORT_SYMBOL_GPL (dev_coredumpm );
293337
338+ /**
339+ * dev_coredumpmsg - create device coredump that uses scatterlist as data
340+ * parameter
341+ * @dev: the struct device for the crashed device
342+ * @table: the dump data
343+ * @datalen: length of the data
344+ * @gfp: allocation flags
345+ *
346+ * Creates a new device coredump for the given device. If a previous one hasn't
347+ * been read yet, the new coredump is discarded. The data lifetime is determined
348+ * by the device coredump framework and when it is no longer needed
349+ * it will free the data.
350+ */
351+ void dev_coredumpsg (struct device * dev , struct scatterlist * table ,
352+ size_t datalen , gfp_t gfp )
353+ {
354+ dev_coredumpm (dev , NULL , table , datalen , gfp , devcd_read_from_sgtable ,
355+ devcd_free_sgtable );
356+ }
357+ EXPORT_SYMBOL_GPL (dev_coredumpsg );
358+
294359static int __init devcoredump_init (void )
295360{
296361 return class_register (& devcd_class );
0 commit comments