0001
0002
0003
0004
0005
0006 #include <linux/mm.h>
0007 #include <linux/sched/signal.h>
0008 #include <linux/device.h>
0009 #include <linux/module.h>
0010
0011 #include "hfi.h"
0012
0013 static unsigned long cache_size = 256;
0014 module_param(cache_size, ulong, S_IRUGO | S_IWUSR);
0015 MODULE_PARM_DESC(cache_size, "Send and receive side cache size limit (in MB)");
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029 bool hfi1_can_pin_pages(struct hfi1_devdata *dd, struct mm_struct *mm,
0030 u32 nlocked, u32 npages)
0031 {
0032 unsigned long ulimit = rlimit(RLIMIT_MEMLOCK), pinned, cache_limit,
0033 size = (cache_size * (1UL << 20));
0034 unsigned int usr_ctxts =
0035 dd->num_rcv_contexts - dd->first_dyn_alloc_ctxt;
0036 bool can_lock = capable(CAP_IPC_LOCK);
0037
0038
0039
0040
0041
0042
0043 cache_limit = (ulimit / usr_ctxts) / 4;
0044
0045
0046 if (ulimit != (-1UL) && size > cache_limit)
0047 size = cache_limit;
0048
0049
0050 size = DIV_ROUND_UP(size, PAGE_SIZE);
0051
0052 pinned = atomic64_read(&mm->pinned_vm);
0053
0054
0055 if (pinned + npages >= ulimit && !can_lock)
0056 return false;
0057
0058 return ((nlocked + npages) <= size) || can_lock;
0059 }
0060
0061 int hfi1_acquire_user_pages(struct mm_struct *mm, unsigned long vaddr, size_t npages,
0062 bool writable, struct page **pages)
0063 {
0064 int ret;
0065 unsigned int gup_flags = FOLL_LONGTERM | (writable ? FOLL_WRITE : 0);
0066
0067 ret = pin_user_pages_fast(vaddr, npages, gup_flags, pages);
0068 if (ret < 0)
0069 return ret;
0070
0071 atomic64_add(ret, &mm->pinned_vm);
0072
0073 return ret;
0074 }
0075
0076 void hfi1_release_user_pages(struct mm_struct *mm, struct page **p,
0077 size_t npages, bool dirty)
0078 {
0079 unpin_user_pages_dirty_lock(p, npages, dirty);
0080
0081 if (mm) {
0082 atomic64_sub(npages, &mm->pinned_vm);
0083 }
0084 }