Re: [PATCH virtio] virtio: virtio_console: fix DMA memory allocation for rproc serial

From: Alexander Lobakin
Date: Thu Nov 05 2020 - 07:22:33 EST


From: Jason Wang <jasowang@xxxxxxxxxx>
Date: Thu, 5 Nov 2020 11:10:24 +0800

Hi Jason,

> On 2020/11/4 下午11:31, Alexander Lobakin wrote:
>> Since commit 086d08725d34 ("remoteproc: create vdev subdevice with
>> specific dma memory pool"), every remoteproc has a DMA subdevice
>> ("remoteprocX#vdevYbuffer") for each virtio device, which inherits
>> DMA capabilities from the corresponding platform device. This allowed
>> to associate different DMA pools with each vdev, and required from
>> virtio drivers to perform DMA operations with the parent device
>> (vdev->dev.parent) instead of grandparent (vdev->dev.parent->parent).
>>
>> virtio_rpmsg_bus was already changed in the same merge cycle with
>> commit d999b622fcfb ("rpmsg: virtio: allocate buffer from parent"),
>> but virtio_console did not. In fact, operations using the grandparent
>> worked fine while the grandparent was the platform device, but since
>> commit c774ad010873 ("remoteproc: Fix and restore the parenting
>> hierarchy for vdev") this was changed, and now the grandparent device
>> is the remoteproc device without any DMA capabilities.
>> So, starting v5.8-rc1 the following warning is observed:
>>
>> [ 2.483925] ------------[ cut here ]------------
>> [ 2.489148] WARNING: CPU: 3 PID: 101 at kernel/dma/mapping.c:427 0x80e7eee8
>> [ 2.489152] Modules linked in: virtio_console(+)
>> [ 2.503737] virtio_rpmsg_bus rpmsg_core
>> [ 2.508903]
>> [ 2.528898] <Other modules, stack and call trace here>
>> [ 2.913043]
>> [ 2.914907] ---[ end trace 93ac8746beab612c ]---
>> [ 2.920102] virtio-ports vport1p0: Error allocating inbufs
>>
>> kernel/dma/mapping.c:427 is:
>>
>> WARN_ON_ONCE(!dev->coherent_dma_mask);
>>
>> obviously because the grandparent now is remoteproc dev without any
>> DMA caps:
>>
>> [ 3.104943] Parent: remoteproc0#vdev1buffer, grandparent: remoteproc0
>>
>> Fix this the same way as it was for virtio_rpmsg_bus, using just the
>> parent device (vdev->dev.parent, "remoteprocX#vdevYbuffer") for DMA
>> operations.
>> This also allows now to reserve DMA pools/buffers for rproc serial
>> via Device Tree.
>>
>> Fixes: c774ad010873 ("remoteproc: Fix and restore the parenting hierarchy for vdev")
>> Cc: stable@xxxxxxxxxxxxxxx # 5.1+
>> Signed-off-by: Alexander Lobakin <alobakin@xxxxx>
>> ---
>> drivers/char/virtio_console.c | 8 ++++----
>> 1 file changed, 4 insertions(+), 4 deletions(-)
>>
>> diff --git a/drivers/char/virtio_console.c b/drivers/char/virtio_console.c
>> index a2da8f768b94..1836cc56e357 100644
>> --- a/drivers/char/virtio_console.c
>> +++ b/drivers/char/virtio_console.c
>> @@ -435,12 +435,12 @@ static struct port_buffer *alloc_buf(struct virtio_device *vdev, size_t buf_size
>> /*
>> * Allocate DMA memory from ancestor. When a virtio
>> * device is created by remoteproc, the DMA memory is
>> - * associated with the grandparent device:
>> - * vdev => rproc => platform-dev.
>> + * associated with the parent device:
>> + * virtioY => remoteprocX#vdevYbuffer.
>> */
>> - if (!vdev->dev.parent || !vdev->dev.parent->parent)
>> + buf->dev = vdev->dev.parent;
>> + if (!buf->dev)
>> goto free_buf;
>> - buf->dev = vdev->dev.parent->parent;
>
>
> I wonder it could be the right time to introduce dma_dev for virtio
> instead of depending on something magic via parent.

This patch are meant to hit RC window and stable trees as a fix of
the bug that is present since v5.8-rc1. So any new features are out
of scope of this particular fix.

The idea of DMAing through "dev->parent" is that "virtioX" itself is a
logical dev, not the real one, but its parent *is*. This logic is used
across the whole tree -- every subsystem creates its own logical device,
but drivers should always use the backing PCI/platform/etc. devices for
DMA operations, which represent the real hardware.

> (Btw I don't even notice that there's transport specific code in virtio
> console, it's better to avoid it)
>
> Thanks

Thanks,
Al

>>
>> /* Increase device refcnt to avoid freeing it */
>> get_device(buf->dev);