powerpc/iommu: Stop crashing the host when VM is terminated
Subsystems (2)
cover powerpc/iommu: Stop crashing the host when VM is terminated
Killing a VM on a host under memory pressure kills a host which is annoying. 1/2 reduces the chances, 2/2 eliminates panic() on ioda2.
This is based on sha1f40ddce88593 Linus Torvalds "Linux 5.11".
Please comment. Thanks.
Alexey Kardashevskiy (2):
powerpc/iommu: Allocate it_map by vmalloc powerpc/iommu: Do not immediately panic when failed IOMMU table
allocation
arch/powerpc/kernel/iommu.c | 19 ++++++------------- arch/powerpc/platforms/cell/iommu.c | 3 ++- arch/powerpc/platforms/pasemi/iommu.c | 4 +++- arch/powerpc/platforms/powernv/pci-ioda.c | 15 ++++++++------- arch/powerpc/platforms/pseries/iommu.c | 10 +++++++--- arch/powerpc/sysdev/dart_iommu.c | 3 ++- 6 files changed, 28 insertions(+), 26 deletions(-)
Signature · 1 line
2.17.1
PATCH 1/2 powerpc/iommu: Allocate it_map by vmalloc
The IOMMU table uses the it_map bitmap to keep track of allocated DMA pages. This has always been a contiguous array allocated at either the boot time or when a passed through device is returned to the host OS. The it_map memory is allocated by alloc_pages() which allocates contiguous physical memory.
Such allocation method occasionally creates a problem when there is no big chunk of memory available (no free memory or too fragmented). On powernv/ioda2 the default DMA window requires 16MB for it_map.
This replaces alloc_pages_node() with vzalloc_node() which allocates contiguous block but in virtual memory. This should reduce changes of failure but should not cause other behavioral changes as it_map is only used by the kernel's DMA hooks/api when MMU is on.
--- arch/powerpc/kernel/iommu.c | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-)
arch/powerpc/kernel/iommu.c +3 -12
@@ -719,7 +719,6 @@ struct iommu_table *iommu_init_table(struct iommu_table *tbl, int nid, {unsigned long sz;static int welcomed =0 ; -struct page *page;unsigned int i;struct iommu_pool *p;
@@ -728,11 +727,9 @@ struct iommu_table *iommu_init_table(struct iommu_table *tbl, int nid,/* number of bytes needed for the bitmap */ sz =BITS_TO_LONGS (tbl->it_size) *sizeof (unsigned long ); - page =alloc_pages_node (nid,GFP_KERNEL ,get_order (sz)); -if (!page) + tbl->it_map =vzalloc_node (sz, nid); +if (!tbl->it_map)panic ("iommu_init_table: Can't allocate %ld bytes\n" , sz); - tbl->it_map =page_address (page); -memset (tbl->it_map,0 , sz);iommu_table_reserve_pages (tbl, res_start, res_end);
@@ -774,8 +771,6 @@ struct iommu_table *iommu_init_table(struct iommu_table *tbl, int nid,static void iommu_table_free (struct kref *kref) { -unsigned long bitmap_sz; -unsigned int order;struct iommu_table *tbl; tbl = container_of(kref, struct iommu_table, it_kref);
@@ -796,12 +791,8 @@ static void iommu_table_free(struct kref *kref)if (!bitmap_empty (tbl->it_map, tbl->it_size))pr_warn ("%s: Unexpected TCEs\n" , __func__); -/* calculate bitmap size in bytes */ - bitmap_sz =BITS_TO_LONGS (tbl->it_size) *sizeof (unsigned long ); -/* free bitmap */ - order =get_order (bitmap_sz); -free_pages ((unsigned long ) tbl->it_map, order); +vfree (tbl->it_map);/* free table */ kfree (tbl);
Signature · 1 line
2.17.1
Re: [PATCH kernel 1/2] powerpc/iommu: Allocate it_map by vmalloc
Quoted 10 lines · Alexey Kardashevskiy
The IOMMU table uses the
it_mapbitmap to keep track of allocated DMA pages. This has always been a contiguous array allocated at either the boot time or when a passed through device is returned to the host OS. Theit_mapmemory is allocated byalloc_pages()which allocates contiguous physical memory.Such allocation method occasionally creates a problem when there is no big chunk of memory available (no free memory or too fragmented). On powernv/ioda2 the default DMA window requires 16MB for
it_map.
Quoted 6 lines
This replaces
alloc_pages_node()withvzalloc_node()which allocates contiguous block but in virtual memory. This should reduce changes of failure but should not cause other behavioral changes asit_mapis only used by the kernel's DMA hooks/api when MMU is on.Signed-off-by Alexey Kardashevskiy <[email protected]>
Quoted 53 lines
--- arch/powerpc/kernel/iommu.c | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-)arch/powerpc/kernel/iommu.c +3
-12@@ -719,7 +719,6 @@ struct iommu_table *iommu_init_table(struct iommu_table *tbl, int nid, {unsigned long sz;static int welcomed =0 ; -struct page *page;unsigned int i;struct iommu_pool *p;@@ -728,11 +727,9 @@ struct iommu_table *iommu_init_table(struct iommu_table *tbl, int nid,/* number of bytes needed for the bitmap */ sz =BITS_TO_LONGS (tbl->it_size) *sizeof (unsigned long ); - page =alloc_pages_node (nid,GFP_KERNEL ,get_order (sz)); -if (!page) + tbl->it_map =vzalloc_node (sz, nid); +if (!tbl->it_map)panic ("iommu_init_table: Can't allocate %ld bytes\n" , sz); - tbl->it_map =page_address (page); -memset (tbl->it_map,0 , sz);iommu_table_reserve_pages (tbl, res_start, res_end);@@ -774,8 +771,6 @@ struct iommu_table *iommu_init_table(struct iommu_table *tbl, int nid,static void iommu_table_free (struct kref *kref) { -unsigned long bitmap_sz; -unsigned int order;struct iommu_table *tbl; tbl = container_of(kref, struct iommu_table, it_kref);@@ -796,12 +791,8 @@ static void iommu_table_free(struct kref *kref)if (!bitmap_empty (tbl->it_map, tbl->it_size))pr_warn ("%s: Unexpected TCEs\n" , __func__); -/* calculate bitmap size in bytes */ - bitmap_sz =BITS_TO_LONGS (tbl->it_size) *sizeof (unsigned long ); -/* free bitmap */ - order =get_order (bitmap_sz); -free_pages ((unsigned long ) tbl->it_map, order); +vfree (tbl->it_map);/* free table */ kfree (tbl);
Signature · 4 lines
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
Re: [PATCH kernel 1/2] powerpc/iommu: Allocate it_map by vmalloc
In reply to Alexey KardashevskiyQuoted 10 lines · Alexey Kardashevskiy
The IOMMU table uses the
it_mapbitmap to keep track of allocated DMA pages. This has always been a contiguous array allocated at either the boot time or when a passed through device is returned to the host OS. Theit_mapmemory is allocated byalloc_pages()which allocates contiguous physical memory.Such allocation method occasionally creates a problem when there is no big chunk of memory available (no free memory or too fragmented). On powernv/ioda2 the default DMA window requires 16MB for
it_map.
Quoted 6 lines
This replaces
alloc_pages_node()withvzalloc_node()which allocates contiguous block but in virtual memory. This should reduce changes of failure but should not cause other behavioral changes asit_mapis only used by the kernel's DMA hooks/api when MMU is on.Signed-off-by Alexey Kardashevskiy <[email protected]>
It looks a very good change, and also makes code much simpler to read.
FWIW:
Quoted 77 lines
---
arch/powerpc/kernel/iommu.c | 15 +++------------1 file changed, 3 insertions(+), 12 deletions(-)arch/powerpc/kernel/iommu.c +0
-1@@ -719,7 +719,6 @@ struct iommu_table *iommu_init_table(struct iommu_table *tbl, int nid, { unsigned long sz; static int welcomed = 0; -struct page *page; unsigned int i; struct iommu_pool *p;arch/powerpc/kernel/iommu.c +0
-0@@ -728,11 +727,9 @@ struct iommu_table *iommu_init_table(struct iommu_table *tbl, int nid, /* number of bytes needed for the bitmap */ sz = BITS_TO_LONGS(tbl->it_size) * sizeof(unsigned long);arch/powerpc/kernel/iommu.c +2
-2- page =alloc_pages_node (nid,GFP_KERNEL ,get_order (sz)); -if (!page) + tbl->it_map =vzalloc_node (sz, nid); +if (!tbl->it_map)
panic("iommu_init_table: Can't allocate %ld bytes\n", sz);arch/powerpc/kernel/iommu.c +0
-2- tbl->it_map =page_address (page); -memset (tbl->it_map,0 , sz);
iommu_table_reserve_pages(tbl, res_start, res_end);arch/powerpc/kernel/iommu.c +0
-0@@ -774,8 +771,6 @@ struct iommu_table *iommu_init_table(struct iommu_table *tbl, int nid,static void
iommu_table_free(struct kref *kref)
{arch/powerpc/kernel/iommu.c +0
-2-unsigned long bitmap_sz; -unsigned int order;struct
iommu_table*tbl;tbl =
container_of(kref, struct iommu_table, it_kref);arch/powerpc/kernel/iommu.c +0
-0@@ -796,12 +791,8 @@ static void iommu_table_free(struct kref *kref) if (!bitmap_empty(tbl->it_map, tbl->it_size)) pr_warn("%s: Unexpected TCEs\n", __func__);arch/powerpc/kernel/iommu.c +0
-3-/* calculate bitmap size in bytes */ - bitmap_sz =BITS_TO_LONGS (tbl->it_size) *sizeof (unsigned long ); -/* free bitmap */
arch/powerpc/kernel/iommu.c +1
-2- order =get_order (bitmap_sz); -free_pages ((unsigned long ) tbl->it_map, order); +vfree (tbl->it_map);/* free table */
kfree(tbl);
PATCH 2/2 powerpc/iommu: Do not immediately panic when failed IOMMU table allocation
In reply to Alexey KardashevskiyMost platforms allocate IOMMU table structures (specifically it_map) at the boot time and when this fails - it is a valid reason for panic().
However the powernv platform allocates it_map after a device is returned to the host OS after being passed through and this happens long after the host OS booted. It is quite possible to trigger the it_map allocation panic() and kill the host even though it is not necessary - the host OS can still use the DMA bypass mode (requires a tiny fraction of it_map's memory) and even if that fails, the host OS is runnnable as it was without the device for which allocating it_map causes the panic.
Instead of immediately crashing in a powernv/ioda2 system, this prints an error and continues. All other platforms still call panic().
--- arch/powerpc/kernel/iommu.c | 6 ++++-- arch/powerpc/platforms/cell/iommu.c | 3 ++- arch/powerpc/platforms/pasemi/iommu.c | 4 +++- arch/powerpc/platforms/powernv/pci-ioda.c | 15 ++++++++------- arch/powerpc/platforms/pseries/iommu.c | 10 +++++++--- arch/powerpc/sysdev/dart_iommu.c | 3 ++- 6 files changed, 26 insertions(+), 15 deletions(-)
arch/powerpc/kernel/iommu.c +4 -2
@@ -728,8 +728,10 @@ struct iommu_table *iommu_init_table(struct iommu_table *tbl, int nid, sz =BITS_TO_LONGS (tbl->it_size) *sizeof (unsigned long ); tbl->it_map =vzalloc_node (sz, nid); -if (!tbl->it_map) -panic ("iommu_init_table: Can't allocate %ld bytes\n" , sz); +if (!tbl->it_map) { +pr_err ("%s: Can't allocate %ld bytes\n" , __func__, sz); +return NULL ; + }iommu_table_reserve_pages (tbl, res_start, res_end);
arch/powerpc/platforms/cell/iommu.c +2 -1
@@ -486,7 +486,8 @@ cell_iommu_setup_window(struct cbe_iommu *iommu, struct device_node *np, window->table.it_size = size >> window->table.it_page_shift; window->table.it_ops = &cell_iommu_ops; -iommu_init_table (&window->table, iommu->nid,0 ,0 ); +if (!iommu_init_table (&window->table, iommu->nid,0 ,0 )) +panic ("Failed to initialize iommu table" );pr_debug ("\tioid %d\n" , window->ioid);pr_debug ("\tblocksize %ld\n" , window->table.it_blocksize);
arch/powerpc/platforms/pasemi/iommu.c +3 -1
@@ -146,7 +146,9 @@ static void iommu_table_iobmap_setup(void)*/ iommu_table_iobmap.it_blocksize =4 ; iommu_table_iobmap.it_ops = &iommu_table_iobmap_ops; -iommu_init_table (&iommu_table_iobmap,0 ,0 ,0 ); +if (!iommu_init_table (&iommu_table_iobmap,0 ,0 ,0 )) +panic ("Failed to initialize iommu table" ); +pr_debug (" <- %s\n" , __func__); }
arch/powerpc/platforms/powernv/pci-ioda.c +8 -7
@@ -1762,7 +1762,8 @@ static void pnv_pci_ioda1_setup_dma_pe(struct pnv_phb *phb, tbl->it_ops = &pnv_ioda1_iommu_ops; pe->table_group.tce32_start = tbl->it_offset << tbl->it_page_shift; pe->table_group.tce32_size = tbl->it_size << tbl->it_page_shift; -iommu_init_table (tbl, phb->hose->node,0 ,0 ); +if (!iommu_init_table (tbl, phb->hose->node,0 ,0 )) +panic ("Failed to initialize iommu table" ); pe->dma_setup_done = true;return ;
@@ -1930,16 +1931,16 @@ static long pnv_pci_ioda2_setup_default_config(struct pnv_ioda_pe *pe) res_start = pe->phb->ioda.m32_pci_base >> tbl->it_page_shift; res_end =min (window_size,SZ_4G ) >> tbl->it_page_shift; } -iommu_init_table (tbl, pe->phb->hose->node, res_start, res_end); - rc =pnv_pci_ioda2_set_window (&pe->table_group,0 , tbl); +if (iommu_init_table (tbl, pe->phb->hose->node, res_start, res_end)) + rc =pnv_pci_ioda2_set_window (&pe->table_group,0 , tbl); +else + rc = -ENOMEM ;if (rc) { -pe_err (pe,"Failed to configure 32-bit TCE table, err %ld\n" , - rc); +pe_err (pe,"Failed to configure 32-bit TCE table, err %ld\n" , rc);iommu_tce_table_put (tbl); -return rc; + tbl =NULL ;/* This clears iommu_table_base below */ } -if (!pnv_iommu_bypass_disabled)pnv_pci_ioda2_set_bypass (pe, true);
arch/powerpc/platforms/pseries/iommu.c +7 -3
@@ -638,7 +638,8 @@ static void pci_dma_bus_setup_pSeries(struct pci_bus *bus)iommu_table_setparms (pci->phb, dn, tbl); tbl->it_ops = &iommu_table_pseries_ops; -iommu_init_table (tbl, pci->phb->node,0 ,0 ); +if (!iommu_init_table (tbl, pci->phb->node,0 ,0 )) +panic ("Failed to initialize iommu table" );/* Divide the rest (1.75GB) among the children */ pci->phb->dma_window_size =0x80000000ul ;
@@ -720,7 +721,8 @@ static void pci_dma_bus_setup_pSeriesLP(struct pci_bus *bus)iommu_table_setparms_lpar (ppci->phb, pdn, tbl, ppci->table_group, dma_window); tbl->it_ops = &iommu_table_lpar_multi_ops; -iommu_init_table (tbl, ppci->phb->node,0 ,0 ); +if (!iommu_init_table (tbl, ppci->phb->node,0 ,0 )) +panic ("Failed to initialize iommu table" );iommu_register_group (ppci->table_group,pci_domain_nr (bus),0 );pr_debug (" created table: %p\n" , ppci->table_group);
@@ -749,7 +751,9 @@ static void pci_dma_dev_setup_pSeries(struct pci_dev *dev) tbl =PCI_DN (dn)->table_group->tables[0 ];iommu_table_setparms (phb, dn, tbl); tbl->it_ops = &iommu_table_pseries_ops; -iommu_init_table (tbl, phb->node,0 ,0 ); +if (!iommu_init_table (tbl, phb->node,0 ,0 )) +panic ("Failed to initialize iommu table" ); +set_iommu_table_base (&dev->dev, tbl);return ; }
arch/powerpc/sysdev/dart_iommu.c +2 -1
@@ -344,7 +344,8 @@ static void iommu_table_dart_setup(void) iommu_table_dart.it_index =0 ; iommu_table_dart.it_blocksize =1 ; iommu_table_dart.it_ops = &iommu_dart_ops; -iommu_init_table (&iommu_table_dart,-1 ,0 ,0 ); +if (!iommu_init_table (&iommu_table_dart,-1 ,0 ,0 )) +panic ("Failed to initialize iommu table" );/* Reserve the last page of the DART to avoid possible prefetch * past the DART mapped area
Signature · 1 line
2.17.1
Re: [PATCH kernel 2/2] powerpc/iommu: Do not immediately panic when failed IOMMU table allocation
Quoted 11 lines · Alexey Kardashevskiy
Most platforms allocate IOMMU table structures (specifically
it_map) at the boot time and when this fails - it is a valid reason forpanic().However the powernv platform allocates
it_mapafter a device is returned to the host OS after being passed through and this happens long after the host OS booted. It is quite possible to trigger theit_mapallocationpanic()and kill the host even though it is not necessary - the host OS can still use the DMA bypass mode (requires a tiny fraction of it_map's memory) and even if that fails, the host OS is runnnable as it was without the device for which allocatingit_mapcauses the panic.
Quoted 4 lines
Instead of immediately crashing in a powernv/ioda2 system, this prints an error and continues. All other platforms still call
panic().Signed-off-by Alexey Kardashevskiy <[email protected]>
Quoted 141 lines
--- arch/powerpc/kernel/iommu.c | 6 ++++-- arch/powerpc/platforms/cell/iommu.c | 3 ++- arch/powerpc/platforms/pasemi/iommu.c | 4 +++- arch/powerpc/platforms/powernv/pci-ioda.c | 15 ++++++++------- arch/powerpc/platforms/pseries/iommu.c | 10 +++++++--- arch/powerpc/sysdev/dart_iommu.c | 3 ++- 6 files changed, 26 insertions(+), 15 deletions(-)arch/powerpc/kernel/iommu.c +4
-2@@ -728,8 +728,10 @@ struct iommu_table *iommu_init_table(struct iommu_table *tbl, int nid, sz =BITS_TO_LONGS (tbl->it_size) *sizeof (unsigned long ); tbl->it_map =vzalloc_node (sz, nid); -if (!tbl->it_map) -panic ("iommu_init_table: Can't allocate %ld bytes\n" , sz); +if (!tbl->it_map) { +pr_err ("%s: Can't allocate %ld bytes\n" , __func__, sz); +return NULL ; + }iommu_table_reserve_pages (tbl, res_start, res_end);arch/powerpc/platforms/cell/iommu.c +2
-1@@ -486,7 +486,8 @@ cell_iommu_setup_window(struct cbe_iommu *iommu, struct device_node *np, window->table.it_size = size >> window->table.it_page_shift; window->table.it_ops = &cell_iommu_ops; -iommu_init_table (&window->table, iommu->nid,0 ,0 ); +if (!iommu_init_table (&window->table, iommu->nid,0 ,0 )) +panic ("Failed to initialize iommu table" );pr_debug ("\tioid %d\n" , window->ioid);pr_debug ("\tblocksize %ld\n" , window->table.it_blocksize);arch/powerpc/platforms/pasemi/iommu.c +3
-1@@ -146,7 +146,9 @@ static void iommu_table_iobmap_setup(void)*/ iommu_table_iobmap.it_blocksize =4 ; iommu_table_iobmap.it_ops = &iommu_table_iobmap_ops; -iommu_init_table (&iommu_table_iobmap,0 ,0 ,0 ); +if (!iommu_init_table (&iommu_table_iobmap,0 ,0 ,0 )) +panic ("Failed to initialize iommu table" ); +pr_debug (" <- %s\n" , __func__); }arch/powerpc/platforms/powernv/pci-ioda.c +8
-7@@ -1762,7 +1762,8 @@ static void pnv_pci_ioda1_setup_dma_pe(struct pnv_phb *phb, tbl->it_ops = &pnv_ioda1_iommu_ops; pe->table_group.tce32_start = tbl->it_offset << tbl->it_page_shift; pe->table_group.tce32_size = tbl->it_size << tbl->it_page_shift; -iommu_init_table (tbl, phb->hose->node,0 ,0 ); +if (!iommu_init_table (tbl, phb->hose->node,0 ,0 )) +panic ("Failed to initialize iommu table" ); pe->dma_setup_done = true;return ;@@ -1930,16 +1931,16 @@ static long pnv_pci_ioda2_setup_default_config(struct pnv_ioda_pe *pe) res_start = pe->phb->ioda.m32_pci_base >> tbl->it_page_shift; res_end =min (window_size,SZ_4G ) >> tbl->it_page_shift; } -iommu_init_table (tbl, pe->phb->hose->node, res_start, res_end); - rc =pnv_pci_ioda2_set_window (&pe->table_group,0 , tbl); +if (iommu_init_table (tbl, pe->phb->hose->node, res_start, res_end)) + rc =pnv_pci_ioda2_set_window (&pe->table_group,0 , tbl); +else + rc = -ENOMEM ;if (rc) { -pe_err (pe,"Failed to configure 32-bit TCE table, err %ld\n" , - rc); +pe_err (pe,"Failed to configure 32-bit TCE table, err %ld\n" , rc);iommu_tce_table_put (tbl); -return rc; + tbl =NULL ;/* This clears iommu_table_base below */ } -if (!pnv_iommu_bypass_disabled)pnv_pci_ioda2_set_bypass (pe, true);arch/powerpc/platforms/pseries/iommu.c +7
-3@@ -638,7 +638,8 @@ static void pci_dma_bus_setup_pSeries(struct pci_bus *bus)iommu_table_setparms (pci->phb, dn, tbl); tbl->it_ops = &iommu_table_pseries_ops; -iommu_init_table (tbl, pci->phb->node,0 ,0 ); +if (!iommu_init_table (tbl, pci->phb->node,0 ,0 )) +panic ("Failed to initialize iommu table" );/* Divide the rest (1.75GB) among the children */ pci->phb->dma_window_size =0x80000000ul ;@@ -720,7 +721,8 @@ static void pci_dma_bus_setup_pSeriesLP(struct pci_bus *bus)iommu_table_setparms_lpar (ppci->phb, pdn, tbl, ppci->table_group, dma_window); tbl->it_ops = &iommu_table_lpar_multi_ops; -iommu_init_table (tbl, ppci->phb->node,0 ,0 ); +if (!iommu_init_table (tbl, ppci->phb->node,0 ,0 )) +panic ("Failed to initialize iommu table" );iommu_register_group (ppci->table_group,pci_domain_nr (bus),0 );pr_debug (" created table: %p\n" , ppci->table_group);@@ -749,7 +751,9 @@ static void pci_dma_dev_setup_pSeries(struct pci_dev *dev) tbl =PCI_DN (dn)->table_group->tables[0 ];iommu_table_setparms (phb, dn, tbl); tbl->it_ops = &iommu_table_pseries_ops; -iommu_init_table (tbl, phb->node,0 ,0 ); +if (!iommu_init_table (tbl, phb->node,0 ,0 )) +panic ("Failed to initialize iommu table" ); +set_iommu_table_base (&dev->dev, tbl);return ; }arch/powerpc/sysdev/dart_iommu.c +2
-1@@ -344,7 +344,8 @@ static void iommu_table_dart_setup(void) iommu_table_dart.it_index =0 ; iommu_table_dart.it_blocksize =1 ; iommu_table_dart.it_ops = &iommu_dart_ops; -iommu_init_table (&iommu_table_dart,-1 ,0 ,0 ); +if (!iommu_init_table (&iommu_table_dart,-1 ,0 ,0 )) +panic ("Failed to initialize iommu table" );/* Reserve the last page of the DART to avoid possible prefetch * past the DART mapped area
Signature · 4 lines
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
Re: [PATCH kernel 2/2] powerpc/iommu: Do not immediately panic when failed IOMMU table allocation
In reply to Alexey KardashevskiyQuoted 11 lines · Alexey Kardashevskiy
Most platforms allocate IOMMU table structures (specifically
it_map) at the boot time and when this fails - it is a valid reason forpanic().However the powernv platform allocates
it_mapafter a device is returned to the host OS after being passed through and this happens long after the host OS booted. It is quite possible to trigger theit_mapallocationpanic()and kill the host even though it is not necessary - the host OS can still use the DMA bypass mode (requires a tiny fraction of it_map's memory) and even if that fails, the host OS is runnnable as it was without the device for which allocatingit_mapcauses the panic.
Quoted 4 lines
Instead of immediately crashing in a powernv/ioda2 system, this prints an error and continues. All other platforms still call
panic().Signed-off-by Alexey Kardashevskiy <[email protected]>
Hello Alexey,
This looks like a good change, that passes panic() decision to platform code. Everything looks pretty straightforward, but I have a question regarding this:
Quoted 22 lines
(quoted diff) +6
-6@@ -1930,16 +1931,16 @@ static long pnv_pci_ioda2_setup_default_config(struct pnv_ioda_pe *pe) res_start = pe->phb->ioda.m32_pci_base >> tbl->it_page_shift; res_end = min(window_size, SZ_4G) >> tbl->it_page_shift; } - iommu_init_table(tbl, pe->phb->hose->node, res_start, res_end); - rc = pnv_pci_ioda2_set_window(&pe->table_group, 0, tbl); + if (iommu_init_table(tbl, pe->phb->hose->node, res_start, res_end)) + rc = pnv_pci_ioda2_set_window(&pe->table_group, 0, tbl); + else + rc = -ENOMEM; if (rc) { - pe_err(pe, "Failed to configure 32-bit TCE table, err %ld\n", - rc); + pe_err(pe, "Failed to configure 32-bit TCE table, err %ld\n", rc); iommu_tce_table_put(tbl); - return rc; + tbl = NULL; /* This clears iommu_table_base below */ } - if (!pnv_iommu_bypass_disabled) pnv_pci_ioda2_set_bypass(pe, true);
Quoted 2 lines
If I could understand correctly, previously if iommu_init_table() did not panic(), and pnv_pci_ioda2_set_window() returned something other than 0, it would return rc in the if (rc) clause, but now it does not happen anymore, going through if (!pnv_iommu_bypass_disabled) onwards.
Is that desired?
As far as I could see, returning rc there seems a good procedure after iommu_init_table returning -ENOMEM.
Best regards,
Leonardo Bras
Re: [PATCH kernel 2/2] powerpc/iommu: Do not immediately panic when failed IOMMU table allocation
Quoted 53 lines · Leonardo Bras
Quoted 11 lines · Alexey Kardashevskiy
Most platforms allocate IOMMU table structures (specifically
it_map) at the boot time and when this fails - it is a valid reason forpanic().However the powernv platform allocates
it_mapafter a device is returned to the host OS after being passed through and this happens long after the host OS booted. It is quite possible to trigger theit_mapallocationpanic()and kill the host even though it is not necessary - the host OS can still use the DMA bypass mode (requires a tiny fraction of it_map's memory) and even if that fails, the host OS is runnnable as it was without the device for which allocatingit_mapcauses the panic.Quoted 4 lines
Instead of immediately crashing in a powernv/ioda2 system, this prints an error and continues. All other platforms still call
panic().Signed-off-by Alexey Kardashevskiy <[email protected]>Hello Alexey,
This looks like a good change, that passes
panic()decision to platform code. Everything looks pretty straightforward, but I have a question regarding this:Quoted 14 lines
(quoted diff) +4
-3@@ -1930,16 +1931,16 @@ static long pnv_pci_ioda2_setup_default_config(struct pnv_ioda_pe *pe) res_start = pe->phb->ioda.m32_pci_base >> tbl->it_page_shift; res_end = min(window_size, SZ_4G) >> tbl->it_page_shift; } - iommu_init_table(tbl, pe->phb->hose->node, res_start, res_end); - rc = pnv_pci_ioda2_set_window(&pe->table_group, 0, tbl); + if (iommu_init_table(tbl, pe->phb->hose->node, res_start, res_end)) + rc = pnv_pci_ioda2_set_window(&pe->table_group, 0, tbl); + else + rc = -ENOMEM; if (rc) { - pe_err(pe, "Failed to configure 32-bit TCE table, err %ld\n",Review static long pnv_pci_ioda2_setup_default_config(struct pnv_ioda_pe *pe) · line 1946
Quoted 10 lines
(quoted diff) +2
-3- rc); + pe_err(pe, "Failed to configure 32-bit TCE table, err %ld\n", rc); iommu_tce_table_put(tbl); - return rc; + tbl = NULL; /* This clears iommu_table_base below */ } - if (!pnv_iommu_bypass_disabled) pnv_pci_ioda2_set_bypass(pe, true);If I could understand correctly, previously if
iommu_init_table()did notpanic(), andpnv_pci_ioda2_set_window()returned something other than 0, it would return rc in the if (rc) clause, but now it does not happen anymore, going through if (!pnv_iommu_bypass_disabled) onwards.
Quoted 1 line
Is that desired?
Yes. A PE (=device, pretty much) has 2 DMA windows:
- the default one which requires some RAM to operate
- a bypass mode which tells the hardware that PCI addresses are statically mapped to RAM 1:1.
This bypass mode does not require extra memory to work and is used in the most cases on the bare metal as long as the device supports 64bit DMA which is everything except GPUs. Since it is cheap to enable and this what we prefer anyway, no urge to fail.
Quoted 2 lines
As far as I could see, returning rc there seems a good procedure after
iommu_init_tablereturning-ENOMEM.
This change is intentional and yes it could be done by a separate patch but I figured there is no that much value in splitting.
Signature · 1 line
Alexey
Leonardo Bras
Reviewed-byRe: [PATCH kernel 2/2] powerpc/iommu: Do not immediately panic when failed IOMMU table allocation
Quoted 72 lines · Alexey Kardashevskiy
Quoted 56 lines · Leonardo Bras
Quoted 11 lines · Alexey Kardashevskiy
Most platforms allocate IOMMU table structures (specifically
it_map) at the boot time and when this fails - it is a valid reason forpanic().However the powernv platform allocates
it_mapafter a device is returned to the host OS after being passed through and this happens long after the host OS booted. It is quite possible to trigger theit_mapallocationpanic()and kill the host even though it is not necessary - the host OS can still use the DMA bypass mode (requires a tiny fraction of it_map's memory) and even if that fails, the host OS is runnnable as it was without the device for which allocatingit_mapcauses the panic.Quoted 4 lines
Instead of immediately crashing in a powernv/ioda2 system, this prints an error and continues. All other platforms still call
panic().Signed-off-by Alexey Kardashevskiy <[email protected]>Hello Alexey,
This looks like a good change, that passes
panic()decision to platform code. Everything looks pretty straightforward, but I have a question regarding this:Quoted 22 lines
(quoted diff) +6
-6@@ -1930,16 +1931,16 @@ static long pnv_pci_ioda2_setup_default_config(struct pnv_ioda_pe *pe) res_start = pe->phb->ioda.m32_pci_base >> tbl->it_page_shift; res_end = min(window_size, SZ_4G) >> tbl->it_page_shift; } - iommu_init_table(tbl, pe->phb->hose->node, res_start, res_end); - rc = pnv_pci_ioda2_set_window(&pe->table_group, 0, tbl); + if (iommu_init_table(tbl, pe->phb->hose->node, res_start, res_end)) + rc = pnv_pci_ioda2_set_window(&pe->table_group, 0, tbl); + else + rc = -ENOMEM; if (rc) { - pe_err(pe, "Failed to configure 32-bit TCE table, err %ld\n", - rc); + pe_err(pe, "Failed to configure 32-bit TCE table, err %ld\n", rc); iommu_tce_table_put(tbl); - return rc; + tbl = NULL; /* This clears iommu_table_base below */ } - if (!pnv_iommu_bypass_disabled) pnv_pci_ioda2_set_bypass(pe, true);Quoted 5 lines
If I could understand correctly, previously if
iommu_init_table()did notpanic(), andpnv_pci_ioda2_set_window()returned something other than 0, it would return rc in the if (rc) clause, but now it does not happen anymore, going through if (!pnv_iommu_bypass_disabled) onwards.Quoted 1 line
Is that desired?
Yes. A PE (=device, pretty much) has 2 DMA windows:
- the default one which requires some RAM to operate
- a bypass mode which tells the hardware that PCI addresses are statically mapped to RAM 1:1.This bypass mode does not require extra memory to work and is used in the most cases on the bare metal as long as the device supports 64bit DMA which is everything except GPUs. Since it is cheap to enable and this what we prefer anyway, no urge to fail.
Quoted 5 lines
Quoted 2 lines
As far as I could see, returning rc there seems a good procedure after
iommu_init_tablereturning-ENOMEM.This change is intentional and yes it could be done by a separate patch but I figured there is no that much value in splitting.
Ok then, thanks for clarifying.
FWIW:
Michael Ellerman
AppliedQuoted 9 lines · Alexey Kardashevskiy
Killing a VM on a host under memory pressure kills a host which is annoying. 1/2 reduces the chances, 2/2 eliminates
panic()on ioda2.This is based on sha1
f40ddce88593Linus Torvalds "Linux 5.11".[...]
Applied to powerpc/next.
[1/2] powerpc/iommu: Allocate it_map by vmalloc
https://git.kernel.org/powerpc/c/7f1fa82d79947dfabb4046e1d787da9db2bc1c20 [2/2] powerpc/iommu: Do not immediately panic when failed IOMMU table allocation https://git.kernel.org/powerpc/c/4be518d838809e21354f32087aa9c26efc50b410
cheers