Skip to content

Commit 186bfe4

Browse files
Gerd Bayerawilliam
authored andcommitted
vfio/pci: Extract duplicated code into macro
vfio_pci_core_do_io_rw() repeats the same code for multiple access widths. Factor this out into a macro Suggested-by: Alex Williamson <[email protected]> Signed-off-by: Gerd Bayer <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alex Williamson <[email protected]>
1 parent 6ba59ff commit 186bfe4

File tree

1 file changed

+46
-60
lines changed

1 file changed

+46
-60
lines changed

drivers/vfio/pci/vfio_pci_rdwr.c

Lines changed: 46 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,40 @@ VFIO_IOREAD(8)
9090
VFIO_IOREAD(16)
9191
VFIO_IOREAD(32)
9292

93+
#define VFIO_IORDWR(size) \
94+
static int vfio_pci_iordwr##size(struct vfio_pci_core_device *vdev,\
95+
bool iswrite, bool test_mem, \
96+
void __iomem *io, char __user *buf, \
97+
loff_t off, size_t *filled) \
98+
{ \
99+
u##size val; \
100+
int ret; \
101+
\
102+
if (iswrite) { \
103+
if (copy_from_user(&val, buf, sizeof(val))) \
104+
return -EFAULT; \
105+
\
106+
ret = vfio_pci_core_iowrite##size(vdev, test_mem, \
107+
val, io + off); \
108+
if (ret) \
109+
return ret; \
110+
} else { \
111+
ret = vfio_pci_core_ioread##size(vdev, test_mem, \
112+
&val, io + off); \
113+
if (ret) \
114+
return ret; \
115+
\
116+
if (copy_to_user(buf, &val, sizeof(val))) \
117+
return -EFAULT; \
118+
} \
119+
\
120+
*filled = sizeof(val); \
121+
return 0; \
122+
} \
123+
124+
VFIO_IORDWR(8)
125+
VFIO_IORDWR(16)
126+
VFIO_IORDWR(32)
93127
/*
94128
* Read or write from an __iomem region (MMIO or I/O port) with an excluded
95129
* range which is inaccessible. The excluded range drops writes and fills
@@ -115,71 +149,23 @@ ssize_t vfio_pci_core_do_io_rw(struct vfio_pci_core_device *vdev, bool test_mem,
115149
fillable = 0;
116150

117151
if (fillable >= 4 && !(off % 4)) {
118-
u32 val;
119-
120-
if (iswrite) {
121-
if (copy_from_user(&val, buf, 4))
122-
return -EFAULT;
123-
124-
ret = vfio_pci_core_iowrite32(vdev, test_mem,
125-
val, io + off);
126-
if (ret)
127-
return ret;
128-
} else {
129-
ret = vfio_pci_core_ioread32(vdev, test_mem,
130-
&val, io + off);
131-
if (ret)
132-
return ret;
133-
134-
if (copy_to_user(buf, &val, 4))
135-
return -EFAULT;
136-
}
152+
ret = vfio_pci_iordwr32(vdev, iswrite, test_mem,
153+
io, buf, off, &filled);
154+
if (ret)
155+
return ret;
137156

138-
filled = 4;
139157
} else if (fillable >= 2 && !(off % 2)) {
140-
u16 val;
141-
142-
if (iswrite) {
143-
if (copy_from_user(&val, buf, 2))
144-
return -EFAULT;
145-
146-
ret = vfio_pci_core_iowrite16(vdev, test_mem,
147-
val, io + off);
148-
if (ret)
149-
return ret;
150-
} else {
151-
ret = vfio_pci_core_ioread16(vdev, test_mem,
152-
&val, io + off);
153-
if (ret)
154-
return ret;
155-
156-
if (copy_to_user(buf, &val, 2))
157-
return -EFAULT;
158-
}
158+
ret = vfio_pci_iordwr16(vdev, iswrite, test_mem,
159+
io, buf, off, &filled);
160+
if (ret)
161+
return ret;
159162

160-
filled = 2;
161163
} else if (fillable) {
162-
u8 val;
163-
164-
if (iswrite) {
165-
if (copy_from_user(&val, buf, 1))
166-
return -EFAULT;
167-
168-
ret = vfio_pci_core_iowrite8(vdev, test_mem,
169-
val, io + off);
170-
if (ret)
171-
return ret;
172-
} else {
173-
ret = vfio_pci_core_ioread8(vdev, test_mem,
174-
&val, io + off);
175-
if (ret)
176-
return ret;
177-
178-
if (copy_to_user(buf, &val, 1))
179-
return -EFAULT;
180-
}
164+
ret = vfio_pci_iordwr8(vdev, iswrite, test_mem,
165+
io, buf, off, &filled);
166+
if (ret)
167+
return ret;
181168

182-
filled = 1;
183169
} else {
184170
/* Fill reads with -1, drop writes */
185171
filled = min(count, (size_t)(x_end - off));

0 commit comments

Comments
 (0)