Skip to content

Commit ab7a237

Browse files
cohuckmstsirkin
authored andcommitted
virtio: hint if callbacks surprisingly might sleep
A virtio transport is free to implement some of the callbacks in virtio_config_ops in a matter that they cannot be called from atomic context (e.g. virtio-ccw, which maps a lot of the callbacks to channel I/O, which is an inherently asynchronous mechanism). This can be very surprising for developers using the much more common virtio-pci transport, just to find out that things break when used on s390. The documentation for virtio_config_ops now contains a comment explaining this, but it makes sense to add a might_sleep() annotation to various wrapper functions in the virtio core to avoid surprises later. Note that annotations are NOT added to two classes of calls: - direct calls from device drivers (all current callers should be fine, however) - calls which clearly won't be made from atomic context (such as those ultimately coming in via the driver core) Signed-off-by: Cornelia Huck <cohuck@redhat.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
1 parent 971bedc commit ab7a237

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

drivers/virtio/virtio.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ EXPORT_SYMBOL_GPL(virtio_config_enable);
161161

162162
void virtio_add_status(struct virtio_device *dev, unsigned int status)
163163
{
164+
might_sleep();
164165
dev->config->set_status(dev, dev->config->get_status(dev) | status);
165166
}
166167
EXPORT_SYMBOL_GPL(virtio_add_status);
@@ -170,6 +171,7 @@ int virtio_finalize_features(struct virtio_device *dev)
170171
int ret = dev->config->finalize_features(dev);
171172
unsigned status;
172173

174+
might_sleep();
173175
if (ret)
174176
return ret;
175177

include/linux/virtio_config.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ static inline __virtio64 cpu_to_virtio64(struct virtio_device *vdev, u64 val)
290290
/* Config space accessors. */
291291
#define virtio_cread(vdev, structname, member, ptr) \
292292
do { \
293+
might_sleep(); \
293294
/* Must match the member's type, and be integer */ \
294295
if (!typecheck(typeof((((structname*)0)->member)), *(ptr))) \
295296
(*ptr) = 1; \
@@ -319,6 +320,7 @@ static inline __virtio64 cpu_to_virtio64(struct virtio_device *vdev, u64 val)
319320
/* Config space accessors. */
320321
#define virtio_cwrite(vdev, structname, member, ptr) \
321322
do { \
323+
might_sleep(); \
322324
/* Must match the member's type, and be integer */ \
323325
if (!typecheck(typeof((((structname*)0)->member)), *(ptr))) \
324326
BUG_ON((*ptr) == 1); \
@@ -358,6 +360,7 @@ static inline void __virtio_cread_many(struct virtio_device *vdev,
358360
vdev->config->generation(vdev) : 0;
359361
int i;
360362

363+
might_sleep();
361364
do {
362365
old = gen;
363366

@@ -380,27 +383,33 @@ static inline void virtio_cread_bytes(struct virtio_device *vdev,
380383
static inline u8 virtio_cread8(struct virtio_device *vdev, unsigned int offset)
381384
{
382385
u8 ret;
386+
387+
might_sleep();
383388
vdev->config->get(vdev, offset, &ret, sizeof(ret));
384389
return ret;
385390
}
386391

387392
static inline void virtio_cwrite8(struct virtio_device *vdev,
388393
unsigned int offset, u8 val)
389394
{
395+
might_sleep();
390396
vdev->config->set(vdev, offset, &val, sizeof(val));
391397
}
392398

393399
static inline u16 virtio_cread16(struct virtio_device *vdev,
394400
unsigned int offset)
395401
{
396402
u16 ret;
403+
404+
might_sleep();
397405
vdev->config->get(vdev, offset, &ret, sizeof(ret));
398406
return virtio16_to_cpu(vdev, (__force __virtio16)ret);
399407
}
400408

401409
static inline void virtio_cwrite16(struct virtio_device *vdev,
402410
unsigned int offset, u16 val)
403411
{
412+
might_sleep();
404413
val = (__force u16)cpu_to_virtio16(vdev, val);
405414
vdev->config->set(vdev, offset, &val, sizeof(val));
406415
}
@@ -409,13 +418,16 @@ static inline u32 virtio_cread32(struct virtio_device *vdev,
409418
unsigned int offset)
410419
{
411420
u32 ret;
421+
422+
might_sleep();
412423
vdev->config->get(vdev, offset, &ret, sizeof(ret));
413424
return virtio32_to_cpu(vdev, (__force __virtio32)ret);
414425
}
415426

416427
static inline void virtio_cwrite32(struct virtio_device *vdev,
417428
unsigned int offset, u32 val)
418429
{
430+
might_sleep();
419431
val = (__force u32)cpu_to_virtio32(vdev, val);
420432
vdev->config->set(vdev, offset, &val, sizeof(val));
421433
}
@@ -431,6 +443,7 @@ static inline u64 virtio_cread64(struct virtio_device *vdev,
431443
static inline void virtio_cwrite64(struct virtio_device *vdev,
432444
unsigned int offset, u64 val)
433445
{
446+
might_sleep();
434447
val = (__force u64)cpu_to_virtio64(vdev, val);
435448
vdev->config->set(vdev, offset, &val, sizeof(val));
436449
}

0 commit comments

Comments
 (0)