drm/virtio: Add memory barriers for capset cache. [Linux 4.4.187]

This Linux kernel change "drm/virtio: Add memory barriers for capset cache" is included in the Linux 4.4.187 release. This change is authored by David Riley <davidriley [at] chromium.org> on Mon Jun 10 14:18:10 2019 -0700. The commit for this change in Linux stable tree is 25d2b1b (patch) which is from upstream commit 9ff3a5c. The same Linux upstream change may have been applied to various maintained Linux releases and you can find all Linux releases containing changes from upstream 9ff3a5c.

drm/virtio: Add memory barriers for capset cache.

[ Upstream commit 9ff3a5c88e1f1ab17a31402b96d45abe14aab9d7 ]

After data is copied to the cache entry, atomic_set is used indicate
that the data is the entry is valid without appropriate memory barriers.
Similarly the read side was missing the corresponding memory barriers.

Signed-off-by: David Riley <davidriley@chromium.org>
Link: http://patchwork.freedesktop.org/patch/msgid/20190610211810.253227-5-davidriley@chromium.org
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>

There are 5 lines of Linux source code added/deleted in this change. Code changes to Linux kernel are as follows.

 drivers/gpu/drm/virtio/virtgpu_ioctl.c | 3 +++
 drivers/gpu/drm/virtio/virtgpu_vq.c    | 2 ++
 2 files changed, 5 insertions(+)

diff --git a/drivers/gpu/drm/virtio/virtgpu_ioctl.c b/drivers/gpu/drm/virtio/virtgpu_ioctl.c
index 6296e9f..0b8f8c1 100644
--- a/drivers/gpu/drm/virtio/virtgpu_ioctl.c
+++ b/drivers/gpu/drm/virtio/virtgpu_ioctl.c
@@ -535,6 +535,9 @@ static int virtio_gpu_get_caps_ioctl(struct drm_device *dev,
    ret = wait_event_timeout(vgdev->resp_wq,
                 atomic_read(&cache_ent->is_valid), 5 * HZ);

+   /* is_valid check must proceed before copy of the cache entry. */
+   smp_rmb();
    ptr = cache_ent->caps_cache;

diff --git a/drivers/gpu/drm/virtio/virtgpu_vq.c b/drivers/gpu/drm/virtio/virtgpu_vq.c
index 52436b3..a1b3ea1 100644
--- a/drivers/gpu/drm/virtio/virtgpu_vq.c
+++ b/drivers/gpu/drm/virtio/virtgpu_vq.c
@@ -618,6 +618,8 @@ static void virtio_gpu_cmd_capset_cb(struct virtio_gpu_device *vgdev,
            cache_ent->id == le32_to_cpu(cmd->capset_id)) {
            memcpy(cache_ent->caps_cache, resp->capset_data,
+           /* Copy must occur before is_valid is signalled. */
+           smp_wmb();
            atomic_set(&cache_ent->is_valid, 1);

Leave a Reply

Your email address will not be published. Required fields are marked *