Skip to content

Commit e4a3d52

Browse files
committed
Merge tag 'iommu-fixes-v6.19-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/iommu/linux
Pull iommu fixes from Joerg Roedel: - iommupt: Fix an oops found by syzcaller in the new generic IO-page-table code. - AMD-Vi: Fix IO_PAGE_FAULTs in kdump kernels triggered by re-using domain-ids from previous kernel. * tag 'iommu-fixes-v6.19-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/iommu/linux: amd/iommu: Make protection domain ID functions non-static amd/iommu: Preserve domain ids inside the kdump kernel iommupt: Return ERR_PTR from _table_alloc()
2 parents d8ba32c + c7fe938 commit e4a3d52

4 files changed

Lines changed: 47 additions & 12 deletions

File tree

drivers/iommu/amd/amd_iommu.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,11 @@ static inline struct protection_domain *to_pdomain(struct iommu_domain *dom)
173173
bool translation_pre_enabled(struct amd_iommu *iommu);
174174
int __init add_special_device(u8 type, u8 id, u32 *devid, bool cmd_line);
175175

176+
int amd_iommu_pdom_id_alloc(void);
177+
int amd_iommu_pdom_id_reserve(u16 id, gfp_t gfp);
178+
void amd_iommu_pdom_id_free(int id);
179+
void amd_iommu_pdom_id_destroy(void);
180+
176181
#ifdef CONFIG_DMI
177182
void amd_iommu_apply_ivrs_quirks(void);
178183
#else

drivers/iommu/amd/init.c

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,8 +1136,11 @@ static void set_dte_bit(struct dev_table_entry *dte, u8 bit)
11361136
static bool __reuse_device_table(struct amd_iommu *iommu)
11371137
{
11381138
struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;
1139-
u32 lo, hi, old_devtb_size;
1139+
struct dev_table_entry *old_dev_tbl_entry;
1140+
u32 lo, hi, old_devtb_size, devid;
11401141
phys_addr_t old_devtb_phys;
1142+
u16 dom_id;
1143+
bool dte_v;
11411144
u64 entry;
11421145

11431146
/* Each IOMMU use separate device table with the same size */
@@ -1173,6 +1176,22 @@ static bool __reuse_device_table(struct amd_iommu *iommu)
11731176
return false;
11741177
}
11751178

1179+
for (devid = 0; devid <= pci_seg->last_bdf; devid++) {
1180+
old_dev_tbl_entry = &pci_seg->old_dev_tbl_cpy[devid];
1181+
dte_v = FIELD_GET(DTE_FLAG_V, old_dev_tbl_entry->data[0]);
1182+
dom_id = FIELD_GET(DEV_DOMID_MASK, old_dev_tbl_entry->data[1]);
1183+
1184+
if (!dte_v || !dom_id)
1185+
continue;
1186+
/*
1187+
* ID reservation can fail with -ENOSPC when there
1188+
* are multiple devices present in the same domain,
1189+
* hence check only for -ENOMEM.
1190+
*/
1191+
if (amd_iommu_pdom_id_reserve(dom_id, GFP_KERNEL) == -ENOMEM)
1192+
return false;
1193+
}
1194+
11761195
return true;
11771196
}
11781197

@@ -3127,8 +3146,7 @@ static bool __init check_ioapic_information(void)
31273146

31283147
static void __init free_dma_resources(void)
31293148
{
3130-
ida_destroy(&pdom_ids);
3131-
3149+
amd_iommu_pdom_id_destroy();
31323150
free_unity_maps();
31333151
}
31343152

drivers/iommu/amd/iommu.c

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1811,17 +1811,26 @@ int amd_iommu_complete_ppr(struct device *dev, u32 pasid, int status, int tag)
18111811
* contain.
18121812
*
18131813
****************************************************************************/
1814-
1815-
static int pdom_id_alloc(void)
1814+
int amd_iommu_pdom_id_alloc(void)
18161815
{
18171816
return ida_alloc_range(&pdom_ids, 1, MAX_DOMAIN_ID - 1, GFP_ATOMIC);
18181817
}
18191818

