dma-debug.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242
  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. pr_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, *ret = NULL;
  208. int matches = 0, match_lvl, last_lvl = 0;
  209. list_for_each_entry(entry, &bucket->list, list) {
  210. if ((entry->dev_addr != ref->dev_addr) ||
  211. (entry->dev != ref->dev))
  212. continue;
  213. /*
  214. * Some drivers map the same physical address multiple
  215. * times. Without a hardware IOMMU this results in the
  216. * same device addresses being put into the dma-debug
  217. * hash multiple times too. This can result in false
  218. * positives being reported. Therfore we implement a
  219. * best-fit algorithm here which returns the entry from
  220. * the hash which fits best to the reference value
  221. * instead of the first-fit.
  222. */
  223. matches += 1;
  224. match_lvl = 0;
  225. entry->size == ref->size ? ++match_lvl : match_lvl;
  226. entry->type == ref->type ? ++match_lvl : match_lvl;
  227. entry->direction == ref->direction ? ++match_lvl : match_lvl;
  228. if (match_lvl == 3) {
  229. /* perfect-fit - return the result */
  230. return entry;
  231. } else if (match_lvl > last_lvl) {
  232. /*
  233. * We found an entry that fits better then the
  234. * previous one
  235. */
  236. last_lvl = match_lvl;
  237. ret = entry;
  238. }
  239. }
  240. /*
  241. * If we have multiple matches but no perfect-fit, just return
  242. * NULL.
  243. */
  244. ret = (matches == 1) ? ret : NULL;
  245. return ret;
  246. }
  247. /*
  248. * Add an entry to a hash bucket
  249. */
  250. static void hash_bucket_add(struct hash_bucket *bucket,
  251. struct dma_debug_entry *entry)
  252. {
  253. list_add_tail(&entry->list, &bucket->list);
  254. }
  255. /*
  256. * Remove entry from a hash bucket list
  257. */
  258. static void hash_bucket_del(struct dma_debug_entry *entry)
  259. {
  260. list_del(&entry->list);
  261. }
  262. /*
  263. * Dump mapping entries for debugging purposes
  264. */
  265. void debug_dma_dump_mappings(struct device *dev)
  266. {
  267. int idx;
  268. for (idx = 0; idx < HASH_SIZE; idx++) {
  269. struct hash_bucket *bucket = &dma_entry_hash[idx];
  270. struct dma_debug_entry *entry;
  271. unsigned long flags;
  272. spin_lock_irqsave(&bucket->lock, flags);
  273. list_for_each_entry(entry, &bucket->list, list) {
  274. if (!dev || dev == entry->dev) {
  275. dev_info(entry->dev,
  276. "%s idx %d P=%Lx D=%Lx L=%Lx %s\n",
  277. type2name[entry->type], idx,
  278. (unsigned long long)entry->paddr,
  279. entry->dev_addr, entry->size,
  280. dir2name[entry->direction]);
  281. }
  282. }
  283. spin_unlock_irqrestore(&bucket->lock, flags);
  284. }
  285. }
  286. EXPORT_SYMBOL(debug_dma_dump_mappings);
  287. /*
  288. * Wrapper function for adding an entry to the hash.
  289. * This function takes care of locking itself.
  290. */
  291. static void add_dma_entry(struct dma_debug_entry *entry)
  292. {
  293. struct hash_bucket *bucket;
  294. unsigned long flags;
  295. bucket = get_hash_bucket(entry, &flags);
  296. hash_bucket_add(bucket, entry);
  297. put_hash_bucket(bucket, &flags);
  298. }
  299. static struct dma_debug_entry *__dma_entry_alloc(void)
  300. {
  301. struct dma_debug_entry *entry;
  302. entry = list_entry(free_entries.next, struct dma_debug_entry, list);
  303. list_del(&entry->list);
  304. memset(entry, 0, sizeof(*entry));
  305. num_free_entries -= 1;
  306. if (num_free_entries < min_free_entries)
  307. min_free_entries = num_free_entries;
  308. return entry;
  309. }
  310. /* struct dma_entry allocator
  311. *
  312. * The next two functions implement the allocator for
  313. * struct dma_debug_entries.
  314. */
  315. static struct dma_debug_entry *dma_entry_alloc(void)
  316. {
  317. struct dma_debug_entry *entry = NULL;
  318. unsigned long flags;
  319. spin_lock_irqsave(&free_entries_lock, flags);
  320. if (list_empty(&free_entries)) {
  321. pr_err("DMA-API: debugging out of memory - disabling\n");
  322. global_disable = true;
  323. goto out;
  324. }
  325. entry = __dma_entry_alloc();
  326. #ifdef CONFIG_STACKTRACE
  327. entry->stacktrace.max_entries = DMA_DEBUG_STACKTRACE_ENTRIES;
  328. entry->stacktrace.entries = entry->st_entries;
  329. entry->stacktrace.skip = 2;
  330. save_stack_trace(&entry->stacktrace);
  331. #endif
  332. out:
  333. spin_unlock_irqrestore(&free_entries_lock, flags);
  334. return entry;
  335. }
  336. static void dma_entry_free(struct dma_debug_entry *entry)
  337. {
  338. unsigned long flags;
  339. /*
  340. * add to beginning of the list - this way the entries are
  341. * more likely cache hot when they are reallocated.
  342. */
  343. spin_lock_irqsave(&free_entries_lock, flags);
  344. list_add(&entry->list, &free_entries);
  345. num_free_entries += 1;
  346. spin_unlock_irqrestore(&free_entries_lock, flags);
  347. }
  348. int dma_debug_resize_entries(u32 num_entries)
  349. {
  350. int i, delta, ret = 0;
  351. unsigned long flags;
  352. struct dma_debug_entry *entry;
  353. LIST_HEAD(tmp);
  354. spin_lock_irqsave(&free_entries_lock, flags);
  355. if (nr_total_entries < num_entries) {
  356. delta = num_entries - nr_total_entries;
  357. spin_unlock_irqrestore(&free_entries_lock, flags);
  358. for (i = 0; i < delta; i++) {
  359. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  360. if (!entry)
  361. break;
  362. list_add_tail(&entry->list, &tmp);
  363. }
  364. spin_lock_irqsave(&free_entries_lock, flags);
  365. list_splice(&tmp, &free_entries);
  366. nr_total_entries += i;
  367. num_free_entries += i;
  368. } else {
  369. delta = nr_total_entries - num_entries;
  370. for (i = 0; i < delta && !list_empty(&free_entries); i++) {
  371. entry = __dma_entry_alloc();
  372. kfree(entry);
  373. }
  374. nr_total_entries -= i;
  375. }
  376. if (nr_total_entries != num_entries)
  377. ret = 1;
  378. spin_unlock_irqrestore(&free_entries_lock, flags);
  379. return ret;
  380. }
  381. EXPORT_SYMBOL(dma_debug_resize_entries);
  382. /*
  383. * DMA-API debugging init code
  384. *
  385. * The init code does two things:
  386. * 1. Initialize core data structures
  387. * 2. Preallocate a given number of dma_debug_entry structs
  388. */
  389. static int prealloc_memory(u32 num_entries)
  390. {
  391. struct dma_debug_entry *entry, *next_entry;
  392. int i;
  393. for (i = 0; i < num_entries; ++i) {
  394. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  395. if (!entry)
  396. goto out_err;
  397. list_add_tail(&entry->list, &free_entries);
  398. }
  399. num_free_entries = num_entries;
  400. min_free_entries = num_entries;
  401. pr_info("DMA-API: preallocated %d debug entries\n", num_entries);
  402. return 0;
  403. out_err:
  404. list_for_each_entry_safe(entry, next_entry, &free_entries, list) {
  405. list_del(&entry->list);
  406. kfree(entry);
  407. }
  408. return -ENOMEM;
  409. }
  410. static ssize_t filter_read(struct file *file, char __user *user_buf,
  411. size_t count, loff_t *ppos)
  412. {
  413. char buf[NAME_MAX_LEN + 1];
  414. unsigned long flags;
  415. int len;
  416. if (!current_driver_name[0])
  417. return 0;
  418. /*
  419. * We can't copy to userspace directly because current_driver_name can
  420. * only be read under the driver_name_lock with irqs disabled. So
  421. * create a temporary copy first.
  422. */
  423. read_lock_irqsave(&driver_name_lock, flags);
  424. len = scnprintf(buf, NAME_MAX_LEN + 1, "%s\n", current_driver_name);
  425. read_unlock_irqrestore(&driver_name_lock, flags);
  426. return simple_read_from_buffer(user_buf, count, ppos, buf, len);
  427. }
  428. static ssize_t filter_write(struct file *file, const char __user *userbuf,
  429. size_t count, loff_t *ppos)
  430. {
  431. char buf[NAME_MAX_LEN];
  432. unsigned long flags;
  433. size_t len;
  434. int i;
  435. /*
  436. * We can't copy from userspace directly. Access to
  437. * current_driver_name is protected with a write_lock with irqs
  438. * disabled. Since copy_from_user can fault and may sleep we
  439. * need to copy to temporary buffer first
  440. */
  441. len = min(count, (size_t)(NAME_MAX_LEN - 1));
  442. if (copy_from_user(buf, userbuf, len))
  443. return -EFAULT;
  444. buf[len] = 0;
  445. write_lock_irqsave(&driver_name_lock, flags);
  446. /*
  447. * Now handle the string we got from userspace very carefully.
  448. * The rules are:
  449. * - only use the first token we got
  450. * - token delimiter is everything looking like a space
  451. * character (' ', '\n', '\t' ...)
  452. *
  453. */
  454. if (!isalnum(buf[0])) {
  455. /*
  456. * If the first character userspace gave us is not
  457. * alphanumerical then assume the filter should be
  458. * switched off.
  459. */
  460. if (current_driver_name[0])
  461. pr_info("DMA-API: switching off dma-debug driver filter\n");
  462. current_driver_name[0] = 0;
  463. current_driver = NULL;
  464. goto out_unlock;
  465. }
  466. /*
  467. * Now parse out the first token and use it as the name for the
  468. * driver to filter for.
  469. */
  470. for (i = 0; i < NAME_MAX_LEN; ++i) {
  471. current_driver_name[i] = buf[i];
  472. if (isspace(buf[i]) || buf[i] == ' ' || buf[i] == 0)
  473. break;
  474. }
  475. current_driver_name[i] = 0;
  476. current_driver = NULL;
  477. pr_info("DMA-API: enable driver filter for driver [%s]\n",
  478. current_driver_name);
  479. out_unlock:
  480. write_unlock_irqrestore(&driver_name_lock, flags);
  481. return count;
  482. }
  483. const struct file_operations filter_fops = {
  484. .read = filter_read,
  485. .write = filter_write,
  486. };
  487. static int dma_debug_fs_init(void)
  488. {
  489. dma_debug_dent = debugfs_create_dir("dma-api", NULL);
  490. if (!dma_debug_dent) {
  491. pr_err("DMA-API: can not create debugfs directory\n");
  492. return -ENOMEM;
  493. }
  494. global_disable_dent = debugfs_create_bool("disabled", 0444,
  495. dma_debug_dent,
  496. (u32 *)&global_disable);
  497. if (!global_disable_dent)
  498. goto out_err;
  499. error_count_dent = debugfs_create_u32("error_count", 0444,
  500. dma_debug_dent, &error_count);
  501. if (!error_count_dent)
  502. goto out_err;
  503. show_all_errors_dent = debugfs_create_u32("all_errors", 0644,
  504. dma_debug_dent,
  505. &show_all_errors);
  506. if (!show_all_errors_dent)
  507. goto out_err;
  508. show_num_errors_dent = debugfs_create_u32("num_errors", 0644,
  509. dma_debug_dent,
  510. &show_num_errors);
  511. if (!show_num_errors_dent)
  512. goto out_err;
  513. num_free_entries_dent = debugfs_create_u32("num_free_entries", 0444,
  514. dma_debug_dent,
  515. &num_free_entries);
  516. if (!num_free_entries_dent)
  517. goto out_err;
  518. min_free_entries_dent = debugfs_create_u32("min_free_entries", 0444,
  519. dma_debug_dent,
  520. &min_free_entries);
  521. if (!min_free_entries_dent)
  522. goto out_err;
  523. filter_dent = debugfs_create_file("driver_filter", 0644,
  524. dma_debug_dent, NULL, &filter_fops);
  525. if (!filter_dent)
  526. goto out_err;
  527. return 0;
  528. out_err:
  529. debugfs_remove_recursive(dma_debug_dent);
  530. return -ENOMEM;
  531. }
  532. static int device_dma_allocations(struct device *dev)
  533. {
  534. struct dma_debug_entry *entry;
  535. unsigned long flags;
  536. int count = 0, i;
  537. for (i = 0; i < HASH_SIZE; ++i) {
  538. spin_lock_irqsave(&dma_entry_hash[i].lock, flags);
  539. list_for_each_entry(entry, &dma_entry_hash[i].list, list) {
  540. if (entry->dev == dev)
  541. count += 1;
  542. }
  543. spin_unlock_irqrestore(&dma_entry_hash[i].lock, flags);
  544. }
  545. return count;
  546. }
  547. static int dma_debug_device_change(struct notifier_block *nb,
  548. unsigned long action, void *data)
  549. {
  550. struct device *dev = data;
  551. int count;
  552. switch (action) {
  553. case BUS_NOTIFY_UNBOUND_DRIVER:
  554. count = device_dma_allocations(dev);
  555. if (count == 0)
  556. break;
  557. err_printk(dev, NULL, "DMA-API: device driver has pending "
  558. "DMA allocations while released from device "
  559. "[count=%d]\n", count);
  560. break;
  561. default:
  562. break;
  563. }
  564. return 0;
  565. }
  566. void dma_debug_add_bus(struct bus_type *bus)
  567. {
  568. struct notifier_block *nb;
  569. nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
  570. if (nb == NULL) {
  571. pr_err("dma_debug_add_bus: out of memory\n");
  572. return;
  573. }
  574. nb->notifier_call = dma_debug_device_change;
  575. bus_register_notifier(bus, nb);
  576. }
  577. /*
  578. * Let the architectures decide how many entries should be preallocated.
  579. */
  580. void dma_debug_init(u32 num_entries)
  581. {
  582. int i;
  583. if (global_disable)
  584. return;
  585. for (i = 0; i < HASH_SIZE; ++i) {
  586. INIT_LIST_HEAD(&dma_entry_hash[i].list);
  587. dma_entry_hash[i].lock = SPIN_LOCK_UNLOCKED;
  588. }
  589. if (dma_debug_fs_init() != 0) {
  590. pr_err("DMA-API: error creating debugfs entries - disabling\n");
  591. global_disable = true;
  592. return;
  593. }
  594. if (req_entries)
  595. num_entries = req_entries;
  596. if (prealloc_memory(num_entries) != 0) {
  597. pr_err("DMA-API: debugging out of memory error - disabled\n");
  598. global_disable = true;
  599. return;
  600. }
  601. nr_total_entries = num_free_entries;
  602. pr_info("DMA-API: debugging enabled by kernel config\n");
  603. }
  604. static __init int dma_debug_cmdline(char *str)
  605. {
  606. if (!str)
  607. return -EINVAL;
  608. if (strncmp(str, "off", 3) == 0) {
  609. pr_info("DMA-API: debugging disabled on kernel command line\n");
  610. global_disable = true;
  611. }
  612. return 0;
  613. }
  614. static __init int dma_debug_entries_cmdline(char *str)
  615. {
  616. int res;
  617. if (!str)
  618. return -EINVAL;
  619. res = get_option(&str, &req_entries);
  620. if (!res)
  621. req_entries = 0;
  622. return 0;
  623. }
  624. __setup("dma_debug=", dma_debug_cmdline);
  625. __setup("dma_debug_entries=", dma_debug_entries_cmdline);
  626. static void check_unmap(struct dma_debug_entry *ref)
  627. {
  628. struct dma_debug_entry *entry;
  629. struct hash_bucket *bucket;
  630. unsigned long flags;
  631. if (dma_mapping_error(ref->dev, ref->dev_addr)) {
  632. err_printk(ref->dev, NULL, "DMA-API: device driver tries "
  633. "to free an invalid DMA memory address\n");
  634. return;
  635. }
  636. bucket = get_hash_bucket(ref, &flags);
  637. entry = hash_bucket_find(bucket, ref);
  638. if (!entry) {
  639. err_printk(ref->dev, NULL, "DMA-API: device driver tries "
  640. "to free DMA memory it has not allocated "
  641. "[device address=0x%016llx] [size=%llu bytes]\n",
  642. ref->dev_addr, ref->size);
  643. goto out;
  644. }
  645. if (ref->size != entry->size) {
  646. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  647. "DMA memory with different size "
  648. "[device address=0x%016llx] [map size=%llu bytes] "
  649. "[unmap size=%llu bytes]\n",
  650. ref->dev_addr, entry->size, ref->size);
  651. }
  652. if (ref->type != entry->type) {
  653. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  654. "DMA memory with wrong function "
  655. "[device address=0x%016llx] [size=%llu bytes] "
  656. "[mapped as %s] [unmapped as %s]\n",
  657. ref->dev_addr, ref->size,
  658. type2name[entry->type], type2name[ref->type]);
  659. } else if ((entry->type == dma_debug_coherent) &&
  660. (ref->paddr != entry->paddr)) {
  661. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  662. "DMA memory with different CPU address "
  663. "[device address=0x%016llx] [size=%llu bytes] "
  664. "[cpu alloc address=%p] [cpu free address=%p]",
  665. ref->dev_addr, ref->size,
  666. (void *)entry->paddr, (void *)ref->paddr);
  667. }
  668. if (ref->sg_call_ents && ref->type == dma_debug_sg &&
  669. ref->sg_call_ents != entry->sg_call_ents) {
  670. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  671. "DMA sg list with different entry count "
  672. "[map count=%d] [unmap count=%d]\n",
  673. entry->sg_call_ents, ref->sg_call_ents);
  674. }
  675. /*
  676. * This may be no bug in reality - but most implementations of the
  677. * DMA API don't handle this properly, so check for it here
  678. */
  679. if (ref->direction != entry->direction) {
  680. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  681. "DMA memory with different direction "
  682. "[device address=0x%016llx] [size=%llu bytes] "
  683. "[mapped with %s] [unmapped with %s]\n",
  684. ref->dev_addr, ref->size,
  685. dir2name[entry->direction],
  686. dir2name[ref->direction]);
  687. }
  688. hash_bucket_del(entry);
  689. dma_entry_free(entry);
  690. out:
  691. put_hash_bucket(bucket, &flags);
  692. }
  693. static void check_for_stack(struct device *dev, void *addr)
  694. {
  695. if (object_is_on_stack(addr))
  696. err_printk(dev, NULL, "DMA-API: device driver maps memory from"
  697. "stack [addr=%p]\n", addr);
  698. }
  699. static inline bool overlap(void *addr, u64 size, void *start, void *end)
  700. {
  701. void *addr2 = (char *)addr + size;
  702. return ((addr >= start && addr < end) ||
  703. (addr2 >= start && addr2 < end) ||
  704. ((addr < start) && (addr2 >= end)));
  705. }
  706. static void check_for_illegal_area(struct device *dev, void *addr, u64 size)
  707. {
  708. if (overlap(addr, size, _text, _etext) ||
  709. overlap(addr, size, __start_rodata, __end_rodata))
  710. err_printk(dev, NULL, "DMA-API: device driver maps "
  711. "memory from kernel text or rodata "
  712. "[addr=%p] [size=%llu]\n", addr, size);
  713. }
  714. static void check_sync(struct device *dev, dma_addr_t addr,
  715. u64 size, u64 offset, int direction, bool to_cpu)
  716. {
  717. struct dma_debug_entry ref = {
  718. .dev = dev,
  719. .dev_addr = addr,
  720. .size = size,
  721. .direction = direction,
  722. };
  723. struct dma_debug_entry *entry;
  724. struct hash_bucket *bucket;
  725. unsigned long flags;
  726. bucket = get_hash_bucket(&ref, &flags);
  727. entry = hash_bucket_find(bucket, &ref);
  728. if (!entry) {
  729. err_printk(dev, NULL, "DMA-API: device driver tries "
  730. "to sync DMA memory it has not allocated "
  731. "[device address=0x%016llx] [size=%llu bytes]\n",
  732. (unsigned long long)addr, size);
  733. goto out;
  734. }
  735. if ((offset + size) > entry->size) {
  736. err_printk(dev, entry, "DMA-API: device driver syncs"
  737. " DMA memory outside allocated range "
  738. "[device address=0x%016llx] "
  739. "[allocation size=%llu bytes] [sync offset=%llu] "
  740. "[sync size=%llu]\n", entry->dev_addr, entry->size,
  741. offset, size);
  742. }
  743. if (direction != entry->direction) {
  744. err_printk(dev, entry, "DMA-API: device driver syncs "
  745. "DMA memory with different direction "
  746. "[device address=0x%016llx] [size=%llu bytes] "
  747. "[mapped with %s] [synced with %s]\n",
  748. (unsigned long long)addr, entry->size,
  749. dir2name[entry->direction],
  750. dir2name[direction]);
  751. }
  752. if (entry->direction == DMA_BIDIRECTIONAL)
  753. goto out;
  754. if (to_cpu && !(entry->direction == DMA_FROM_DEVICE) &&
  755. !(direction == DMA_TO_DEVICE))
  756. err_printk(dev, entry, "DMA-API: device driver syncs "
  757. "device read-only DMA memory for cpu "
  758. "[device address=0x%016llx] [size=%llu bytes] "
  759. "[mapped with %s] [synced with %s]\n",
  760. (unsigned long long)addr, entry->size,
  761. dir2name[entry->direction],
  762. dir2name[direction]);
  763. if (!to_cpu && !(entry->direction == DMA_TO_DEVICE) &&
  764. !(direction == DMA_FROM_DEVICE))
  765. err_printk(dev, entry, "DMA-API: device driver syncs "
  766. "device write-only DMA memory to device "
  767. "[device address=0x%016llx] [size=%llu bytes] "
  768. "[mapped with %s] [synced with %s]\n",
  769. (unsigned long long)addr, entry->size,
  770. dir2name[entry->direction],
  771. dir2name[direction]);
  772. out:
  773. put_hash_bucket(bucket, &flags);
  774. }
  775. void debug_dma_map_page(struct device *dev, struct page *page, size_t offset,
  776. size_t size, int direction, dma_addr_t dma_addr,
  777. bool map_single)
  778. {
  779. struct dma_debug_entry *entry;
  780. if (unlikely(global_disable))
  781. return;
  782. if (unlikely(dma_mapping_error(dev, dma_addr)))
  783. return;
  784. entry = dma_entry_alloc();
  785. if (!entry)
  786. return;
  787. entry->dev = dev;
  788. entry->type = dma_debug_page;
  789. entry->paddr = page_to_phys(page) + offset;
  790. entry->dev_addr = dma_addr;
  791. entry->size = size;
  792. entry->direction = direction;
  793. if (map_single)
  794. entry->type = dma_debug_single;
  795. if (!PageHighMem(page)) {
  796. void *addr = ((char *)page_address(page)) + offset;
  797. check_for_stack(dev, addr);
  798. check_for_illegal_area(dev, addr, size);
  799. }
  800. add_dma_entry(entry);
  801. }
  802. EXPORT_SYMBOL(debug_dma_map_page);
  803. void debug_dma_unmap_page(struct device *dev, dma_addr_t addr,
  804. size_t size, int direction, bool map_single)
  805. {
  806. struct dma_debug_entry ref = {
  807. .type = dma_debug_page,
  808. .dev = dev,
  809. .dev_addr = addr,
  810. .size = size,
  811. .direction = direction,
  812. };
  813. if (unlikely(global_disable))
  814. return;
  815. if (map_single)
  816. ref.type = dma_debug_single;
  817. check_unmap(&ref);
  818. }
  819. EXPORT_SYMBOL(debug_dma_unmap_page);
  820. void debug_dma_map_sg(struct device *dev, struct scatterlist *sg,
  821. int nents, int mapped_ents, int direction)
  822. {
  823. struct dma_debug_entry *entry;
  824. struct scatterlist *s;
  825. int i;
  826. if (unlikely(global_disable))
  827. return;
  828. for_each_sg(sg, s, mapped_ents, i) {
  829. entry = dma_entry_alloc();
  830. if (!entry)
  831. return;
  832. entry->type = dma_debug_sg;
  833. entry->dev = dev;
  834. entry->paddr = sg_phys(s);
  835. entry->size = sg_dma_len(s);
  836. entry->dev_addr = sg_dma_address(s);
  837. entry->direction = direction;
  838. entry->sg_call_ents = nents;
  839. entry->sg_mapped_ents = mapped_ents;
  840. if (!PageHighMem(sg_page(s))) {
  841. check_for_stack(dev, sg_virt(s));
  842. check_for_illegal_area(dev, sg_virt(s), sg_dma_len(s));
  843. }
  844. add_dma_entry(entry);
  845. }
  846. }
  847. EXPORT_SYMBOL(debug_dma_map_sg);
  848. static int get_nr_mapped_entries(struct device *dev, struct scatterlist *s)
  849. {
  850. struct dma_debug_entry *entry, ref;
  851. struct hash_bucket *bucket;
  852. unsigned long flags;
  853. int mapped_ents;
  854. ref.dev = dev;
  855. ref.dev_addr = sg_dma_address(s);
  856. ref.size = sg_dma_len(s),
  857. bucket = get_hash_bucket(&ref, &flags);
  858. entry = hash_bucket_find(bucket, &ref);
  859. mapped_ents = 0;
  860. if (entry)
  861. mapped_ents = entry->sg_mapped_ents;
  862. put_hash_bucket(bucket, &flags);
  863. return mapped_ents;
  864. }
  865. void debug_dma_unmap_sg(struct device *dev, struct scatterlist *sglist,
  866. int nelems, int dir)
  867. {
  868. struct scatterlist *s;
  869. int mapped_ents = 0, i;
  870. if (unlikely(global_disable))
  871. return;
  872. for_each_sg(sglist, s, nelems, i) {
  873. struct dma_debug_entry ref = {
  874. .type = dma_debug_sg,
  875. .dev = dev,
  876. .paddr = sg_phys(s),
  877. .dev_addr = sg_dma_address(s),
  878. .size = sg_dma_len(s),
  879. .direction = dir,
  880. .sg_call_ents = 0,
  881. };
  882. if (mapped_ents && i >= mapped_ents)
  883. break;
  884. if (!i) {
  885. ref.sg_call_ents = nelems;
  886. mapped_ents = get_nr_mapped_entries(dev, s);
  887. }
  888. check_unmap(&ref);
  889. }
  890. }
  891. EXPORT_SYMBOL(debug_dma_unmap_sg);
  892. void debug_dma_alloc_coherent(struct device *dev, size_t size,
  893. dma_addr_t dma_addr, void *virt)
  894. {
  895. struct dma_debug_entry *entry;
  896. if (unlikely(global_disable))
  897. return;
  898. if (unlikely(virt == NULL))
  899. return;
  900. entry = dma_entry_alloc();
  901. if (!entry)
  902. return;
  903. entry->type = dma_debug_coherent;
  904. entry->dev = dev;
  905. entry->paddr = virt_to_phys(virt);
  906. entry->size = size;
  907. entry->dev_addr = dma_addr;
  908. entry->direction = DMA_BIDIRECTIONAL;
  909. add_dma_entry(entry);
  910. }
  911. EXPORT_SYMBOL(debug_dma_alloc_coherent);
  912. void debug_dma_free_coherent(struct device *dev, size_t size,
  913. void *virt, dma_addr_t addr)
  914. {
  915. struct dma_debug_entry ref = {
  916. .type = dma_debug_coherent,
  917. .dev = dev,
  918. .paddr = virt_to_phys(virt),
  919. .dev_addr = addr,
  920. .size = size,
  921. .direction = DMA_BIDIRECTIONAL,
  922. };
  923. if (unlikely(global_disable))
  924. return;
  925. check_unmap(&ref);
  926. }
  927. EXPORT_SYMBOL(debug_dma_free_coherent);
  928. void debug_dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
  929. size_t size, int direction)
  930. {
  931. if (unlikely(global_disable))
  932. return;
  933. check_sync(dev, dma_handle, size, 0, direction, true);
  934. }
  935. EXPORT_SYMBOL(debug_dma_sync_single_for_cpu);
  936. void debug_dma_sync_single_for_device(struct device *dev,
  937. dma_addr_t dma_handle, size_t size,
  938. int direction)
  939. {
  940. if (unlikely(global_disable))
  941. return;
  942. check_sync(dev, dma_handle, size, 0, direction, false);
  943. }
  944. EXPORT_SYMBOL(debug_dma_sync_single_for_device);
  945. void debug_dma_sync_single_range_for_cpu(struct device *dev,
  946. dma_addr_t dma_handle,
  947. unsigned long offset, size_t size,
  948. int direction)
  949. {
  950. if (unlikely(global_disable))
  951. return;
  952. check_sync(dev, dma_handle, size, offset, direction, true);
  953. }
  954. EXPORT_SYMBOL(debug_dma_sync_single_range_for_cpu);
  955. void debug_dma_sync_single_range_for_device(struct device *dev,
  956. dma_addr_t dma_handle,
  957. unsigned long offset,
  958. size_t size, int direction)
  959. {
  960. if (unlikely(global_disable))
  961. return;
  962. check_sync(dev, dma_handle, size, offset, direction, false);
  963. }
  964. EXPORT_SYMBOL(debug_dma_sync_single_range_for_device);
  965. void debug_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  966. int nelems, int direction)
  967. {
  968. struct scatterlist *s;
  969. int mapped_ents = 0, i;
  970. if (unlikely(global_disable))
  971. return;
  972. for_each_sg(sg, s, nelems, i) {
  973. if (!i)
  974. mapped_ents = get_nr_mapped_entries(dev, s);
  975. if (i >= mapped_ents)
  976. break;
  977. check_sync(dev, sg_dma_address(s), sg_dma_len(s), 0,
  978. direction, true);
  979. }
  980. }
  981. EXPORT_SYMBOL(debug_dma_sync_sg_for_cpu);
  982. void debug_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  983. int nelems, int direction)
  984. {
  985. struct scatterlist *s;
  986. int mapped_ents = 0, i;
  987. if (unlikely(global_disable))
  988. return;
  989. for_each_sg(sg, s, nelems, i) {
  990. if (!i)
  991. mapped_ents = get_nr_mapped_entries(dev, s);
  992. if (i >= mapped_ents)
  993. break;
  994. check_sync(dev, sg_dma_address(s), sg_dma_len(s), 0,
  995. direction, false);
  996. }
  997. }
  998. EXPORT_SYMBOL(debug_dma_sync_sg_for_device);
  999. static int __init dma_debug_driver_setup(char *str)
  1000. {
  1001. int i;
  1002. for (i = 0; i < NAME_MAX_LEN - 1; ++i, ++str) {
  1003. current_driver_name[i] = *str;
  1004. if (*str == 0)
  1005. break;
  1006. }
  1007. if (current_driver_name[0])
  1008. pr_info("DMA-API: enable driver filter for driver [%s]\n",
  1009. current_driver_name);
  1010. return 1;
  1011. }
  1012. __setup("dma_debug_driver=", dma_debug_driver_setup);