scatterlist.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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_nents - return total count of entries in scatterlist
  39. * @sg: The scatterlist
  40. *
  41. * Description:
  42. * Allows to know how many entries are in sg, taking into acount
  43. * chaining as well
  44. *
  45. **/
  46. int sg_nents(struct scatterlist *sg)
  47. {
  48. int nents;
  49. for (nents = 0; sg; sg = sg_next(sg))
  50. nents++;
  51. return nents;
  52. }
  53. EXPORT_SYMBOL(sg_nents);
  54. /**
  55. * sg_last - return the last scatterlist entry in a list
  56. * @sgl: First entry in the scatterlist
  57. * @nents: Number of entries in the scatterlist
  58. *
  59. * Description:
  60. * Should only be used casually, it (currently) scans the entire list
  61. * to get the last entry.
  62. *
  63. * Note that the @sgl@ pointer passed in need not be the first one,
  64. * the important bit is that @nents@ denotes the number of entries that
  65. * exist from @sgl@.
  66. *
  67. **/
  68. struct scatterlist *sg_last(struct scatterlist *sgl, unsigned int nents)
  69. {
  70. #ifndef ARCH_HAS_SG_CHAIN
  71. struct scatterlist *ret = &sgl[nents - 1];
  72. #else
  73. struct scatterlist *sg, *ret = NULL;
  74. unsigned int i;
  75. for_each_sg(sgl, sg, nents, i)
  76. ret = sg;
  77. #endif
  78. #ifdef CONFIG_DEBUG_SG
  79. BUG_ON(sgl[0].sg_magic != SG_MAGIC);
  80. BUG_ON(!sg_is_last(ret));
  81. #endif
  82. return ret;
  83. }
  84. EXPORT_SYMBOL(sg_last);
  85. /**
  86. * sg_init_table - Initialize SG table
  87. * @sgl: The SG table
  88. * @nents: Number of entries in table
  89. *
  90. * Notes:
  91. * If this is part of a chained sg table, sg_mark_end() should be
  92. * used only on the last table part.
  93. *
  94. **/
  95. void sg_init_table(struct scatterlist *sgl, unsigned int nents)
  96. {
  97. memset(sgl, 0, sizeof(*sgl) * nents);
  98. #ifdef CONFIG_DEBUG_SG
  99. {
  100. unsigned int i;
  101. for (i = 0; i < nents; i++)
  102. sgl[i].sg_magic = SG_MAGIC;
  103. }
  104. #endif
  105. sg_mark_end(&sgl[nents - 1]);
  106. }
  107. EXPORT_SYMBOL(sg_init_table);
  108. /**
  109. * sg_init_one - Initialize a single entry sg list
  110. * @sg: SG entry
  111. * @buf: Virtual address for IO
  112. * @buflen: IO length
  113. *
  114. **/
  115. void sg_init_one(struct scatterlist *sg, const void *buf, unsigned int buflen)
  116. {
  117. sg_init_table(sg, 1);
  118. sg_set_buf(sg, buf, buflen);
  119. }
  120. EXPORT_SYMBOL(sg_init_one);
  121. /*
  122. * The default behaviour of sg_alloc_table() is to use these kmalloc/kfree
  123. * helpers.
  124. */
  125. static struct scatterlist *sg_kmalloc(unsigned int nents, gfp_t gfp_mask)
  126. {
  127. if (nents == SG_MAX_SINGLE_ALLOC) {
  128. /*
  129. * Kmemleak doesn't track page allocations as they are not
  130. * commonly used (in a raw form) for kernel data structures.
  131. * As we chain together a list of pages and then a normal
  132. * kmalloc (tracked by kmemleak), in order to for that last
  133. * allocation not to become decoupled (and thus a
  134. * false-positive) we need to inform kmemleak of all the
  135. * intermediate allocations.
  136. */
  137. void *ptr = (void *) __get_free_page(gfp_mask);
  138. kmemleak_alloc(ptr, PAGE_SIZE, 1, gfp_mask);
  139. return ptr;
  140. } else
  141. return kmalloc(nents * sizeof(struct scatterlist), gfp_mask);
  142. }
  143. static void sg_kfree(struct scatterlist *sg, unsigned int nents)
  144. {
  145. if (nents == SG_MAX_SINGLE_ALLOC) {
  146. kmemleak_free(sg);
  147. free_page((unsigned long) sg);
  148. } else
  149. kfree(sg);
  150. }
  151. /**
  152. * __sg_free_table - Free a previously mapped sg table
  153. * @table: The sg table header to use
  154. * @max_ents: The maximum number of entries per single scatterlist
  155. * @free_fn: Free function
  156. *
  157. * Description:
  158. * Free an sg table previously allocated and setup with
  159. * __sg_alloc_table(). The @max_ents value must be identical to
  160. * that previously used with __sg_alloc_table().
  161. *
  162. **/
  163. void __sg_free_table(struct sg_table *table, unsigned int max_ents,
  164. sg_free_fn *free_fn)
  165. {
  166. struct scatterlist *sgl, *next;
  167. if (unlikely(!table->sgl))
  168. return;
  169. sgl = table->sgl;
  170. while (table->orig_nents) {
  171. unsigned int alloc_size = table->orig_nents;
  172. unsigned int sg_size;
  173. /*
  174. * If we have more than max_ents segments left,
  175. * then assign 'next' to the sg table after the current one.
  176. * sg_size is then one less than alloc size, since the last
  177. * element is the chain pointer.
  178. */
  179. if (alloc_size > max_ents) {
  180. next = sg_chain_ptr(&sgl[max_ents - 1]);
  181. alloc_size = max_ents;
  182. sg_size = alloc_size - 1;
  183. } else {
  184. sg_size = alloc_size;
  185. next = NULL;
  186. }
  187. table->orig_nents -= sg_size;
  188. free_fn(sgl, alloc_size);
  189. sgl = next;
  190. }
  191. table->sgl = NULL;
  192. }
  193. EXPORT_SYMBOL(__sg_free_table);
  194. /**
  195. * sg_free_table - Free a previously allocated sg table
  196. * @table: The mapped sg table header
  197. *
  198. **/
  199. void sg_free_table(struct sg_table *table)
  200. {
  201. __sg_free_table(table, SG_MAX_SINGLE_ALLOC, sg_kfree);
  202. }
  203. EXPORT_SYMBOL(sg_free_table);
  204. /**
  205. * __sg_alloc_table - Allocate and initialize an sg table with given allocator
  206. * @table: The sg table header to use
  207. * @nents: Number of entries in sg list
  208. * @max_ents: The maximum number of entries the allocator returns per call
  209. * @gfp_mask: GFP allocation mask
  210. * @alloc_fn: Allocator to use
  211. *
  212. * Description:
  213. * This function returns a @table @nents long. The allocator is
  214. * defined to return scatterlist chunks of maximum size @max_ents.
  215. * Thus if @nents is bigger than @max_ents, the scatterlists will be
  216. * chained in units of @max_ents.
  217. *
  218. * Notes:
  219. * If this function returns non-0 (eg failure), the caller must call
  220. * __sg_free_table() to cleanup any leftover allocations.
  221. *
  222. **/
  223. int __sg_alloc_table(struct sg_table *table, unsigned int nents,
  224. unsigned int max_ents, gfp_t gfp_mask,
  225. sg_alloc_fn *alloc_fn)
  226. {
  227. struct scatterlist *sg, *prv;
  228. unsigned int left;
  229. #ifndef ARCH_HAS_SG_CHAIN
  230. if (WARN_ON_ONCE(nents > max_ents))
  231. return -EINVAL;
  232. #endif
  233. memset(table, 0, sizeof(*table));
  234. left = nents;
  235. prv = NULL;
  236. do {
  237. unsigned int sg_size, alloc_size = left;
  238. if (alloc_size > max_ents) {
  239. alloc_size = max_ents;
  240. sg_size = alloc_size - 1;
  241. } else
  242. sg_size = alloc_size;
  243. left -= sg_size;
  244. sg = alloc_fn(alloc_size, gfp_mask);
  245. if (unlikely(!sg)) {
  246. /*
  247. * Adjust entry count to reflect that the last
  248. * entry of the previous table won't be used for
  249. * linkage. Without this, sg_kfree() may get
  250. * confused.
  251. */
  252. if (prv)
  253. table->nents = ++table->orig_nents;
  254. return -ENOMEM;
  255. }
  256. sg_init_table(sg, alloc_size);
  257. table->nents = table->orig_nents += sg_size;
  258. /*
  259. * If this is the first mapping, assign the sg table header.
  260. * If this is not the first mapping, chain previous part.
  261. */
  262. if (prv)
  263. sg_chain(prv, max_ents, sg);
  264. else
  265. table->sgl = sg;
  266. /*
  267. * If no more entries after this one, mark the end
  268. */
  269. if (!left)
  270. sg_mark_end(&sg[sg_size - 1]);
  271. prv = sg;
  272. } while (left);
  273. return 0;
  274. }
  275. EXPORT_SYMBOL(__sg_alloc_table);
  276. /**
  277. * sg_alloc_table - Allocate and initialize an sg table
  278. * @table: The sg table header to use
  279. * @nents: Number of entries in sg list
  280. * @gfp_mask: GFP allocation mask
  281. *
  282. * Description:
  283. * Allocate and initialize an sg table. If @nents@ is larger than
  284. * SG_MAX_SINGLE_ALLOC a chained sg table will be setup.
  285. *
  286. **/
  287. int sg_alloc_table(struct sg_table *table, unsigned int nents, gfp_t gfp_mask)
  288. {
  289. int ret;
  290. ret = __sg_alloc_table(table, nents, SG_MAX_SINGLE_ALLOC,
  291. gfp_mask, sg_kmalloc);
  292. if (unlikely(ret))
  293. __sg_free_table(table, SG_MAX_SINGLE_ALLOC, sg_kfree);
  294. return ret;
  295. }
  296. EXPORT_SYMBOL(sg_alloc_table);
  297. /**
  298. * sg_alloc_table_from_pages - Allocate and initialize an sg table from
  299. * an array of pages
  300. * @sgt: The sg table header to use
  301. * @pages: Pointer to an array of page pointers
  302. * @n_pages: Number of pages in the pages array
  303. * @offset: Offset from start of the first page to the start of a buffer
  304. * @size: Number of valid bytes in the buffer (after offset)
  305. * @gfp_mask: GFP allocation mask
  306. *
  307. * Description:
  308. * Allocate and initialize an sg table from a list of pages. Contiguous
  309. * ranges of the pages are squashed into a single scatterlist node. A user
  310. * may provide an offset at a start and a size of valid data in a buffer
  311. * specified by the page array. The returned sg table is released by
  312. * sg_free_table.
  313. *
  314. * Returns:
  315. * 0 on success, negative error on failure
  316. */
  317. int sg_alloc_table_from_pages(struct sg_table *sgt,
  318. struct page **pages, unsigned int n_pages,
  319. unsigned long offset, unsigned long size,
  320. gfp_t gfp_mask)
  321. {
  322. unsigned int chunks;
  323. unsigned int i;
  324. unsigned int cur_page;
  325. int ret;
  326. struct scatterlist *s;
  327. /* compute number of contiguous chunks */
  328. chunks = 1;
  329. for (i = 1; i < n_pages; ++i)
  330. if (page_to_pfn(pages[i]) != page_to_pfn(pages[i - 1]) + 1)
  331. ++chunks;
  332. ret = sg_alloc_table(sgt, chunks, gfp_mask);
  333. if (unlikely(ret))
  334. return ret;
  335. /* merging chunks and putting them into the scatterlist */
  336. cur_page = 0;
  337. for_each_sg(sgt->sgl, s, sgt->orig_nents, i) {
  338. unsigned long chunk_size;
  339. unsigned int j;
  340. /* look for the end of the current chunk */
  341. for (j = cur_page + 1; j < n_pages; ++j)
  342. if (page_to_pfn(pages[j]) !=
  343. page_to_pfn(pages[j - 1]) + 1)
  344. break;
  345. chunk_size = ((j - cur_page) << PAGE_SHIFT) - offset;
  346. sg_set_page(s, pages[cur_page], min(size, chunk_size), offset);
  347. size -= chunk_size;
  348. offset = 0;
  349. cur_page = j;
  350. }
  351. return 0;
  352. }
  353. EXPORT_SYMBOL(sg_alloc_table_from_pages);
  354. /**
  355. * sg_miter_start - start mapping iteration over a sg list
  356. * @miter: sg mapping iter to be started
  357. * @sgl: sg list to iterate over
  358. * @nents: number of sg entries
  359. *
  360. * Description:
  361. * Starts mapping iterator @miter.
  362. *
  363. * Context:
  364. * Don't care.
  365. */
  366. void sg_miter_start(struct sg_mapping_iter *miter, struct scatterlist *sgl,
  367. unsigned int nents, unsigned int flags)
  368. {
  369. memset(miter, 0, sizeof(struct sg_mapping_iter));
  370. miter->__sg = sgl;
  371. miter->__nents = nents;
  372. miter->__offset = 0;
  373. WARN_ON(!(flags & (SG_MITER_TO_SG | SG_MITER_FROM_SG)));
  374. miter->__flags = flags;
  375. }
  376. EXPORT_SYMBOL(sg_miter_start);
  377. /**
  378. * sg_miter_next - proceed mapping iterator to the next mapping
  379. * @miter: sg mapping iter to proceed
  380. *
  381. * Description:
  382. * Proceeds @miter to the next mapping. @miter should have been started
  383. * using sg_miter_start(). On successful return, @miter->page,
  384. * @miter->addr and @miter->length point to the current mapping.
  385. *
  386. * Context:
  387. * Preemption disabled if SG_MITER_ATOMIC. Preemption must stay disabled
  388. * till @miter is stopped. May sleep if !SG_MITER_ATOMIC.
  389. *
  390. * Returns:
  391. * true if @miter contains the next mapping. false if end of sg
  392. * list is reached.
  393. */
  394. bool sg_miter_next(struct sg_mapping_iter *miter)
  395. {
  396. unsigned int off, len;
  397. /* check for end and drop resources from the last iteration */
  398. if (!miter->__nents)
  399. return false;
  400. sg_miter_stop(miter);
  401. /* get to the next sg if necessary. __offset is adjusted by stop */
  402. while (miter->__offset == miter->__sg->length) {
  403. if (--miter->__nents) {
  404. miter->__sg = sg_next(miter->__sg);
  405. miter->__offset = 0;
  406. } else
  407. return false;
  408. }
  409. /* map the next page */
  410. off = miter->__sg->offset + miter->__offset;
  411. len = miter->__sg->length - miter->__offset;
  412. miter->page = nth_page(sg_page(miter->__sg), off >> PAGE_SHIFT);
  413. off &= ~PAGE_MASK;
  414. miter->length = min_t(unsigned int, len, PAGE_SIZE - off);
  415. miter->consumed = miter->length;
  416. if (miter->__flags & SG_MITER_ATOMIC)
  417. miter->addr = kmap_atomic(miter->page) + off;
  418. else
  419. miter->addr = kmap(miter->page) + off;
  420. return true;
  421. }
  422. EXPORT_SYMBOL(sg_miter_next);
  423. /**
  424. * sg_miter_stop - stop mapping iteration
  425. * @miter: sg mapping iter to be stopped
  426. *
  427. * Description:
  428. * Stops mapping iterator @miter. @miter should have been started
  429. * started using sg_miter_start(). A stopped iteration can be
  430. * resumed by calling sg_miter_next() on it. This is useful when
  431. * resources (kmap) need to be released during iteration.
  432. *
  433. * Context:
  434. * Preemption disabled if the SG_MITER_ATOMIC is set. Don't care
  435. * otherwise.
  436. */
  437. void sg_miter_stop(struct sg_mapping_iter *miter)
  438. {
  439. WARN_ON(miter->consumed > miter->length);
  440. /* drop resources from the last iteration */
  441. if (miter->addr) {
  442. miter->__offset += miter->consumed;
  443. if (miter->__flags & SG_MITER_TO_SG)
  444. flush_kernel_dcache_page(miter->page);
  445. if (miter->__flags & SG_MITER_ATOMIC) {
  446. WARN_ON_ONCE(preemptible());
  447. kunmap_atomic(miter->addr);
  448. } else
  449. kunmap(miter->page);
  450. miter->page = NULL;
  451. miter->addr = NULL;
  452. miter->length = 0;
  453. miter->consumed = 0;
  454. }
  455. }
  456. EXPORT_SYMBOL(sg_miter_stop);
  457. /**
  458. * sg_copy_buffer - Copy data between a linear buffer and an SG list
  459. * @sgl: The SG list
  460. * @nents: Number of SG entries
  461. * @buf: Where to copy from
  462. * @buflen: The number of bytes to copy
  463. * @to_buffer: transfer direction (non zero == from an sg list to a
  464. * buffer, 0 == from a buffer to an sg list
  465. *
  466. * Returns the number of copied bytes.
  467. *
  468. **/
  469. static size_t sg_copy_buffer(struct scatterlist *sgl, unsigned int nents,
  470. void *buf, size_t buflen, int to_buffer)
  471. {
  472. unsigned int offset = 0;
  473. struct sg_mapping_iter miter;
  474. unsigned long flags;
  475. unsigned int sg_flags = SG_MITER_ATOMIC;
  476. if (to_buffer)
  477. sg_flags |= SG_MITER_FROM_SG;
  478. else
  479. sg_flags |= SG_MITER_TO_SG;
  480. sg_miter_start(&miter, sgl, nents, sg_flags);
  481. local_irq_save(flags);
  482. while (sg_miter_next(&miter) && offset < buflen) {
  483. unsigned int len;
  484. len = min(miter.length, buflen - offset);
  485. if (to_buffer)
  486. memcpy(buf + offset, miter.addr, len);
  487. else
  488. memcpy(miter.addr, buf + offset, len);
  489. offset += len;
  490. }
  491. sg_miter_stop(&miter);
  492. local_irq_restore(flags);
  493. return offset;
  494. }
  495. /**
  496. * sg_copy_from_buffer - Copy from a linear buffer to an SG list
  497. * @sgl: The SG list
  498. * @nents: Number of SG entries
  499. * @buf: Where to copy from
  500. * @buflen: The number of bytes to copy
  501. *
  502. * Returns the number of copied bytes.
  503. *
  504. **/
  505. size_t sg_copy_from_buffer(struct scatterlist *sgl, unsigned int nents,
  506. void *buf, size_t buflen)
  507. {
  508. return sg_copy_buffer(sgl, nents, buf, buflen, 0);
  509. }
  510. EXPORT_SYMBOL(sg_copy_from_buffer);
  511. /**
  512. * sg_copy_to_buffer - Copy from an SG list to a linear buffer
  513. * @sgl: The SG list
  514. * @nents: Number of SG entries
  515. * @buf: Where to copy to
  516. * @buflen: The number of bytes to copy
  517. *
  518. * Returns the number of copied bytes.
  519. *
  520. **/
  521. size_t sg_copy_to_buffer(struct scatterlist *sgl, unsigned int nents,
  522. void *buf, size_t buflen)
  523. {
  524. return sg_copy_buffer(sgl, nents, buf, buflen, 1);
  525. }
  526. EXPORT_SYMBOL(sg_copy_to_buffer);