Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

vfio: Use GFP_KERNEL_ACCOUNT for userspace persistent allocations

Use GFP_KERNEL_ACCOUNT for userspace persistent allocations.

The GFP_KERNEL_ACCOUNT option lets the memory allocator know that this
is untrusted allocation triggered from userspace and should be a subject
of kmem accounting, and as such it is controlled by the cgroup
mechanism.

The way to find the relevant allocations was for example to look at the
close_device function and trace back all the kfrees to their
allocations.

Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
Signed-off-by: Yishai Hadas <yishaih@nvidia.com>
Reviewed-by: Jason Gunthorpe <jgg@nvidia.com>
Link: https://lore.kernel.org/r/20230108154427.32609-4-yishaih@nvidia.com
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>

authored by

Jason Gunthorpe and committed by
Alex Williamson
0886196c 83ff6095

+17 -14
+1 -1
drivers/vfio/container.c
··· 367 367 { 368 368 struct vfio_container *container; 369 369 370 - container = kzalloc(sizeof(*container), GFP_KERNEL); 370 + container = kzalloc(sizeof(*container), GFP_KERNEL_ACCOUNT); 371 371 if (!container) 372 372 return -ENOMEM; 373 373
+3 -3
drivers/vfio/pci/vfio_pci_config.c
··· 1244 1244 if (vdev->msi_perm) 1245 1245 return len; 1246 1246 1247 - vdev->msi_perm = kmalloc(sizeof(struct perm_bits), GFP_KERNEL); 1247 + vdev->msi_perm = kmalloc(sizeof(struct perm_bits), GFP_KERNEL_ACCOUNT); 1248 1248 if (!vdev->msi_perm) 1249 1249 return -ENOMEM; 1250 1250 ··· 1731 1731 * no requirements on the length of a capability, so the gap between 1732 1732 * capabilities needs byte granularity. 1733 1733 */ 1734 - map = kmalloc(pdev->cfg_size, GFP_KERNEL); 1734 + map = kmalloc(pdev->cfg_size, GFP_KERNEL_ACCOUNT); 1735 1735 if (!map) 1736 1736 return -ENOMEM; 1737 1737 1738 - vconfig = kmalloc(pdev->cfg_size, GFP_KERNEL); 1738 + vconfig = kmalloc(pdev->cfg_size, GFP_KERNEL_ACCOUNT); 1739 1739 if (!vconfig) { 1740 1740 kfree(map); 1741 1741 return -ENOMEM;
+4 -3
drivers/vfio/pci/vfio_pci_core.c
··· 144 144 * of the exclusive page in case that hot-add 145 145 * device's bar is assigned into it. 146 146 */ 147 - dummy_res = kzalloc(sizeof(*dummy_res), GFP_KERNEL); 147 + dummy_res = 148 + kzalloc(sizeof(*dummy_res), GFP_KERNEL_ACCOUNT); 148 149 if (dummy_res == NULL) 149 150 goto no_mmap; 150 151 ··· 864 863 865 864 region = krealloc(vdev->region, 866 865 (vdev->num_regions + 1) * sizeof(*region), 867 - GFP_KERNEL); 866 + GFP_KERNEL_ACCOUNT); 868 867 if (!region) 869 868 return -ENOMEM; 870 869 ··· 1645 1644 { 1646 1645 struct vfio_pci_mmap_vma *mmap_vma; 1647 1646 1648 - mmap_vma = kmalloc(sizeof(*mmap_vma), GFP_KERNEL); 1647 + mmap_vma = kmalloc(sizeof(*mmap_vma), GFP_KERNEL_ACCOUNT); 1649 1648 if (!mmap_vma) 1650 1649 return -ENOMEM; 1651 1650
+1 -1
drivers/vfio/pci/vfio_pci_igd.c
··· 180 180 if (!addr || !(~addr)) 181 181 return -ENODEV; 182 182 183 - opregionvbt = kzalloc(sizeof(*opregionvbt), GFP_KERNEL); 183 + opregionvbt = kzalloc(sizeof(*opregionvbt), GFP_KERNEL_ACCOUNT); 184 184 if (!opregionvbt) 185 185 return -ENOMEM; 186 186
+6 -4
drivers/vfio/pci/vfio_pci_intrs.c
··· 177 177 if (!vdev->pdev->irq) 178 178 return -ENODEV; 179 179 180 - vdev->ctx = kzalloc(sizeof(struct vfio_pci_irq_ctx), GFP_KERNEL); 180 + vdev->ctx = kzalloc(sizeof(struct vfio_pci_irq_ctx), GFP_KERNEL_ACCOUNT); 181 181 if (!vdev->ctx) 182 182 return -ENOMEM; 183 183 ··· 216 216 if (fd < 0) /* Disable only */ 217 217 return 0; 218 218 219 - vdev->ctx[0].name = kasprintf(GFP_KERNEL, "vfio-intx(%s)", 219 + vdev->ctx[0].name = kasprintf(GFP_KERNEL_ACCOUNT, "vfio-intx(%s)", 220 220 pci_name(pdev)); 221 221 if (!vdev->ctx[0].name) 222 222 return -ENOMEM; ··· 284 284 if (!is_irq_none(vdev)) 285 285 return -EINVAL; 286 286 287 - vdev->ctx = kcalloc(nvec, sizeof(struct vfio_pci_irq_ctx), GFP_KERNEL); 287 + vdev->ctx = kcalloc(nvec, sizeof(struct vfio_pci_irq_ctx), 288 + GFP_KERNEL_ACCOUNT); 288 289 if (!vdev->ctx) 289 290 return -ENOMEM; 290 291 ··· 344 343 if (fd < 0) 345 344 return 0; 346 345 347 - vdev->ctx[vector].name = kasprintf(GFP_KERNEL, "vfio-msi%s[%d](%s)", 346 + vdev->ctx[vector].name = kasprintf(GFP_KERNEL_ACCOUNT, 347 + "vfio-msi%s[%d](%s)", 348 348 msix ? "x" : "", vector, 349 349 pci_name(pdev)); 350 350 if (!vdev->ctx[vector].name)
+1 -1
drivers/vfio/pci/vfio_pci_rdwr.c
··· 470 470 goto out_unlock; 471 471 } 472 472 473 - ioeventfd = kzalloc(sizeof(*ioeventfd), GFP_KERNEL); 473 + ioeventfd = kzalloc(sizeof(*ioeventfd), GFP_KERNEL_ACCOUNT); 474 474 if (!ioeventfd) { 475 475 ret = -ENOMEM; 476 476 goto out_unlock;
+1 -1
drivers/vfio/virqfd.c
··· 112 112 int ret = 0; 113 113 __poll_t events; 114 114 115 - virqfd = kzalloc(sizeof(*virqfd), GFP_KERNEL); 115 + virqfd = kzalloc(sizeof(*virqfd), GFP_KERNEL_ACCOUNT); 116 116 if (!virqfd) 117 117 return -ENOMEM; 118 118