dma-debug.c 33 KB

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