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