1820-
static void pdom_id_free(int id)
1819+
int amd_iommu_pdom_id_reserve(u16 id, gfp_t gfp)
1820+
{
1821+
return ida_alloc_range(&pdom_ids, id, id, gfp);
1822+
}
1823+
1824+
void amd_iommu_pdom_id_free(int id)
18211825
{
18221826
ida_free(&pdom_ids, id);
18231827
}
18241828

1829+
void amd_iommu_pdom_id_destroy(void)
1830+
{
1831+
ida_destroy(&pdom_ids);
1832+
}
1833+
18251834
static void free_gcr3_tbl_level1(u64 *tbl)
18261835
{
18271836
u64 *ptr;
@@ -1864,7 +1873,7 @@ static void free_gcr3_table(struct gcr3_tbl_info *gcr3_info)
18641873
gcr3_info->glx = 0;
18651874

18661875
/* Free per device domain ID */
1867-
pdom_id_free(gcr3_info->domid);
1876+
amd_iommu_pdom_id_free(gcr3_info->domid);
18681877

18691878
iommu_free_pages(gcr3_info->gcr3_tbl);
18701879
gcr3_info->gcr3_tbl = NULL;
@@ -1900,14 +1909,14 @@ static int setup_gcr3_table(struct gcr3_tbl_info *gcr3_info,
19001909
return -EBUSY;
19011910

19021911
/* Allocate per device domain ID */
1903-
domid = pdom_id_alloc();
1912+
domid = amd_iommu_pdom_id_alloc();
19041913
if (domid <= 0)
19051914
return -ENOSPC;
19061915
gcr3_info->domid = domid;
19071916

19081917
gcr3_info->gcr3_tbl = iommu_alloc_pages_node_sz(nid, GFP_ATOMIC, SZ_4K);
19091918
if (gcr3_info->gcr3_tbl == NULL) {
1910-
pdom_id_free(domid);
1919+
amd_iommu_pdom_id_free(domid);
19111920
return -ENOMEM;
19121921
}
19131922

@@ -2503,7 +2512,7 @@ struct protection_domain *protection_domain_alloc(void)
25032512
if (!domain)
25042513
return NULL;
25052514

2506-
domid = pdom_id_alloc();
2515+
domid = amd_iommu_pdom_id_alloc();
25072516
if (domid <= 0) {
25082517
kfree(domain);
25092518
return NULL;
@@ -2802,7 +2811,7 @@ void amd_iommu_domain_free(struct iommu_domain *dom)
28022811

28032812
WARN_ON(!list_empty(&domain->dev_list));
28042813
pt_iommu_deinit(&domain->iommu);
2805-
pdom_id_free(domain->id);
2814+
amd_iommu_pdom_id_free(domain->id);
28062815
kfree(domain);
28072816
}
28082817

@@ -2853,7 +2862,7 @@ void amd_iommu_init_identity_domain(void)
28532862
domain->ops = &identity_domain_ops;
28542863
domain->owner = &amd_iommu_ops;
28552864

2856-
identity_domain.id = pdom_id_alloc();
2865+
identity_domain.id = amd_iommu_pdom_id_alloc();
28572866

28582867
protection_domain_init(&identity_domain);
28592868
}

drivers/iommu/generic_pt/iommu_pt.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,9 @@ static inline struct pt_table_p *_table_alloc(struct pt_common *common,
372372

373373
table_mem = iommu_alloc_pages_node_sz(iommu_table->nid, gfp,
374374
log2_to_int(lg2sz));
375+
if (!table_mem)
376+
return ERR_PTR(-ENOMEM);
377+
375378
if (pt_feature(common, PT_FEAT_DMA_INCOHERENT) &&
376379
mode == ALLOC_NORMAL) {
377380
int ret = iommu_pages_start_incoherent(

0 commit comments

Comments
 (0)