dma-debug.c 31 KB

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