Merge branch 'hwmon-for-linus' of git://jdelvare.pck.nerim.net/jdelvare-2.6
[linux-2.6.git] / drivers / infiniband / core / uverbs_mem.c
blobc95fe952abd5e8e7b47bd5bc92c9bcbfa8d74cfb
1 /*
2 * Copyright (c) 2005 Topspin Communications. All rights reserved.
3 * Copyright (c) 2005 Cisco Systems. All rights reserved.
4 * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer.
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 * SOFTWARE.
34 * $Id: uverbs_mem.c 2743 2005-06-28 22:27:59Z roland $
37 #include <linux/mm.h>
38 #include <linux/dma-mapping.h>
40 #include "uverbs.h"
42 struct ib_umem_account_work {
43 struct work_struct work;
44 struct mm_struct *mm;
45 unsigned long diff;
49 static void __ib_umem_release(struct ib_device *dev, struct ib_umem *umem, int dirty)
51 struct ib_umem_chunk *chunk, *tmp;
52 int i;
54 list_for_each_entry_safe(chunk, tmp, &umem->chunk_list, list) {
55 ib_dma_unmap_sg(dev, chunk->page_list,
56 chunk->nents, DMA_BIDIRECTIONAL);
57 for (i = 0; i < chunk->nents; ++i) {
58 if (umem->writable && dirty)
59 set_page_dirty_lock(chunk->page_list[i].page);
60 put_page(chunk->page_list[i].page);
63 kfree(chunk);
67 int ib_umem_get(struct ib_device *dev, struct ib_umem *mem,
68 void *addr, size_t size, int write)
70 struct page **page_list;
71 struct ib_umem_chunk *chunk;
72 unsigned long locked;
73 unsigned long lock_limit;
74 unsigned long cur_base;
75 unsigned long npages;
76 int ret = 0;
77 int off;
78 int i;
80 if (!can_do_mlock())
81 return -EPERM;
83 page_list = (struct page **) __get_free_page(GFP_KERNEL);
84 if (!page_list)
85 return -ENOMEM;
87 mem->user_base = (unsigned long) addr;
88 mem->length = size;
89 mem->offset = (unsigned long) addr & ~PAGE_MASK;
90 mem->page_size = PAGE_SIZE;
91 mem->writable = write;
93 INIT_LIST_HEAD(&mem->chunk_list);
95 npages = PAGE_ALIGN(size + mem->offset) >> PAGE_SHIFT;
97 down_write(&current->mm->mmap_sem);
99 locked = npages + current->mm->locked_vm;
100 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur >> PAGE_SHIFT;
102 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
103 ret = -ENOMEM;
104 goto out;
107 cur_base = (unsigned long) addr & PAGE_MASK;
109 while (npages) {
110 ret = get_user_pages(current, current->mm, cur_base,
111 min_t(int, npages,
112 PAGE_SIZE / sizeof (struct page *)),
113 1, !write, page_list, NULL);
115 if (ret < 0)
116 goto out;
118 cur_base += ret * PAGE_SIZE;
119 npages -= ret;
121 off = 0;
123 while (ret) {
124 chunk = kmalloc(sizeof *chunk + sizeof (struct scatterlist) *
125 min_t(int, ret, IB_UMEM_MAX_PAGE_CHUNK),
126 GFP_KERNEL);
127 if (!chunk) {
128 ret = -ENOMEM;
129 goto out;
132 chunk->nents = min_t(int, ret, IB_UMEM_MAX_PAGE_CHUNK);
133 for (i = 0; i < chunk->nents; ++i) {
134 chunk->page_list[i].page = page_list[i + off];
135 chunk->page_list[i].offset = 0;
136 chunk->page_list[i].length = PAGE_SIZE;
139 chunk->nmap = ib_dma_map_sg(dev,
140 &chunk->page_list[0],
141 chunk->nents,
142 DMA_BIDIRECTIONAL);
143 if (chunk->nmap <= 0) {
144 for (i = 0; i < chunk->nents; ++i)
145 put_page(chunk->page_list[i].page);
146 kfree(chunk);
148 ret = -ENOMEM;
149 goto out;
152 ret -= chunk->nents;
153 off += chunk->nents;
154 list_add_tail(&chunk->list, &mem->chunk_list);
157 ret = 0;
160 out:
161 if (ret < 0)
162 __ib_umem_release(dev, mem, 0);
163 else
164 current->mm->locked_vm = locked;
166 up_write(&current->mm->mmap_sem);
167 free_page((unsigned long) page_list);
169 return ret;
172 void ib_umem_release(struct ib_device *dev, struct ib_umem *umem)
174 __ib_umem_release(dev, umem, 1);
176 down_write(&current->mm->mmap_sem);
177 current->mm->locked_vm -=
178 PAGE_ALIGN(umem->length + umem->offset) >> PAGE_SHIFT;
179 up_write(&current->mm->mmap_sem);
182 static void ib_umem_account(struct work_struct *_work)
184 struct ib_umem_account_work *work =
185 container_of(_work, struct ib_umem_account_work, work);
187 down_write(&work->mm->mmap_sem);
188 work->mm->locked_vm -= work->diff;
189 up_write(&work->mm->mmap_sem);
190 mmput(work->mm);
191 kfree(work);
194 void ib_umem_release_on_close(struct ib_device *dev, struct ib_umem *umem)
196 struct ib_umem_account_work *work;
197 struct mm_struct *mm;
199 __ib_umem_release(dev, umem, 1);
201 mm = get_task_mm(current);
202 if (!mm)
203 return;
206 * We may be called with the mm's mmap_sem already held. This
207 * can happen when a userspace munmap() is the call that drops
208 * the last reference to our file and calls our release
209 * method. If there are memory regions to destroy, we'll end
210 * up here and not be able to take the mmap_sem. Therefore we
211 * defer the vm_locked accounting to the system workqueue.
214 work = kmalloc(sizeof *work, GFP_KERNEL);
215 if (!work) {
216 mmput(mm);
217 return;
220 INIT_WORK(&work->work, ib_umem_account);
221 work->mm = mm;
222 work->diff = PAGE_ALIGN(umem->length + umem->offset) >> PAGE_SHIFT;
224 schedule_work(&work->work);