user_pages.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright(c) 2015-2017 Intel Corporation.
  3. *
  4. * This file is provided under a dual BSD/GPLv2 license. When using or
  5. * redistributing this file, you may do so under either license.
  6. *
  7. * GPL LICENSE SUMMARY
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * BSD LICENSE
  19. *
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted provided that the following conditions
  22. * are met:
  23. *
  24. * - Redistributions of source code must retain the above copyright
  25. * notice, this list of conditions and the following disclaimer.
  26. * - Redistributions in binary form must reproduce the above copyright
  27. * notice, this list of conditions and the following disclaimer in
  28. * the documentation and/or other materials provided with the
  29. * distribution.
  30. * - Neither the name of Intel Corporation nor the names of its
  31. * contributors may be used to endorse or promote products derived
  32. * from this software without specific prior written permission.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  35. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  36. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  37. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  38. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  39. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  40. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  41. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  42. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  43. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  44. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. *
  46. */
  47. #include <linux/mm.h>
  48. #include <linux/sched/signal.h>
  49. #include <linux/device.h>
  50. #include <linux/module.h>
  51. #include "hfi.h"
  52. static unsigned long cache_size = 256;
  53. module_param(cache_size, ulong, S_IRUGO | S_IWUSR);
  54. MODULE_PARM_DESC(cache_size, "Send and receive side cache size limit (in MB)");
  55. /*
  56. * Determine whether the caller can pin pages.
  57. *
  58. * This function should be used in the implementation of buffer caches.
  59. * The cache implementation should call this function prior to attempting
  60. * to pin buffer pages in order to determine whether they should do so.
  61. * The function computes cache limits based on the configured ulimit and
  62. * cache size. Use of this function is especially important for caches
  63. * which are not limited in any other way (e.g. by HW resources) and, thus,
  64. * could keeping caching buffers.
  65. *
  66. */
  67. bool hfi1_can_pin_pages(struct hfi1_devdata *dd, struct mm_struct *mm,
  68. u32 nlocked, u32 npages)
  69. {
  70. unsigned long ulimit = rlimit(RLIMIT_MEMLOCK), pinned, cache_limit,
  71. size = (cache_size * (1UL << 20)); /* convert to bytes */
  72. unsigned int usr_ctxts =
  73. dd->num_rcv_contexts - dd->first_dyn_alloc_ctxt;
  74. bool can_lock = capable(CAP_IPC_LOCK);
  75. /*
  76. * Calculate per-cache size. The calculation below uses only a quarter
  77. * of the available per-context limit. This leaves space for other
  78. * pinning. Should we worry about shared ctxts?
  79. */
  80. cache_limit = (ulimit / usr_ctxts) / 4;
  81. /* If ulimit isn't set to "unlimited" and is smaller than cache_size. */
  82. if (ulimit != (-1UL) && size > cache_limit)
  83. size = cache_limit;
  84. /* Convert to number of pages */
  85. size = DIV_ROUND_UP(size, PAGE_SIZE);
  86. down_read(&mm->mmap_sem);
  87. pinned = mm->pinned_vm;
  88. up_read(&mm->mmap_sem);
  89. /* First, check the absolute limit against all pinned pages. */
  90. if (pinned + npages >= ulimit && !can_lock)
  91. return false;
  92. return ((nlocked + npages) <= size) || can_lock;
  93. }
  94. int hfi1_acquire_user_pages(struct mm_struct *mm, unsigned long vaddr, size_t npages,
  95. bool writable, struct page **pages)
  96. {
  97. int ret;
  98. ret = get_user_pages_fast(vaddr, npages, writable, pages);
  99. if (ret < 0)
  100. return ret;
  101. down_write(&mm->mmap_sem);
  102. mm->pinned_vm += ret;
  103. up_write(&mm->mmap_sem);
  104. return ret;
  105. }
  106. void hfi1_release_user_pages(struct mm_struct *mm, struct page **p,
  107. size_t npages, bool dirty)
  108. {
  109. size_t i;
  110. for (i = 0; i < npages; i++) {
  111. if (dirty)
  112. set_page_dirty_lock(p[i]);
  113. put_page(p[i]);
  114. }
  115. if (mm) { /* during close after signal, mm can be NULL */
  116. down_write(&mm->mmap_sem);
  117. mm->pinned_vm -= npages;
  118. up_write(&mm->mmap_sem);
  119. }
  120. }