qemu-arm.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Auger Eric <eric.auger@redhat.com>
To: Peter Maydell <peter.maydell@linaro.org>
Cc: "patches@linaro.org" <patches@linaro.org>,
	"QEMU Developers" <qemu-devel@nongnu.org>,
	qemu-arm <qemu-arm@nongnu.org>,
	"Paolo Bonzini" <pbonzini@redhat.com>,
	"Alex Bennée" <alex.bennee@linaro.org>,
	"Richard Henderson" <rth@twiddle.net>
Subject: Re: [Qemu-devel] [PATCH 17/27] exec.c: Handle IOMMUs in address_space_translate_for_iotlb()
Date: Fri, 25 May 2018 11:50:43 +0200	[thread overview]
Message-ID: <e253665e-18db-49c7-afca-8284e975b80f@redhat.com> (raw)
In-Reply-To: <CAFEAcA--wyed8qg=4P-DG9QyZe1eACWh+68g99Gowx71CFAh_w@mail.gmail.com>

Hi Peter,

On 05/25/2018 10:52 AM, Peter Maydell wrote:
> On 24 May 2018 at 20:54, Auger Eric <eric.auger@redhat.com> wrote:
>> Hi Peter,
>>
>> On 05/23/2018 11:51 AM, Alex Bennée wrote:
>>>
>>> Peter Maydell <peter.maydell@linaro.org> writes:
>>>
>>>> Currently we don't support board configurations that put an IOMMU
>>>> in the path of the CPU's memory transactions, and instead just
>>>> assert() if the memory region fonud in address_space_translate_for_iotlb()
>> found
>>>> is an IOMMUMemoryRegion.
>>>>
>>>> Remove this limitation by having the function handle IOMMUs.
>>>> This is mostly straightforward, but we must make sure we have
>>>> a notifier registered for every IOMMU that a transaction has
>>>> passed through, so that we can flush the TLB appropriately
>> Can you elaborate on what (TCG) TLB we are talking about?
> 
> The TCG TLB, as implemented in accel/tcg/cputlb.c. Basically
> the thing that caches the results it gets back from the memory
> system so it can fast path device and memory accesses.
> 
>> The concept of IOMMUs downstream to a CPU is not obvious to me. Maybe an
>> example may be documented in the commit message?
> 
> The MPC implemented in this patchset is an example.
> 
> 
> 
>>>> +static void tcg_iommu_unmap_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
>>>> +{
>>>> +    TCGIOMMUNotifier *notifier = container_of(n, TCGIOMMUNotifier, n);
>>>> +
>>>> +    if (!notifier->active) {
>>>> +        return;
>>>> +    }
>>>> +    tlb_flush(notifier->cpu);
>>>> +    notifier->active = false;
>>>> +    /* We leave the notifier struct on the list to avoid reallocating it later.
>>>> +     * Generally the number of IOMMUs a CPU deals with will be small.
>>>> +     * In any case we can't unregister the iommu notifier from a notify
>>>> +     * callback.
>>>> +     */
>> I don't get the life cycle of the notifier and why it becomes inactive
>> after the invalidate. Could you detail the specificity of this one?
> 
> Once we've flushed the TLB it is empty and will have no cached
> information from the IOMMU. So there's no point in flushing the
> TLB again (which is expensive) until the next time a transaction
> goes through the IOMMU and we're caching something from it.
Ak OK. there is no finer granularity for TLB flush?

> 
> So the cycle goes:
>  * CPU makes transaction that goes through an IOMMU
>  * in tcg_register_iommu_notifier() we register the notifier
>    if we haven't already, and make sure it's got active = true
>  * in the unmap notify, we flush the whole TLB for the CPU, and
>    set active = false
>  * repeat...
OK thank you for the explanation
> 
> 
>>>> +static void tcg_iommu_notifier_destroy(gpointer data)
>>>> +{
>>>> +    TCGIOMMUNotifier *notifier = data;
>>>> +
>>>> +    if (notifier->active) {
>>>> +        memory_region_unregister_iommu_notifier(notifier->mr, &notifier->n);
>>>> +    }
>> Is it safe to leave the notifier registered to an IOMMU whereas it gets
>> freed?
> 
> Oh, this is a bug, left over from my first idea (which was to
> unregister the IOMMU notifier in the notifier unmap callback,
> in which case active == true would be the only case when we
> had a registered notifier).
> 
> We should unconditionally unregister the notifier here.
> 
> 
>>>>  /* Called from RCU critical section */
>>>>  MemoryRegionSection *
>>>>  address_space_translate_for_iotlb(CPUState *cpu, int asidx, hwaddr addr,
>>>> -                                  hwaddr *xlat, hwaddr *plen)
>>>> +                                  hwaddr *xlat, hwaddr *plen,
>>>> +                                  MemTxAttrs attrs, int *prot)
>>>>  {
>>>>      MemoryRegionSection *section;
>>>> +    IOMMUMemoryRegion *iommu_mr;
>>>> +    IOMMUMemoryRegionClass *imrc;
>>>> +    IOMMUTLBEntry iotlb;
>>>> +    int iommu_idx;
>>>>      AddressSpaceDispatch *d = atomic_rcu_read(&cpu->cpu_ases[asidx].memory_dispatch);
>>>>
>>>> -    section = address_space_translate_internal(d, addr, xlat, plen, false);
>>>> +    for (;;) {
>>>> +        section = address_space_translate_internal(d, addr, &addr, plen, false);
>>>> +
>>>> +        iommu_mr = memory_region_get_iommu(section->mr);
>>>> +        if (!iommu_mr) {
>>>> +            break;
>>>> +        }
>>>> +
>>>> +        imrc = memory_region_get_iommu_class_nocheck(iommu_mr);
>>>> +
>>>> +        iommu_idx = imrc->attrs_to_index(iommu_mr, attrs);
>>>> +        tcg_register_iommu_notifier(cpu, iommu_mr, iommu_idx);
>>>> +        /* We need all the permissions, so pass IOMMU_NONE so the IOMMU
>>>> +         * doesn't short-cut its translation table walk.
>> it is not clear to me why you don't use the access flag as you seem to
>> handle the perm fault after the translate() call.
> 
> We need to know all the permissions (because we'll cache the result
> in the TCG TLB and later use them for future read and write accesses),
> so we pass IOMMU_NONE.
> 
> My understanding from previous discussion is that the only
> reason to pass in some other access flag value is if you
> only care about one of read or write and want to allow the
> IOMMU to stop walking the page table early as soon as it decides
> it doesn't have permissions.

agreed. So you need to fetch the whole set of table permissions to
update the TLB. By the way where is the TLB updated?

Thanks

Eric
> 
> thanks
> -- PMM
> 

  reply	other threads:[~2018-05-25  9:50 UTC|newest]

Thread overview: 114+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-05-21 14:03 [PATCH 00/27] iommu: support txattrs, support TCG execution, implement TZ MPC Peter Maydell
2018-05-21 14:03 ` [PATCH 01/27] memory.h: Improve IOMMU related documentation Peter Maydell
2018-05-21 19:46   ` Richard Henderson
2018-05-22  9:16   ` Alex Bennée
2018-05-22 11:40   ` [Qemu-devel] " Auger Eric
2018-05-21 14:03 ` [PATCH 02/27] Make tb_invalidate_phys_addr() take a MemTxAttrs argument Peter Maydell
2018-05-21 23:54   ` Richard Henderson
2018-05-22  9:21   ` Alex Bennée
2018-05-21 14:03 ` [PATCH 03/27] Make address_space_translate{,_cached}() " Peter Maydell
2018-05-22 10:49   ` Alex Bennée
2018-05-22 16:12   ` Richard Henderson
2018-05-21 14:03 ` [PATCH 04/27] Make address_space_map() " Peter Maydell
2018-05-22 10:49   ` Alex Bennée
2018-05-22 16:13   ` Richard Henderson
2018-05-21 14:03 ` [PATCH 05/27] Make address_space_access_valid() " Peter Maydell
2018-05-22 10:50   ` Alex Bennée
2018-05-22 16:14   ` Richard Henderson
2018-05-21 14:03 ` [PATCH 06/27] Make flatview_extend_translation() " Peter Maydell
2018-05-22 10:56   ` Alex Bennée
2018-05-22 16:15   ` Richard Henderson
2018-05-21 14:03 ` [PATCH 07/27] Make memory_region_access_valid() " Peter Maydell
2018-05-22 10:57   ` Alex Bennée
2018-05-22 16:17   ` Richard Henderson
2018-05-21 14:03 ` [PATCH 08/27] Make MemoryRegion valid.accepts callback " Peter Maydell
2018-05-22 10:58   ` Alex Bennée
2018-05-22 16:20   ` Richard Henderson
2018-05-21 14:03 ` [PATCH 09/27] Make flatview_access_valid() " Peter Maydell
2018-05-22 10:58   ` Alex Bennée
2018-05-22 16:33   ` Richard Henderson
2018-05-22 16:37     ` Peter Maydell
2018-05-21 14:03 ` [PATCH 10/27] Make flatview_translate() " Peter Maydell
2018-05-22 10:58   ` Alex Bennée
2018-05-22 16:33   ` Richard Henderson
2018-05-21 14:03 ` [PATCH 11/27] Make address_space_get_iotlb_entry() " Peter Maydell
2018-05-22 11:00   ` Alex Bennée
2018-05-22 17:29   ` Richard Henderson
2018-05-21 14:03 ` [PATCH 12/27] Make flatview_do_translate() " Peter Maydell
2018-05-22 11:00   ` Alex Bennée
2018-05-22 17:29   ` Richard Henderson
2018-05-21 14:03 ` [PATCH 13/27] Make address_space_translate_iommu " Peter Maydell
2018-05-22 11:00   ` Alex Bennée
2018-05-22 17:30   ` Richard Henderson
2018-05-21 14:03 ` [PATCH 14/27] iommu: Add IOMMU index concept to IOMMU API Peter Maydell
2018-05-22  3:03   ` [Qemu-devel] " Peter Xu
2018-05-22  8:40     ` Peter Maydell
2018-05-22 11:02       ` Peter Xu
2018-05-22 11:11         ` Peter Maydell
2018-05-23  1:06           ` Peter Xu
2018-05-23 11:47             ` Peter Maydell
2018-05-24  6:23               ` Peter Xu
2018-05-24 10:54                 ` Peter Maydell
2018-05-25  2:50                   ` Peter Xu
2018-05-25  9:27                   ` Auger Eric
2018-05-25  9:34                     ` Peter Maydell
2018-05-22 12:58   ` Auger Eric
2018-05-22 13:22     ` Peter Maydell
2018-05-22 14:11       ` Auger Eric
2018-05-22 14:19         ` Peter Maydell
2018-05-22 14:22           ` Auger Eric
2018-05-22 17:42   ` Richard Henderson
2018-05-22 17:51     ` Peter Maydell
2018-05-22 17:52       ` Richard Henderson
2018-05-21 14:03 ` [PATCH 15/27] iommu: Add IOMMU index argument to notifier APIs Peter Maydell
2018-05-22 17:45   ` Richard Henderson
2018-05-23  9:08   ` Alex Bennée
2018-06-04 13:03     ` Peter Maydell
2018-06-04 15:09       ` Alex Bennée
2018-06-04 15:23         ` Peter Maydell
2018-05-24 15:29   ` [Qemu-devel] " Auger Eric
2018-05-24 17:03     ` Peter Maydell
2018-05-24 19:13       ` Auger Eric
2018-05-21 14:03 ` [PATCH 16/27] iommu: Add IOMMU index argument to translate method Peter Maydell
2018-05-22 18:06   ` Richard Henderson
2018-05-23  9:11   ` Alex Bennée
2018-05-21 14:03 ` [PATCH 17/27] exec.c: Handle IOMMUs in address_space_translate_for_iotlb() Peter Maydell
2018-05-23  9:51   ` Alex Bennée
2018-05-23 11:52     ` Peter Maydell
2018-05-24 19:54     ` [Qemu-devel] " Auger Eric
2018-05-25  8:52       ` Peter Maydell
2018-05-25  9:50         ` Auger Eric [this message]
2018-05-25  9:59           ` Peter Maydell
2018-05-21 14:03 ` [PATCH 18/27] hw/misc/tz-mpc.c: Implement the Arm TrustZone Memory Protection Controller Peter Maydell
2018-05-22 11:30   ` [Qemu-devel] " Auger Eric
2018-05-22 11:56     ` Peter Maydell
2018-05-22 12:23       ` Auger Eric
2018-05-23 10:41   ` Alex Bennée
2018-05-21 14:03 ` [PATCH 19/27] hw/misc/tz-mpc.c: Implement registers Peter Maydell
2018-05-23 10:44   ` Alex Bennée
2018-05-21 14:03 ` [PATCH 20/27] hw/misc/tz-mpc.c: Implement correct blocked-access behaviour Peter Maydell
2018-05-23 10:49   ` Alex Bennée
2018-05-23 11:54     ` Peter Maydell
2018-05-21 14:03 ` [PATCH 21/27] hw/misc/tz_mpc.c: Honour the BLK_LUT settings in translate Peter Maydell
2018-05-21 14:03 ` [PATCH 22/27] vmstate.h: Provide VMSTATE_BOOL_SUB_ARRAY Peter Maydell
2018-05-23 11:01   ` Alex Bennée
2018-05-21 14:03 ` [PATCH 23/27] hw/core/or-irq: Support more than 16 inputs to an OR gate Peter Maydell
2018-05-21 14:34   ` Paolo Bonzini
2018-05-21 15:02     ` Peter Maydell
2018-05-30 16:59       ` [Qemu-devel] " Paolo Bonzini
2018-05-30 17:35         ` Peter Maydell
2018-05-31 10:21           ` Paolo Bonzini
2018-05-31 10:50             ` Peter Maydell
2018-05-31 11:50               ` Paolo Bonzini
2018-05-31 11:59                 ` Peter Maydell
2018-05-21 14:03 ` [PATCH 24/27] hw/misc/iotkit-secctl.c: Implement SECMPCINTSTATUS Peter Maydell
2018-05-21 14:04 ` [PATCH 25/27] hw/arm/iotkit: Instantiate MPC Peter Maydell
2018-05-23 11:38   ` Alex Bennée
2018-05-21 14:04 ` [PATCH 26/27] hw/arm/iotkit: Wire up MPC interrupt lines Peter Maydell
2018-05-23 11:39   ` Alex Bennée
2018-05-21 14:04 ` [PATCH 27/27] hw/arm/mps2-tz.c: Instantiate MPCs Peter Maydell
2018-05-23 11:41   ` Alex Bennée
2018-05-21 15:10 ` [Qemu-devel] [PATCH 00/27] iommu: support txattrs, support TCG execution, implement TZ MPC no-reply
2018-05-30 16:58 ` Paolo Bonzini
2018-05-31  9:54   ` Peter Maydell
2018-05-31 13:37     ` Peter Maydell

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=e253665e-18db-49c7-afca-8284e975b80f@redhat.com \
    --to=eric.auger@redhat.com \
    --cc=alex.bennee@linaro.org \
    --cc=patches@linaro.org \
    --cc=pbonzini@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-arm@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=rth@twiddle.net \
    /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).