-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvirtio_accel-zc.c
111 lines (93 loc) · 2.5 KB
/
virtio_accel-zc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// SPDX-License-Identifier: GPL-2.0
/* based on drivers/scsi/st.c & cryptodev-linux */
#include <linux/atomic.h>
#include <linux/err.h>
#include <linux/highmem.h>
#include <linux/scatterlist.h>
#include <linux/slab.h>
#include <linux/virtio.h>
#include "accel.h"
#include "virtio_accel-common.h"
#define PAGECOUNT(buf, buflen) \
((buflen) ? \
((((unsigned long)((unsigned long)(buf) + (buflen) - 1)) >> \
PAGE_SHIFT) - \
(((unsigned long)(buf)) >> PAGE_SHIFT) + 1) : \
0)
int virtaccel_map_user_buf(struct sg_table **m_sgt, struct page ***m_pages,
void __user *_uaddr, size_t ulen, int write,
struct virtio_device *vdev)
{
unsigned long uaddr = (unsigned long)_uaddr;
const int max_pages = PAGECOUNT(uaddr, ulen);
int ret = 0;
int i;
int pg_len;
int nr_pages;
struct page **pages;
struct scatterlist *sg;
/* User attempted overflow! */
if ((uaddr + ulen) < uaddr)
return -EINVAL;
if (ulen == 0)
return 0;
pages = kzalloc(max_pages * sizeof(*m_pages), GFP_ATOMIC);
if (!pages)
return -ENOMEM;
nr_pages = get_user_pages_fast(uaddr, max_pages, write ? FOLL_WRITE : 0,
pages);
if (nr_pages < max_pages) {
if (nr_pages < 0)
ret = nr_pages;
else
ret = -EFAULT;
goto out_unmap;
}
*m_sgt = kzalloc(sizeof(**m_sgt), GFP_ATOMIC);
if (!*m_sgt) {
ret = -ENOMEM;
goto out_unmap;
}
ret = sg_alloc_table(*m_sgt, nr_pages, GFP_ATOMIC);
if (ret < 0)
goto out_free_table;
i = 0;
pg_len = min((ptrdiff_t)(PAGE_SIZE - PAGEOFFSET(uaddr)),
(ptrdiff_t)ulen);
sg_set_page((*m_sgt)->sgl, pages[i], pg_len, PAGEOFFSET(uaddr));
ulen -= pg_len;
for_each_sg(sg_next((*m_sgt)->sgl), sg, (*m_sgt)->orig_nents - 1, i)
{
pg_len = min(PAGE_SIZE, ulen);
sg_set_page(sg, pages[i + 1], pg_len, 0);
ulen -= pg_len;
if (!ulen)
break;
}
*m_pages = pages;
return nr_pages;
out_free_table:
kfree(*m_sgt);
out_unmap:
if (nr_pages > 0) {
for (i = 0; i < nr_pages; i++)
put_page(pages[i]);
}
kfree(pages);
return ret;
}
void virtaccel_unmap_user_buf(struct sg_table *m_sgt, struct page **m_pages,
const unsigned int nr_pages)
{
int i;
sg_free_table(m_sgt);
for (i = 0; i < nr_pages; i++) {
struct page *page = m_pages[i];
if (!PageReserved(page))
SetPageDirty(page);
// FIXME: cache flush missing for rw==READ
// flush_dcache_page(page);
put_page(page);
}
kfree(m_pages);
}