debug.c 46 KB

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