LKML Archive on lore.kernel.org
 help / Atom feed
* [PATCH] sample: vfio-mdev: avoid deadlock in mdev_access()
@ 2018-07-06 20:50 Alexey Khoroshilov
  2018-07-11 19:30 ` Alex Williamson
  0 siblings, 1 reply; 2+ messages in thread
From: Alexey Khoroshilov @ 2018-07-06 20:50 UTC (permalink / raw)
  To: Gerd Hoffmann, Alex Williamson, Kirti Wankhede
  Cc: Alexey Khoroshilov, kvm, linux-kernel, ldv-project

mdev_access() calls mbochs_get_page() with mdev_state->ops_lock held,
while mbochs_get_page() locks the mutex by itself.
It leads to unavoidable deadlock.

Found by Linux Driver Verification project (linuxtesting.org).

Signed-off-by: Alexey Khoroshilov <khoroshilov@ispras.ru>
---
 samples/vfio-mdev/mbochs.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/samples/vfio-mdev/mbochs.c b/samples/vfio-mdev/mbochs.c
index 2960e26c6ea4..6295a21381eb 100644
--- a/samples/vfio-mdev/mbochs.c
+++ b/samples/vfio-mdev/mbochs.c
@@ -178,6 +178,8 @@ static const char *vbe_name(u32 index)
 	return "(invalid)";
 }
 
+static struct page *__mbochs_get_page(struct mdev_state *mdev_state,
+				      pgoff_t pgoff);
 static struct page *mbochs_get_page(struct mdev_state *mdev_state,
 				    pgoff_t pgoff);
 
@@ -394,7 +396,7 @@ static ssize_t mdev_access(struct mdev_device *mdev, char *buf, size_t count,
 		   MBOCHS_MEMORY_BAR_OFFSET + mdev_state->memsize) {
 		pos -= MBOCHS_MMIO_BAR_OFFSET;
 		poff = pos & ~PAGE_MASK;
-		pg = mbochs_get_page(mdev_state, pos >> PAGE_SHIFT);
+		pg = __mbochs_get_page(mdev_state, pos >> PAGE_SHIFT);
 		map = kmap(pg);
 		if (is_write)
 			memcpy(map + poff, buf, count);
-- 
2.7.4


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] sample: vfio-mdev: avoid deadlock in mdev_access()
  2018-07-06 20:50 [PATCH] sample: vfio-mdev: avoid deadlock in mdev_access() Alexey Khoroshilov
@ 2018-07-11 19:30 ` Alex Williamson
  0 siblings, 0 replies; 2+ messages in thread
From: Alex Williamson @ 2018-07-11 19:30 UTC (permalink / raw)
  To: Alexey Khoroshilov
  Cc: Gerd Hoffmann, Kirti Wankhede, kvm, linux-kernel, ldv-project

On Fri,  6 Jul 2018 23:50:06 +0300
Alexey Khoroshilov <khoroshilov@ispras.ru> wrote:

> mdev_access() calls mbochs_get_page() with mdev_state->ops_lock held,
> while mbochs_get_page() locks the mutex by itself.
> It leads to unavoidable deadlock.
> 
> Found by Linux Driver Verification project (linuxtesting.org).
> 
> Signed-off-by: Alexey Khoroshilov <khoroshilov@ispras.ru>
> ---
>  samples/vfio-mdev/mbochs.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/samples/vfio-mdev/mbochs.c b/samples/vfio-mdev/mbochs.c
> index 2960e26c6ea4..6295a21381eb 100644
> --- a/samples/vfio-mdev/mbochs.c
> +++ b/samples/vfio-mdev/mbochs.c
> @@ -178,6 +178,8 @@ static const char *vbe_name(u32 index)
>  	return "(invalid)";
>  }
>  
> +static struct page *__mbochs_get_page(struct mdev_state *mdev_state,
> +				      pgoff_t pgoff);
>  static struct page *mbochs_get_page(struct mdev_state *mdev_state,
>  				    pgoff_t pgoff);
>  
> @@ -394,7 +396,7 @@ static ssize_t mdev_access(struct mdev_device *mdev, char *buf, size_t count,
>  		   MBOCHS_MEMORY_BAR_OFFSET + mdev_state->memsize) {
>  		pos -= MBOCHS_MMIO_BAR_OFFSET;
>  		poff = pos & ~PAGE_MASK;
> -		pg = mbochs_get_page(mdev_state, pos >> PAGE_SHIFT);
> +		pg = __mbochs_get_page(mdev_state, pos >> PAGE_SHIFT);
>  		map = kmap(pg);
>  		if (is_write)
>  			memcpy(map + poff, buf, count);

Looks good, applied to vfio for-linus branch for v4.18.  Thanks,

Alex

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, back to index

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-07-06 20:50 [PATCH] sample: vfio-mdev: avoid deadlock in mdev_access() Alexey Khoroshilov
2018-07-11 19:30 ` Alex Williamson

LKML Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/lkml/0 lkml/git/0.git
	git clone --mirror https://lore.kernel.org/lkml/1 lkml/git/1.git
	git clone --mirror https://lore.kernel.org/lkml/2 lkml/git/2.git
	git clone --mirror https://lore.kernel.org/lkml/3 lkml/git/3.git
	git clone --mirror https://lore.kernel.org/lkml/4 lkml/git/4.git
	git clone --mirror https://lore.kernel.org/lkml/5 lkml/git/5.git
	git clone --mirror https://lore.kernel.org/lkml/6 lkml/git/6.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 lkml lkml/ https://lore.kernel.org/lkml \
		linux-kernel@vger.kernel.org linux-kernel@archiver.kernel.org
	public-inbox-index lkml


Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-kernel


AGPL code for this site: git clone https://public-inbox.org/ public-inbox