iommu/amd: Change rlookup, irq_lookup, and alias to use kvalloc()
authorJason Gunthorpe <jgg@nvidia.com>
Tue, 8 Apr 2025 16:54:04 +0000 (13:54 -0300)
committerJoerg Roedel <jroedel@suse.de>
Thu, 17 Apr 2025 14:22:47 +0000 (16:22 +0200)
This is just CPU memory used by the driver to track things, it doesn't
need to use iommu-pages. All of them are indexed by devid and devid is
bounded by pci_seg->last_bdf or we are already out of bounds on the page
allocation.

Switch them to use some version of kvmalloc_array() and drop the now
unused constants and remove the tbl_size() round up to PAGE_SIZE multiples
logic.

Tested-by: Alejandro Jimenez <alejandro.j.jimenez@oracle.com>
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
Link: https://lore.kernel.org/r/16-v4-c8663abbb606+3f7-iommu_pages_jgg@nvidia.com
Signed-off-by: Joerg Roedel <jroedel@suse.de>
drivers/iommu/amd/amd_iommu_types.h
drivers/iommu/amd/init.c

index 5089b58..5ea73f0 100644 (file)
@@ -29,8 +29,6 @@
  * some size calculation constants
  */
 #define DEV_TABLE_ENTRY_SIZE           32
-#define ALIAS_TABLE_ENTRY_SIZE         2
-#define RLOOKUP_TABLE_ENTRY_SIZE       (sizeof(void *))
 
 /* Capability offsets used by the driver */
 #define MMIO_CAP_HDR_OFFSET    0x00
@@ -616,12 +614,6 @@ struct amd_iommu_pci_seg {
        /* Size of the device table */
        u32 dev_table_size;
 
-       /* Size of the alias table */
-       u32 alias_table_size;
-
-       /* Size of the rlookup table */
-       u32 rlookup_table_size;
-
        /*
         * device table virtual address
         *
index b5415dd..3acdbef 100644 (file)
@@ -651,8 +651,9 @@ static inline void free_dev_table(struct amd_iommu_pci_seg *pci_seg)
 /* Allocate per PCI segment IOMMU rlookup table. */
 static inline int __init alloc_rlookup_table(struct amd_iommu_pci_seg *pci_seg)
 {
-       pci_seg->rlookup_table = iommu_alloc_pages(GFP_KERNEL,
-                                                  get_order(pci_seg->rlookup_table_size));
+       pci_seg->rlookup_table = kvcalloc(pci_seg->last_bdf + 1,
+                                         sizeof(*pci_seg->rlookup_table),
+                                         GFP_KERNEL);
        if (pci_seg->rlookup_table == NULL)
                return -ENOMEM;
 
@@ -661,16 +662,15 @@ static inline int __init alloc_rlookup_table(struct amd_iommu_pci_seg *pci_seg)
 
 static inline void free_rlookup_table(struct amd_iommu_pci_seg *pci_seg)
 {
-       iommu_free_pages(pci_seg->rlookup_table);
+       kvfree(pci_seg->rlookup_table);
        pci_seg->rlookup_table = NULL;
 }
 
 static inline int __init alloc_irq_lookup_table(struct amd_iommu_pci_seg *pci_seg)
 {
-       pci_seg->irq_lookup_table = iommu_alloc_pages(GFP_KERNEL,
-                                                     get_order(pci_seg->rlookup_table_size));
-       kmemleak_alloc(pci_seg->irq_lookup_table,
-                      pci_seg->rlookup_table_size, 1, GFP_KERNEL);
+       pci_seg->irq_lookup_table = kvcalloc(pci_seg->last_bdf + 1,
+                                            sizeof(*pci_seg->irq_lookup_table),
+                                            GFP_KERNEL);
        if (pci_seg->irq_lookup_table == NULL)
                return -ENOMEM;
 
@@ -679,8 +679,7 @@ static inline int __init alloc_irq_lookup_table(struct amd_iommu_pci_seg *pci_se
 
 static inline void free_irq_lookup_table(struct amd_iommu_pci_seg *pci_seg)
 {
-       kmemleak_free(pci_seg->irq_lookup_table);
-       iommu_free_pages(pci_seg->irq_lookup_table);
+       kvfree(pci_seg->irq_lookup_table);
        pci_seg->irq_lookup_table = NULL;
 }
 
@@ -688,8 +687,9 @@ static int __init alloc_alias_table(struct amd_iommu_pci_seg *pci_seg)
 {
        int i;
 
-       pci_seg->alias_table = iommu_alloc_pages(GFP_KERNEL,
-                                                get_order(pci_seg->alias_table_size));
+       pci_seg->alias_table = kvmalloc_array(pci_seg->last_bdf + 1,
+                                             sizeof(*pci_seg->alias_table),
+                                             GFP_KERNEL);
        if (!pci_seg->alias_table)
                return -ENOMEM;
 
@@ -704,7 +704,7 @@ static int __init alloc_alias_table(struct amd_iommu_pci_seg *pci_seg)
 
 static void __init free_alias_table(struct amd_iommu_pci_seg *pci_seg)
 {
-       iommu_free_pages(pci_seg->alias_table);
+       kvfree(pci_seg->alias_table);
        pci_seg->alias_table = NULL;
 }
 
@@ -1596,8 +1596,6 @@ static struct amd_iommu_pci_seg *__init alloc_pci_segment(u16 id,
        pci_seg->last_bdf = last_bdf;
        DUMP_printk("PCI segment : 0x%0x, last bdf : 0x%04x\n", id, last_bdf);
        pci_seg->dev_table_size     = tbl_size(DEV_TABLE_ENTRY_SIZE, last_bdf);
-       pci_seg->alias_table_size   = tbl_size(ALIAS_TABLE_ENTRY_SIZE, last_bdf);
-       pci_seg->rlookup_table_size = tbl_size(RLOOKUP_TABLE_ENTRY_SIZE, last_bdf);
 
        pci_seg->id = id;
        init_llist_head(&pci_seg->dev_data_list);