Skip to content

Commit d71de9e

Browse files
committed
virtio: core support for config generation
virtio 1.0 spec says: Drivers MUST NOT assume reads from fields greater than 32 bits wide are atomic, nor are reads from multiple fields: drivers SHOULD read device configuration space fields like so: u32 before, after; do { before = get_config_generation(device); // read config entry/entries. after = get_config_generation(device); } while (after != before); Do exactly this, for transports that support it. Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
1 parent 0dce377 commit d71de9e

1 file changed

Lines changed: 28 additions & 4 deletions

File tree

‎include/linux/virtio_config.h‎

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
* offset: the offset of the configuration field
2020
* buf: the buffer to read the field value from.
2121
* len: the length of the buffer
22+
* @generation: config generation counter
23+
* vdev: the virtio_device
24+
* Returns the config generation counter
2225
* @get_status: read the status byte
2326
* vdev: the virtio_device
2427
* Returns the status byte
@@ -60,6 +63,7 @@ struct virtio_config_ops {
6063
void *buf, unsigned len);
6164
void (*set)(struct virtio_device *vdev, unsigned offset,
6265
const void *buf, unsigned len);
66+
u32 (*generation)(struct virtio_device *vdev);
6367
u8 (*get_status)(struct virtio_device *vdev);
6468
void (*set_status)(struct virtio_device *vdev, u8 status);
6569
void (*reset)(struct virtio_device *vdev);
@@ -301,14 +305,33 @@ static inline u8 virtio_cread8(struct virtio_device *vdev, unsigned int offset)
301305
return ret;
302306
}
303307

308+
/* Read @count fields, @bytes each. */
309+
static inline void __virtio_cread_many(struct virtio_device *vdev,
310+
unsigned int offset,
311+
void *buf, size_t count, size_t bytes)
312+
{
313+
u32 old, gen = vdev->config->generation ?
314+
vdev->config->generation(vdev) : 0;
315+
int i;
316+
317+
do {
318+
old = gen;
319+
320+
for (i = 0; i < count; i++)
321+
vdev->config->get(vdev, offset + bytes * i,
322+
buf + i * bytes, bytes);
323+
324+
gen = vdev->config->generation ?
325+
vdev->config->generation(vdev) : 0;
326+
} while (gen != old);
327+
}
328+
329+
304330
static inline void virtio_cread_bytes(struct virtio_device *vdev,
305331
unsigned int offset,
306332
void *buf, size_t len)
307333
{
308-
int i;
309-
310-
for (i = 0; i < len; i++)
311-
vdev->config->get(vdev, offset + i, buf + i, 1);
334+
__virtio_cread_many(vdev, offset, buf, len, 1);
312335
}
313336

314337
static inline void virtio_cwrite8(struct virtio_device *vdev,
@@ -352,6 +375,7 @@ static inline u64 virtio_cread64(struct virtio_device *vdev,
352375
{
353376
u64 ret;
354377
vdev->config->get(vdev, offset, &ret, sizeof(ret));
378+
__virtio_cread_many(vdev, offset, &ret, 1, sizeof(ret));
355379
return virtio64_to_cpu(vdev, (__force __virtio64)ret);
356380
}
357381

0 commit comments

Comments
 (0)