orangefs-bufmap.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * (C) 2001 Clemson University and The University of Chicago
  4. *
  5. * See COPYING in top-level directory.
  6. */
  7. #include "protocol.h"
  8. #include "orangefs-kernel.h"
  9. #include "orangefs-bufmap.h"
  10. struct slot_map {
  11. int c;
  12. wait_queue_head_t q;
  13. int count;
  14. unsigned long *map;
  15. };
  16. static struct slot_map rw_map = {
  17. .c = -1,
  18. .q = __WAIT_QUEUE_HEAD_INITIALIZER(rw_map.q)
  19. };
  20. static struct slot_map readdir_map = {
  21. .c = -1,
  22. .q = __WAIT_QUEUE_HEAD_INITIALIZER(readdir_map.q)
  23. };
  24. static void install(struct slot_map *m, int count, unsigned long *map)
  25. {
  26. spin_lock(&m->q.lock);
  27. m->c = m->count = count;
  28. m->map = map;
  29. wake_up_all_locked(&m->q);
  30. spin_unlock(&m->q.lock);
  31. }
  32. static void mark_killed(struct slot_map *m)
  33. {
  34. spin_lock(&m->q.lock);
  35. m->c -= m->count + 1;
  36. spin_unlock(&m->q.lock);
  37. }
  38. static void run_down(struct slot_map *m)
  39. {
  40. DEFINE_WAIT(wait);
  41. spin_lock(&m->q.lock);
  42. if (m->c != -1) {
  43. for (;;) {
  44. if (likely(list_empty(&wait.entry)))
  45. __add_wait_queue_entry_tail(&m->q, &wait);
  46. set_current_state(TASK_UNINTERRUPTIBLE);
  47. if (m->c == -1)
  48. break;
  49. spin_unlock(&m->q.lock);
  50. schedule();
  51. spin_lock(&m->q.lock);
  52. }
  53. __remove_wait_queue(&m->q, &wait);
  54. __set_current_state(TASK_RUNNING);
  55. }
  56. m->map = NULL;
  57. spin_unlock(&m->q.lock);
  58. }
  59. static void put(struct slot_map *m, int slot)
  60. {
  61. int v;
  62. spin_lock(&m->q.lock);
  63. __clear_bit(slot, m->map);
  64. v = ++m->c;
  65. if (v > 0)
  66. wake_up_locked(&m->q);
  67. if (unlikely(v == -1)) /* finished dying */
  68. wake_up_all_locked(&m->q);
  69. spin_unlock(&m->q.lock);
  70. }
  71. static int wait_for_free(struct slot_map *m)
  72. {
  73. long left = slot_timeout_secs * HZ;
  74. DEFINE_WAIT(wait);
  75. do {
  76. long n = left, t;
  77. if (likely(list_empty(&wait.entry)))
  78. __add_wait_queue_entry_tail_exclusive(&m->q, &wait);
  79. set_current_state(TASK_INTERRUPTIBLE);
  80. if (m->c > 0)
  81. break;
  82. if (m->c < 0) {
  83. /* we are waiting for map to be installed */
  84. /* it would better be there soon, or we go away */
  85. if (n > ORANGEFS_BUFMAP_WAIT_TIMEOUT_SECS * HZ)
  86. n = ORANGEFS_BUFMAP_WAIT_TIMEOUT_SECS * HZ;
  87. }
  88. spin_unlock(&m->q.lock);
  89. t = schedule_timeout(n);
  90. spin_lock(&m->q.lock);
  91. if (unlikely(!t) && n != left && m->c < 0)
  92. left = t;
  93. else
  94. left = t + (left - n);
  95. if (unlikely(signal_pending(current)))
  96. left = -EINTR;
  97. } while (left > 0);
  98. if (!list_empty(&wait.entry))
  99. list_del(&wait.entry);
  100. else if (left <= 0 && waitqueue_active(&m->q))
  101. __wake_up_locked_key(&m->q, TASK_INTERRUPTIBLE, NULL);
  102. __set_current_state(TASK_RUNNING);
  103. if (likely(left > 0))
  104. return 0;
  105. return left < 0 ? -EINTR : -ETIMEDOUT;
  106. }
  107. static int get(struct slot_map *m)
  108. {
  109. int res = 0;
  110. spin_lock(&m->q.lock);
  111. if (unlikely(m->c <= 0))
  112. res = wait_for_free(m);
  113. if (likely(!res)) {
  114. m->c--;
  115. res = find_first_zero_bit(m->map, m->count);
  116. __set_bit(res, m->map);
  117. }
  118. spin_unlock(&m->q.lock);
  119. return res;
  120. }
  121. /* used to describe mapped buffers */
  122. struct orangefs_bufmap_desc {
  123. void __user *uaddr; /* user space address pointer */
  124. struct page **page_array; /* array of mapped pages */
  125. int array_count; /* size of above arrays */
  126. struct list_head list_link;
  127. };
  128. static struct orangefs_bufmap {
  129. int desc_size;
  130. int desc_shift;
  131. int desc_count;
  132. int total_size;
  133. int page_count;
  134. struct page **page_array;
  135. struct orangefs_bufmap_desc *desc_array;
  136. /* array to track usage of buffer descriptors */
  137. unsigned long *buffer_index_array;
  138. /* array to track usage of buffer descriptors for readdir */
  139. #define N DIV_ROUND_UP(ORANGEFS_READDIR_DEFAULT_DESC_COUNT, BITS_PER_LONG)
  140. unsigned long readdir_index_array[N];
  141. #undef N
  142. } *__orangefs_bufmap;
  143. static DEFINE_SPINLOCK(orangefs_bufmap_lock);
  144. static void
  145. orangefs_bufmap_unmap(struct orangefs_bufmap *bufmap)
  146. {
  147. int i;
  148. for (i = 0; i < bufmap->page_count; i++)
  149. put_page(bufmap->page_array[i]);
  150. }
  151. static void
  152. orangefs_bufmap_free(struct orangefs_bufmap *bufmap)
  153. {
  154. kfree(bufmap->page_array);
  155. kfree(bufmap->desc_array);
  156. kfree(bufmap->buffer_index_array);
  157. kfree(bufmap);
  158. }
  159. /*
  160. * XXX: Can the size and shift change while the caller gives up the
  161. * XXX: lock between calling this and doing something useful?
  162. */
  163. int orangefs_bufmap_size_query(void)
  164. {
  165. struct orangefs_bufmap *bufmap;
  166. int size = 0;
  167. spin_lock(&orangefs_bufmap_lock);
  168. bufmap = __orangefs_bufmap;
  169. if (bufmap)
  170. size = bufmap->desc_size;
  171. spin_unlock(&orangefs_bufmap_lock);
  172. return size;
  173. }
  174. int orangefs_bufmap_shift_query(void)
  175. {
  176. struct orangefs_bufmap *bufmap;
  177. int shift = 0;
  178. spin_lock(&orangefs_bufmap_lock);
  179. bufmap = __orangefs_bufmap;
  180. if (bufmap)
  181. shift = bufmap->desc_shift;
  182. spin_unlock(&orangefs_bufmap_lock);
  183. return shift;
  184. }
  185. static DECLARE_WAIT_QUEUE_HEAD(bufmap_waitq);
  186. static DECLARE_WAIT_QUEUE_HEAD(readdir_waitq);
  187. static struct orangefs_bufmap *
  188. orangefs_bufmap_alloc(struct ORANGEFS_dev_map_desc *user_desc)
  189. {
  190. struct orangefs_bufmap *bufmap;
  191. bufmap = kzalloc(sizeof(*bufmap), GFP_KERNEL);
  192. if (!bufmap)
  193. goto out;
  194. bufmap->total_size = user_desc->total_size;
  195. bufmap->desc_count = user_desc->count;
  196. bufmap->desc_size = user_desc->size;
  197. bufmap->desc_shift = ilog2(bufmap->desc_size);
  198. bufmap->buffer_index_array =
  199. kzalloc(DIV_ROUND_UP(bufmap->desc_count, BITS_PER_LONG), GFP_KERNEL);
  200. if (!bufmap->buffer_index_array)
  201. goto out_free_bufmap;
  202. bufmap->desc_array =
  203. kcalloc(bufmap->desc_count, sizeof(struct orangefs_bufmap_desc),
  204. GFP_KERNEL);
  205. if (!bufmap->desc_array)
  206. goto out_free_index_array;
  207. bufmap->page_count = bufmap->total_size / PAGE_SIZE;
  208. /* allocate storage to track our page mappings */
  209. bufmap->page_array =
  210. kcalloc(bufmap->page_count, sizeof(struct page *), GFP_KERNEL);
  211. if (!bufmap->page_array)
  212. goto out_free_desc_array;
  213. return bufmap;
  214. out_free_desc_array:
  215. kfree(bufmap->desc_array);
  216. out_free_index_array:
  217. kfree(bufmap->buffer_index_array);
  218. out_free_bufmap:
  219. kfree(bufmap);
  220. out:
  221. return NULL;
  222. }
  223. static int
  224. orangefs_bufmap_map(struct orangefs_bufmap *bufmap,
  225. struct ORANGEFS_dev_map_desc *user_desc)
  226. {
  227. int pages_per_desc = bufmap->desc_size / PAGE_SIZE;
  228. int offset = 0, ret, i;
  229. /* map the pages */
  230. ret = get_user_pages_fast((unsigned long)user_desc->ptr,
  231. bufmap->page_count, 1, bufmap->page_array);
  232. if (ret < 0)
  233. return ret;
  234. if (ret != bufmap->page_count) {
  235. gossip_err("orangefs error: asked for %d pages, only got %d.\n",
  236. bufmap->page_count, ret);
  237. for (i = 0; i < ret; i++) {
  238. SetPageError(bufmap->page_array[i]);
  239. put_page(bufmap->page_array[i]);
  240. }
  241. return -ENOMEM;
  242. }
  243. /*
  244. * ideally we want to get kernel space pointers for each page, but
  245. * we can't kmap that many pages at once if highmem is being used.
  246. * so instead, we just kmap/kunmap the page address each time the
  247. * kaddr is needed.
  248. */
  249. for (i = 0; i < bufmap->page_count; i++)
  250. flush_dcache_page(bufmap->page_array[i]);
  251. /* build a list of available descriptors */
  252. for (offset = 0, i = 0; i < bufmap->desc_count; i++) {
  253. bufmap->desc_array[i].page_array = &bufmap->page_array[offset];
  254. bufmap->desc_array[i].array_count = pages_per_desc;
  255. bufmap->desc_array[i].uaddr =
  256. (user_desc->ptr + (i * pages_per_desc * PAGE_SIZE));
  257. offset += pages_per_desc;
  258. }
  259. return 0;
  260. }
  261. /*
  262. * orangefs_bufmap_initialize()
  263. *
  264. * initializes the mapped buffer interface
  265. *
  266. * returns 0 on success, -errno on failure
  267. */
  268. int orangefs_bufmap_initialize(struct ORANGEFS_dev_map_desc *user_desc)
  269. {
  270. struct orangefs_bufmap *bufmap;
  271. int ret = -EINVAL;
  272. gossip_debug(GOSSIP_BUFMAP_DEBUG,
  273. "orangefs_bufmap_initialize: called (ptr ("
  274. "%p) sz (%d) cnt(%d).\n",
  275. user_desc->ptr,
  276. user_desc->size,
  277. user_desc->count);
  278. if (user_desc->total_size < 0 ||
  279. user_desc->size < 0 ||
  280. user_desc->count < 0)
  281. goto out;
  282. /*
  283. * sanity check alignment and size of buffer that caller wants to
  284. * work with
  285. */
  286. if (PAGE_ALIGN((unsigned long)user_desc->ptr) !=
  287. (unsigned long)user_desc->ptr) {
  288. gossip_err("orangefs error: memory alignment (front). %p\n",
  289. user_desc->ptr);
  290. goto out;
  291. }
  292. if (PAGE_ALIGN(((unsigned long)user_desc->ptr + user_desc->total_size))
  293. != (unsigned long)(user_desc->ptr + user_desc->total_size)) {
  294. gossip_err("orangefs error: memory alignment (back).(%p + %d)\n",
  295. user_desc->ptr,
  296. user_desc->total_size);
  297. goto out;
  298. }
  299. if (user_desc->total_size != (user_desc->size * user_desc->count)) {
  300. gossip_err("orangefs error: user provided an oddly sized buffer: (%d, %d, %d)\n",
  301. user_desc->total_size,
  302. user_desc->size,
  303. user_desc->count);
  304. goto out;
  305. }
  306. if ((user_desc->size % PAGE_SIZE) != 0) {
  307. gossip_err("orangefs error: bufmap size not page size divisible (%d).\n",
  308. user_desc->size);
  309. goto out;
  310. }
  311. ret = -ENOMEM;
  312. bufmap = orangefs_bufmap_alloc(user_desc);
  313. if (!bufmap)
  314. goto out;
  315. ret = orangefs_bufmap_map(bufmap, user_desc);
  316. if (ret)
  317. goto out_free_bufmap;
  318. spin_lock(&orangefs_bufmap_lock);
  319. if (__orangefs_bufmap) {
  320. spin_unlock(&orangefs_bufmap_lock);
  321. gossip_err("orangefs: error: bufmap already initialized.\n");
  322. ret = -EINVAL;
  323. goto out_unmap_bufmap;
  324. }
  325. __orangefs_bufmap = bufmap;
  326. install(&rw_map,
  327. bufmap->desc_count,
  328. bufmap->buffer_index_array);
  329. install(&readdir_map,
  330. ORANGEFS_READDIR_DEFAULT_DESC_COUNT,
  331. bufmap->readdir_index_array);
  332. spin_unlock(&orangefs_bufmap_lock);
  333. gossip_debug(GOSSIP_BUFMAP_DEBUG,
  334. "orangefs_bufmap_initialize: exiting normally\n");
  335. return 0;
  336. out_unmap_bufmap:
  337. orangefs_bufmap_unmap(bufmap);
  338. out_free_bufmap:
  339. orangefs_bufmap_free(bufmap);
  340. out:
  341. return ret;
  342. }
  343. /*
  344. * orangefs_bufmap_finalize()
  345. *
  346. * shuts down the mapped buffer interface and releases any resources
  347. * associated with it
  348. *
  349. * no return value
  350. */
  351. void orangefs_bufmap_finalize(void)
  352. {
  353. struct orangefs_bufmap *bufmap = __orangefs_bufmap;
  354. if (!bufmap)
  355. return;
  356. gossip_debug(GOSSIP_BUFMAP_DEBUG, "orangefs_bufmap_finalize: called\n");
  357. mark_killed(&rw_map);
  358. mark_killed(&readdir_map);
  359. gossip_debug(GOSSIP_BUFMAP_DEBUG,
  360. "orangefs_bufmap_finalize: exiting normally\n");
  361. }
  362. void orangefs_bufmap_run_down(void)
  363. {
  364. struct orangefs_bufmap *bufmap = __orangefs_bufmap;
  365. if (!bufmap)
  366. return;
  367. run_down(&rw_map);
  368. run_down(&readdir_map);
  369. spin_lock(&orangefs_bufmap_lock);
  370. __orangefs_bufmap = NULL;
  371. spin_unlock(&orangefs_bufmap_lock);
  372. orangefs_bufmap_unmap(bufmap);
  373. orangefs_bufmap_free(bufmap);
  374. }
  375. /*
  376. * orangefs_bufmap_get()
  377. *
  378. * gets a free mapped buffer descriptor, will sleep until one becomes
  379. * available if necessary
  380. *
  381. * returns slot on success, -errno on failure
  382. */
  383. int orangefs_bufmap_get(void)
  384. {
  385. return get(&rw_map);
  386. }
  387. /*
  388. * orangefs_bufmap_put()
  389. *
  390. * returns a mapped buffer descriptor to the collection
  391. *
  392. * no return value
  393. */
  394. void orangefs_bufmap_put(int buffer_index)
  395. {
  396. put(&rw_map, buffer_index);
  397. }
  398. /*
  399. * orangefs_readdir_index_get()
  400. *
  401. * gets a free descriptor, will sleep until one becomes
  402. * available if necessary.
  403. * Although the readdir buffers are not mapped into kernel space
  404. * we could do that at a later point of time. Regardless, these
  405. * indices are used by the client-core.
  406. *
  407. * returns slot on success, -errno on failure
  408. */
  409. int orangefs_readdir_index_get(void)
  410. {
  411. return get(&readdir_map);
  412. }
  413. void orangefs_readdir_index_put(int buffer_index)
  414. {
  415. put(&readdir_map, buffer_index);
  416. }
  417. /*
  418. * we've been handed an iovec, we need to copy it to
  419. * the shared memory descriptor at "buffer_index".
  420. */
  421. int orangefs_bufmap_copy_from_iovec(struct iov_iter *iter,
  422. int buffer_index,
  423. size_t size)
  424. {
  425. struct orangefs_bufmap_desc *to;
  426. int i;
  427. gossip_debug(GOSSIP_BUFMAP_DEBUG,
  428. "%s: buffer_index:%d: size:%zu:\n",
  429. __func__, buffer_index, size);
  430. to = &__orangefs_bufmap->desc_array[buffer_index];
  431. for (i = 0; size; i++) {
  432. struct page *page = to->page_array[i];
  433. size_t n = size;
  434. if (n > PAGE_SIZE)
  435. n = PAGE_SIZE;
  436. if (copy_page_from_iter(page, 0, n, iter) != n)
  437. return -EFAULT;
  438. size -= n;
  439. }
  440. return 0;
  441. }
  442. /*
  443. * we've been handed an iovec, we need to fill it from
  444. * the shared memory descriptor at "buffer_index".
  445. */
  446. int orangefs_bufmap_copy_to_iovec(struct iov_iter *iter,
  447. int buffer_index,
  448. size_t size)
  449. {
  450. struct orangefs_bufmap_desc *from;
  451. int i;
  452. from = &__orangefs_bufmap->desc_array[buffer_index];
  453. gossip_debug(GOSSIP_BUFMAP_DEBUG,
  454. "%s: buffer_index:%d: size:%zu:\n",
  455. __func__, buffer_index, size);
  456. for (i = 0; size; i++) {
  457. struct page *page = from->page_array[i];
  458. size_t n = size;
  459. if (n > PAGE_SIZE)
  460. n = PAGE_SIZE;
  461. n = copy_page_to_iter(page, 0, n, iter);
  462. if (!n)
  463. return -EFAULT;
  464. size -= n;
  465. }
  466. return 0;
  467. }