All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jason Gunthorpe <jgg@nvidia.com>
To: Steve Sistare <steven.sistare@oracle.com>
Cc: kvm@vger.kernel.org, Alex Williamson <alex.williamson@redhat.com>,
	Cornelia Huck <cohuck@redhat.com>
Subject: Re: [PATCH V4 2/5] vfio/type1: prevent locked_vm underflow
Date: Thu, 15 Dec 2022 10:20:20 -0400	[thread overview]
Message-ID: <Y5stJHFWK/ZLzA1q@nvidia.com> (raw)
In-Reply-To: <1671053097-138498-3-git-send-email-steven.sistare@oracle.com>

On Wed, Dec 14, 2022 at 01:24:54PM -0800, Steve Sistare wrote:
> When a vfio container is preserved across exec using the VFIO_UPDATE_VADDR
> interfaces, locked_vm of the new mm becomes 0.  If the user later unmaps a
> dma mapping, locked_vm underflows to a large unsigned value, and a
> subsequent dma map request fails with ENOMEM in __account_locked_vm.
> 
> To avoid underflow, do not decrement locked_vm during unmap if the
> dma's mm has changed.  To restore the correct locked_vm count, when
> VFIO_DMA_MAP_FLAG_VADDR is used and the dma's mm has changed, add
> the mapping's pinned page count to the new mm->locked_vm, subject
> to the rlimit.  Now that mediated devices are excluded when using
> VFIO_UPDATE_VADDR, the amount of pinned memory equals the size of
> the mapping.
> 
> Fixes: c3cbab24db38 ("vfio/type1: implement interfaces to update vaddr")
> 
> Signed-off-by: Steve Sistare <steven.sistare@oracle.com>
> ---
>  drivers/vfio/vfio_iommu_type1.c | 50 ++++++++++++++++++++++++++++++++++++++++-
>  1 file changed, 49 insertions(+), 1 deletion(-)

> diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c
> index bdfc13c..33dc8ec 100644
> --- a/drivers/vfio/vfio_iommu_type1.c
> +++ b/drivers/vfio/vfio_iommu_type1.c
> @@ -100,6 +100,7 @@ struct vfio_dma {
>  	struct task_struct	*task;
>  	struct rb_root		pfn_list;	/* Ex-user pinned pfn list */
>  	unsigned long		*bitmap;
> +	struct mm_struct	*mm;
>  };

I'm not sure this is quite, right, or at least the comment isn't quite
right..

The issue is that the vfio_dma does not store the mm that provided the
pages. By going through the task every time it allows the mm to change
under its feet which of course can corrupt the assumed accounting in
various ways.

To fix this, the mm should be kept, as you did above. All the code
that is touching the task to get the mm should be dropped. The only
purpose of the task is to check the rlimit.

That in of itself should be a single patch with a clear description
that is the change.

Beyond that you want a second patch that make the vaddr stuff to
transfer the pin accounting from one mm to another in the process of
updating the dma to have a new task and mm.

There is no "underflow" here, only that the current code is using the
wrong mm in some case because it gets it through the task.

Jason

  parent reply	other threads:[~2022-12-15 14:20 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-14 21:24 [PATCH V4 0/5] fixes for virtual address update Steve Sistare
2022-12-14 21:24 ` [PATCH V4 1/5] vfio/type1: exclude mdevs from VFIO_UPDATE_VADDR Steve Sistare
2022-12-15  4:34   ` Tian, Kevin
2022-12-15 14:32     ` Steven Sistare
2022-12-14 21:24 ` [PATCH V4 2/5] vfio/type1: prevent locked_vm underflow Steve Sistare
2022-12-15  4:52   ` Tian, Kevin
2022-12-15 14:33     ` Steven Sistare
2022-12-15 14:20   ` Jason Gunthorpe [this message]
2022-12-15 14:38     ` Steven Sistare
2022-12-15 15:03       ` Jason Gunthorpe
2022-12-14 21:24 ` [PATCH V4 3/5] vfio/type1: revert "block on invalid vaddr" Steve Sistare
2022-12-14 21:24 ` [PATCH V4 4/5] vfio/type1: revert "implement notify callback" Steve Sistare
2022-12-14 21:24 ` [PATCH V4 5/5] vfio: revert "iommu driver " Steve Sistare
2022-12-14 21:42 ` [PATCH V4 0/5] fixes for virtual address update Alex Williamson
2022-12-15 14:25   ` Jason Gunthorpe
2022-12-15 18:37     ` Alex Williamson
  -- strict thread matches above, loose matches on Subject: below --
2022-12-15 21:53 Steve Sistare
2022-12-15 21:53 ` [PATCH V4 2/5] vfio/type1: prevent locked_vm underflow Steve Sistare

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Y5stJHFWK/ZLzA1q@nvidia.com \
    --to=jgg@nvidia.com \
    --cc=alex.williamson@redhat.com \
    --cc=cohuck@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=steven.sistare@oracle.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.