dma-debug.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635
  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. enum map_err_types {
  44. MAP_ERR_CHECK_NOT_APPLICABLE,
  45. MAP_ERR_NOT_CHECKED,
  46. MAP_ERR_CHECKED,
  47. };
  48. #define DMA_DEBUG_STACKTRACE_ENTRIES 5
  49. /**
  50. * struct dma_debug_entry - track a dma_map* or dma_alloc_coherent mapping
  51. * @list: node on pre-allocated free_entries list
  52. * @dev: 'dev' argument to dma_map_{page|single|sg} or dma_alloc_coherent
  53. * @type: single, page, sg, coherent
  54. * @pfn: page frame of the start address
  55. * @offset: offset of mapping relative to pfn
  56. * @size: length of the mapping
  57. * @direction: enum dma_data_direction
  58. * @sg_call_ents: 'nents' from dma_map_sg
  59. * @sg_mapped_ents: 'mapped_ents' from dma_map_sg
  60. * @map_err_type: track whether dma_mapping_error() was checked
  61. * @stacktrace: support backtraces when a violation is detected
  62. */
  63. struct dma_debug_entry {
  64. struct list_head list;
  65. struct device *dev;
  66. int type;
  67. unsigned long pfn;
  68. size_t offset;
  69. u64 dev_addr;
  70. u64 size;
  71. int direction;
  72. int sg_call_ents;
  73. int sg_mapped_ents;
  74. enum map_err_types map_err_type;
  75. #ifdef CONFIG_STACKTRACE
  76. struct stack_trace stacktrace;
  77. unsigned long st_entries[DMA_DEBUG_STACKTRACE_ENTRIES];
  78. #endif
  79. };
  80. typedef bool (*match_fn)(struct dma_debug_entry *, struct dma_debug_entry *);
  81. struct hash_bucket {
  82. struct list_head list;
  83. spinlock_t lock;
  84. } ____cacheline_aligned_in_smp;
  85. /* Hash list to save the allocated dma addresses */
  86. static struct hash_bucket dma_entry_hash[HASH_SIZE];
  87. /* List of pre-allocated dma_debug_entry's */
  88. static LIST_HEAD(free_entries);
  89. /* Lock for the list above */
  90. static DEFINE_SPINLOCK(free_entries_lock);
  91. /* Global disable flag - will be set in case of an error */
  92. static u32 global_disable __read_mostly;
  93. /* Global error count */
  94. static u32 error_count;
  95. /* Global error show enable*/
  96. static u32 show_all_errors __read_mostly;
  97. /* Number of errors to show */
  98. static u32 show_num_errors = 1;
  99. static u32 num_free_entries;
  100. static u32 min_free_entries;
  101. static u32 nr_total_entries;
  102. /* number of preallocated entries requested by kernel cmdline */
  103. static u32 req_entries;
  104. /* debugfs dentry's for the stuff above */
  105. static struct dentry *dma_debug_dent __read_mostly;
  106. static struct dentry *global_disable_dent __read_mostly;
  107. static struct dentry *error_count_dent __read_mostly;
  108. static struct dentry *show_all_errors_dent __read_mostly;
  109. static struct dentry *show_num_errors_dent __read_mostly;
  110. static struct dentry *num_free_entries_dent __read_mostly;
  111. static struct dentry *min_free_entries_dent __read_mostly;
  112. static struct dentry *filter_dent __read_mostly;
  113. /* per-driver filter related state */
  114. #define NAME_MAX_LEN 64
  115. static char current_driver_name[NAME_MAX_LEN] __read_mostly;
  116. static struct device_driver *current_driver __read_mostly;
  117. static DEFINE_RWLOCK(driver_name_lock);
  118. static const char *const maperr2str[] = {
  119. [MAP_ERR_CHECK_NOT_APPLICABLE] = "dma map error check not applicable",
  120. [MAP_ERR_NOT_CHECKED] = "dma map error not checked",
  121. [MAP_ERR_CHECKED] = "dma map error checked",
  122. };
  123. static const char *type2name[4] = { "single", "page",
  124. "scather-gather", "coherent" };
  125. static const char *dir2name[4] = { "DMA_BIDIRECTIONAL", "DMA_TO_DEVICE",
  126. "DMA_FROM_DEVICE", "DMA_NONE" };
  127. /*
  128. * The access to some variables in this macro is racy. We can't use atomic_t
  129. * here because all these variables are exported to debugfs. Some of them even
  130. * writeable. This is also the reason why a lock won't help much. But anyway,
  131. * the races are no big deal. Here is why:
  132. *
  133. * error_count: the addition is racy, but the worst thing that can happen is
  134. * that we don't count some errors
  135. * show_num_errors: the subtraction is racy. Also no big deal because in
  136. * worst case this will result in one warning more in the
  137. * system log than the user configured. This variable is
  138. * writeable via debugfs.
  139. */
  140. static inline void dump_entry_trace(struct dma_debug_entry *entry)
  141. {
  142. #ifdef CONFIG_STACKTRACE
  143. if (entry) {
  144. pr_warning("Mapped at:\n");
  145. print_stack_trace(&entry->stacktrace, 0);
  146. }
  147. #endif
  148. }
  149. static bool driver_filter(struct device *dev)
  150. {
  151. struct device_driver *drv;
  152. unsigned long flags;
  153. bool ret;
  154. /* driver filter off */
  155. if (likely(!current_driver_name[0]))
  156. return true;
  157. /* driver filter on and initialized */
  158. if (current_driver && dev && dev->driver == current_driver)
  159. return true;
  160. /* driver filter on, but we can't filter on a NULL device... */
  161. if (!dev)
  162. return false;
  163. if (current_driver || !current_driver_name[0])
  164. return false;
  165. /* driver filter on but not yet initialized */
  166. drv = dev->driver;
  167. if (!drv)
  168. return false;
  169. /* lock to protect against change of current_driver_name */
  170. read_lock_irqsave(&driver_name_lock, flags);
  171. ret = false;
  172. if (drv->name &&
  173. strncmp(current_driver_name, drv->name, NAME_MAX_LEN - 1) == 0) {
  174. current_driver = drv;
  175. ret = true;
  176. }
  177. read_unlock_irqrestore(&driver_name_lock, flags);
  178. return ret;
  179. }
  180. #define err_printk(dev, entry, format, arg...) do { \
  181. error_count += 1; \
  182. if (driver_filter(dev) && \
  183. (show_all_errors || show_num_errors > 0)) { \
  184. WARN(1, "%s %s: " format, \
  185. dev ? dev_driver_string(dev) : "NULL", \
  186. dev ? dev_name(dev) : "NULL", ## arg); \
  187. dump_entry_trace(entry); \
  188. } \
  189. if (!show_all_errors && show_num_errors > 0) \
  190. show_num_errors -= 1; \
  191. } while (0);
  192. /*
  193. * Hash related functions
  194. *
  195. * Every DMA-API request is saved into a struct dma_debug_entry. To
  196. * have quick access to these structs they are stored into a hash.
  197. */
  198. static int hash_fn(struct dma_debug_entry *entry)
  199. {
  200. /*
  201. * Hash function is based on the dma address.
  202. * We use bits 20-27 here as the index into the hash
  203. */
  204. return (entry->dev_addr >> HASH_FN_SHIFT) & HASH_FN_MASK;
  205. }
  206. /*
  207. * Request exclusive access to a hash bucket for a given dma_debug_entry.
  208. */
  209. static struct hash_bucket *get_hash_bucket(struct dma_debug_entry *entry,
  210. unsigned long *flags)
  211. {
  212. int idx = hash_fn(entry);
  213. unsigned long __flags;
  214. spin_lock_irqsave(&dma_entry_hash[idx].lock, __flags);
  215. *flags = __flags;
  216. return &dma_entry_hash[idx];
  217. }
  218. /*
  219. * Give up exclusive access to the hash bucket
  220. */
  221. static void put_hash_bucket(struct hash_bucket *bucket,
  222. unsigned long *flags)
  223. {
  224. unsigned long __flags = *flags;
  225. spin_unlock_irqrestore(&bucket->lock, __flags);
  226. }
  227. static bool exact_match(struct dma_debug_entry *a, struct dma_debug_entry *b)
  228. {
  229. return ((a->dev_addr == b->dev_addr) &&
  230. (a->dev == b->dev)) ? true : false;
  231. }
  232. static bool containing_match(struct dma_debug_entry *a,
  233. struct dma_debug_entry *b)
  234. {
  235. if (a->dev != b->dev)
  236. return false;
  237. if ((b->dev_addr <= a->dev_addr) &&
  238. ((b->dev_addr + b->size) >= (a->dev_addr + a->size)))
  239. return true;
  240. return false;
  241. }
  242. /*
  243. * Search a given entry in the hash bucket list
  244. */
  245. static struct dma_debug_entry *__hash_bucket_find(struct hash_bucket *bucket,
  246. struct dma_debug_entry *ref,
  247. match_fn match)
  248. {
  249. struct dma_debug_entry *entry, *ret = NULL;
  250. int matches = 0, match_lvl, last_lvl = -1;
  251. list_for_each_entry(entry, &bucket->list, list) {
  252. if (!match(ref, entry))
  253. continue;
  254. /*
  255. * Some drivers map the same physical address multiple
  256. * times. Without a hardware IOMMU this results in the
  257. * same device addresses being put into the dma-debug
  258. * hash multiple times too. This can result in false
  259. * positives being reported. Therefore we implement a
  260. * best-fit algorithm here which returns the entry from
  261. * the hash which fits best to the reference value
  262. * instead of the first-fit.
  263. */
  264. matches += 1;
  265. match_lvl = 0;
  266. entry->size == ref->size ? ++match_lvl : 0;
  267. entry->type == ref->type ? ++match_lvl : 0;
  268. entry->direction == ref->direction ? ++match_lvl : 0;
  269. entry->sg_call_ents == ref->sg_call_ents ? ++match_lvl : 0;
  270. if (match_lvl == 4) {
  271. /* perfect-fit - return the result */
  272. return entry;
  273. } else if (match_lvl > last_lvl) {
  274. /*
  275. * We found an entry that fits better then the
  276. * previous one or it is the 1st match.
  277. */
  278. last_lvl = match_lvl;
  279. ret = entry;
  280. }
  281. }
  282. /*
  283. * If we have multiple matches but no perfect-fit, just return
  284. * NULL.
  285. */
  286. ret = (matches == 1) ? ret : NULL;
  287. return ret;
  288. }
  289. static struct dma_debug_entry *bucket_find_exact(struct hash_bucket *bucket,
  290. struct dma_debug_entry *ref)
  291. {
  292. return __hash_bucket_find(bucket, ref, exact_match);
  293. }
  294. static struct dma_debug_entry *bucket_find_contain(struct hash_bucket **bucket,
  295. struct dma_debug_entry *ref,
  296. unsigned long *flags)
  297. {
  298. unsigned int max_range = dma_get_max_seg_size(ref->dev);
  299. struct dma_debug_entry *entry, index = *ref;
  300. unsigned int range = 0;
  301. while (range <= max_range) {
  302. entry = __hash_bucket_find(*bucket, &index, containing_match);
  303. if (entry)
  304. return entry;
  305. /*
  306. * Nothing found, go back a hash bucket
  307. */
  308. put_hash_bucket(*bucket, flags);
  309. range += (1 << HASH_FN_SHIFT);
  310. index.dev_addr -= (1 << HASH_FN_SHIFT);
  311. *bucket = get_hash_bucket(&index, flags);
  312. }
  313. return NULL;
  314. }
  315. /*
  316. * Add an entry to a hash bucket
  317. */
  318. static void hash_bucket_add(struct hash_bucket *bucket,
  319. struct dma_debug_entry *entry)
  320. {
  321. list_add_tail(&entry->list, &bucket->list);
  322. }
  323. /*
  324. * Remove entry from a hash bucket list
  325. */
  326. static void hash_bucket_del(struct dma_debug_entry *entry)
  327. {
  328. list_del(&entry->list);
  329. }
  330. static unsigned long long phys_addr(struct dma_debug_entry *entry)
  331. {
  332. return page_to_phys(pfn_to_page(entry->pfn)) + entry->offset;
  333. }
  334. /*
  335. * Dump mapping entries for debugging purposes
  336. */
  337. void debug_dma_dump_mappings(struct device *dev)
  338. {
  339. int idx;
  340. for (idx = 0; idx < HASH_SIZE; idx++) {
  341. struct hash_bucket *bucket = &dma_entry_hash[idx];
  342. struct dma_debug_entry *entry;
  343. unsigned long flags;
  344. spin_lock_irqsave(&bucket->lock, flags);
  345. list_for_each_entry(entry, &bucket->list, list) {
  346. if (!dev || dev == entry->dev) {
  347. dev_info(entry->dev,
  348. "%s idx %d P=%Lx N=%lx D=%Lx L=%Lx %s %s\n",
  349. type2name[entry->type], idx,
  350. phys_addr(entry), entry->pfn,
  351. entry->dev_addr, entry->size,
  352. dir2name[entry->direction],
  353. maperr2str[entry->map_err_type]);
  354. }
  355. }
  356. spin_unlock_irqrestore(&bucket->lock, flags);
  357. }
  358. }
  359. EXPORT_SYMBOL(debug_dma_dump_mappings);
  360. /*
  361. * For each mapping (initial cacheline in the case of
  362. * dma_alloc_coherent/dma_map_page, initial cacheline in each page of a
  363. * scatterlist, or the cacheline specified in dma_map_single) insert
  364. * into this tree using the cacheline as the key. At
  365. * dma_unmap_{single|sg|page} or dma_free_coherent delete the entry. If
  366. * the entry already exists at insertion time add a tag as a reference
  367. * count for the overlapping mappings. For now, the overlap tracking
  368. * just ensures that 'unmaps' balance 'maps' before marking the
  369. * cacheline idle, but we should also be flagging overlaps as an API
  370. * violation.
  371. *
  372. * Memory usage is mostly constrained by the maximum number of available
  373. * dma-debug entries in that we need a free dma_debug_entry before
  374. * inserting into the tree. In the case of dma_map_page and
  375. * dma_alloc_coherent there is only one dma_debug_entry and one
  376. * dma_active_cacheline entry to track per event. dma_map_sg(), on the
  377. * other hand, consumes a single dma_debug_entry, but inserts 'nents'
  378. * entries into the tree.
  379. *
  380. * At any time debug_dma_assert_idle() can be called to trigger a
  381. * warning if any cachelines in the given page are in the active set.
  382. */
  383. static RADIX_TREE(dma_active_cacheline, GFP_NOWAIT);
  384. static DEFINE_SPINLOCK(radix_lock);
  385. #define ACTIVE_CACHELINE_MAX_OVERLAP ((1 << RADIX_TREE_MAX_TAGS) - 1)
  386. #define CACHELINE_PER_PAGE_SHIFT (PAGE_SHIFT - L1_CACHE_SHIFT)
  387. #define CACHELINES_PER_PAGE (1 << CACHELINE_PER_PAGE_SHIFT)
  388. static phys_addr_t to_cacheline_number(struct dma_debug_entry *entry)
  389. {
  390. return (entry->pfn << CACHELINE_PER_PAGE_SHIFT) +
  391. (entry->offset >> L1_CACHE_SHIFT);
  392. }
  393. static int active_cacheline_read_overlap(phys_addr_t cln)
  394. {
  395. int overlap = 0, i;
  396. for (i = RADIX_TREE_MAX_TAGS - 1; i >= 0; i--)
  397. if (radix_tree_tag_get(&dma_active_cacheline, cln, i))
  398. overlap |= 1 << i;
  399. return overlap;
  400. }
  401. static int active_cacheline_set_overlap(phys_addr_t cln, int overlap)
  402. {
  403. int i;
  404. if (overlap > ACTIVE_CACHELINE_MAX_OVERLAP || overlap < 0)
  405. return overlap;
  406. for (i = RADIX_TREE_MAX_TAGS - 1; i >= 0; i--)
  407. if (overlap & 1 << i)
  408. radix_tree_tag_set(&dma_active_cacheline, cln, i);
  409. else
  410. radix_tree_tag_clear(&dma_active_cacheline, cln, i);
  411. return overlap;
  412. }
  413. static void active_cacheline_inc_overlap(phys_addr_t cln)
  414. {
  415. int overlap = active_cacheline_read_overlap(cln);
  416. overlap = active_cacheline_set_overlap(cln, ++overlap);
  417. /* If we overflowed the overlap counter then we're potentially
  418. * leaking dma-mappings. Otherwise, if maps and unmaps are
  419. * balanced then this overflow may cause false negatives in
  420. * debug_dma_assert_idle() as the cacheline may be marked idle
  421. * prematurely.
  422. */
  423. WARN_ONCE(overlap > ACTIVE_CACHELINE_MAX_OVERLAP,
  424. "DMA-API: exceeded %d overlapping mappings of cacheline %pa\n",
  425. ACTIVE_CACHELINE_MAX_OVERLAP, &cln);
  426. }
  427. static int active_cacheline_dec_overlap(phys_addr_t cln)
  428. {
  429. int overlap = active_cacheline_read_overlap(cln);
  430. return active_cacheline_set_overlap(cln, --overlap);
  431. }
  432. static int active_cacheline_insert(struct dma_debug_entry *entry)
  433. {
  434. phys_addr_t cln = to_cacheline_number(entry);
  435. unsigned long flags;
  436. int rc;
  437. /* If the device is not writing memory then we don't have any
  438. * concerns about the cpu consuming stale data. This mitigates
  439. * legitimate usages of overlapping mappings.
  440. */
  441. if (entry->direction == DMA_TO_DEVICE)
  442. return 0;
  443. spin_lock_irqsave(&radix_lock, flags);
  444. rc = radix_tree_insert(&dma_active_cacheline, cln, entry);
  445. if (rc == -EEXIST)
  446. active_cacheline_inc_overlap(cln);
  447. spin_unlock_irqrestore(&radix_lock, flags);
  448. return rc;
  449. }
  450. static void active_cacheline_remove(struct dma_debug_entry *entry)
  451. {
  452. phys_addr_t cln = to_cacheline_number(entry);
  453. unsigned long flags;
  454. /* ...mirror the insert case */
  455. if (entry->direction == DMA_TO_DEVICE)
  456. return;
  457. spin_lock_irqsave(&radix_lock, flags);
  458. /* since we are counting overlaps the final put of the
  459. * cacheline will occur when the overlap count is 0.
  460. * active_cacheline_dec_overlap() returns -1 in that case
  461. */
  462. if (active_cacheline_dec_overlap(cln) < 0)
  463. radix_tree_delete(&dma_active_cacheline, cln);
  464. spin_unlock_irqrestore(&radix_lock, flags);
  465. }
  466. /**
  467. * debug_dma_assert_idle() - assert that a page is not undergoing dma
  468. * @page: page to lookup in the dma_active_cacheline tree
  469. *
  470. * Place a call to this routine in cases where the cpu touching the page
  471. * before the dma completes (page is dma_unmapped) will lead to data
  472. * corruption.
  473. */
  474. void debug_dma_assert_idle(struct page *page)
  475. {
  476. static struct dma_debug_entry *ents[CACHELINES_PER_PAGE];
  477. struct dma_debug_entry *entry = NULL;
  478. void **results = (void **) &ents;
  479. unsigned int nents, i;
  480. unsigned long flags;
  481. phys_addr_t cln;
  482. if (!page)
  483. return;
  484. cln = (phys_addr_t) page_to_pfn(page) << CACHELINE_PER_PAGE_SHIFT;
  485. spin_lock_irqsave(&radix_lock, flags);
  486. nents = radix_tree_gang_lookup(&dma_active_cacheline, results, cln,
  487. CACHELINES_PER_PAGE);
  488. for (i = 0; i < nents; i++) {
  489. phys_addr_t ent_cln = to_cacheline_number(ents[i]);
  490. if (ent_cln == cln) {
  491. entry = ents[i];
  492. break;
  493. } else if (ent_cln >= cln + CACHELINES_PER_PAGE)
  494. break;
  495. }
  496. spin_unlock_irqrestore(&radix_lock, flags);
  497. if (!entry)
  498. return;
  499. cln = to_cacheline_number(entry);
  500. err_printk(entry->dev, entry,
  501. "DMA-API: cpu touching an active dma mapped cacheline [cln=%pa]\n",
  502. &cln);
  503. }
  504. /*
  505. * Wrapper function for adding an entry to the hash.
  506. * This function takes care of locking itself.
  507. */
  508. static void add_dma_entry(struct dma_debug_entry *entry)
  509. {
  510. struct hash_bucket *bucket;
  511. unsigned long flags;
  512. int rc;
  513. bucket = get_hash_bucket(entry, &flags);
  514. hash_bucket_add(bucket, entry);
  515. put_hash_bucket(bucket, &flags);
  516. rc = active_cacheline_insert(entry);
  517. if (rc == -ENOMEM) {
  518. pr_err("DMA-API: cacheline tracking ENOMEM, dma-debug disabled\n");
  519. global_disable = true;
  520. }
  521. /* TODO: report -EEXIST errors here as overlapping mappings are
  522. * not supported by the DMA API
  523. */
  524. }
  525. static struct dma_debug_entry *__dma_entry_alloc(void)
  526. {
  527. struct dma_debug_entry *entry;
  528. entry = list_entry(free_entries.next, struct dma_debug_entry, list);
  529. list_del(&entry->list);
  530. memset(entry, 0, sizeof(*entry));
  531. num_free_entries -= 1;
  532. if (num_free_entries < min_free_entries)
  533. min_free_entries = num_free_entries;
  534. return entry;
  535. }
  536. /* struct dma_entry allocator
  537. *
  538. * The next two functions implement the allocator for
  539. * struct dma_debug_entries.
  540. */
  541. static struct dma_debug_entry *dma_entry_alloc(void)
  542. {
  543. struct dma_debug_entry *entry;
  544. unsigned long flags;
  545. spin_lock_irqsave(&free_entries_lock, flags);
  546. if (list_empty(&free_entries)) {
  547. pr_err("DMA-API: debugging out of memory - disabling\n");
  548. global_disable = true;
  549. spin_unlock_irqrestore(&free_entries_lock, flags);
  550. return NULL;
  551. }
  552. entry = __dma_entry_alloc();
  553. spin_unlock_irqrestore(&free_entries_lock, flags);
  554. #ifdef CONFIG_STACKTRACE
  555. entry->stacktrace.max_entries = DMA_DEBUG_STACKTRACE_ENTRIES;
  556. entry->stacktrace.entries = entry->st_entries;
  557. entry->stacktrace.skip = 2;
  558. save_stack_trace(&entry->stacktrace);
  559. #endif
  560. return entry;
  561. }
  562. static void dma_entry_free(struct dma_debug_entry *entry)
  563. {
  564. unsigned long flags;
  565. active_cacheline_remove(entry);
  566. /*
  567. * add to beginning of the list - this way the entries are
  568. * more likely cache hot when they are reallocated.
  569. */
  570. spin_lock_irqsave(&free_entries_lock, flags);
  571. list_add(&entry->list, &free_entries);
  572. num_free_entries += 1;
  573. spin_unlock_irqrestore(&free_entries_lock, flags);
  574. }
  575. int dma_debug_resize_entries(u32 num_entries)
  576. {
  577. int i, delta, ret = 0;
  578. unsigned long flags;
  579. struct dma_debug_entry *entry;
  580. LIST_HEAD(tmp);
  581. spin_lock_irqsave(&free_entries_lock, flags);
  582. if (nr_total_entries < num_entries) {
  583. delta = num_entries - nr_total_entries;
  584. spin_unlock_irqrestore(&free_entries_lock, flags);
  585. for (i = 0; i < delta; i++) {
  586. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  587. if (!entry)
  588. break;
  589. list_add_tail(&entry->list, &tmp);
  590. }
  591. spin_lock_irqsave(&free_entries_lock, flags);
  592. list_splice(&tmp, &free_entries);
  593. nr_total_entries += i;
  594. num_free_entries += i;
  595. } else {
  596. delta = nr_total_entries - num_entries;
  597. for (i = 0; i < delta && !list_empty(&free_entries); i++) {
  598. entry = __dma_entry_alloc();
  599. kfree(entry);
  600. }
  601. nr_total_entries -= i;
  602. }
  603. if (nr_total_entries != num_entries)
  604. ret = 1;
  605. spin_unlock_irqrestore(&free_entries_lock, flags);
  606. return ret;
  607. }
  608. EXPORT_SYMBOL(dma_debug_resize_entries);
  609. /*
  610. * DMA-API debugging init code
  611. *
  612. * The init code does two things:
  613. * 1. Initialize core data structures
  614. * 2. Preallocate a given number of dma_debug_entry structs
  615. */
  616. static int prealloc_memory(u32 num_entries)
  617. {
  618. struct dma_debug_entry *entry, *next_entry;
  619. int i;
  620. for (i = 0; i < num_entries; ++i) {
  621. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  622. if (!entry)
  623. goto out_err;
  624. list_add_tail(&entry->list, &free_entries);
  625. }
  626. num_free_entries = num_entries;
  627. min_free_entries = num_entries;
  628. pr_info("DMA-API: preallocated %d debug entries\n", num_entries);
  629. return 0;
  630. out_err:
  631. list_for_each_entry_safe(entry, next_entry, &free_entries, list) {
  632. list_del(&entry->list);
  633. kfree(entry);
  634. }
  635. return -ENOMEM;
  636. }
  637. static ssize_t filter_read(struct file *file, char __user *user_buf,
  638. size_t count, loff_t *ppos)
  639. {
  640. char buf[NAME_MAX_LEN + 1];
  641. unsigned long flags;
  642. int len;
  643. if (!current_driver_name[0])
  644. return 0;
  645. /*
  646. * We can't copy to userspace directly because current_driver_name can
  647. * only be read under the driver_name_lock with irqs disabled. So
  648. * create a temporary copy first.
  649. */
  650. read_lock_irqsave(&driver_name_lock, flags);
  651. len = scnprintf(buf, NAME_MAX_LEN + 1, "%s\n", current_driver_name);
  652. read_unlock_irqrestore(&driver_name_lock, flags);
  653. return simple_read_from_buffer(user_buf, count, ppos, buf, len);
  654. }
  655. static ssize_t filter_write(struct file *file, const char __user *userbuf,
  656. size_t count, loff_t *ppos)
  657. {
  658. char buf[NAME_MAX_LEN];
  659. unsigned long flags;
  660. size_t len;
  661. int i;
  662. /*
  663. * We can't copy from userspace directly. Access to
  664. * current_driver_name is protected with a write_lock with irqs
  665. * disabled. Since copy_from_user can fault and may sleep we
  666. * need to copy to temporary buffer first
  667. */
  668. len = min(count, (size_t)(NAME_MAX_LEN - 1));
  669. if (copy_from_user(buf, userbuf, len))
  670. return -EFAULT;
  671. buf[len] = 0;
  672. write_lock_irqsave(&driver_name_lock, flags);
  673. /*
  674. * Now handle the string we got from userspace very carefully.
  675. * The rules are:
  676. * - only use the first token we got
  677. * - token delimiter is everything looking like a space
  678. * character (' ', '\n', '\t' ...)
  679. *
  680. */
  681. if (!isalnum(buf[0])) {
  682. /*
  683. * If the first character userspace gave us is not
  684. * alphanumerical then assume the filter should be
  685. * switched off.
  686. */
  687. if (current_driver_name[0])
  688. pr_info("DMA-API: switching off dma-debug driver filter\n");
  689. current_driver_name[0] = 0;
  690. current_driver = NULL;
  691. goto out_unlock;
  692. }
  693. /*
  694. * Now parse out the first token and use it as the name for the
  695. * driver to filter for.
  696. */
  697. for (i = 0; i < NAME_MAX_LEN - 1; ++i) {
  698. current_driver_name[i] = buf[i];
  699. if (isspace(buf[i]) || buf[i] == ' ' || buf[i] == 0)
  700. break;
  701. }
  702. current_driver_name[i] = 0;
  703. current_driver = NULL;
  704. pr_info("DMA-API: enable driver filter for driver [%s]\n",
  705. current_driver_name);
  706. out_unlock:
  707. write_unlock_irqrestore(&driver_name_lock, flags);
  708. return count;
  709. }
  710. static const struct file_operations filter_fops = {
  711. .read = filter_read,
  712. .write = filter_write,
  713. .llseek = default_llseek,
  714. };
  715. static int dma_debug_fs_init(void)
  716. {
  717. dma_debug_dent = debugfs_create_dir("dma-api", NULL);
  718. if (!dma_debug_dent) {
  719. pr_err("DMA-API: can not create debugfs directory\n");
  720. return -ENOMEM;
  721. }
  722. global_disable_dent = debugfs_create_bool("disabled", 0444,
  723. dma_debug_dent,
  724. &global_disable);
  725. if (!global_disable_dent)
  726. goto out_err;
  727. error_count_dent = debugfs_create_u32("error_count", 0444,
  728. dma_debug_dent, &error_count);
  729. if (!error_count_dent)
  730. goto out_err;
  731. show_all_errors_dent = debugfs_create_u32("all_errors", 0644,
  732. dma_debug_dent,
  733. &show_all_errors);
  734. if (!show_all_errors_dent)
  735. goto out_err;
  736. show_num_errors_dent = debugfs_create_u32("num_errors", 0644,
  737. dma_debug_dent,
  738. &show_num_errors);
  739. if (!show_num_errors_dent)
  740. goto out_err;
  741. num_free_entries_dent = debugfs_create_u32("num_free_entries", 0444,
  742. dma_debug_dent,
  743. &num_free_entries);
  744. if (!num_free_entries_dent)
  745. goto out_err;
  746. min_free_entries_dent = debugfs_create_u32("min_free_entries", 0444,
  747. dma_debug_dent,
  748. &min_free_entries);
  749. if (!min_free_entries_dent)
  750. goto out_err;
  751. filter_dent = debugfs_create_file("driver_filter", 0644,
  752. dma_debug_dent, NULL, &filter_fops);
  753. if (!filter_dent)
  754. goto out_err;
  755. return 0;
  756. out_err:
  757. debugfs_remove_recursive(dma_debug_dent);
  758. return -ENOMEM;
  759. }
  760. static int device_dma_allocations(struct device *dev, struct dma_debug_entry **out_entry)
  761. {
  762. struct dma_debug_entry *entry;
  763. unsigned long flags;
  764. int count = 0, i;
  765. local_irq_save(flags);
  766. for (i = 0; i < HASH_SIZE; ++i) {
  767. spin_lock(&dma_entry_hash[i].lock);
  768. list_for_each_entry(entry, &dma_entry_hash[i].list, list) {
  769. if (entry->dev == dev) {
  770. count += 1;
  771. *out_entry = entry;
  772. }
  773. }
  774. spin_unlock(&dma_entry_hash[i].lock);
  775. }
  776. local_irq_restore(flags);
  777. return count;
  778. }
  779. static int dma_debug_device_change(struct notifier_block *nb, unsigned long action, void *data)
  780. {
  781. struct device *dev = data;
  782. struct dma_debug_entry *uninitialized_var(entry);
  783. int count;
  784. if (global_disable)
  785. return 0;
  786. switch (action) {
  787. case BUS_NOTIFY_UNBOUND_DRIVER:
  788. count = device_dma_allocations(dev, &entry);
  789. if (count == 0)
  790. break;
  791. err_printk(dev, entry, "DMA-API: device driver has pending "
  792. "DMA allocations while released from device "
  793. "[count=%d]\n"
  794. "One of leaked entries details: "
  795. "[device address=0x%016llx] [size=%llu bytes] "
  796. "[mapped with %s] [mapped as %s]\n",
  797. count, entry->dev_addr, entry->size,
  798. dir2name[entry->direction], type2name[entry->type]);
  799. break;
  800. default:
  801. break;
  802. }
  803. return 0;
  804. }
  805. void dma_debug_add_bus(struct bus_type *bus)
  806. {
  807. struct notifier_block *nb;
  808. if (global_disable)
  809. return;
  810. nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
  811. if (nb == NULL) {
  812. pr_err("dma_debug_add_bus: out of memory\n");
  813. return;
  814. }
  815. nb->notifier_call = dma_debug_device_change;
  816. bus_register_notifier(bus, nb);
  817. }
  818. /*
  819. * Let the architectures decide how many entries should be preallocated.
  820. */
  821. void dma_debug_init(u32 num_entries)
  822. {
  823. int i;
  824. if (global_disable)
  825. return;
  826. for (i = 0; i < HASH_SIZE; ++i) {
  827. INIT_LIST_HEAD(&dma_entry_hash[i].list);
  828. spin_lock_init(&dma_entry_hash[i].lock);
  829. }
  830. if (dma_debug_fs_init() != 0) {
  831. pr_err("DMA-API: error creating debugfs entries - disabling\n");
  832. global_disable = true;
  833. return;
  834. }
  835. if (req_entries)
  836. num_entries = req_entries;
  837. if (prealloc_memory(num_entries) != 0) {
  838. pr_err("DMA-API: debugging out of memory error - disabled\n");
  839. global_disable = true;
  840. return;
  841. }
  842. nr_total_entries = num_free_entries;
  843. pr_info("DMA-API: debugging enabled by kernel config\n");
  844. }
  845. static __init int dma_debug_cmdline(char *str)
  846. {
  847. if (!str)
  848. return -EINVAL;
  849. if (strncmp(str, "off", 3) == 0) {
  850. pr_info("DMA-API: debugging disabled on kernel command line\n");
  851. global_disable = true;
  852. }
  853. return 0;
  854. }
  855. static __init int dma_debug_entries_cmdline(char *str)
  856. {
  857. int res;
  858. if (!str)
  859. return -EINVAL;
  860. res = get_option(&str, &req_entries);
  861. if (!res)
  862. req_entries = 0;
  863. return 0;
  864. }
  865. __setup("dma_debug=", dma_debug_cmdline);
  866. __setup("dma_debug_entries=", dma_debug_entries_cmdline);
  867. static void check_unmap(struct dma_debug_entry *ref)
  868. {
  869. struct dma_debug_entry *entry;
  870. struct hash_bucket *bucket;
  871. unsigned long flags;
  872. bucket = get_hash_bucket(ref, &flags);
  873. entry = bucket_find_exact(bucket, ref);
  874. if (!entry) {
  875. /* must drop lock before calling dma_mapping_error */
  876. put_hash_bucket(bucket, &flags);
  877. if (dma_mapping_error(ref->dev, ref->dev_addr)) {
  878. err_printk(ref->dev, NULL,
  879. "DMA-API: device driver tries to free an "
  880. "invalid DMA memory address\n");
  881. } else {
  882. err_printk(ref->dev, NULL,
  883. "DMA-API: device driver tries to free DMA "
  884. "memory it has not allocated [device "
  885. "address=0x%016llx] [size=%llu bytes]\n",
  886. ref->dev_addr, ref->size);
  887. }
  888. return;
  889. }
  890. if (ref->size != entry->size) {
  891. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  892. "DMA memory with different size "
  893. "[device address=0x%016llx] [map size=%llu bytes] "
  894. "[unmap size=%llu bytes]\n",
  895. ref->dev_addr, entry->size, ref->size);
  896. }
  897. if (ref->type != entry->type) {
  898. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  899. "DMA memory with wrong function "
  900. "[device address=0x%016llx] [size=%llu bytes] "
  901. "[mapped as %s] [unmapped as %s]\n",
  902. ref->dev_addr, ref->size,
  903. type2name[entry->type], type2name[ref->type]);
  904. } else if ((entry->type == dma_debug_coherent) &&
  905. (phys_addr(ref) != phys_addr(entry))) {
  906. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  907. "DMA memory with different CPU address "
  908. "[device address=0x%016llx] [size=%llu bytes] "
  909. "[cpu alloc address=0x%016llx] "
  910. "[cpu free address=0x%016llx]",
  911. ref->dev_addr, ref->size,
  912. phys_addr(entry),
  913. phys_addr(ref));
  914. }
  915. if (ref->sg_call_ents && ref->type == dma_debug_sg &&
  916. ref->sg_call_ents != entry->sg_call_ents) {
  917. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  918. "DMA sg list with different entry count "
  919. "[map count=%d] [unmap count=%d]\n",
  920. entry->sg_call_ents, ref->sg_call_ents);
  921. }
  922. /*
  923. * This may be no bug in reality - but most implementations of the
  924. * DMA API don't handle this properly, so check for it here
  925. */
  926. if (ref->direction != entry->direction) {
  927. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  928. "DMA memory with different direction "
  929. "[device address=0x%016llx] [size=%llu bytes] "
  930. "[mapped with %s] [unmapped with %s]\n",
  931. ref->dev_addr, ref->size,
  932. dir2name[entry->direction],
  933. dir2name[ref->direction]);
  934. }
  935. if (entry->map_err_type == MAP_ERR_NOT_CHECKED) {
  936. err_printk(ref->dev, entry,
  937. "DMA-API: device driver failed to check map error"
  938. "[device address=0x%016llx] [size=%llu bytes] "
  939. "[mapped as %s]",
  940. ref->dev_addr, ref->size,
  941. type2name[entry->type]);
  942. }
  943. hash_bucket_del(entry);
  944. dma_entry_free(entry);
  945. put_hash_bucket(bucket, &flags);
  946. }
  947. static void check_for_stack(struct device *dev, void *addr)
  948. {
  949. if (object_is_on_stack(addr))
  950. err_printk(dev, NULL, "DMA-API: device driver maps memory from "
  951. "stack [addr=%p]\n", addr);
  952. }
  953. static inline bool overlap(void *addr, unsigned long len, void *start, void *end)
  954. {
  955. unsigned long a1 = (unsigned long)addr;
  956. unsigned long b1 = a1 + len;
  957. unsigned long a2 = (unsigned long)start;
  958. unsigned long b2 = (unsigned long)end;
  959. return !(b1 <= a2 || a1 >= b2);
  960. }
  961. static void check_for_illegal_area(struct device *dev, void *addr, unsigned long len)
  962. {
  963. if (overlap(addr, len, _text, _etext) ||
  964. overlap(addr, len, __start_rodata, __end_rodata))
  965. err_printk(dev, NULL, "DMA-API: device driver maps memory from kernel text or rodata [addr=%p] [len=%lu]\n", addr, len);
  966. }
  967. static void check_sync(struct device *dev,
  968. struct dma_debug_entry *ref,
  969. bool to_cpu)
  970. {
  971. struct dma_debug_entry *entry;
  972. struct hash_bucket *bucket;
  973. unsigned long flags;
  974. bucket = get_hash_bucket(ref, &flags);
  975. entry = bucket_find_contain(&bucket, ref, &flags);
  976. if (!entry) {
  977. err_printk(dev, NULL, "DMA-API: device driver tries "
  978. "to sync DMA memory it has not allocated "
  979. "[device address=0x%016llx] [size=%llu bytes]\n",
  980. (unsigned long long)ref->dev_addr, ref->size);
  981. goto out;
  982. }
  983. if (ref->size > entry->size) {
  984. err_printk(dev, entry, "DMA-API: device driver syncs"
  985. " DMA memory outside allocated range "
  986. "[device address=0x%016llx] "
  987. "[allocation size=%llu bytes] "
  988. "[sync offset+size=%llu]\n",
  989. entry->dev_addr, entry->size,
  990. ref->size);
  991. }
  992. if (entry->direction == DMA_BIDIRECTIONAL)
  993. goto out;
  994. if (ref->direction != entry->direction) {
  995. err_printk(dev, entry, "DMA-API: device driver syncs "
  996. "DMA memory with different direction "
  997. "[device address=0x%016llx] [size=%llu bytes] "
  998. "[mapped with %s] [synced with %s]\n",
  999. (unsigned long long)ref->dev_addr, entry->size,
  1000. dir2name[entry->direction],
  1001. dir2name[ref->direction]);
  1002. }
  1003. if (to_cpu && !(entry->direction == DMA_FROM_DEVICE) &&
  1004. !(ref->direction == DMA_TO_DEVICE))
  1005. err_printk(dev, entry, "DMA-API: device driver syncs "
  1006. "device read-only DMA memory for cpu "
  1007. "[device address=0x%016llx] [size=%llu bytes] "
  1008. "[mapped with %s] [synced with %s]\n",
  1009. (unsigned long long)ref->dev_addr, entry->size,
  1010. dir2name[entry->direction],
  1011. dir2name[ref->direction]);
  1012. if (!to_cpu && !(entry->direction == DMA_TO_DEVICE) &&
  1013. !(ref->direction == DMA_FROM_DEVICE))
  1014. err_printk(dev, entry, "DMA-API: device driver syncs "
  1015. "device write-only DMA memory to device "
  1016. "[device address=0x%016llx] [size=%llu bytes] "
  1017. "[mapped with %s] [synced with %s]\n",
  1018. (unsigned long long)ref->dev_addr, entry->size,
  1019. dir2name[entry->direction],
  1020. dir2name[ref->direction]);
  1021. out:
  1022. put_hash_bucket(bucket, &flags);
  1023. }
  1024. void debug_dma_map_page(struct device *dev, struct page *page, size_t offset,
  1025. size_t size, int direction, dma_addr_t dma_addr,
  1026. bool map_single)
  1027. {
  1028. struct dma_debug_entry *entry;
  1029. if (unlikely(global_disable))
  1030. return;
  1031. if (dma_mapping_error(dev, dma_addr))
  1032. return;
  1033. entry = dma_entry_alloc();
  1034. if (!entry)
  1035. return;
  1036. entry->dev = dev;
  1037. entry->type = dma_debug_page;
  1038. entry->pfn = page_to_pfn(page);
  1039. entry->offset = offset,
  1040. entry->dev_addr = dma_addr;
  1041. entry->size = size;
  1042. entry->direction = direction;
  1043. entry->map_err_type = MAP_ERR_NOT_CHECKED;
  1044. if (map_single)
  1045. entry->type = dma_debug_single;
  1046. if (!PageHighMem(page)) {
  1047. void *addr = page_address(page) + offset;
  1048. check_for_stack(dev, addr);
  1049. check_for_illegal_area(dev, addr, size);
  1050. }
  1051. add_dma_entry(entry);
  1052. }
  1053. EXPORT_SYMBOL(debug_dma_map_page);
  1054. void debug_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
  1055. {
  1056. struct dma_debug_entry ref;
  1057. struct dma_debug_entry *entry;
  1058. struct hash_bucket *bucket;
  1059. unsigned long flags;
  1060. if (unlikely(global_disable))
  1061. return;
  1062. ref.dev = dev;
  1063. ref.dev_addr = dma_addr;
  1064. bucket = get_hash_bucket(&ref, &flags);
  1065. list_for_each_entry(entry, &bucket->list, list) {
  1066. if (!exact_match(&ref, entry))
  1067. continue;
  1068. /*
  1069. * The same physical address can be mapped multiple
  1070. * times. Without a hardware IOMMU this results in the
  1071. * same device addresses being put into the dma-debug
  1072. * hash multiple times too. This can result in false
  1073. * positives being reported. Therefore we implement a
  1074. * best-fit algorithm here which updates the first entry
  1075. * from the hash which fits the reference value and is
  1076. * not currently listed as being checked.
  1077. */
  1078. if (entry->map_err_type == MAP_ERR_NOT_CHECKED) {
  1079. entry->map_err_type = MAP_ERR_CHECKED;
  1080. break;
  1081. }
  1082. }
  1083. put_hash_bucket(bucket, &flags);
  1084. }
  1085. EXPORT_SYMBOL(debug_dma_mapping_error);
  1086. void debug_dma_unmap_page(struct device *dev, dma_addr_t addr,
  1087. size_t size, int direction, bool map_single)
  1088. {
  1089. struct dma_debug_entry ref = {
  1090. .type = dma_debug_page,
  1091. .dev = dev,
  1092. .dev_addr = addr,
  1093. .size = size,
  1094. .direction = direction,
  1095. };
  1096. if (unlikely(global_disable))
  1097. return;
  1098. if (map_single)
  1099. ref.type = dma_debug_single;
  1100. check_unmap(&ref);
  1101. }
  1102. EXPORT_SYMBOL(debug_dma_unmap_page);
  1103. void debug_dma_map_sg(struct device *dev, struct scatterlist *sg,
  1104. int nents, int mapped_ents, int direction)
  1105. {
  1106. struct dma_debug_entry *entry;
  1107. struct scatterlist *s;
  1108. int i;
  1109. if (unlikely(global_disable))
  1110. return;
  1111. for_each_sg(sg, s, mapped_ents, i) {
  1112. entry = dma_entry_alloc();
  1113. if (!entry)
  1114. return;
  1115. entry->type = dma_debug_sg;
  1116. entry->dev = dev;
  1117. entry->pfn = page_to_pfn(sg_page(s));
  1118. entry->offset = s->offset,
  1119. entry->size = sg_dma_len(s);
  1120. entry->dev_addr = sg_dma_address(s);
  1121. entry->direction = direction;
  1122. entry->sg_call_ents = nents;
  1123. entry->sg_mapped_ents = mapped_ents;
  1124. if (!PageHighMem(sg_page(s))) {
  1125. check_for_stack(dev, sg_virt(s));
  1126. check_for_illegal_area(dev, sg_virt(s), sg_dma_len(s));
  1127. }
  1128. add_dma_entry(entry);
  1129. }
  1130. }
  1131. EXPORT_SYMBOL(debug_dma_map_sg);
  1132. static int get_nr_mapped_entries(struct device *dev,
  1133. struct dma_debug_entry *ref)
  1134. {
  1135. struct dma_debug_entry *entry;
  1136. struct hash_bucket *bucket;
  1137. unsigned long flags;
  1138. int mapped_ents;
  1139. bucket = get_hash_bucket(ref, &flags);
  1140. entry = bucket_find_exact(bucket, ref);
  1141. mapped_ents = 0;
  1142. if (entry)
  1143. mapped_ents = entry->sg_mapped_ents;
  1144. put_hash_bucket(bucket, &flags);
  1145. return mapped_ents;
  1146. }
  1147. void debug_dma_unmap_sg(struct device *dev, struct scatterlist *sglist,
  1148. int nelems, int dir)
  1149. {
  1150. struct scatterlist *s;
  1151. int mapped_ents = 0, i;
  1152. if (unlikely(global_disable))
  1153. return;
  1154. for_each_sg(sglist, s, nelems, i) {
  1155. struct dma_debug_entry ref = {
  1156. .type = dma_debug_sg,
  1157. .dev = dev,
  1158. .pfn = page_to_pfn(sg_page(s)),
  1159. .offset = s->offset,
  1160. .dev_addr = sg_dma_address(s),
  1161. .size = sg_dma_len(s),
  1162. .direction = dir,
  1163. .sg_call_ents = nelems,
  1164. };
  1165. if (mapped_ents && i >= mapped_ents)
  1166. break;
  1167. if (!i)
  1168. mapped_ents = get_nr_mapped_entries(dev, &ref);
  1169. check_unmap(&ref);
  1170. }
  1171. }
  1172. EXPORT_SYMBOL(debug_dma_unmap_sg);
  1173. void debug_dma_alloc_coherent(struct device *dev, size_t size,
  1174. dma_addr_t dma_addr, void *virt)
  1175. {
  1176. struct dma_debug_entry *entry;
  1177. if (unlikely(global_disable))
  1178. return;
  1179. if (unlikely(virt == NULL))
  1180. return;
  1181. entry = dma_entry_alloc();
  1182. if (!entry)
  1183. return;
  1184. entry->type = dma_debug_coherent;
  1185. entry->dev = dev;
  1186. entry->pfn = page_to_pfn(virt_to_page(virt));
  1187. entry->offset = (size_t) virt & PAGE_MASK;
  1188. entry->size = size;
  1189. entry->dev_addr = dma_addr;
  1190. entry->direction = DMA_BIDIRECTIONAL;
  1191. add_dma_entry(entry);
  1192. }
  1193. EXPORT_SYMBOL(debug_dma_alloc_coherent);
  1194. void debug_dma_free_coherent(struct device *dev, size_t size,
  1195. void *virt, dma_addr_t addr)
  1196. {
  1197. struct dma_debug_entry ref = {
  1198. .type = dma_debug_coherent,
  1199. .dev = dev,
  1200. .pfn = page_to_pfn(virt_to_page(virt)),
  1201. .offset = (size_t) virt & PAGE_MASK,
  1202. .dev_addr = addr,
  1203. .size = size,
  1204. .direction = DMA_BIDIRECTIONAL,
  1205. };
  1206. if (unlikely(global_disable))
  1207. return;
  1208. check_unmap(&ref);
  1209. }
  1210. EXPORT_SYMBOL(debug_dma_free_coherent);
  1211. void debug_dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
  1212. size_t size, int direction)
  1213. {
  1214. struct dma_debug_entry ref;
  1215. if (unlikely(global_disable))
  1216. return;
  1217. ref.type = dma_debug_single;
  1218. ref.dev = dev;
  1219. ref.dev_addr = dma_handle;
  1220. ref.size = size;
  1221. ref.direction = direction;
  1222. ref.sg_call_ents = 0;
  1223. check_sync(dev, &ref, true);
  1224. }
  1225. EXPORT_SYMBOL(debug_dma_sync_single_for_cpu);
  1226. void debug_dma_sync_single_for_device(struct device *dev,
  1227. dma_addr_t dma_handle, size_t size,
  1228. int direction)
  1229. {
  1230. struct dma_debug_entry ref;
  1231. if (unlikely(global_disable))
  1232. return;
  1233. ref.type = dma_debug_single;
  1234. ref.dev = dev;
  1235. ref.dev_addr = dma_handle;
  1236. ref.size = size;
  1237. ref.direction = direction;
  1238. ref.sg_call_ents = 0;
  1239. check_sync(dev, &ref, false);
  1240. }
  1241. EXPORT_SYMBOL(debug_dma_sync_single_for_device);
  1242. void debug_dma_sync_single_range_for_cpu(struct device *dev,
  1243. dma_addr_t dma_handle,
  1244. unsigned long offset, size_t size,
  1245. int direction)
  1246. {
  1247. struct dma_debug_entry ref;
  1248. if (unlikely(global_disable))
  1249. return;
  1250. ref.type = dma_debug_single;
  1251. ref.dev = dev;
  1252. ref.dev_addr = dma_handle;
  1253. ref.size = offset + size;
  1254. ref.direction = direction;
  1255. ref.sg_call_ents = 0;
  1256. check_sync(dev, &ref, true);
  1257. }
  1258. EXPORT_SYMBOL(debug_dma_sync_single_range_for_cpu);
  1259. void debug_dma_sync_single_range_for_device(struct device *dev,
  1260. dma_addr_t dma_handle,
  1261. unsigned long offset,
  1262. size_t size, int direction)
  1263. {
  1264. struct dma_debug_entry ref;
  1265. if (unlikely(global_disable))
  1266. return;
  1267. ref.type = dma_debug_single;
  1268. ref.dev = dev;
  1269. ref.dev_addr = dma_handle;
  1270. ref.size = offset + size;
  1271. ref.direction = direction;
  1272. ref.sg_call_ents = 0;
  1273. check_sync(dev, &ref, false);
  1274. }
  1275. EXPORT_SYMBOL(debug_dma_sync_single_range_for_device);
  1276. void debug_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  1277. int nelems, int direction)
  1278. {
  1279. struct scatterlist *s;
  1280. int mapped_ents = 0, i;
  1281. if (unlikely(global_disable))
  1282. return;
  1283. for_each_sg(sg, s, nelems, i) {
  1284. struct dma_debug_entry ref = {
  1285. .type = dma_debug_sg,
  1286. .dev = dev,
  1287. .pfn = page_to_pfn(sg_page(s)),
  1288. .offset = s->offset,
  1289. .dev_addr = sg_dma_address(s),
  1290. .size = sg_dma_len(s),
  1291. .direction = direction,
  1292. .sg_call_ents = nelems,
  1293. };
  1294. if (!i)
  1295. mapped_ents = get_nr_mapped_entries(dev, &ref);
  1296. if (i >= mapped_ents)
  1297. break;
  1298. check_sync(dev, &ref, true);
  1299. }
  1300. }
  1301. EXPORT_SYMBOL(debug_dma_sync_sg_for_cpu);
  1302. void debug_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  1303. int nelems, int direction)
  1304. {
  1305. struct scatterlist *s;
  1306. int mapped_ents = 0, i;
  1307. if (unlikely(global_disable))
  1308. return;
  1309. for_each_sg(sg, s, nelems, i) {
  1310. struct dma_debug_entry ref = {
  1311. .type = dma_debug_sg,
  1312. .dev = dev,
  1313. .pfn = page_to_pfn(sg_page(s)),
  1314. .offset = s->offset,
  1315. .dev_addr = sg_dma_address(s),
  1316. .size = sg_dma_len(s),
  1317. .direction = direction,
  1318. .sg_call_ents = nelems,
  1319. };
  1320. if (!i)
  1321. mapped_ents = get_nr_mapped_entries(dev, &ref);
  1322. if (i >= mapped_ents)
  1323. break;
  1324. check_sync(dev, &ref, false);
  1325. }
  1326. }
  1327. EXPORT_SYMBOL(debug_dma_sync_sg_for_device);
  1328. static int __init dma_debug_driver_setup(char *str)
  1329. {
  1330. int i;
  1331. for (i = 0; i < NAME_MAX_LEN - 1; ++i, ++str) {
  1332. current_driver_name[i] = *str;
  1333. if (*str == 0)
  1334. break;
  1335. }
  1336. if (current_driver_name[0])
  1337. pr_info("DMA-API: enable driver filter for driver [%s]\n",
  1338. current_driver_name);
  1339. return 1;
  1340. }
  1341. __setup("dma_debug_driver=", dma_debug_driver_setup);