From: David Gibson <david@gibson.dropbear.id.au>
To: Alexey Kardashevskiy <aik@ozlabs.ru>
Cc: linuxppc-dev@lists.ozlabs.org,
Alex Williamson <alex.williamson@redhat.com>,
Paul Mackerras <paulus@samba.org>,
kvm@vger.kernel.org
Subject: Re: [PATCH kernel v5 5/6] vfio/spapr: Reference mm in tce_container
Date: Wed, 23 Nov 2016 12:36:55 +1100 [thread overview]
Message-ID: <20161123013655.GI28479@umbus.fritz.box> (raw)
In-Reply-To: <9e20c926-c600-dedd-7e71-f8585ebe554b@ozlabs.ru>
[-- Attachment #1: Type: text/plain, Size: 4777 bytes --]
On Tue, Nov 22, 2016 at 06:34:25PM +1100, Alexey Kardashevskiy wrote:
> On 22/11/16 14:49, Alexey Kardashevskiy wrote:
> > On 22/11/16 13:38, David Gibson wrote:
> >> On Thu, Nov 17, 2016 at 06:39:41PM +1100, Alexey Kardashevskiy wrote:
> >>> On 11/11/16 23:32, Alexey Kardashevskiy wrote:
> >>>> In some situations the userspace memory context may live longer than
> >>>> the userspace process itself so if we need to do proper memory context
> >>>> cleanup, we better have tce_container take a reference to mm_struct and
> >>>> use it later when the process is gone (@current or @current->mm is NULL).
> >>>>
> >>>> This references mm and stores the pointer in the container; this is done
> >>>> in a new helper - tce_iommu_mm_set() - when one of the following happens:
> >>>> - a container is enabled (IOMMU v1);
> >>>> - a first attempt to pre-register memory is made (IOMMU v2);
> >>>> - a DMA window is created (IOMMU v2).
> >>>> The @mm stays referenced till the container is destroyed.
> >>>>
> >>>> This replaces current->mm with container->mm everywhere except debug
> >>>> prints.
> >>>>
> >>>> This adds a check that current->mm is the same as the one stored in
> >>>> the container to prevent userspace from making changes to a memory
> >>>> context of other processes.
> >>>>
> >>>> DMA map/unmap ioctls() do not check for @mm as they already check
> >>>> for @enabled which is set after tce_iommu_mm_set() is called.
> >>>>
> >>>> Signed-off-by: Alexey Kardashevskiy <aik@ozlabs.ru>
> >>>> ---
> >>>> Changes:
> >>>> v5:
> >>>> * postpone referencing of mm
> >>>>
> >>>> v4:
> >>>> * added check for container->mm!=current->mm in tce_iommu_ioctl()
> >>>> for all ioctls and removed other redundand checks
> >>>> ---
> >>>> drivers/vfio/vfio_iommu_spapr_tce.c | 159 ++++++++++++++++++++++--------------
> >>>> 1 file changed, 99 insertions(+), 60 deletions(-)
> >>>>
> >>>> diff --git a/drivers/vfio/vfio_iommu_spapr_tce.c b/drivers/vfio/vfio_iommu_spapr_tce.c
> >>>> index 1c02498..9a81a7e 100644
> >>>> --- a/drivers/vfio/vfio_iommu_spapr_tce.c
> >>>> +++ b/drivers/vfio/vfio_iommu_spapr_tce.c
> >>>> @@ -31,49 +31,49 @@
> >>>> static void tce_iommu_detach_group(void *iommu_data,
> >>>> struct iommu_group *iommu_group);
> >>>>
> >>>> -static long try_increment_locked_vm(long npages)
> >>>> +static long try_increment_locked_vm(struct mm_struct *mm, long npages)
> >>>> {
> >>>> long ret = 0, locked, lock_limit;
> >>>>
> >>>> - if (!current || !current->mm)
> >>>> - return -ESRCH; /* process exited */
> >>>> + if (!mm)
> >>>> + return -EPERM;
> >>>>
> >>>> if (!npages)
> >>>> return 0;
> >>>>
> >>>> - down_write(¤t->mm->mmap_sem);
> >>>> - locked = current->mm->locked_vm + npages;
> >>>> + down_write(&mm->mmap_sem);
> >>>> + locked = mm->locked_vm + npages;
> >>>> lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
> >>>> if (locked > lock_limit && !capable(CAP_IPC_LOCK))
> >>>
> >>>
> >>>
> >>> Oh boy. Now it seems I have to reference a task, not just mm (which I may
> >>> not have to reference at all after all as the task reference should keep mm
> >>> alive) as I missed the fact capable() and rlimit() are working with
> >>> @current.
> >>
> >> Sorry, what? I'm not seeing how a task reference comes into this.
> >
> > I reference @mm to make sure that just one mm uses a container. If mm
> > changes, I return an error, sanity check.
> >
> > The code also increments locked_vm in mm. But it looks at the current task
> > if there is room for increments and for CAP_IPC_LOCK.
> >
> > So, the options are:
> > 1. I do not reference the current task, and if mm changes, then the mm
> > sanity check won't let me proceed to the code which tries using current OR
> > 2. reference a task when I reference mm and do that sanity check not just
> > for mm but also for current task.
> >
> > Makes sense?
>
> I had a chat with Nick and now I think that having mm referenced and
> checked should be enough and I do not need to reference the task as
> multiple threads within the same mm are allowed to ioctl() to vfio and
> supposedly they will have same limits and capabilities and if they do not,
> we'll just fail and that's it, I cannot see any harm from this approach,
> can you?
Yeah, that sounds sane to me. If the different threads in the mm
somehow have different caps / limits, we could get some weird results
depending on which thread attempts to do the mapping, but it shouldn't
actually be harmful.
--
David Gibson | I'll have my music baroque, and my code
david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_
| _way_ _around_!
http://www.ozlabs.org/~dgibson
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 819 bytes --]
next prev parent reply other threads:[~2016-11-23 2:02 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-11-11 12:32 [PATCH kernel v5 0/6] powerpc/spapr/vfio: Put pages on VFIO container shutdown Alexey Kardashevskiy
2016-11-11 12:32 ` [PATCH kernel v5 1/6] powerpc/iommu: Pass mm_struct to init/cleanup helpers Alexey Kardashevskiy
2016-11-11 12:32 ` [PATCH kernel v5 2/6] powerpc/iommu: Stop using @current in mm_iommu_xxx Alexey Kardashevskiy
2016-11-11 12:32 ` [PATCH kernel v5 3/6] vfio/spapr: Postpone allocation of userspace version of TCE table Alexey Kardashevskiy
2016-11-21 23:27 ` David Gibson
2016-11-11 12:32 ` [PATCH kernel v5 4/6] vfio/spapr: Postpone default window creation Alexey Kardashevskiy
2016-11-22 2:50 ` David Gibson
2016-11-22 7:29 ` Alexey Kardashevskiy
2016-11-23 1:35 ` David Gibson
2016-11-23 5:06 ` Alexey Kardashevskiy
2016-11-24 4:08 ` David Gibson
2016-11-11 12:32 ` [PATCH kernel v5 5/6] vfio/spapr: Reference mm in tce_container Alexey Kardashevskiy
2016-11-17 7:39 ` Alexey Kardashevskiy
2016-11-17 21:56 ` Alex Williamson
2016-11-22 2:38 ` David Gibson
2016-11-22 3:49 ` Alexey Kardashevskiy
2016-11-22 7:34 ` Alexey Kardashevskiy
2016-11-23 1:36 ` David Gibson [this message]
2016-11-11 12:32 ` [PATCH kernel v5 6/6] powerpc/mm/iommu, vfio/spapr: Put pages on VFIO container shutdown Alexey Kardashevskiy
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=20161123013655.GI28479@umbus.fritz.box \
--to=david@gibson.dropbear.id.au \
--cc=aik@ozlabs.ru \
--cc=alex.williamson@redhat.com \
--cc=kvm@vger.kernel.org \
--cc=linuxppc-dev@lists.ozlabs.org \
--cc=paulus@samba.org \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).