dma-debug.c 31 KB

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