blob: 20f4ddcac3b0f1c78d26c4ede74a5645f71bb641 [file] [log] [blame]
Mike Marciniszyn77241052015-07-30 15:17:43 -04001/*
Jubin John05d6ac12016-02-14 20:22:17 -08002 * Copyright(c) 2015, 2016 Intel Corporation.
Mike Marciniszyn77241052015-07-30 15:17:43 -04003 *
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 *
Mike Marciniszyn77241052015-07-30 15:17:43 -04009 * 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 *
Mike Marciniszyn77241052015-07-30 15:17:43 -040020 * 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
48#include <linux/mm.h>
Mitko Haralanovdef82282015-12-08 17:10:09 -050049#include <linux/sched.h>
Mike Marciniszyn77241052015-07-30 15:17:43 -040050#include <linux/device.h>
Mitko Haralanov2c97ce42016-03-08 11:15:28 -080051#include <linux/module.h>
Mike Marciniszyn77241052015-07-30 15:17:43 -040052
53#include "hfi.h"
54
Mitko Haralanov2c97ce42016-03-08 11:15:28 -080055static unsigned long cache_size = 256;
56module_param(cache_size, ulong, S_IRUGO | S_IWUSR);
57MODULE_PARM_DESC(cache_size, "Send and receive side cache size limit (in MB)");
58
59/*
60 * Determine whether the caller can pin pages.
61 *
62 * This function should be used in the implementation of buffer caches.
63 * The cache implementation should call this function prior to attempting
64 * to pin buffer pages in order to determine whether they should do so.
65 * The function computes cache limits based on the configured ulimit and
66 * cache size. Use of this function is especially important for caches
67 * which are not limited in any other way (e.g. by HW resources) and, thus,
68 * could keeping caching buffers.
69 *
70 */
Ira Weiny3faa3d92016-07-28 15:21:19 -040071bool hfi1_can_pin_pages(struct hfi1_devdata *dd, struct mm_struct *mm,
72 u32 nlocked, u32 npages)
Mike Marciniszyn77241052015-07-30 15:17:43 -040073{
Mitko Haralanov2c97ce42016-03-08 11:15:28 -080074 unsigned long ulimit = rlimit(RLIMIT_MEMLOCK), pinned, cache_limit,
75 size = (cache_size * (1UL << 20)); /* convert to bytes */
76 unsigned usr_ctxts = dd->num_rcv_contexts - dd->first_user_ctxt;
Mitko Haralanovdef82282015-12-08 17:10:09 -050077 bool can_lock = capable(CAP_IPC_LOCK);
Mitko Haralanov2c97ce42016-03-08 11:15:28 -080078
79 /*
80 * Calculate per-cache size. The calculation below uses only a quarter
81 * of the available per-context limit. This leaves space for other
82 * pinning. Should we worry about shared ctxts?
83 */
84 cache_limit = (ulimit / usr_ctxts) / 4;
85
86 /* If ulimit isn't set to "unlimited" and is smaller than cache_size. */
87 if (ulimit != (-1UL) && size > cache_limit)
88 size = cache_limit;
89
90 /* Convert to number of pages */
91 size = DIV_ROUND_UP(size, PAGE_SIZE);
Mike Marciniszyn77241052015-07-30 15:17:43 -040092
Ira Weiny3faa3d92016-07-28 15:21:19 -040093 down_read(&mm->mmap_sem);
94 pinned = mm->pinned_vm;
95 up_read(&mm->mmap_sem);
Mitko Haralanovdef82282015-12-08 17:10:09 -050096
Mitko Haralanov2c97ce42016-03-08 11:15:28 -080097 /* First, check the absolute limit against all pinned pages. */
98 if (pinned + npages >= ulimit && !can_lock)
99 return false;
100
101 return ((nlocked + npages) <= size) || can_lock;
102}
103
Ira Weiny3faa3d92016-07-28 15:21:19 -0400104int hfi1_acquire_user_pages(struct mm_struct *mm, unsigned long vaddr, size_t npages,
105 bool writable, struct page **pages)
Mitko Haralanov2c97ce42016-03-08 11:15:28 -0800106{
107 int ret;
Mitko Haralanovdef82282015-12-08 17:10:09 -0500108
109 ret = get_user_pages_fast(vaddr, npages, writable, pages);
110 if (ret < 0)
111 return ret;
112
Ira Weiny3faa3d92016-07-28 15:21:19 -0400113 down_write(&mm->mmap_sem);
114 mm->pinned_vm += ret;
115 up_write(&mm->mmap_sem);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400116
117 return ret;
118}
119
Mitko Haralanovbd3a8942016-03-08 11:15:33 -0800120void hfi1_release_user_pages(struct mm_struct *mm, struct page **p,
121 size_t npages, bool dirty)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400122{
Mitko Haralanovdef82282015-12-08 17:10:09 -0500123 size_t i;
124
125 for (i = 0; i < npages; i++) {
126 if (dirty)
127 set_page_dirty_lock(p[i]);
128 put_page(p[i]);
129 }
130
Mitko Haralanovbd3a8942016-03-08 11:15:33 -0800131 if (mm) { /* during close after signal, mm can be NULL */
132 down_write(&mm->mmap_sem);
133 mm->pinned_vm -= npages;
134 up_write(&mm->mmap_sem);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400135 }
136}