2 * Copyright (c) 2006 QLogic, Inc. All rights reserved.
3 * Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved.
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:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
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.
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
35 #include <linux/device.h>
36 #include <linux/vs_memory.h>
38 #include "ipath_kernel.h"
40 static void __ipath_release_user_pages(struct page **p, size_t num_pages,
45 for (i = 0; i < num_pages; i++) {
46 ipath_cdbg(MM, "%lu/%lu put_page %p\n", (unsigned long) i,
47 (unsigned long) num_pages, p[i]);
49 set_page_dirty_lock(p[i]);
54 /* call with current->mm->mmap_sem held */
55 static int __get_user_pages(unsigned long start_page, size_t num_pages,
56 struct page **p, struct vm_area_struct **vma)
58 unsigned long lock_limit;
62 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur >>
65 if (num_pages > lock_limit ||
66 !vx_vmlocked_avail(current->mm, num_pages)) {
71 ipath_cdbg(VERBOSE, "pin %lx pages from vaddr %lx\n",
72 (unsigned long) num_pages, start_page);
74 for (got = 0; got < num_pages; got += ret) {
75 ret = get_user_pages(current, current->mm,
76 start_page + got * PAGE_SIZE,
77 num_pages - got, 1, 1,
83 vx_vmlocked_add(current->mm, num_pages);
89 __ipath_release_user_pages(p, got, 0);
95 * ipath_get_user_pages - lock user pages into memory
96 * @start_page: the start page
97 * @num_pages: the number of pages
98 * @p: the output page structures
100 * This function takes a given start page (page aligned user virtual
101 * address) and pins it and the following specified number of pages. For
102 * now, num_pages is always 1, but that will probably change at some point
103 * (because caller is doing expected sends on a single virtually contiguous
104 * buffer, so we can do all pages at once).
106 int ipath_get_user_pages(unsigned long start_page, size_t num_pages,
111 down_write(¤t->mm->mmap_sem);
113 ret = __get_user_pages(start_page, num_pages, p, NULL);
115 up_write(¤t->mm->mmap_sem);
121 * ipath_get_user_pages_nocopy - lock a single page for I/O and mark shared
122 * @start_page: the page to lock
123 * @p: the output page structure
125 * This is similar to ipath_get_user_pages, but it's always one page, and we
126 * mark the page as locked for I/O, and shared. This is used for the user
127 * process page that contains the destination address for the rcvhdrq tail
128 * update, so we need to have the vma. If we don't do this, the page can be
129 * taken away from us on fork, even if the child never touches it, and then
130 * the user process never sees the tail register updates.
132 int ipath_get_user_pages_nocopy(unsigned long page, struct page **p)
134 struct vm_area_struct *vma;
137 down_write(¤t->mm->mmap_sem);
139 ret = __get_user_pages(page, 1, p, &vma);
141 up_write(¤t->mm->mmap_sem);
146 void ipath_release_user_pages(struct page **p, size_t num_pages)
148 down_write(¤t->mm->mmap_sem);
150 __ipath_release_user_pages(p, num_pages, 1);
152 vx_vmlocked_sub(current->mm, num_pages);
154 up_write(¤t->mm->mmap_sem);
157 struct ipath_user_pages_work {
158 struct work_struct work;
159 struct mm_struct *mm;
160 unsigned long num_pages;
163 static void user_pages_account(void *ptr)
165 struct ipath_user_pages_work *work = ptr;
167 down_write(&work->mm->mmap_sem);
168 vx_vmlocked_sub(work->mm, work->num_pages);
169 up_write(&work->mm->mmap_sem);
174 void ipath_release_user_pages_on_close(struct page **p, size_t num_pages)
176 struct ipath_user_pages_work *work;
177 struct mm_struct *mm;
179 __ipath_release_user_pages(p, num_pages, 1);
181 mm = get_task_mm(current);
185 work = kmalloc(sizeof(*work), GFP_KERNEL);
191 INIT_WORK(&work->work, user_pages_account, work);
193 work->num_pages = num_pages;