Re: [PATCH] s390/pci: remove custom and misleading bitmap_vzalloc

From: Niklas Schnelle
Date: Mon Oct 23 2023 - 12:31:54 EST


On Mon, 2023-10-23 at 18:11 +0200, Vasily Gorbik wrote:
> This commit effectively reverts commit c1ae1c59c8c6 ("s390/pci: fix
> iommu bitmap allocation") and applies a simpler fix instead. Commit
> c1ae1c59c8c6 introduced a custom bitmap_vzalloc() function that included
> unnecessary and misleading overflow handling.
>
> This fix is only relevant for the current v6.6 and stable backports. It
> will be superseded by the upcoming conversion to use the common
> code DMA API on s390 (pending in linux-next [2]), which eliminates
> arch/s390/pci/pci_dma.c entirely and, therefore, addresses the original
> problem in another way.
>
> Instead of relying on a custom bitmap_vzalloc() function, this change goes
> back to straightforward allocation using vzalloc() with the appropriate
> size calculated using the BITS_TO_LONGS() macro.
>
> Link: https://lore.kernel.org/all/CAHk-=wgTUz1bdY6zvsN4ED0arCLE8Sb==1GH8d0sjm5bu7zesQ@xxxxxxxxxxxxxx/
> Link: https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git/commit/?h=next-20231020&id=c76c067e488c
> Cc: stable@xxxxxxxxxxxxxxx
> Fixes: c1ae1c59c8c6 ("s390/pci: fix iommu bitmap allocation")
> Suggested-by: Linus Torvalds <torvalds@xxxxxxxxxxxxxxxxxxxx>
> Signed-off-by: Vasily Gorbik <gor@xxxxxxxxxxxxx>
> ---
> arch/s390/pci/pci_dma.c | 17 ++++-------------
> 1 file changed, 4 insertions(+), 13 deletions(-)
>
> diff --git a/arch/s390/pci/pci_dma.c b/arch/s390/pci/pci_dma.c
> index 99209085c75b..1b4b123d79aa 100644
> --- a/arch/s390/pci/pci_dma.c
> +++ b/arch/s390/pci/pci_dma.c
> @@ -565,17 +565,6 @@ static void s390_dma_unmap_sg(struct device *dev, struct scatterlist *sg,
> }
> }
>
> -static unsigned long *bitmap_vzalloc(size_t bits, gfp_t flags)
> -{
> - size_t n = BITS_TO_LONGS(bits);
> - size_t bytes;
> -
> - if (unlikely(check_mul_overflow(n, sizeof(unsigned long), &bytes)))
> - return NULL;
> -
> - return vzalloc(bytes);
> -}
> -
> int zpci_dma_init_device(struct zpci_dev *zdev)
> {
> u8 status;
> @@ -615,13 +604,15 @@ int zpci_dma_init_device(struct zpci_dev *zdev)
> zdev->end_dma - zdev->start_dma + 1);
> zdev->end_dma = zdev->start_dma + zdev->iommu_size - 1;
> zdev->iommu_pages = zdev->iommu_size >> PAGE_SHIFT;
> - zdev->iommu_bitmap = bitmap_vzalloc(zdev->iommu_pages, GFP_KERNEL);
> + zdev->iommu_bitmap = vzalloc(BITS_TO_LONGS(zdev->iommu_pages) *
> + sizeof(unsigned long));
> if (!zdev->iommu_bitmap) {
> rc = -ENOMEM;
> goto free_dma_table;
> }
> if (!s390_iommu_strict) {
> - zdev->lazy_bitmap = bitmap_vzalloc(zdev->iommu_pages, GFP_KERNEL);
> + zdev->lazy_bitmap = vzalloc(BITS_TO_LONGS(zdev->iommu_pages) *
> + sizeof(unsigned long));
> if (!zdev->lazy_bitmap) {
> rc = -ENOMEM;
> goto free_bitmap;

Mea culpa for the useless and misleading overflow check. I'm sorry, I
should not have copied this over from kvmalloc_array() without actually
thinking through whether it makes sense in the new place and you're
right Linus it doesn't.

Thank you Vasily for cleaning this up! Also as an additional point,
note that the size of the bitmap is limited by the above min3() which
in the largest possible case ensures a maximum of 128 MiB bitmaps which
only happens for very large memory systems or if a user sets an
unreasonably large s390_iommu_aperture kernel parameter.

Also:
Reviewed-by: Niklas Schnelle <schnelle@xxxxxxxxxxxxx>

Best regards,
Niklas