scatterlist.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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/export.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. memset(table, 0, sizeof(*table));
  213. if (nents == 0)
  214. return -EINVAL;
  215. #ifndef ARCH_HAS_SG_CHAIN
  216. BUG_ON(nents > max_ents);
  217. #endif
  218. left = nents;
  219. prv = NULL;
  220. do {
  221. unsigned int sg_size, alloc_size = left;
  222. if (alloc_size > max_ents) {
  223. alloc_size = max_ents;
  224. sg_size = alloc_size - 1;
  225. } else
  226. sg_size = alloc_size;
  227. left -= sg_size;
  228. sg = alloc_fn(alloc_size, gfp_mask);
  229. if (unlikely(!sg)) {
  230. /*
  231. * Adjust entry count to reflect that the last
  232. * entry of the previous table won't be used for
  233. * linkage. Without this, sg_kfree() may get
  234. * confused.
  235. */
  236. if (prv)
  237. table->nents = ++table->orig_nents;
  238. return -ENOMEM;
  239. }
  240. sg_init_table(sg, alloc_size);
  241. table->nents = table->orig_nents += sg_size;
  242. /*
  243. * If this is the first mapping, assign the sg table header.
  244. * If this is not the first mapping, chain previous part.
  245. */
  246. if (prv)
  247. sg_chain(prv, max_ents, sg);
  248. else
  249. table->sgl = sg;
  250. /*
  251. * If no more entries after this one, mark the end
  252. */
  253. if (!left)
  254. sg_mark_end(&sg[sg_size - 1]);
  255. /*
  256. * only really needed for mempool backed sg allocations (like
  257. * SCSI), a possible improvement here would be to pass the
  258. * table pointer into the allocator and let that clear these
  259. * flags
  260. */
  261. gfp_mask &= ~__GFP_WAIT;
  262. gfp_mask |= __GFP_HIGH;
  263. prv = sg;
  264. } while (left);
  265. return 0;
  266. }
  267. EXPORT_SYMBOL(__sg_alloc_table);
  268. /**
  269. * sg_alloc_table - Allocate and initialize an sg table
  270. * @table: The sg table header to use
  271. * @nents: Number of entries in sg list
  272. * @gfp_mask: GFP allocation mask
  273. *
  274. * Description:
  275. * Allocate and initialize an sg table. If @nents@ is larger than
  276. * SG_MAX_SINGLE_ALLOC a chained sg table will be setup.
  277. *
  278. **/
  279. int sg_alloc_table(struct sg_table *table, unsigned int nents, gfp_t gfp_mask)
  280. {
  281. int ret;
  282. ret = __sg_alloc_table(table, nents, SG_MAX_SINGLE_ALLOC,
  283. gfp_mask, sg_kmalloc);
  284. if (unlikely(ret))
  285. __sg_free_table(table, SG_MAX_SINGLE_ALLOC, sg_kfree);
  286. return ret;
  287. }
  288. EXPORT_SYMBOL(sg_alloc_table);
  289. /**
  290. * sg_miter_start - start mapping iteration over a sg list
  291. * @miter: sg mapping iter to be started
  292. * @sgl: sg list to iterate over
  293. * @nents: number of sg entries
  294. *
  295. * Description:
  296. * Starts mapping iterator @miter.
  297. *
  298. * Context:
  299. * Don't care.
  300. */
  301. void sg_miter_start(struct sg_mapping_iter *miter, struct scatterlist *sgl,
  302. unsigned int nents, unsigned int flags)
  303. {
  304. memset(miter, 0, sizeof(struct sg_mapping_iter));
  305. miter->__sg = sgl;
  306. miter->__nents = nents;
  307. miter->__offset = 0;
  308. WARN_ON(!(flags & (SG_MITER_TO_SG | SG_MITER_FROM_SG)));
  309. miter->__flags = flags;
  310. }
  311. EXPORT_SYMBOL(sg_miter_start);
  312. /**
  313. * sg_miter_next - proceed mapping iterator to the next mapping
  314. * @miter: sg mapping iter to proceed
  315. *
  316. * Description:
  317. * Proceeds @miter@ to the next mapping. @miter@ should have been
  318. * started using sg_miter_start(). On successful return,
  319. * @miter@->page, @miter@->addr and @miter@->length point to the
  320. * current mapping.
  321. *
  322. * Context:
  323. * IRQ disabled if SG_MITER_ATOMIC. IRQ must stay disabled till
  324. * @miter@ is stopped. May sleep if !SG_MITER_ATOMIC.
  325. *
  326. * Returns:
  327. * true if @miter contains the next mapping. false if end of sg
  328. * list is reached.
  329. */
  330. bool sg_miter_next(struct sg_mapping_iter *miter)
  331. {
  332. unsigned int off, len;
  333. /* check for end and drop resources from the last iteration */
  334. if (!miter->__nents)
  335. return false;
  336. sg_miter_stop(miter);
  337. /* get to the next sg if necessary. __offset is adjusted by stop */
  338. while (miter->__offset == miter->__sg->length) {
  339. if (--miter->__nents) {
  340. miter->__sg = sg_next(miter->__sg);
  341. miter->__offset = 0;
  342. } else
  343. return false;
  344. }
  345. /* map the next page */
  346. off = miter->__sg->offset + miter->__offset;
  347. len = miter->__sg->length - miter->__offset;
  348. miter->page = nth_page(sg_page(miter->__sg), off >> PAGE_SHIFT);
  349. off &= ~PAGE_MASK;
  350. miter->length = min_t(unsigned int, len, PAGE_SIZE - off);
  351. miter->consumed = miter->length;
  352. if (miter->__flags & SG_MITER_ATOMIC)
  353. miter->addr = kmap_atomic(miter->page) + off;
  354. else
  355. miter->addr = kmap(miter->page) + off;
  356. return true;
  357. }
  358. EXPORT_SYMBOL(sg_miter_next);
  359. /**
  360. * sg_miter_stop - stop mapping iteration
  361. * @miter: sg mapping iter to be stopped
  362. *
  363. * Description:
  364. * Stops mapping iterator @miter. @miter should have been started
  365. * started using sg_miter_start(). A stopped iteration can be
  366. * resumed by calling sg_miter_next() on it. This is useful when
  367. * resources (kmap) need to be released during iteration.
  368. *
  369. * Context:
  370. * IRQ disabled if the SG_MITER_ATOMIC is set. Don't care otherwise.
  371. */
  372. void sg_miter_stop(struct sg_mapping_iter *miter)
  373. {
  374. WARN_ON(miter->consumed > miter->length);
  375. /* drop resources from the last iteration */
  376. if (miter->addr) {
  377. miter->__offset += miter->consumed;
  378. if ((miter->__flags & SG_MITER_TO_SG) &&
  379. !PageSlab(miter->page))
  380. flush_kernel_dcache_page(miter->page);
  381. if (miter->__flags & SG_MITER_ATOMIC) {
  382. WARN_ON(!irqs_disabled());
  383. kunmap_atomic(miter->addr);
  384. } else
  385. kunmap(miter->page);
  386. miter->page = NULL;
  387. miter->addr = NULL;
  388. miter->length = 0;
  389. miter->consumed = 0;
  390. }
  391. }
  392. EXPORT_SYMBOL(sg_miter_stop);
  393. /**
  394. * sg_copy_buffer - Copy data between a linear buffer and an SG list
  395. * @sgl: The SG list
  396. * @nents: Number of SG entries
  397. * @buf: Where to copy from
  398. * @buflen: The number of bytes to copy
  399. * @to_buffer: transfer direction (non zero == from an sg list to a
  400. * buffer, 0 == from a buffer to an sg list
  401. *
  402. * Returns the number of copied bytes.
  403. *
  404. **/
  405. static size_t sg_copy_buffer(struct scatterlist *sgl, unsigned int nents,
  406. void *buf, size_t buflen, int to_buffer)
  407. {
  408. unsigned int offset = 0;
  409. struct sg_mapping_iter miter;
  410. unsigned long flags;
  411. unsigned int sg_flags = SG_MITER_ATOMIC;
  412. if (to_buffer)
  413. sg_flags |= SG_MITER_FROM_SG;
  414. else
  415. sg_flags |= SG_MITER_TO_SG;
  416. sg_miter_start(&miter, sgl, nents, sg_flags);
  417. local_irq_save(flags);
  418. while (sg_miter_next(&miter) && offset < buflen) {
  419. unsigned int len;
  420. len = min(miter.length, buflen - offset);
  421. if (to_buffer)
  422. memcpy(buf + offset, miter.addr, len);
  423. else
  424. memcpy(miter.addr, buf + offset, len);
  425. offset += len;
  426. }
  427. sg_miter_stop(&miter);
  428. local_irq_restore(flags);
  429. return offset;
  430. }
  431. /**
  432. * sg_copy_from_buffer - Copy from a linear buffer to an SG list
  433. * @sgl: The SG list
  434. * @nents: Number of SG entries
  435. * @buf: Where to copy from
  436. * @buflen: The number of bytes to copy
  437. *
  438. * Returns the number of copied bytes.
  439. *
  440. **/
  441. size_t sg_copy_from_buffer(struct scatterlist *sgl, unsigned int nents,
  442. void *buf, size_t buflen)
  443. {
  444. return sg_copy_buffer(sgl, nents, buf, buflen, 0);
  445. }
  446. EXPORT_SYMBOL(sg_copy_from_buffer);
  447. /**
  448. * sg_copy_to_buffer - Copy from an SG list to a linear buffer
  449. * @sgl: The SG list
  450. * @nents: Number of SG entries
  451. * @buf: Where to copy to
  452. * @buflen: The number of bytes to copy
  453. *
  454. * Returns the number of copied bytes.
  455. *
  456. **/
  457. size_t sg_copy_to_buffer(struct scatterlist *sgl, unsigned int nents,
  458. void *buf, size_t buflen)
  459. {
  460. return sg_copy_buffer(sgl, nents, buf, buflen, 1);
  461. }
  462. EXPORT_SYMBOL(sg_copy_to_buffer);