Commit | Line | Data |
---|---|---|
7f510b46 | 1 | /* |
759d5768 | 2 | * Copyright (c) 2006 QLogic, Inc. All rights reserved. |
7f510b46 BS |
3 | * Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved. |
4 | * | |
5 | * This software is available to you under a choice of one of two | |
6 | * licenses. You may choose to be licensed under the terms of the GNU | |
7 | * General Public License (GPL) Version 2, available from the file | |
8 | * COPYING in the main directory of this source tree, or the | |
9 | * OpenIB.org BSD license below: | |
10 | * | |
11 | * Redistribution and use in source and binary forms, with or | |
12 | * without modification, are permitted provided that the following | |
13 | * conditions are met: | |
14 | * | |
15 | * - Redistributions of source code must retain the above | |
16 | * copyright notice, this list of conditions and the following | |
17 | * disclaimer. | |
18 | * | |
19 | * - Redistributions in binary form must reproduce the above | |
20 | * copyright notice, this list of conditions and the following | |
21 | * disclaimer in the documentation and/or other materials | |
22 | * provided with the distribution. | |
23 | * | |
24 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
25 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
26 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
27 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS | |
28 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | |
29 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
30 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
31 | * SOFTWARE. | |
32 | */ | |
33 | ||
34 | #include <linux/mm.h> | |
35 | #include <linux/device.h> | |
36 | ||
37 | #include "ipath_kernel.h" | |
38 | ||
39 | static void __ipath_release_user_pages(struct page **p, size_t num_pages, | |
40 | int dirty) | |
41 | { | |
42 | size_t i; | |
43 | ||
44 | for (i = 0; i < num_pages; i++) { | |
45 | ipath_cdbg(MM, "%lu/%lu put_page %p\n", (unsigned long) i, | |
46 | (unsigned long) num_pages, p[i]); | |
47 | if (dirty) | |
48 | set_page_dirty_lock(p[i]); | |
49 | put_page(p[i]); | |
50 | } | |
51 | } | |
52 | ||
53 | /* call with current->mm->mmap_sem held */ | |
54 | static int __get_user_pages(unsigned long start_page, size_t num_pages, | |
55 | struct page **p, struct vm_area_struct **vma) | |
56 | { | |
57 | unsigned long lock_limit; | |
58 | size_t got; | |
59 | int ret; | |
60 | ||
7f510b46 BS |
61 | lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur >> |
62 | PAGE_SHIFT; | |
63 | ||
64 | if (num_pages > lock_limit) { | |
65 | ret = -ENOMEM; | |
66 | goto bail; | |
67 | } | |
68 | ||
69 | ipath_cdbg(VERBOSE, "pin %lx pages from vaddr %lx\n", | |
70 | (unsigned long) num_pages, start_page); | |
71 | ||
72 | for (got = 0; got < num_pages; got += ret) { | |
73 | ret = get_user_pages(current, current->mm, | |
74 | start_page + got * PAGE_SIZE, | |
75 | num_pages - got, 1, 1, | |
76 | p + got, vma); | |
77 | if (ret < 0) | |
78 | goto bail_release; | |
79 | } | |
80 | ||
81 | current->mm->locked_vm += num_pages; | |
82 | ||
83 | ret = 0; | |
84 | goto bail; | |
85 | ||
86 | bail_release: | |
87 | __ipath_release_user_pages(p, got, 0); | |
88 | bail: | |
89 | return ret; | |
90 | } | |
91 | ||
92 | /** | |
93 | * ipath_get_user_pages - lock user pages into memory | |
94 | * @start_page: the start page | |
95 | * @num_pages: the number of pages | |
96 | * @p: the output page structures | |
97 | * | |
98 | * This function takes a given start page (page aligned user virtual | |
99 | * address) and pins it and the following specified number of pages. For | |
100 | * now, num_pages is always 1, but that will probably change at some point | |
101 | * (because caller is doing expected sends on a single virtually contiguous | |
102 | * buffer, so we can do all pages at once). | |
103 | */ | |
104 | int ipath_get_user_pages(unsigned long start_page, size_t num_pages, | |
105 | struct page **p) | |
106 | { | |
107 | int ret; | |
108 | ||
109 | down_write(¤t->mm->mmap_sem); | |
110 | ||
111 | ret = __get_user_pages(start_page, num_pages, p, NULL); | |
112 | ||
113 | up_write(¤t->mm->mmap_sem); | |
114 | ||
115 | return ret; | |
116 | } | |
117 | ||
118 | /** | |
119 | * ipath_get_user_pages_nocopy - lock a single page for I/O and mark shared | |
120 | * @start_page: the page to lock | |
121 | * @p: the output page structure | |
122 | * | |
123 | * This is similar to ipath_get_user_pages, but it's always one page, and we | |
124 | * mark the page as locked for I/O, and shared. This is used for the user | |
125 | * process page that contains the destination address for the rcvhdrq tail | |
126 | * update, so we need to have the vma. If we don't do this, the page can be | |
127 | * taken away from us on fork, even if the child never touches it, and then | |
128 | * the user process never sees the tail register updates. | |
129 | */ | |
130 | int ipath_get_user_pages_nocopy(unsigned long page, struct page **p) | |
131 | { | |
132 | struct vm_area_struct *vma; | |
133 | int ret; | |
134 | ||
135 | down_write(¤t->mm->mmap_sem); | |
136 | ||
137 | ret = __get_user_pages(page, 1, p, &vma); | |
138 | ||
139 | up_write(¤t->mm->mmap_sem); | |
140 | ||
141 | return ret; | |
142 | } | |
143 | ||
144 | void ipath_release_user_pages(struct page **p, size_t num_pages) | |
145 | { | |
146 | down_write(¤t->mm->mmap_sem); | |
147 | ||
148 | __ipath_release_user_pages(p, num_pages, 1); | |
149 | ||
150 | current->mm->locked_vm -= num_pages; | |
151 | ||
152 | up_write(¤t->mm->mmap_sem); | |
153 | } | |
154 | ||
155 | struct ipath_user_pages_work { | |
156 | struct work_struct work; | |
157 | struct mm_struct *mm; | |
158 | unsigned long num_pages; | |
159 | }; | |
160 | ||
161 | static void user_pages_account(void *ptr) | |
162 | { | |
163 | struct ipath_user_pages_work *work = ptr; | |
164 | ||
165 | down_write(&work->mm->mmap_sem); | |
166 | work->mm->locked_vm -= work->num_pages; | |
167 | up_write(&work->mm->mmap_sem); | |
168 | mmput(work->mm); | |
169 | kfree(work); | |
170 | } | |
171 | ||
172 | void ipath_release_user_pages_on_close(struct page **p, size_t num_pages) | |
173 | { | |
174 | struct ipath_user_pages_work *work; | |
175 | struct mm_struct *mm; | |
176 | ||
177 | __ipath_release_user_pages(p, num_pages, 1); | |
178 | ||
179 | mm = get_task_mm(current); | |
180 | if (!mm) | |
181 | goto bail; | |
182 | ||
183 | work = kmalloc(sizeof(*work), GFP_KERNEL); | |
184 | if (!work) | |
185 | goto bail_mm; | |
186 | ||
187 | goto bail; | |
188 | ||
189 | INIT_WORK(&work->work, user_pages_account, work); | |
190 | work->mm = mm; | |
191 | work->num_pages = num_pages; | |
192 | ||
193 | bail_mm: | |
194 | mmput(mm); | |
195 | bail: | |
196 | return; | |
197 | } |