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
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
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
34 * $Id: uverbs_mem.c 2743 2005-06-28 22:27:59Z roland $
38 #include <linux/dma-mapping.h>
39 #include <linux/vs_memory.h>
43 struct ib_umem_account_work {
44 struct work_struct work;
50 static void __ib_umem_release(struct ib_device *dev, struct ib_umem *umem, int dirty)
52 struct ib_umem_chunk *chunk, *tmp;
55 list_for_each_entry_safe(chunk, tmp, &umem->chunk_list, list) {
56 dma_unmap_sg(dev->dma_device, 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);
68 int ib_umem_get(struct ib_device *dev, struct ib_umem *mem,
69 void *addr, size_t size, int write)
71 struct page **page_list;
72 struct ib_umem_chunk *chunk;
74 unsigned long lock_limit;
75 unsigned long cur_base;
84 page_list = (struct page **) __get_free_page(GFP_KERNEL);
88 mem->user_base = (unsigned long) addr;
90 mem->offset = (unsigned long) addr & ~PAGE_MASK;
91 mem->page_size = PAGE_SIZE;
92 mem->writable = write;
94 INIT_LIST_HEAD(&mem->chunk_list);
96 npages = PAGE_ALIGN(size + mem->offset) >> PAGE_SHIFT;
98 down_write(¤t->mm->mmap_sem);
100 locked = npages + current->mm->locked_vm;
101 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur >> PAGE_SHIFT;
103 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
108 cur_base = (unsigned long) addr & PAGE_MASK;
111 ret = get_user_pages(current, current->mm, cur_base,
113 PAGE_SIZE / sizeof (struct page *)),
114 1, !write, page_list, NULL);
119 cur_base += ret * PAGE_SIZE;
125 chunk = kmalloc(sizeof *chunk + sizeof (struct scatterlist) *
126 min_t(int, ret, IB_UMEM_MAX_PAGE_CHUNK),
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;
140 chunk->nmap = dma_map_sg(dev->dma_device,
141 &chunk->page_list[0],
144 if (chunk->nmap <= 0) {
145 for (i = 0; i < chunk->nents; ++i)
146 put_page(chunk->page_list[i].page);
155 list_add_tail(&chunk->list, &mem->chunk_list);
163 __ib_umem_release(dev, mem, 0);
165 vx_vmlocked_sub(current->mm, current->mm->locked_vm - locked);
167 up_write(¤t->mm->mmap_sem);
168 free_page((unsigned long) page_list);
173 void ib_umem_release(struct ib_device *dev, struct ib_umem *umem)
175 __ib_umem_release(dev, umem, 1);
177 down_write(¤t->mm->mmap_sem);
178 vx_vmlocked_sub(current->mm,
179 PAGE_ALIGN(umem->length + umem->offset) >> PAGE_SHIFT);
180 up_write(¤t->mm->mmap_sem);
183 static void ib_umem_account(void *work_ptr)
185 struct ib_umem_account_work *work = work_ptr;
187 down_write(&work->mm->mmap_sem);
188 vx_vmlocked_sub(work->mm, work->diff);
189 up_write(&work->mm->mmap_sem);
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);
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);
220 INIT_WORK(&work->work, ib_umem_account, work);
222 work->diff = PAGE_ALIGN(umem->length + umem->offset) >> PAGE_SHIFT;
224 schedule_work(&work->work);