scatterlist.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. /*
  2. * Copyright (C) 2007 Jens Axboe <jens.axboe@oracle.com>
  3. *
  4. * Scatterlist handling helpers.
  5. *
  6. * This source code is licensed under the GNU General Public License,
  7. * Version 2. See the file COPYING for more details.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/slab.h>
  11. #include <linux/scatterlist.h>
  12. #include <linux/highmem.h>
  13. #include <linux/kmemleak.h>
  14. /**
  15. * sg_next - return the next scatterlist entry in a list
  16. * @sg: The current sg entry
  17. *
  18. * Description:
  19. * Usually the next entry will be @sg@ + 1, but if this sg element is part
  20. * of a chained scatterlist, it could jump to the start of a new
  21. * scatterlist array.
  22. *
  23. **/
  24. struct scatterlist *sg_next(struct scatterlist *sg)
  25. {
  26. #ifdef CONFIG_DEBUG_SG
  27. BUG_ON(sg->sg_magic != SG_MAGIC);
  28. #endif
  29. if (sg_is_last(sg))
  30. return NULL;
  31. sg++;
  32. if (unlikely(sg_is_chain(sg)))
  33. sg = sg_chain_ptr(sg);
  34. return sg;
  35. }
  36. EXPORT_SYMBOL(sg_next);
  37. /**
  38. * sg_last - return the last scatterlist entry in a list
  39. * @sgl: First entry in the scatterlist
  40. * @nents: Number of entries in the scatterlist
  41. *
  42. * Description:
  43. * Should only be used casually, it (currently) scans the entire list
  44. * to get the last entry.
  45. *
  46. * Note that the @sgl@ pointer passed in need not be the first one,
  47. * the important bit is that @nents@ denotes the number of entries that
  48. * exist from @sgl@.
  49. *
  50. **/
  51. struct scatterlist *sg_last(struct scatterlist *sgl, unsigned int nents)
  52. {
  53. #ifndef ARCH_HAS_SG_CHAIN
  54. struct scatterlist *ret = &sgl[nents - 1];
  55. #else
  56. struct scatterlist *sg, *ret = NULL;
  57. unsigned int i;
  58. for_each_sg(sgl, sg, nents, i)
  59. ret = sg;
  60. #endif
  61. #ifdef CONFIG_DEBUG_SG
  62. BUG_ON(sgl[0].sg_magic != SG_MAGIC);
  63. BUG_ON(!sg_is_last(ret));
  64. #endif
  65. return ret;
  66. }
  67. EXPORT_SYMBOL(sg_last);
  68. /**
  69. * sg_init_table - Initialize SG table
  70. * @sgl: The SG table
  71. * @nents: Number of entries in table
  72. *
  73. * Notes:
  74. * If this is part of a chained sg table, sg_mark_end() should be
  75. * used only on the last table part.
  76. *
  77. **/
  78. void sg_init_table(struct scatterlist *sgl, unsigned int nents)
  79. {
  80. memset(sgl, 0, sizeof(*sgl) * nents);
  81. #ifdef CONFIG_DEBUG_SG
  82. {
  83. unsigned int i;
  84. for (i = 0; i < nents; i++)
  85. sgl[i].sg_magic = SG_MAGIC;
  86. }
  87. #endif
  88. sg_mark_end(&sgl[nents - 1]);
  89. }
  90. EXPORT_SYMBOL(sg_init_table);
  91. /**
  92. * sg_init_one - Initialize a single entry sg list
  93. * @sg: SG entry
  94. * @buf: Virtual address for IO
  95. * @buflen: IO length
  96. *
  97. **/
  98. void sg_init_one(struct scatterlist *sg, const void *buf, unsigned int buflen)
  99. {
  100. sg_init_table(sg, 1);
  101. sg_set_buf(sg, buf, buflen);
  102. }
  103. EXPORT_SYMBOL(sg_init_one);
  104. /*
  105. * The default behaviour of sg_alloc_table() is to use these kmalloc/kfree
  106. * helpers.
  107. */
  108. static struct scatterlist *sg_kmalloc(unsigned int nents, gfp_t gfp_mask)
  109. {
  110. if (nents == SG_MAX_SINGLE_ALLOC) {
  111. /*
  112. * Kmemleak doesn't track page allocations as they are not
  113. * commonly used (in a raw form) for kernel data structures.
  114. * As we chain together a list of pages and then a normal
  115. * kmalloc (tracked by kmemleak), in order to for that last
  116. * allocation not to become decoupled (and thus a
  117. * false-positive) we need to inform kmemleak of all the
  118. * intermediate allocations.
  119. */
  120. void *ptr = (void *) __get_free_page(gfp_mask);
  121. kmemleak_alloc(ptr, PAGE_SIZE, 1, gfp_mask);
  122. return ptr;
  123. } else
  124. return kmalloc(nents * sizeof(struct scatterlist), gfp_mask);
  125. }
  126. static void sg_kfree(struct scatterlist *sg, unsigned int nents)
  127. {
  128. if (nents == SG_MAX_SINGLE_ALLOC) {
  129. kmemleak_free(sg);
  130. free_page((unsigned long) sg);
  131. } else
  132. kfree(sg);
  133. }
  134. /**
  135. * __sg_free_table - Free a previously mapped sg table
  136. * @table: The sg table header to use
  137. * @max_ents: The maximum number of entries per single scatterlist
  138. * @free_fn: Free function
  139. *
  140. * Description:
  141. * Free an sg table previously allocated and setup with
  142. * __sg_alloc_table(). The @max_ents value must be identical to
  143. * that previously used with __sg_alloc_table().
  144. *
  145. **/
  146. void __sg_free_table(struct sg_table *table, unsigned int max_ents,
  147. sg_free_fn *free_fn)
  148. {
  149. struct scatterlist *sgl, *next;
  150. if (unlikely(!table->sgl))
  151. return;
  152. sgl = table->sgl;
  153. while (table->orig_nents) {
  154. unsigned int alloc_size = table->orig_nents;
  155. unsigned int sg_size;
  156. /*
  157. * If we have more than max_ents segments left,
  158. * then assign 'next' to the sg table after the current one.
  159. * sg_size is then one less than alloc size, since the last
  160. * element is the chain pointer.
  161. */
  162. if (alloc_size > max_ents) {
  163. next = sg_chain_ptr(&sgl[max_ents - 1]);
  164. alloc_size = max_ents;
  165. sg_size = alloc_size - 1;
  166. } else {
  167. sg_size = alloc_size;
  168. next = NULL;
  169. }
  170. table->orig_nents -= sg_size;
  171. free_fn(sgl, alloc_size);
  172. sgl = next;
  173. }
  174. table->sgl = NULL;
  175. }
  176. EXPORT_SYMBOL(__sg_free_table);
  177. /**
  178. * sg_free_table - Free a previously allocated sg table
  179. * @table: The mapped sg table header
  180. *
  181. **/
  182. void sg_free_table(struct sg_table *table)
  183. {
  184. __sg_free_table(table, SG_MAX_SINGLE_ALLOC, sg_kfree);
  185. }
  186. EXPORT_SYMBOL(sg_free_table);
  187. /**
  188. * __sg_alloc_table - Allocate and initialize an sg table with given allocator
  189. * @table: The sg table header to use
  190. * @nents: Number of entries in sg list
  191. * @max_ents: The maximum number of entries the allocator returns per call
  192. * @gfp_mask: GFP allocation mask
  193. * @alloc_fn: Allocator to use
  194. *
  195. * Description:
  196. * This function returns a @table @nents long. The allocator is
  197. * defined to return scatterlist chunks of maximum size @max_ents.
  198. * Thus if @nents is bigger than @max_ents, the scatterlists will be
  199. * chained in units of @max_ents.
  200. *
  201. * Notes:
  202. * If this function returns non-0 (eg failure), the caller must call
  203. * __sg_free_table() to cleanup any leftover allocations.
  204. *
  205. **/
  206. int __sg_alloc_table(struct sg_table *table, unsigned int nents,
  207. unsigned int max_ents, gfp_t gfp_mask,
  208. sg_alloc_fn *alloc_fn)
  209. {
  210. struct scatterlist *sg, *prv;
  211. unsigned int left;
  212. #ifndef ARCH_HAS_SG_CHAIN
  213. BUG_ON(nents > max_ents);
  214. #endif
  215. memset(table, 0, sizeof(*table));
  216. left = nents;
  217. prv = NULL;
  218. do {
  219. unsigned int sg_size, alloc_size = left;
  220. if (alloc_size > max_ents) {
  221. alloc_size = max_ents;
  222. sg_size = alloc_size - 1;
  223. } else
  224. sg_size = alloc_size;
  225. left -= sg_size;
  226. sg = alloc_fn(alloc_size, gfp_mask);
  227. if (unlikely(!sg)) {
  228. /*
  229. * Adjust entry count to reflect that the last
  230. * entry of the previous table won't be used for
  231. * linkage. Without this, sg_kfree() may get
  232. * confused.
  233. */
  234. if (prv)
  235. table->nents = ++table->orig_nents;
  236. return -ENOMEM;
  237. }
  238. sg_init_table(sg, alloc_size);
  239. table->nents = table->orig_nents += sg_size;
  240. /*
  241. * If this is the first mapping, assign the sg table header.
  242. * If this is not the first mapping, chain previous part.
  243. */
  244. if (prv)
  245. sg_chain(prv, max_ents, sg);
  246. else
  247. table->sgl = sg;
  248. /*
  249. * If no more entries after this one, mark the end
  250. */
  251. if (!left)
  252. sg_mark_end(&sg[sg_size - 1]);
  253. /*
  254. * only really needed for mempool backed sg allocations (like
  255. * SCSI), a possible improvement here would be to pass the
  256. * table pointer into the allocator and let that clear these
  257. * flags
  258. */
  259. gfp_mask &= ~__GFP_WAIT;
  260. gfp_mask |= __GFP_HIGH;
  261. prv = sg;
  262. } while (left);
  263. return 0;
  264. }
  265. EXPORT_SYMBOL(__sg_alloc_table);
  266. /**
  267. * sg_alloc_table - Allocate and initialize an sg table
  268. * @table: The sg table header to use
  269. * @nents: Number of entries in sg list
  270. * @gfp_mask: GFP allocation mask
  271. *
  272. * Description:
  273. * Allocate and initialize an sg table. If @nents@ is larger than
  274. * SG_MAX_SINGLE_ALLOC a chained sg table will be setup.
  275. *
  276. **/
  277. int sg_alloc_table(struct sg_table *table, unsigned int nents, gfp_t gfp_mask)
  278. {
  279. int ret;
  280. ret = __sg_alloc_table(table, nents, SG_MAX_SINGLE_ALLOC,
  281. gfp_mask, sg_kmalloc);
  282. if (unlikely(ret))
  283. __sg_free_table(table, SG_MAX_SINGLE_ALLOC, sg_kfree);
  284. return ret;
  285. }
  286. EXPORT_SYMBOL(sg_alloc_table);
  287. /**
  288. * sg_miter_start - start mapping iteration over a sg list
  289. * @miter: sg mapping iter to be started
  290. * @sgl: sg list to iterate over
  291. * @nents: number of sg entries
  292. *
  293. * Description:
  294. * Starts mapping iterator @miter.
  295. *
  296. * Context:
  297. * Don't care.
  298. */
  299. void sg_miter_start(struct sg_mapping_iter *miter, struct scatterlist *sgl,
  300. unsigned int nents, unsigned int flags)
  301. {
  302. memset(miter, 0, sizeof(struct sg_mapping_iter));
  303. miter->__sg = sgl;
  304. miter->__nents = nents;
  305. miter->__offset = 0;
  306. WARN_ON(!(flags & (SG_MITER_TO_SG | SG_MITER_FROM_SG)));
  307. miter->__flags = flags;
  308. }
  309. EXPORT_SYMBOL(sg_miter_start);
  310. /**
  311. * sg_miter_next - proceed mapping iterator to the next mapping
  312. * @miter: sg mapping iter to proceed
  313. *
  314. * Description:
  315. * Proceeds @miter@ to the next mapping. @miter@ should have been
  316. * started using sg_miter_start(). On successful return,
  317. * @miter@->page, @miter@->addr and @miter@->length point to the
  318. * current mapping.
  319. *
  320. * Context:
  321. * IRQ disabled if SG_MITER_ATOMIC. IRQ must stay disabled till
  322. * @miter@ is stopped. May sleep if !SG_MITER_ATOMIC.
  323. *
  324. * Returns:
  325. * true if @miter contains the next mapping. false if end of sg
  326. * list is reached.
  327. */
  328. bool sg_miter_next(struct sg_mapping_iter *miter)
  329. {
  330. unsigned int off, len;
  331. /* check for end and drop resources from the last iteration */
  332. if (!miter->__nents)
  333. return false;
  334. sg_miter_stop(miter);
  335. /* get to the next sg if necessary. __offset is adjusted by stop */
  336. while (miter->__offset == miter->__sg->length) {
  337. if (--miter->__nents) {
  338. miter->__sg = sg_next(miter->__sg);
  339. miter->__offset = 0;
  340. } else
  341. return false;
  342. }
  343. /* map the next page */
  344. off = miter->__sg->offset + miter->__offset;
  345. len = miter->__sg->length - miter->__offset;
  346. miter->page = nth_page(sg_page(miter->__sg), off >> PAGE_SHIFT);
  347. off &= ~PAGE_MASK;
  348. miter->length = min_t(unsigned int, len, PAGE_SIZE - off);
  349. miter->consumed = miter->length;
  350. if (miter->__flags & SG_MITER_ATOMIC)
  351. miter->addr = kmap_atomic(miter->page, KM_BIO_SRC_IRQ) + off;
  352. else
  353. miter->addr = kmap(miter->page) + off;
  354. return true;
  355. }
  356. EXPORT_SYMBOL(sg_miter_next);
  357. /**
  358. * sg_miter_stop - stop mapping iteration
  359. * @miter: sg mapping iter to be stopped
  360. *
  361. * Description:
  362. * Stops mapping iterator @miter. @miter should have been started
  363. * started using sg_miter_start(). A stopped iteration can be
  364. * resumed by calling sg_miter_next() on it. This is useful when
  365. * resources (kmap) need to be released during iteration.
  366. *
  367. * Context:
  368. * IRQ disabled if the SG_MITER_ATOMIC is set. Don't care otherwise.
  369. */
  370. void sg_miter_stop(struct sg_mapping_iter *miter)
  371. {
  372. WARN_ON(miter->consumed > miter->length);
  373. /* drop resources from the last iteration */
  374. if (miter->addr) {
  375. miter->__offset += miter->consumed;
  376. if (miter->__flags & SG_MITER_TO_SG)
  377. flush_kernel_dcache_page(miter->page);
  378. if (miter->__flags & SG_MITER_ATOMIC) {
  379. WARN_ON(!irqs_disabled());
  380. kunmap_atomic(miter->addr, KM_BIO_SRC_IRQ);
  381. } else
  382. kunmap(miter->page);
  383. miter->page = NULL;
  384. miter->addr = NULL;
  385. miter->length = 0;
  386. miter->consumed = 0;
  387. }
  388. }
  389. EXPORT_SYMBOL(sg_miter_stop);
  390. /**
  391. * sg_copy_buffer - Copy data between a linear buffer and an SG list
  392. * @sgl: The SG list
  393. * @nents: Number of SG entries
  394. * @buf: Where to copy from
  395. * @buflen: The number of bytes to copy
  396. * @to_buffer: transfer direction (non zero == from an sg list to a
  397. * buffer, 0 == from a buffer to an sg list
  398. *
  399. * Returns the number of copied bytes.
  400. *
  401. **/
  402. static size_t sg_copy_buffer(struct scatterlist *sgl, unsigned int nents,
  403. void *buf, size_t buflen, int to_buffer)
  404. {
  405. unsigned int offset = 0;
  406. struct sg_mapping_iter miter;
  407. unsigned long flags;
  408. unsigned int sg_flags = SG_MITER_ATOMIC;
  409. if (to_buffer)
  410. sg_flags |= SG_MITER_FROM_SG;
  411. else
  412. sg_flags |= SG_MITER_TO_SG;
  413. sg_miter_start(&miter, sgl, nents, sg_flags);
  414. local_irq_save(flags);
  415. while (sg_miter_next(&miter) && offset < buflen) {
  416. unsigned int len;
  417. len = min(miter.length, buflen - offset);
  418. if (to_buffer)
  419. memcpy(buf + offset, miter.addr, len);
  420. else
  421. memcpy(miter.addr, buf + offset, len);
  422. offset += len;
  423. }
  424. sg_miter_stop(&miter);
  425. local_irq_restore(flags);
  426. return offset;
  427. }
  428. /**
  429. * sg_copy_from_buffer - Copy from a linear buffer to an SG list
  430. * @sgl: The SG list
  431. * @nents: Number of SG entries
  432. * @buf: Where to copy from
  433. * @buflen: The number of bytes to copy
  434. *
  435. * Returns the number of copied bytes.
  436. *
  437. **/
  438. size_t sg_copy_from_buffer(struct scatterlist *sgl, unsigned int nents,
  439. void *buf, size_t buflen)
  440. {
  441. return sg_copy_buffer(sgl, nents, buf, buflen, 0);
  442. }
  443. EXPORT_SYMBOL(sg_copy_from_buffer);
  444. /**
  445. * sg_copy_to_buffer - Copy from an SG list to a linear buffer
  446. * @sgl: The SG list
  447. * @nents: Number of SG entries
  448. * @buf: Where to copy to
  449. * @buflen: The number of bytes to copy
  450. *
  451. * Returns the number of copied bytes.
  452. *
  453. **/
  454. size_t sg_copy_to_buffer(struct scatterlist *sgl, unsigned int nents,
  455. void *buf, size_t buflen)
  456. {
  457. return sg_copy_buffer(sgl, nents, buf, buflen, 1);
  458. }
  459. EXPORT_SYMBOL(sg_copy_to_buffer);