mmu_notifier: add mmu_notifier_invalidate_range()
mmu_notifier: add mmu_notifier_invalidate_range()

This notifier closes an important gap in the current mmu_notifier
implementation, the existing callbacks are called too early or too late to
reliably manage a non-CPU TLB.  Specifically, invalidate_range_start() is
called when all pages are still mapped and invalidate_range_end() when all
pages are unmapped and potentially freed.

This is fine when the users of the mmu_notifiers manage their own SoftTLB,
like KVM does.  When the TLB is managed in software it is easy to wipe out
entries for a given range and prevent new entries to be established until
invalidate_range_end is called.

But when the user of mmu_notifiers has to manage a hardware TLB it can
still wipe out TLB entries in invalidate_range_start, but it can't make
sure that no new TLB entries in the given range are established between
invalidate_range_start and invalidate_range_end.

To avoid silent data corruption the entries in the non-CPU TLB need to be
flushed when the pages are unmapped (at this point in time no _new_ TLB
entries can be established in the non-CPU TLB) but not yet freed (as the
non-CPU TLB may still have _existing_ entries pointing to the pages about
to be freed).

To fix this problem we need to catch the moment when the Linux VMM flushes
remote TLBs (as a non-CPU TLB is not very CPU TLB), as this is the point
in time when the pages are unmapped but _not_ yet freed.

The mmu_notifier_invalidate_range() function aims to catch that moment.

IOMMU code will be one user of the notifier-callback.  Currently this is
only the AMD IOMMUv2 driver, but its code is about to be more generalized
and converted to a generic IOMMU-API extension to fit the needs of similar
functionality in other IOMMUs as well.

The current attempt in the AMD IOMMUv2 driver to work around the
invalidate_range_start/end() shortcoming is to assign an empty page table
to the non-CPU TLB between any invalidata_range_start/end calls.  With the
empty page-table assigned, every page-table walk to re-fill the non-CPU
TLB will cause a page-fault reported to the IOMMU driver via an interrupt,
possibly causing interrupt storms.

The page-fault handler in the AMD IOMMUv2 driver doesn't handle the fault
if an invalidate_range_start/end pair is active, it just reports back
SUCCESS to the device and let it refault the page.  But existing hardware
(newer Radeon GPUs) that makes use of this feature don't re-fault
indefinitly, after a certain number of faults for the same address the
device enters a failure state and needs to be resetted.

To avoid the GPUs entering a failure state we need to get rid of the
empty-page-table workaround and use the mmu_notifier_invalidate_range()
function introduced with this patch.

Signed-off-by: Joerg Roedel <61aff96566804ea1da8a65de5bcc892ce07caceb@suse.de>
Reviewed-by: Andrea Arcangeli <6cb163d975a8c4c46420677e0054b55c39bdb339@redhat.com>
Reviewed-by: Jérôme Glisse <0620d428a1bd3756b1319066bf466b3b8be0b2b2@redhat.com>
Cc: Peter Zijlstra <645ca7d3a8d3d4f60557176cd361ea8351edc32b@chello.nl>
Cc: Rik van Riel <a21938f5d463ddf41aa718934c205ca2cce8ebbc@redhat.com>
Cc: Hugh Dickins <d3abdf3e2800e6cb849f11b81e6eb50b34d96431@google.com>
Cc: Mel Gorman <39ea6a09710987173e32e37c86918024a0f441c1@suse.de>
Cc: Johannes Weiner <5332d5d6802f0c4180a1f65eb38cf05f0ccdfd97@redhat.com>
Cc: Jay Cornwall <42b6848d3a77d11f80610341ab7fdb229b7fd4dd@amd.com>
Cc: Oded Gabbay <3ff97af39cccd19ff306a235c07f733018def4e5@amd.com>
Cc: Suravee Suthikulpanit <ed04488ebbbd0c8ebb072b7ab25add9fa816eb84@amd.com>
Cc: Jesse Barnes <bc7add126c2dbb8382bf1c28ac262b9363a32706@virtuousgeek.org>
Cc: David Woodhouse <97b3379caa91f4ee97e44013ae4dc6350540fa9d@infradead.org>
Signed-off-by: Andrew Morton <5c1e68b099950c134891f0b6e179498a8ebe9cf9@linux-foundation.org>
Signed-off-by: Oded Gabbay <c2fce4493a16d94b58efb315151c29911fc5d347@amd.com>

KristFoundation/Programs,KristFoundation/Programs,KristFoundation/Programs,KristFoundation/Programs,KristFoundation/Programs,KristFoundation/Programs