iommu/amd: Fix corruption when mapping large pages from 0
authorJason Gunthorpe <jgg@nvidia.com>
Fri, 18 Oct 2024 17:12:19 +0000 (14:12 -0300)
committerJoerg Roedel <jroedel@suse.de>
Tue, 29 Oct 2024 08:55:48 +0000 (09:55 +0100)
commite3a682eaf2af51a83f5313145ef592ce50fa787f
treebbf7661f05c1a921dbb85c8ec38c3553f5bca299
parent3f6eeada6930056c38f20964120ac402cf0030b9
iommu/amd: Fix corruption when mapping large pages from 0

If a page is mapped starting at 0 that is equal to or larger than can fit
in the current mode (number of table levels) it results in corrupting the
mapping as the following logic assumes the mode is correct for the page
size being requested.

There are two issues here, the check if the address fits within the table
uses the start address, it should use the last address to ensure that last
byte of the mapping fits within the current table mode.

The second is if the mapping is exactly the size of the full page table it
has to add another level to instead hold a single IOPTE for the large
size.

Since both corner cases require a 0 IOVA to be hit and doesn't start until
a page size of 2^48 it is unlikely to ever hit in a real system.

Reported-by: Alejandro Jimenez <alejandro.j.jimenez@oracle.com>
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
Link: https://lore.kernel.org/r/0-v1-27ab08d646a1+29-amd_0map_jgg@nvidia.com
Signed-off-by: Joerg Roedel <jroedel@suse.de>
drivers/iommu/amd/io_pgtable.c