fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / drivers / infiniband / core / uverbs_mem.c
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.
5  *
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:
11  *
12  *     Redistribution and use in source and binary forms, with or
13  *     without modification, are permitted provided that the following
14  *     conditions are met:
15  *
16  *      - Redistributions of source code must retain the above
17  *        copyright notice, this list of conditions and the following
18  *        disclaimer.
19  *
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.
24  *
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.
33  *
34  * $Id: uverbs_mem.c 2743 2005-06-28 22:27:59Z roland $
35  */
36
37 #include <linux/mm.h>
38 #include <linux/dma-mapping.h>
39 #include <linux/vs_memory.h>
40
41 #include "uverbs.h"
42
43 struct ib_umem_account_work {
44         struct work_struct work;
45         struct mm_struct  *mm;
46         unsigned long      diff;
47 };
48
49
50 static void __ib_umem_release(struct ib_device *dev, struct ib_umem *umem, int dirty)
51 {
52         struct ib_umem_chunk *chunk, *tmp;
53         int i;
54
55         list_for_each_entry_safe(chunk, tmp, &umem->chunk_list, list) {
56                 ib_dma_unmap_sg(dev, chunk->page_list,
57                                 chunk->nents, DMA_BIDIRECTIONAL);
58                 for (i = 0; i < chunk->nents; ++i) {
59                         if (umem->writable && dirty)
60                                 set_page_dirty_lock(chunk->page_list[i].page);
61                         put_page(chunk->page_list[i].page);
62                 }
63
64                 kfree(chunk);
65         }
66 }
67
68 int ib_umem_get(struct ib_device *dev, struct ib_umem *mem,
69                 void *addr, size_t size, int write)
70 {
71         struct page **page_list;
72         struct ib_umem_chunk *chunk;
73         unsigned long locked;
74         unsigned long lock_limit;
75         unsigned long cur_base;
76         unsigned long npages;
77         int ret = 0;
78         int off;
79         int i;
80
81         if (!can_do_mlock())
82                 return -EPERM;
83
84         page_list = (struct page **) __get_free_page(GFP_KERNEL);
85         if (!page_list)
86                 return -ENOMEM;
87
88         mem->user_base = (unsigned long) addr;
89         mem->length    = size;
90         mem->offset    = (unsigned long) addr & ~PAGE_MASK;
91         mem->page_size = PAGE_SIZE;
92         mem->writable  = write;
93
94         INIT_LIST_HEAD(&mem->chunk_list);
95
96         npages = PAGE_ALIGN(size + mem->offset) >> PAGE_SHIFT;
97
98         down_write(&current->mm->mmap_sem);
99
100         locked     = npages + current->mm->locked_vm;
101         lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur >> PAGE_SHIFT;
102
103         if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
104                 ret = -ENOMEM;
105                 goto out;
106         }
107
108         cur_base = (unsigned long) addr & PAGE_MASK;
109
110         while (npages) {
111                 ret = get_user_pages(current, current->mm, cur_base,
112                                      min_t(int, npages,
113                                            PAGE_SIZE / sizeof (struct page *)),
114                                      1, !write, page_list, NULL);
115
116                 if (ret < 0)
117                         goto out;
118
119                 cur_base += ret * PAGE_SIZE;
120                 npages   -= ret;
121
122                 off = 0;
123
124                 while (ret) {
125                         chunk = kmalloc(sizeof *chunk + sizeof (struct scatterlist) *
126                                         min_t(int, ret, IB_UMEM_MAX_PAGE_CHUNK),
127                                         GFP_KERNEL);
128                         if (!chunk) {
129                                 ret = -ENOMEM;
130                                 goto out;
131                         }
132
133                         chunk->nents = min_t(int, ret, IB_UMEM_MAX_PAGE_CHUNK);
134                         for (i = 0; i < chunk->nents; ++i) {
135                                 chunk->page_list[i].page   = page_list[i + off];
136                                 chunk->page_list[i].offset = 0;
137                                 chunk->page_list[i].length = PAGE_SIZE;
138                         }
139
140                         chunk->nmap = ib_dma_map_sg(dev,
141                                                     &chunk->page_list[0],
142                                                     chunk->nents,
143                                                     DMA_BIDIRECTIONAL);
144                         if (chunk->nmap <= 0) {
145                                 for (i = 0; i < chunk->nents; ++i)
146                                         put_page(chunk->page_list[i].page);
147                                 kfree(chunk);
148
149                                 ret = -ENOMEM;
150                                 goto out;
151                         }
152
153                         ret -= chunk->nents;
154                         off += chunk->nents;
155                         list_add_tail(&chunk->list, &mem->chunk_list);
156                 }
157
158                 ret = 0;
159         }
160
161 out:
162         if (ret < 0)
163                 __ib_umem_release(dev, mem, 0);
164         else
165                 vx_vmlocked_sub(current->mm, current->mm->locked_vm - locked);
166
167         up_write(&current->mm->mmap_sem);
168         free_page((unsigned long) page_list);
169
170         return ret;
171 }
172
173 void ib_umem_release(struct ib_device *dev, struct ib_umem *umem)
174 {
175         __ib_umem_release(dev, umem, 1);
176
177         down_write(&current->mm->mmap_sem);
178         vx_vmlocked_sub(current->mm,
179                 PAGE_ALIGN(umem->length + umem->offset) >> PAGE_SHIFT);
180         up_write(&current->mm->mmap_sem);
181 }
182
183 static void ib_umem_account(struct work_struct *_work)
184 {
185         struct ib_umem_account_work *work =
186                 container_of(_work, struct ib_umem_account_work, work);
187
188         down_write(&work->mm->mmap_sem);
189         vx_vmlocked_sub(work->mm, work->diff);
190         up_write(&work->mm->mmap_sem);
191         mmput(work->mm);
192         kfree(work);
193 }
194
195 void ib_umem_release_on_close(struct ib_device *dev, struct ib_umem *umem)
196 {
197         struct ib_umem_account_work *work;
198         struct mm_struct *mm;
199
200         __ib_umem_release(dev, umem, 1);
201
202         mm = get_task_mm(current);
203         if (!mm)
204                 return;
205
206         /*
207          * We may be called with the mm's mmap_sem already held.  This
208          * can happen when a userspace munmap() is the call that drops
209          * the last reference to our file and calls our release
210          * method.  If there are memory regions to destroy, we'll end
211          * up here and not be able to take the mmap_sem.  Therefore we
212          * defer the vm_locked accounting to the system workqueue.
213          */
214
215         work = kmalloc(sizeof *work, GFP_KERNEL);
216         if (!work) {
217                 mmput(mm);
218                 return;
219         }
220
221         INIT_WORK(&work->work, ib_umem_account);
222         work->mm   = mm;
223         work->diff = PAGE_ALIGN(umem->length + umem->offset) >> PAGE_SHIFT;
224
225         schedule_work(&work->work);
226 }