]> git.ipfire.org Git - thirdparty/kernel/stable-queue.git/blob - queue-4.19/powerpc-powernv-ioda-fix-locked_vm-counting-for-memo.patch
e2ac9f8eaa2f0c470933d85df1d22d3cefeff4e2
[thirdparty/kernel/stable-queue.git] / queue-4.19 / powerpc-powernv-ioda-fix-locked_vm-counting-for-memo.patch
1 From d547ae1ff1384b9d876237ae2ee3649098d921bd Mon Sep 17 00:00:00 2001
2 From: Alexey Kardashevskiy <aik@ozlabs.ru>
3 Date: Wed, 13 Feb 2019 14:38:18 +1100
4 Subject: powerpc/powernv/ioda: Fix locked_vm counting for memory used by IOMMU
5 tables
6
7 [ Upstream commit 11f5acce2fa43b015a8120fa7620fa4efd0a2952 ]
8
9 We store 2 multilevel tables in iommu_table - one for the hardware and
10 one with the corresponding userspace addresses. Before allocating
11 the tables, the iommu_table_group_ops::get_table_size() hook returns
12 the combined size of the two and VFIO SPAPR TCE IOMMU driver adjusts
13 the locked_vm counter correctly. When the table is actually allocated,
14 the amount of allocated memory is stored in iommu_table::it_allocated_size
15 and used to decrement the locked_vm counter when we release the memory
16 used by the table; .get_table_size() and .create_table() calculate it
17 independently but the result is expected to be the same.
18
19 However the allocator does not add the userspace table size to
20 .it_allocated_size so when we destroy the table because of VFIO PCI
21 unplug (i.e. VFIO container is gone but the userspace keeps running),
22 we decrement locked_vm by just a half of size of memory we are
23 releasing.
24
25 To make things worse, since we enabled on-demand allocation of
26 indirect levels, it_allocated_size contains only the amount of memory
27 actually allocated at the table creation time which can just be a
28 fraction. It is not a problem with incrementing locked_vm (as
29 get_table_size() value is used) but it is with decrementing.
30
31 As the result, we leak locked_vm and may not be able to allocate more
32 IOMMU tables after few iterations of hotplug/unplug.
33
34 This sets it_allocated_size in the pnv_pci_ioda2_ops::create_table()
35 hook to what pnv_pci_ioda2_get_table_size() returns so from now on we
36 have a single place which calculates the maximum memory a table can
37 occupy. The original meaning of it_allocated_size is somewhat lost now
38 though.
39
40 We do not ditch it_allocated_size whatsoever here and we do not call
41 get_table_size() from vfio_iommu_spapr_tce.c when decrementing
42 locked_vm as we may have multiple IOMMU groups per container and even
43 though they all are supposed to have the same get_table_size()
44 implementation, there is a small chance for failure or confusion.
45
46 Fixes: 090bad39b237 ("powerpc/powernv: Add indirect levels to it_userspace")
47 Signed-off-by: Alexey Kardashevskiy <aik@ozlabs.ru>
48 Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
49 Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
50 Signed-off-by: Sasha Levin <sashal@kernel.org>
51 ---
52 arch/powerpc/platforms/powernv/pci-ioda-tce.c | 1 -
53 arch/powerpc/platforms/powernv/pci-ioda.c | 7 ++++++-
54 2 files changed, 6 insertions(+), 2 deletions(-)
55
56 diff --git a/arch/powerpc/platforms/powernv/pci-ioda-tce.c b/arch/powerpc/platforms/powernv/pci-ioda-tce.c
57 index 7639b2168755..f5adb6b756f7 100644
58 --- a/arch/powerpc/platforms/powernv/pci-ioda-tce.c
59 +++ b/arch/powerpc/platforms/powernv/pci-ioda-tce.c
60 @@ -313,7 +313,6 @@ long pnv_pci_ioda2_table_alloc_pages(int nid, __u64 bus_offset,
61 page_shift);
62 tbl->it_level_size = 1ULL << (level_shift - 3);
63 tbl->it_indirect_levels = levels - 1;
64 - tbl->it_allocated_size = total_allocated;
65 tbl->it_userspace = uas;
66 tbl->it_nid = nid;
67
68 diff --git a/arch/powerpc/platforms/powernv/pci-ioda.c b/arch/powerpc/platforms/powernv/pci-ioda.c
69 index cde710297a4e..326ca6288bb1 100644
70 --- a/arch/powerpc/platforms/powernv/pci-ioda.c
71 +++ b/arch/powerpc/platforms/powernv/pci-ioda.c
72 @@ -2603,8 +2603,13 @@ static long pnv_pci_ioda2_create_table_userspace(
73 int num, __u32 page_shift, __u64 window_size, __u32 levels,
74 struct iommu_table **ptbl)
75 {
76 - return pnv_pci_ioda2_create_table(table_group,
77 + long ret = pnv_pci_ioda2_create_table(table_group,
78 num, page_shift, window_size, levels, true, ptbl);
79 +
80 + if (!ret)
81 + (*ptbl)->it_allocated_size = pnv_pci_ioda2_get_table_size(
82 + page_shift, window_size, levels);
83 + return ret;
84 }
85
86 static void pnv_ioda2_take_ownership(struct iommu_table_group *table_group)
87 --
88 2.19.1
89