dma-debug.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211
  1. /*
  2. * Copyright (C) 2008 Advanced Micro Devices, Inc.
  3. *
  4. * Author: Joerg Roedel <joerg.roedel@amd.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published
  8. * by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/scatterlist.h>
  20. #include <linux/dma-mapping.h>
  21. #include <linux/stacktrace.h>
  22. #include <linux/dma-debug.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/debugfs.h>
  25. #include <linux/uaccess.h>
  26. #include <linux/device.h>
  27. #include <linux/types.h>
  28. #include <linux/sched.h>
  29. #include <linux/ctype.h>
  30. #include <linux/list.h>
  31. #include <linux/slab.h>
  32. #include <asm/sections.h>
  33. #define HASH_SIZE 1024ULL
  34. #define HASH_FN_SHIFT 13
  35. #define HASH_FN_MASK (HASH_SIZE - 1)
  36. enum {
  37. dma_debug_single,
  38. dma_debug_page,
  39. dma_debug_sg,
  40. dma_debug_coherent,
  41. };
  42. #define DMA_DEBUG_STACKTRACE_ENTRIES 5
  43. struct dma_debug_entry {
  44. struct list_head list;
  45. struct device *dev;
  46. int type;
  47. phys_addr_t paddr;
  48. u64 dev_addr;
  49. u64 size;
  50. int direction;
  51. int sg_call_ents;
  52. int sg_mapped_ents;
  53. #ifdef CONFIG_STACKTRACE
  54. struct stack_trace stacktrace;
  55. unsigned long st_entries[DMA_DEBUG_STACKTRACE_ENTRIES];
  56. #endif
  57. };
  58. struct hash_bucket {
  59. struct list_head list;
  60. spinlock_t lock;
  61. } ____cacheline_aligned_in_smp;
  62. /* Hash list to save the allocated dma addresses */
  63. static struct hash_bucket dma_entry_hash[HASH_SIZE];
  64. /* List of pre-allocated dma_debug_entry's */
  65. static LIST_HEAD(free_entries);
  66. /* Lock for the list above */
  67. static DEFINE_SPINLOCK(free_entries_lock);
  68. /* Global disable flag - will be set in case of an error */
  69. static bool global_disable __read_mostly;
  70. /* Global error count */
  71. static u32 error_count;
  72. /* Global error show enable*/
  73. static u32 show_all_errors __read_mostly;
  74. /* Number of errors to show */
  75. static u32 show_num_errors = 1;
  76. static u32 num_free_entries;
  77. static u32 min_free_entries;
  78. static u32 nr_total_entries;
  79. /* number of preallocated entries requested by kernel cmdline */
  80. static u32 req_entries;
  81. /* debugfs dentry's for the stuff above */
  82. static struct dentry *dma_debug_dent __read_mostly;
  83. static struct dentry *global_disable_dent __read_mostly;
  84. static struct dentry *error_count_dent __read_mostly;
  85. static struct dentry *show_all_errors_dent __read_mostly;
  86. static struct dentry *show_num_errors_dent __read_mostly;
  87. static struct dentry *num_free_entries_dent __read_mostly;
  88. static struct dentry *min_free_entries_dent __read_mostly;
  89. static struct dentry *filter_dent __read_mostly;
  90. /* per-driver filter related state */
  91. #define NAME_MAX_LEN 64
  92. static char current_driver_name[NAME_MAX_LEN] __read_mostly;
  93. static struct device_driver *current_driver __read_mostly;
  94. static DEFINE_RWLOCK(driver_name_lock);
  95. static const char *type2name[4] = { "single", "page",
  96. "scather-gather", "coherent" };
  97. static const char *dir2name[4] = { "DMA_BIDIRECTIONAL", "DMA_TO_DEVICE",
  98. "DMA_FROM_DEVICE", "DMA_NONE" };
  99. /* little merge helper - remove it after the merge window */
  100. #ifndef BUS_NOTIFY_UNBOUND_DRIVER
  101. #define BUS_NOTIFY_UNBOUND_DRIVER 0x0005
  102. #endif
  103. /*
  104. * The access to some variables in this macro is racy. We can't use atomic_t
  105. * here because all these variables are exported to debugfs. Some of them even
  106. * writeable. This is also the reason why a lock won't help much. But anyway,
  107. * the races are no big deal. Here is why:
  108. *
  109. * error_count: the addition is racy, but the worst thing that can happen is
  110. * that we don't count some errors
  111. * show_num_errors: the subtraction is racy. Also no big deal because in
  112. * worst case this will result in one warning more in the
  113. * system log than the user configured. This variable is
  114. * writeable via debugfs.
  115. */
  116. static inline void dump_entry_trace(struct dma_debug_entry *entry)
  117. {
  118. #ifdef CONFIG_STACKTRACE
  119. if (entry) {
  120. printk(KERN_WARNING "Mapped at:\n");
  121. print_stack_trace(&entry->stacktrace, 0);
  122. }
  123. #endif
  124. }
  125. static bool driver_filter(struct device *dev)
  126. {
  127. /* driver filter off */
  128. if (likely(!current_driver_name[0]))
  129. return true;
  130. /* driver filter on and initialized */
  131. if (current_driver && dev->driver == current_driver)
  132. return true;
  133. /* driver filter on but not yet initialized */
  134. if (!current_driver && current_driver_name[0]) {
  135. struct device_driver *drv = get_driver(dev->driver);
  136. unsigned long flags;
  137. bool ret = false;
  138. if (!drv)
  139. return false;
  140. /* lock to protect against change of current_driver_name */
  141. read_lock_irqsave(&driver_name_lock, flags);
  142. if (drv->name &&
  143. strncmp(current_driver_name, drv->name,
  144. NAME_MAX_LEN-1) == 0) {
  145. current_driver = drv;
  146. ret = true;
  147. }
  148. read_unlock_irqrestore(&driver_name_lock, flags);
  149. put_driver(drv);
  150. return ret;
  151. }
  152. return false;
  153. }
  154. #define err_printk(dev, entry, format, arg...) do { \
  155. error_count += 1; \
  156. if (driver_filter(dev) && \
  157. (show_all_errors || show_num_errors > 0)) { \
  158. WARN(1, "%s %s: " format, \
  159. dev_driver_string(dev), \
  160. dev_name(dev) , ## arg); \
  161. dump_entry_trace(entry); \
  162. } \
  163. if (!show_all_errors && show_num_errors > 0) \
  164. show_num_errors -= 1; \
  165. } while (0);
  166. /*
  167. * Hash related functions
  168. *
  169. * Every DMA-API request is saved into a struct dma_debug_entry. To
  170. * have quick access to these structs they are stored into a hash.
  171. */
  172. static int hash_fn(struct dma_debug_entry *entry)
  173. {
  174. /*
  175. * Hash function is based on the dma address.
  176. * We use bits 20-27 here as the index into the hash
  177. */
  178. return (entry->dev_addr >> HASH_FN_SHIFT) & HASH_FN_MASK;
  179. }
  180. /*
  181. * Request exclusive access to a hash bucket for a given dma_debug_entry.
  182. */
  183. static struct hash_bucket *get_hash_bucket(struct dma_debug_entry *entry,
  184. unsigned long *flags)
  185. {
  186. int idx = hash_fn(entry);
  187. unsigned long __flags;
  188. spin_lock_irqsave(&dma_entry_hash[idx].lock, __flags);
  189. *flags = __flags;
  190. return &dma_entry_hash[idx];
  191. }
  192. /*
  193. * Give up exclusive access to the hash bucket
  194. */
  195. static void put_hash_bucket(struct hash_bucket *bucket,
  196. unsigned long *flags)
  197. {
  198. unsigned long __flags = *flags;
  199. spin_unlock_irqrestore(&bucket->lock, __flags);
  200. }
  201. /*
  202. * Search a given entry in the hash bucket list
  203. */
  204. static struct dma_debug_entry *hash_bucket_find(struct hash_bucket *bucket,
  205. struct dma_debug_entry *ref)
  206. {
  207. struct dma_debug_entry *entry;
  208. list_for_each_entry(entry, &bucket->list, list) {
  209. if ((entry->dev_addr == ref->dev_addr) &&
  210. (entry->dev == ref->dev))
  211. return entry;
  212. }
  213. return NULL;
  214. }
  215. /*
  216. * Add an entry to a hash bucket
  217. */
  218. static void hash_bucket_add(struct hash_bucket *bucket,
  219. struct dma_debug_entry *entry)
  220. {
  221. list_add_tail(&entry->list, &bucket->list);
  222. }
  223. /*
  224. * Remove entry from a hash bucket list
  225. */
  226. static void hash_bucket_del(struct dma_debug_entry *entry)
  227. {
  228. list_del(&entry->list);
  229. }
  230. /*
  231. * Dump mapping entries for debugging purposes
  232. */
  233. void debug_dma_dump_mappings(struct device *dev)
  234. {
  235. int idx;
  236. for (idx = 0; idx < HASH_SIZE; idx++) {
  237. struct hash_bucket *bucket = &dma_entry_hash[idx];
  238. struct dma_debug_entry *entry;
  239. unsigned long flags;
  240. spin_lock_irqsave(&bucket->lock, flags);
  241. list_for_each_entry(entry, &bucket->list, list) {
  242. if (!dev || dev == entry->dev) {
  243. dev_info(entry->dev,
  244. "%s idx %d P=%Lx D=%Lx L=%Lx %s\n",
  245. type2name[entry->type], idx,
  246. (unsigned long long)entry->paddr,
  247. entry->dev_addr, entry->size,
  248. dir2name[entry->direction]);
  249. }
  250. }
  251. spin_unlock_irqrestore(&bucket->lock, flags);
  252. }
  253. }
  254. EXPORT_SYMBOL(debug_dma_dump_mappings);
  255. /*
  256. * Wrapper function for adding an entry to the hash.
  257. * This function takes care of locking itself.
  258. */
  259. static void add_dma_entry(struct dma_debug_entry *entry)
  260. {
  261. struct hash_bucket *bucket;
  262. unsigned long flags;
  263. bucket = get_hash_bucket(entry, &flags);
  264. hash_bucket_add(bucket, entry);
  265. put_hash_bucket(bucket, &flags);
  266. }
  267. static struct dma_debug_entry *__dma_entry_alloc(void)
  268. {
  269. struct dma_debug_entry *entry;
  270. entry = list_entry(free_entries.next, struct dma_debug_entry, list);
  271. list_del(&entry->list);
  272. memset(entry, 0, sizeof(*entry));
  273. num_free_entries -= 1;
  274. if (num_free_entries < min_free_entries)
  275. min_free_entries = num_free_entries;
  276. return entry;
  277. }
  278. /* struct dma_entry allocator
  279. *
  280. * The next two functions implement the allocator for
  281. * struct dma_debug_entries.
  282. */
  283. static struct dma_debug_entry *dma_entry_alloc(void)
  284. {
  285. struct dma_debug_entry *entry = NULL;
  286. unsigned long flags;
  287. spin_lock_irqsave(&free_entries_lock, flags);
  288. if (list_empty(&free_entries)) {
  289. printk(KERN_ERR "DMA-API: debugging out of memory "
  290. "- disabling\n");
  291. global_disable = true;
  292. goto out;
  293. }
  294. entry = __dma_entry_alloc();
  295. #ifdef CONFIG_STACKTRACE
  296. entry->stacktrace.max_entries = DMA_DEBUG_STACKTRACE_ENTRIES;
  297. entry->stacktrace.entries = entry->st_entries;
  298. entry->stacktrace.skip = 2;
  299. save_stack_trace(&entry->stacktrace);
  300. #endif
  301. out:
  302. spin_unlock_irqrestore(&free_entries_lock, flags);
  303. return entry;
  304. }
  305. static void dma_entry_free(struct dma_debug_entry *entry)
  306. {
  307. unsigned long flags;
  308. /*
  309. * add to beginning of the list - this way the entries are
  310. * more likely cache hot when they are reallocated.
  311. */
  312. spin_lock_irqsave(&free_entries_lock, flags);
  313. list_add(&entry->list, &free_entries);
  314. num_free_entries += 1;
  315. spin_unlock_irqrestore(&free_entries_lock, flags);
  316. }
  317. int dma_debug_resize_entries(u32 num_entries)
  318. {
  319. int i, delta, ret = 0;
  320. unsigned long flags;
  321. struct dma_debug_entry *entry;
  322. LIST_HEAD(tmp);
  323. spin_lock_irqsave(&free_entries_lock, flags);
  324. if (nr_total_entries < num_entries) {
  325. delta = num_entries - nr_total_entries;
  326. spin_unlock_irqrestore(&free_entries_lock, flags);
  327. for (i = 0; i < delta; i++) {
  328. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  329. if (!entry)
  330. break;
  331. list_add_tail(&entry->list, &tmp);
  332. }
  333. spin_lock_irqsave(&free_entries_lock, flags);
  334. list_splice(&tmp, &free_entries);
  335. nr_total_entries += i;
  336. num_free_entries += i;
  337. } else {
  338. delta = nr_total_entries - num_entries;
  339. for (i = 0; i < delta && !list_empty(&free_entries); i++) {
  340. entry = __dma_entry_alloc();
  341. kfree(entry);
  342. }
  343. nr_total_entries -= i;
  344. }
  345. if (nr_total_entries != num_entries)
  346. ret = 1;
  347. spin_unlock_irqrestore(&free_entries_lock, flags);
  348. return ret;
  349. }
  350. EXPORT_SYMBOL(dma_debug_resize_entries);
  351. /*
  352. * DMA-API debugging init code
  353. *
  354. * The init code does two things:
  355. * 1. Initialize core data structures
  356. * 2. Preallocate a given number of dma_debug_entry structs
  357. */
  358. static int prealloc_memory(u32 num_entries)
  359. {
  360. struct dma_debug_entry *entry, *next_entry;
  361. int i;
  362. for (i = 0; i < num_entries; ++i) {
  363. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  364. if (!entry)
  365. goto out_err;
  366. list_add_tail(&entry->list, &free_entries);
  367. }
  368. num_free_entries = num_entries;
  369. min_free_entries = num_entries;
  370. printk(KERN_INFO "DMA-API: preallocated %d debug entries\n",
  371. num_entries);
  372. return 0;
  373. out_err:
  374. list_for_each_entry_safe(entry, next_entry, &free_entries, list) {
  375. list_del(&entry->list);
  376. kfree(entry);
  377. }
  378. return -ENOMEM;
  379. }
  380. static ssize_t filter_read(struct file *file, char __user *user_buf,
  381. size_t count, loff_t *ppos)
  382. {
  383. unsigned long flags;
  384. char buf[NAME_MAX_LEN + 1];
  385. int len;
  386. if (!current_driver_name[0])
  387. return 0;
  388. /*
  389. * We can't copy to userspace directly because current_driver_name can
  390. * only be read under the driver_name_lock with irqs disabled. So
  391. * create a temporary copy first.
  392. */
  393. read_lock_irqsave(&driver_name_lock, flags);
  394. len = scnprintf(buf, NAME_MAX_LEN + 1, "%s\n", current_driver_name);
  395. read_unlock_irqrestore(&driver_name_lock, flags);
  396. return simple_read_from_buffer(user_buf, count, ppos, buf, len);
  397. }
  398. static ssize_t filter_write(struct file *file, const char __user *userbuf,
  399. size_t count, loff_t *ppos)
  400. {
  401. unsigned long flags;
  402. char buf[NAME_MAX_LEN];
  403. size_t len = NAME_MAX_LEN - 1;
  404. int i;
  405. /*
  406. * We can't copy from userspace directly. Access to
  407. * current_driver_name is protected with a write_lock with irqs
  408. * disabled. Since copy_from_user can fault and may sleep we
  409. * need to copy to temporary buffer first
  410. */
  411. len = min(count, len);
  412. if (copy_from_user(buf, userbuf, len))
  413. return -EFAULT;
  414. buf[len] = 0;
  415. write_lock_irqsave(&driver_name_lock, flags);
  416. /* Now handle the string we got from userspace very carefully.
  417. * The rules are:
  418. * - only use the first token we got
  419. * - token delimiter is everything looking like a space
  420. * character (' ', '\n', '\t' ...)
  421. *
  422. */
  423. if (!isalnum(buf[0])) {
  424. /*
  425. If the first character userspace gave us is not
  426. * alphanumerical then assume the filter should be
  427. * switched off.
  428. */
  429. if (current_driver_name[0])
  430. printk(KERN_INFO "DMA-API: switching off dma-debug "
  431. "driver filter\n");
  432. current_driver_name[0] = 0;
  433. current_driver = NULL;
  434. goto out_unlock;
  435. }
  436. /*
  437. * Now parse out the first token and use it as the name for the
  438. * driver to filter for.
  439. */
  440. for (i = 0; i < NAME_MAX_LEN; ++i) {
  441. current_driver_name[i] = buf[i];
  442. if (isspace(buf[i]) || buf[i] == ' ' || buf[i] == 0)
  443. break;
  444. }
  445. current_driver_name[i] = 0;
  446. current_driver = NULL;
  447. printk(KERN_INFO "DMA-API: enable driver filter for driver [%s]\n",
  448. current_driver_name);
  449. out_unlock:
  450. write_unlock_irqrestore(&driver_name_lock, flags);
  451. return count;
  452. }
  453. const struct file_operations filter_fops = {
  454. .read = filter_read,
  455. .write = filter_write,
  456. };
  457. static int dma_debug_fs_init(void)
  458. {
  459. dma_debug_dent = debugfs_create_dir("dma-api", NULL);
  460. if (!dma_debug_dent) {
  461. printk(KERN_ERR "DMA-API: can not create debugfs directory\n");
  462. return -ENOMEM;
  463. }
  464. global_disable_dent = debugfs_create_bool("disabled", 0444,
  465. dma_debug_dent,
  466. (u32 *)&global_disable);
  467. if (!global_disable_dent)
  468. goto out_err;
  469. error_count_dent = debugfs_create_u32("error_count", 0444,
  470. dma_debug_dent, &error_count);
  471. if (!error_count_dent)
  472. goto out_err;
  473. show_all_errors_dent = debugfs_create_u32("all_errors", 0644,
  474. dma_debug_dent,
  475. &show_all_errors);
  476. if (!show_all_errors_dent)
  477. goto out_err;
  478. show_num_errors_dent = debugfs_create_u32("num_errors", 0644,
  479. dma_debug_dent,
  480. &show_num_errors);
  481. if (!show_num_errors_dent)
  482. goto out_err;
  483. num_free_entries_dent = debugfs_create_u32("num_free_entries", 0444,
  484. dma_debug_dent,
  485. &num_free_entries);
  486. if (!num_free_entries_dent)
  487. goto out_err;
  488. min_free_entries_dent = debugfs_create_u32("min_free_entries", 0444,
  489. dma_debug_dent,
  490. &min_free_entries);
  491. if (!min_free_entries_dent)
  492. goto out_err;
  493. filter_dent = debugfs_create_file("driver_filter", 0644,
  494. dma_debug_dent, NULL, &filter_fops);
  495. if (!filter_dent)
  496. goto out_err;
  497. return 0;
  498. out_err:
  499. debugfs_remove_recursive(dma_debug_dent);
  500. return -ENOMEM;
  501. }
  502. static int device_dma_allocations(struct device *dev)
  503. {
  504. struct dma_debug_entry *entry;
  505. unsigned long flags;
  506. int count = 0, i;
  507. for (i = 0; i < HASH_SIZE; ++i) {
  508. spin_lock_irqsave(&dma_entry_hash[i].lock, flags);
  509. list_for_each_entry(entry, &dma_entry_hash[i].list, list) {
  510. if (entry->dev == dev)
  511. count += 1;
  512. }
  513. spin_unlock_irqrestore(&dma_entry_hash[i].lock, flags);
  514. }
  515. return count;
  516. }
  517. static int dma_debug_device_change(struct notifier_block *nb,
  518. unsigned long action, void *data)
  519. {
  520. struct device *dev = data;
  521. int count;
  522. switch (action) {
  523. case BUS_NOTIFY_UNBOUND_DRIVER:
  524. count = device_dma_allocations(dev);
  525. if (count == 0)
  526. break;
  527. err_printk(dev, NULL, "DMA-API: device driver has pending "
  528. "DMA allocations while released from device "
  529. "[count=%d]\n", count);
  530. break;
  531. default:
  532. break;
  533. }
  534. return 0;
  535. }
  536. void dma_debug_add_bus(struct bus_type *bus)
  537. {
  538. struct notifier_block *nb;
  539. nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
  540. if (nb == NULL) {
  541. printk(KERN_ERR "dma_debug_add_bus: out of memory\n");
  542. return;
  543. }
  544. nb->notifier_call = dma_debug_device_change;
  545. bus_register_notifier(bus, nb);
  546. }
  547. /*
  548. * Let the architectures decide how many entries should be preallocated.
  549. */
  550. void dma_debug_init(u32 num_entries)
  551. {
  552. int i;
  553. if (global_disable)
  554. return;
  555. for (i = 0; i < HASH_SIZE; ++i) {
  556. INIT_LIST_HEAD(&dma_entry_hash[i].list);
  557. dma_entry_hash[i].lock = SPIN_LOCK_UNLOCKED;
  558. }
  559. if (dma_debug_fs_init() != 0) {
  560. printk(KERN_ERR "DMA-API: error creating debugfs entries "
  561. "- disabling\n");
  562. global_disable = true;
  563. return;
  564. }
  565. if (req_entries)
  566. num_entries = req_entries;
  567. if (prealloc_memory(num_entries) != 0) {
  568. printk(KERN_ERR "DMA-API: debugging out of memory error "
  569. "- disabled\n");
  570. global_disable = true;
  571. return;
  572. }
  573. nr_total_entries = num_free_entries;
  574. printk(KERN_INFO "DMA-API: debugging enabled by kernel config\n");
  575. }
  576. static __init int dma_debug_cmdline(char *str)
  577. {
  578. if (!str)
  579. return -EINVAL;
  580. if (strncmp(str, "off", 3) == 0) {
  581. printk(KERN_INFO "DMA-API: debugging disabled on kernel "
  582. "command line\n");
  583. global_disable = true;
  584. }
  585. return 0;
  586. }
  587. static __init int dma_debug_entries_cmdline(char *str)
  588. {
  589. int res;
  590. if (!str)
  591. return -EINVAL;
  592. res = get_option(&str, &req_entries);
  593. if (!res)
  594. req_entries = 0;
  595. return 0;
  596. }
  597. __setup("dma_debug=", dma_debug_cmdline);
  598. __setup("dma_debug_entries=", dma_debug_entries_cmdline);
  599. static void check_unmap(struct dma_debug_entry *ref)
  600. {
  601. struct dma_debug_entry *entry;
  602. struct hash_bucket *bucket;
  603. unsigned long flags;
  604. if (dma_mapping_error(ref->dev, ref->dev_addr)) {
  605. err_printk(ref->dev, NULL, "DMA-API: device driver tries "
  606. "to free an invalid DMA memory address\n");
  607. return;
  608. }
  609. bucket = get_hash_bucket(ref, &flags);
  610. entry = hash_bucket_find(bucket, ref);
  611. if (!entry) {
  612. err_printk(ref->dev, NULL, "DMA-API: device driver tries "
  613. "to free DMA memory it has not allocated "
  614. "[device address=0x%016llx] [size=%llu bytes]\n",
  615. ref->dev_addr, ref->size);
  616. goto out;
  617. }
  618. if (ref->size != entry->size) {
  619. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  620. "DMA memory with different size "
  621. "[device address=0x%016llx] [map size=%llu bytes] "
  622. "[unmap size=%llu bytes]\n",
  623. ref->dev_addr, entry->size, ref->size);
  624. }
  625. if (ref->type != entry->type) {
  626. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  627. "DMA memory with wrong function "
  628. "[device address=0x%016llx] [size=%llu bytes] "
  629. "[mapped as %s] [unmapped as %s]\n",
  630. ref->dev_addr, ref->size,
  631. type2name[entry->type], type2name[ref->type]);
  632. } else if ((entry->type == dma_debug_coherent) &&
  633. (ref->paddr != entry->paddr)) {
  634. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  635. "DMA memory with different CPU address "
  636. "[device address=0x%016llx] [size=%llu bytes] "
  637. "[cpu alloc address=%p] [cpu free address=%p]",
  638. ref->dev_addr, ref->size,
  639. (void *)entry->paddr, (void *)ref->paddr);
  640. }
  641. if (ref->sg_call_ents && ref->type == dma_debug_sg &&
  642. ref->sg_call_ents != entry->sg_call_ents) {
  643. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  644. "DMA sg list with different entry count "
  645. "[map count=%d] [unmap count=%d]\n",
  646. entry->sg_call_ents, ref->sg_call_ents);
  647. }
  648. /*
  649. * This may be no bug in reality - but most implementations of the
  650. * DMA API don't handle this properly, so check for it here
  651. */
  652. if (ref->direction != entry->direction) {
  653. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  654. "DMA memory with different direction "
  655. "[device address=0x%016llx] [size=%llu bytes] "
  656. "[mapped with %s] [unmapped with %s]\n",
  657. ref->dev_addr, ref->size,
  658. dir2name[entry->direction],
  659. dir2name[ref->direction]);
  660. }
  661. hash_bucket_del(entry);
  662. dma_entry_free(entry);
  663. out:
  664. put_hash_bucket(bucket, &flags);
  665. }
  666. static void check_for_stack(struct device *dev, void *addr)
  667. {
  668. if (object_is_on_stack(addr))
  669. err_printk(dev, NULL, "DMA-API: device driver maps memory from"
  670. "stack [addr=%p]\n", addr);
  671. }
  672. static inline bool overlap(void *addr, u64 size, void *start, void *end)
  673. {
  674. void *addr2 = (char *)addr + size;
  675. return ((addr >= start && addr < end) ||
  676. (addr2 >= start && addr2 < end) ||
  677. ((addr < start) && (addr2 >= end)));
  678. }
  679. static void check_for_illegal_area(struct device *dev, void *addr, u64 size)
  680. {
  681. if (overlap(addr, size, _text, _etext) ||
  682. overlap(addr, size, __start_rodata, __end_rodata))
  683. err_printk(dev, NULL, "DMA-API: device driver maps "
  684. "memory from kernel text or rodata "
  685. "[addr=%p] [size=%llu]\n", addr, size);
  686. }
  687. static void check_sync(struct device *dev, dma_addr_t addr,
  688. u64 size, u64 offset, int direction, bool to_cpu)
  689. {
  690. struct dma_debug_entry ref = {
  691. .dev = dev,
  692. .dev_addr = addr,
  693. .size = size,
  694. .direction = direction,
  695. };
  696. struct dma_debug_entry *entry;
  697. struct hash_bucket *bucket;
  698. unsigned long flags;
  699. bucket = get_hash_bucket(&ref, &flags);
  700. entry = hash_bucket_find(bucket, &ref);
  701. if (!entry) {
  702. err_printk(dev, NULL, "DMA-API: device driver tries "
  703. "to sync DMA memory it has not allocated "
  704. "[device address=0x%016llx] [size=%llu bytes]\n",
  705. (unsigned long long)addr, size);
  706. goto out;
  707. }
  708. if ((offset + size) > entry->size) {
  709. err_printk(dev, entry, "DMA-API: device driver syncs"
  710. " DMA memory outside allocated range "
  711. "[device address=0x%016llx] "
  712. "[allocation size=%llu bytes] [sync offset=%llu] "
  713. "[sync size=%llu]\n", entry->dev_addr, entry->size,
  714. offset, size);
  715. }
  716. if (direction != entry->direction) {
  717. err_printk(dev, entry, "DMA-API: device driver syncs "
  718. "DMA memory with different direction "
  719. "[device address=0x%016llx] [size=%llu bytes] "
  720. "[mapped with %s] [synced with %s]\n",
  721. (unsigned long long)addr, entry->size,
  722. dir2name[entry->direction],
  723. dir2name[direction]);
  724. }
  725. if (entry->direction == DMA_BIDIRECTIONAL)
  726. goto out;
  727. if (to_cpu && !(entry->direction == DMA_FROM_DEVICE) &&
  728. !(direction == DMA_TO_DEVICE))
  729. err_printk(dev, entry, "DMA-API: device driver syncs "
  730. "device read-only DMA memory for cpu "
  731. "[device address=0x%016llx] [size=%llu bytes] "
  732. "[mapped with %s] [synced with %s]\n",
  733. (unsigned long long)addr, entry->size,
  734. dir2name[entry->direction],
  735. dir2name[direction]);
  736. if (!to_cpu && !(entry->direction == DMA_TO_DEVICE) &&
  737. !(direction == DMA_FROM_DEVICE))
  738. err_printk(dev, entry, "DMA-API: device driver syncs "
  739. "device write-only DMA memory to device "
  740. "[device address=0x%016llx] [size=%llu bytes] "
  741. "[mapped with %s] [synced with %s]\n",
  742. (unsigned long long)addr, entry->size,
  743. dir2name[entry->direction],
  744. dir2name[direction]);
  745. out:
  746. put_hash_bucket(bucket, &flags);
  747. }
  748. void debug_dma_map_page(struct device *dev, struct page *page, size_t offset,
  749. size_t size, int direction, dma_addr_t dma_addr,
  750. bool map_single)
  751. {
  752. struct dma_debug_entry *entry;
  753. if (unlikely(global_disable))
  754. return;
  755. if (unlikely(dma_mapping_error(dev, dma_addr)))
  756. return;
  757. entry = dma_entry_alloc();
  758. if (!entry)
  759. return;
  760. entry->dev = dev;
  761. entry->type = dma_debug_page;
  762. entry->paddr = page_to_phys(page) + offset;
  763. entry->dev_addr = dma_addr;
  764. entry->size = size;
  765. entry->direction = direction;
  766. if (map_single)
  767. entry->type = dma_debug_single;
  768. if (!PageHighMem(page)) {
  769. void *addr = ((char *)page_address(page)) + offset;
  770. check_for_stack(dev, addr);
  771. check_for_illegal_area(dev, addr, size);
  772. }
  773. add_dma_entry(entry);
  774. }
  775. EXPORT_SYMBOL(debug_dma_map_page);
  776. void debug_dma_unmap_page(struct device *dev, dma_addr_t addr,
  777. size_t size, int direction, bool map_single)
  778. {
  779. struct dma_debug_entry ref = {
  780. .type = dma_debug_page,
  781. .dev = dev,
  782. .dev_addr = addr,
  783. .size = size,
  784. .direction = direction,
  785. };
  786. if (unlikely(global_disable))
  787. return;
  788. if (map_single)
  789. ref.type = dma_debug_single;
  790. check_unmap(&ref);
  791. }
  792. EXPORT_SYMBOL(debug_dma_unmap_page);
  793. void debug_dma_map_sg(struct device *dev, struct scatterlist *sg,
  794. int nents, int mapped_ents, int direction)
  795. {
  796. struct dma_debug_entry *entry;
  797. struct scatterlist *s;
  798. int i;
  799. if (unlikely(global_disable))
  800. return;
  801. for_each_sg(sg, s, mapped_ents, i) {
  802. entry = dma_entry_alloc();
  803. if (!entry)
  804. return;
  805. entry->type = dma_debug_sg;
  806. entry->dev = dev;
  807. entry->paddr = sg_phys(s);
  808. entry->size = sg_dma_len(s);
  809. entry->dev_addr = sg_dma_address(s);
  810. entry->direction = direction;
  811. entry->sg_call_ents = nents;
  812. entry->sg_mapped_ents = mapped_ents;
  813. if (!PageHighMem(sg_page(s))) {
  814. check_for_stack(dev, sg_virt(s));
  815. check_for_illegal_area(dev, sg_virt(s), sg_dma_len(s));
  816. }
  817. add_dma_entry(entry);
  818. }
  819. }
  820. EXPORT_SYMBOL(debug_dma_map_sg);
  821. static int get_nr_mapped_entries(struct device *dev, struct scatterlist *s)
  822. {
  823. struct dma_debug_entry *entry;
  824. struct hash_bucket *bucket;
  825. unsigned long flags;
  826. int mapped_ents = 0;
  827. struct dma_debug_entry ref;
  828. ref.dev = dev;
  829. ref.dev_addr = sg_dma_address(s);
  830. ref.size = sg_dma_len(s),
  831. bucket = get_hash_bucket(&ref, &flags);
  832. entry = hash_bucket_find(bucket, &ref);
  833. if (entry)
  834. mapped_ents = entry->sg_mapped_ents;
  835. put_hash_bucket(bucket, &flags);
  836. return mapped_ents;
  837. }
  838. void debug_dma_unmap_sg(struct device *dev, struct scatterlist *sglist,
  839. int nelems, int dir)
  840. {
  841. struct scatterlist *s;
  842. int mapped_ents = 0, i;
  843. if (unlikely(global_disable))
  844. return;
  845. for_each_sg(sglist, s, nelems, i) {
  846. struct dma_debug_entry ref = {
  847. .type = dma_debug_sg,
  848. .dev = dev,
  849. .paddr = sg_phys(s),
  850. .dev_addr = sg_dma_address(s),
  851. .size = sg_dma_len(s),
  852. .direction = dir,
  853. .sg_call_ents = 0,
  854. };
  855. if (mapped_ents && i >= mapped_ents)
  856. break;
  857. if (!i) {
  858. ref.sg_call_ents = nelems;
  859. mapped_ents = get_nr_mapped_entries(dev, s);
  860. }
  861. check_unmap(&ref);
  862. }
  863. }
  864. EXPORT_SYMBOL(debug_dma_unmap_sg);
  865. void debug_dma_alloc_coherent(struct device *dev, size_t size,
  866. dma_addr_t dma_addr, void *virt)
  867. {
  868. struct dma_debug_entry *entry;
  869. if (unlikely(global_disable))
  870. return;
  871. if (unlikely(virt == NULL))
  872. return;
  873. entry = dma_entry_alloc();
  874. if (!entry)
  875. return;
  876. entry->type = dma_debug_coherent;
  877. entry->dev = dev;
  878. entry->paddr = virt_to_phys(virt);
  879. entry->size = size;
  880. entry->dev_addr = dma_addr;
  881. entry->direction = DMA_BIDIRECTIONAL;
  882. add_dma_entry(entry);
  883. }
  884. EXPORT_SYMBOL(debug_dma_alloc_coherent);
  885. void debug_dma_free_coherent(struct device *dev, size_t size,
  886. void *virt, dma_addr_t addr)
  887. {
  888. struct dma_debug_entry ref = {
  889. .type = dma_debug_coherent,
  890. .dev = dev,
  891. .paddr = virt_to_phys(virt),
  892. .dev_addr = addr,
  893. .size = size,
  894. .direction = DMA_BIDIRECTIONAL,
  895. };
  896. if (unlikely(global_disable))
  897. return;
  898. check_unmap(&ref);
  899. }
  900. EXPORT_SYMBOL(debug_dma_free_coherent);
  901. void debug_dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
  902. size_t size, int direction)
  903. {
  904. if (unlikely(global_disable))
  905. return;
  906. check_sync(dev, dma_handle, size, 0, direction, true);
  907. }
  908. EXPORT_SYMBOL(debug_dma_sync_single_for_cpu);
  909. void debug_dma_sync_single_for_device(struct device *dev,
  910. dma_addr_t dma_handle, size_t size,
  911. int direction)
  912. {
  913. if (unlikely(global_disable))
  914. return;
  915. check_sync(dev, dma_handle, size, 0, direction, false);
  916. }
  917. EXPORT_SYMBOL(debug_dma_sync_single_for_device);
  918. void debug_dma_sync_single_range_for_cpu(struct device *dev,
  919. dma_addr_t dma_handle,
  920. unsigned long offset, size_t size,
  921. int direction)
  922. {
  923. if (unlikely(global_disable))
  924. return;
  925. check_sync(dev, dma_handle, size, offset, direction, true);
  926. }
  927. EXPORT_SYMBOL(debug_dma_sync_single_range_for_cpu);
  928. void debug_dma_sync_single_range_for_device(struct device *dev,
  929. dma_addr_t dma_handle,
  930. unsigned long offset,
  931. size_t size, int direction)
  932. {
  933. if (unlikely(global_disable))
  934. return;
  935. check_sync(dev, dma_handle, size, offset, direction, false);
  936. }
  937. EXPORT_SYMBOL(debug_dma_sync_single_range_for_device);
  938. void debug_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  939. int nelems, int direction)
  940. {
  941. struct scatterlist *s;
  942. int mapped_ents = 0, i;
  943. if (unlikely(global_disable))
  944. return;
  945. for_each_sg(sg, s, nelems, i) {
  946. if (!i)
  947. mapped_ents = get_nr_mapped_entries(dev, s);
  948. if (i >= mapped_ents)
  949. break;
  950. check_sync(dev, sg_dma_address(s), sg_dma_len(s), 0,
  951. direction, true);
  952. }
  953. }
  954. EXPORT_SYMBOL(debug_dma_sync_sg_for_cpu);
  955. void debug_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  956. int nelems, int direction)
  957. {
  958. struct scatterlist *s;
  959. int mapped_ents = 0, i;
  960. if (unlikely(global_disable))
  961. return;
  962. for_each_sg(sg, s, nelems, i) {
  963. if (!i)
  964. mapped_ents = get_nr_mapped_entries(dev, s);
  965. if (i >= mapped_ents)
  966. break;
  967. check_sync(dev, sg_dma_address(s), sg_dma_len(s), 0,
  968. direction, false);
  969. }
  970. }
  971. EXPORT_SYMBOL(debug_dma_sync_sg_for_device);
  972. static int __init dma_debug_driver_setup(char *str)
  973. {
  974. int i;
  975. for (i = 0; i < NAME_MAX_LEN - 1; ++i, ++str) {
  976. current_driver_name[i] = *str;
  977. if (*str == 0)
  978. break;
  979. }
  980. if (current_driver_name[0])
  981. printk(KERN_INFO "DMA-API: enable driver filter for "
  982. "driver [%s]\n", current_driver_name);
  983. return 1;
  984. }
  985. __setup("dma_debug_driver=", dma_debug_driver_setup);