Loading...
1/*
2 * Copyright (c) 2006, 2007, 2008, 2009 QLogic Corporation. All rights reserved.
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/sched/signal.h>
36#include <linux/device.h>
37
38#include "qib.h"
39
40static void __qib_release_user_pages(struct page **p, size_t num_pages,
41 int dirty)
42{
43 unpin_user_pages_dirty_lock(p, num_pages, dirty);
44}
45
46/*
47 * qib_map_page - a safety wrapper around pci_map_page()
48 *
49 * A dma_addr of all 0's is interpreted by the chip as "disabled".
50 * Unfortunately, it can also be a valid dma_addr returned on some
51 * architectures.
52 *
53 * The powerpc iommu assigns dma_addrs in ascending order, so we don't
54 * have to bother with retries or mapping a dummy page to insure we
55 * don't just get the same mapping again.
56 *
57 * I'm sure we won't be so lucky with other iommu's, so FIXME.
58 */
59int qib_map_page(struct pci_dev *hwdev, struct page *page, dma_addr_t *daddr)
60{
61 dma_addr_t phys;
62
63 phys = dma_map_page(&hwdev->dev, page, 0, PAGE_SIZE, DMA_FROM_DEVICE);
64 if (dma_mapping_error(&hwdev->dev, phys))
65 return -ENOMEM;
66
67 if (!phys) {
68 dma_unmap_page(&hwdev->dev, phys, PAGE_SIZE, DMA_FROM_DEVICE);
69 phys = dma_map_page(&hwdev->dev, page, 0, PAGE_SIZE,
70 DMA_FROM_DEVICE);
71 if (dma_mapping_error(&hwdev->dev, phys))
72 return -ENOMEM;
73 /*
74 * FIXME: If we get 0 again, we should keep this page,
75 * map another, then free the 0 page.
76 */
77 }
78 *daddr = phys;
79 return 0;
80}
81
82/**
83 * qib_get_user_pages - lock user pages into memory
84 * @start_page: the start page
85 * @num_pages: the number of pages
86 * @p: the output page structures
87 *
88 * This function takes a given start page (page aligned user virtual
89 * address) and pins it and the following specified number of pages. For
90 * now, num_pages is always 1, but that will probably change at some point
91 * (because caller is doing expected sends on a single virtually contiguous
92 * buffer, so we can do all pages at once).
93 */
94int qib_get_user_pages(unsigned long start_page, size_t num_pages,
95 struct page **p)
96{
97 unsigned long locked, lock_limit;
98 size_t got;
99 int ret;
100
101 lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
102 locked = atomic64_add_return(num_pages, ¤t->mm->pinned_vm);
103
104 if (locked > lock_limit && !capable(CAP_IPC_LOCK)) {
105 ret = -ENOMEM;
106 goto bail;
107 }
108
109 mmap_read_lock(current->mm);
110 for (got = 0; got < num_pages; got += ret) {
111 ret = pin_user_pages(start_page + got * PAGE_SIZE,
112 num_pages - got,
113 FOLL_LONGTERM | FOLL_WRITE,
114 p + got);
115 if (ret < 0) {
116 mmap_read_unlock(current->mm);
117 goto bail_release;
118 }
119 }
120 mmap_read_unlock(current->mm);
121
122 return 0;
123bail_release:
124 __qib_release_user_pages(p, got, 0);
125bail:
126 atomic64_sub(num_pages, ¤t->mm->pinned_vm);
127 return ret;
128}
129
130void qib_release_user_pages(struct page **p, size_t num_pages)
131{
132 __qib_release_user_pages(p, num_pages, 1);
133
134 /* during close after signal, mm can be NULL */
135 if (current->mm)
136 atomic64_sub(num_pages, ¤t->mm->pinned_vm);
137}
1/*
2 * Copyright (c) 2006, 2007, 2008, 2009 QLogic Corporation. All rights reserved.
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/sched/signal.h>
36#include <linux/device.h>
37
38#include "qib.h"
39
40static void __qib_release_user_pages(struct page **p, size_t num_pages,
41 int dirty)
42{
43 size_t i;
44
45 for (i = 0; i < num_pages; i++) {
46 if (dirty)
47 set_page_dirty_lock(p[i]);
48 put_page(p[i]);
49 }
50}
51
52/*
53 * Call with current->mm->mmap_sem held.
54 */
55static int __qib_get_user_pages(unsigned long start_page, size_t num_pages,
56 struct page **p)
57{
58 unsigned long lock_limit;
59 size_t got;
60 int ret;
61
62 lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
63
64 if (num_pages > lock_limit && !capable(CAP_IPC_LOCK)) {
65 ret = -ENOMEM;
66 goto bail;
67 }
68
69 for (got = 0; got < num_pages; got += ret) {
70 ret = get_user_pages(start_page + got * PAGE_SIZE,
71 num_pages - got,
72 FOLL_WRITE | FOLL_FORCE,
73 p + got, NULL);
74 if (ret < 0)
75 goto bail_release;
76 }
77
78 current->mm->pinned_vm += num_pages;
79
80 ret = 0;
81 goto bail;
82
83bail_release:
84 __qib_release_user_pages(p, got, 0);
85bail:
86 return ret;
87}
88
89/**
90 * qib_map_page - a safety wrapper around pci_map_page()
91 *
92 * A dma_addr of all 0's is interpreted by the chip as "disabled".
93 * Unfortunately, it can also be a valid dma_addr returned on some
94 * architectures.
95 *
96 * The powerpc iommu assigns dma_addrs in ascending order, so we don't
97 * have to bother with retries or mapping a dummy page to insure we
98 * don't just get the same mapping again.
99 *
100 * I'm sure we won't be so lucky with other iommu's, so FIXME.
101 */
102dma_addr_t qib_map_page(struct pci_dev *hwdev, struct page *page,
103 unsigned long offset, size_t size, int direction)
104{
105 dma_addr_t phys;
106
107 phys = pci_map_page(hwdev, page, offset, size, direction);
108
109 if (phys == 0) {
110 pci_unmap_page(hwdev, phys, size, direction);
111 phys = pci_map_page(hwdev, page, offset, size, direction);
112 /*
113 * FIXME: If we get 0 again, we should keep this page,
114 * map another, then free the 0 page.
115 */
116 }
117
118 return phys;
119}
120
121/**
122 * qib_get_user_pages - lock user pages into memory
123 * @start_page: the start page
124 * @num_pages: the number of pages
125 * @p: the output page structures
126 *
127 * This function takes a given start page (page aligned user virtual
128 * address) and pins it and the following specified number of pages. For
129 * now, num_pages is always 1, but that will probably change at some point
130 * (because caller is doing expected sends on a single virtually contiguous
131 * buffer, so we can do all pages at once).
132 */
133int qib_get_user_pages(unsigned long start_page, size_t num_pages,
134 struct page **p)
135{
136 int ret;
137
138 down_write(¤t->mm->mmap_sem);
139
140 ret = __qib_get_user_pages(start_page, num_pages, p);
141
142 up_write(¤t->mm->mmap_sem);
143
144 return ret;
145}
146
147void qib_release_user_pages(struct page **p, size_t num_pages)
148{
149 if (current->mm) /* during close after signal, mm can be NULL */
150 down_write(¤t->mm->mmap_sem);
151
152 __qib_release_user_pages(p, num_pages, 1);
153
154 if (current->mm) {
155 current->mm->pinned_vm -= num_pages;
156 up_write(¤t->mm->mmap_sem);
157 }
158}