dma-debug.c 45 KB

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