user_pages.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // SPDX-License-Identifier: GPL-2.0 or BSD-3-Clause
  2. /*
  3. * Copyright(c) 2015-2017 Intel Corporation.
  4. */
  5. #include <linux/mm.h>
  6. #include <linux/sched/signal.h>
  7. #include <linux/device.h>
  8. #include <linux/module.h>
  9. #include "hfi.h"
  10. static unsigned long cache_size = 256;
  11. module_param(cache_size, ulong, S_IRUGO | S_IWUSR);
  12. MODULE_PARM_DESC(cache_size, "Send and receive side cache size limit (in MB)");
  13. /*
  14. * Determine whether the caller can pin pages.
  15. *
  16. * This function should be used in the implementation of buffer caches.
  17. * The cache implementation should call this function prior to attempting
  18. * to pin buffer pages in order to determine whether they should do so.
  19. * The function computes cache limits based on the configured ulimit and
  20. * cache size. Use of this function is especially important for caches
  21. * which are not limited in any other way (e.g. by HW resources) and, thus,
  22. * could keeping caching buffers.
  23. *
  24. */
  25. bool hfi1_can_pin_pages(struct hfi1_devdata *dd, struct mm_struct *mm,
  26. u32 nlocked, u32 npages)
  27. {
  28. unsigned long ulimit_pages;
  29. unsigned long cache_limit_pages;
  30. unsigned int usr_ctxts;
  31. /*
  32. * Perform RLIMIT_MEMLOCK based checks unless CAP_IPC_LOCK is present.
  33. */
  34. if (!capable(CAP_IPC_LOCK)) {
  35. ulimit_pages =
  36. DIV_ROUND_DOWN_ULL(rlimit(RLIMIT_MEMLOCK), PAGE_SIZE);
  37. /*
  38. * Pinning these pages would exceed this process's locked memory
  39. * limit.
  40. */
  41. if (atomic64_read(&mm->pinned_vm) + npages > ulimit_pages)
  42. return false;
  43. /*
  44. * Only allow 1/4 of the user's RLIMIT_MEMLOCK to be used for HFI
  45. * caches. This fraction is then equally distributed among all
  46. * existing user contexts. Note that if RLIMIT_MEMLOCK is
  47. * 'unlimited' (-1), the value of this limit will be > 2^42 pages
  48. * (2^64 / 2^12 / 2^8 / 2^2).
  49. *
  50. * The effectiveness of this check may be reduced if I/O occurs on
  51. * some user contexts before all user contexts are created. This
  52. * check assumes that this process is the only one using this
  53. * context (e.g., the corresponding fd was not passed to another
  54. * process for concurrent access) as there is no per-context,
  55. * per-process tracking of pinned pages. It also assumes that each
  56. * user context has only one cache to limit.
  57. */
  58. usr_ctxts = dd->num_rcv_contexts - dd->first_dyn_alloc_ctxt;
  59. if (nlocked + npages > (ulimit_pages / usr_ctxts / 4))
  60. return false;
  61. }
  62. /*
  63. * Pinning these pages would exceed the size limit for this cache.
  64. */
  65. cache_limit_pages = cache_size * (1024 * 1024) / PAGE_SIZE;
  66. if (nlocked + npages > cache_limit_pages)
  67. return false;
  68. return true;
  69. }
  70. int hfi1_acquire_user_pages(struct mm_struct *mm, unsigned long vaddr, size_t npages,
  71. bool writable, struct page **pages)
  72. {
  73. int ret;
  74. unsigned int gup_flags = FOLL_LONGTERM | (writable ? FOLL_WRITE : 0);
  75. ret = pin_user_pages_fast(vaddr, npages, gup_flags, pages);
  76. if (ret < 0)
  77. return ret;
  78. atomic64_add(ret, &mm->pinned_vm);
  79. return ret;
  80. }
  81. void hfi1_release_user_pages(struct mm_struct *mm, struct page **p,
  82. size_t npages, bool dirty)
  83. {
  84. unpin_user_pages_dirty_lock(p, npages, dirty);
  85. if (mm) { /* during close after signal, mm can be NULL */
  86. atomic64_sub(npages, &mm->pinned_vm);
  87. }
  88. }