git: 14dcd4098374 - main - linuxkpi: `GFP_KERNEL` equals `M_NOWAIT` now
Date: Fri, 24 Nov 2023 17:31:54 UTC
The branch main has been updated by dumbbell: URL: https://cgit.FreeBSD.org/src/commit/?id=14dcd40983748596d116d91acb934a8a95ac76bc commit 14dcd40983748596d116d91acb934a8a95ac76bc Author: Jean-Sébastien Pédron <dumbbell@FreeBSD.org> AuthorDate: 2023-11-24 17:30:33 +0000 Commit: Jean-Sébastien Pédron <dumbbell@FreeBSD.org> CommitDate: 2023-11-24 17:31:32 +0000 linuxkpi: `GFP_KERNEL` equals `M_NOWAIT` now ... instead of `M_WAITOK`. [Why] The reason is that in some places in the DRM drivers (in particular, the framebuffer management code), kmalloc() is called from a non-sleepable context, such as after a call to mtx_lock(8) with an MTX_DEF mutex. If `GFP_KERNEL` is defined as `M_WAITOK`, we hit an assertion from witness(4). [How] The definition of `GFP_KERNEL` is changed to `M_NOWAIT`. This means that callers should verify the return value of kmalloc(). Fortunately, this is always the case in Linux. Reviewed by: bz, emaste, manu Approved by: manu Differential Revision: https://reviews.freebsd.org/D42054 --- sys/compat/linuxkpi/common/include/linux/gfp.h | 2 +- sys/compat/linuxkpi/common/include/linux/slab.h | 8 ++++---- sys/compat/linuxkpi/common/src/linux_compat.c | 7 +++++-- sys/compat/linuxkpi/common/src/linux_interrupt.c | 3 +++ sys/compat/linuxkpi/common/src/linux_pci.c | 4 ++-- sys/compat/linuxkpi/common/src/linux_shmemfs.c | 11 +++++++++-- 6 files changed, 24 insertions(+), 11 deletions(-) diff --git a/sys/compat/linuxkpi/common/include/linux/gfp.h b/sys/compat/linuxkpi/common/include/linux/gfp.h index c5de09e896de..17590806343f 100644 --- a/sys/compat/linuxkpi/common/include/linux/gfp.h +++ b/sys/compat/linuxkpi/common/include/linux/gfp.h @@ -64,7 +64,7 @@ #define GFP_NOWAIT M_NOWAIT #define GFP_ATOMIC (M_NOWAIT | M_USE_RESERVE) -#define GFP_KERNEL M_WAITOK +#define GFP_KERNEL M_NOWAIT #define GFP_USER M_WAITOK #define GFP_HIGHUSER M_WAITOK #define GFP_HIGHUSER_MOVABLE M_WAITOK diff --git a/sys/compat/linuxkpi/common/include/linux/slab.h b/sys/compat/linuxkpi/common/include/linux/slab.h index 8557f831bb60..3e857a4adc54 100644 --- a/sys/compat/linuxkpi/common/include/linux/slab.h +++ b/sys/compat/linuxkpi/common/include/linux/slab.h @@ -47,12 +47,12 @@ MALLOC_DECLARE(M_KMALLOC); #define kzalloc(size, flags) kmalloc(size, (flags) | __GFP_ZERO) #define kzalloc_node(size, flags, node) kmalloc_node(size, (flags) | __GFP_ZERO, node) #define kfree_const(ptr) kfree(ptr) -#define vzalloc(size) __vmalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO, 0) +#define vzalloc(size) __vmalloc(size, M_WAITOK | __GFP_NOWARN | __GFP_ZERO, 0) #define vfree(arg) kfree(arg) #define kvfree(arg) kfree(arg) -#define vmalloc_node(size, node) __vmalloc_node(size, GFP_KERNEL, node) -#define vmalloc_user(size) __vmalloc(size, GFP_KERNEL | __GFP_ZERO, 0) -#define vmalloc(size) __vmalloc(size, GFP_KERNEL, 0) +#define vmalloc_node(size, node) __vmalloc_node(size, M_WAITOK, node) +#define vmalloc_user(size) __vmalloc(size, M_WAITOK | __GFP_ZERO, 0) +#define vmalloc(size) __vmalloc(size, M_WAITOK, 0) #define __kmalloc(...) kmalloc(__VA_ARGS__) /* diff --git a/sys/compat/linuxkpi/common/src/linux_compat.c b/sys/compat/linuxkpi/common/src/linux_compat.c index b913ae602ab3..baa4ff2fee44 100644 --- a/sys/compat/linuxkpi/common/src/linux_compat.c +++ b/sys/compat/linuxkpi/common/src/linux_compat.c @@ -574,7 +574,7 @@ linux_file_alloc(void) { struct linux_file *filp; - filp = kzalloc(sizeof(*filp), GFP_KERNEL); + filp = kzalloc(sizeof(*filp), M_WAITOK); /* set initial refcount */ filp->f_count = 1; @@ -1412,6 +1412,9 @@ linux_file_mmap_single(struct file *fp, const struct file_operations *fop, return (EINVAL); vmap = kzalloc(sizeof(*vmap), GFP_KERNEL); + if (vmap == NULL) + return (ENOMEM); + vmap->vm_start = 0; vmap->vm_end = size; vmap->vm_pgoff = *offset / PAGE_SIZE; @@ -1941,7 +1944,7 @@ vmmap_add(void *addr, unsigned long size) { struct vmmap *vmmap; - vmmap = kmalloc(sizeof(*vmmap), GFP_KERNEL); + vmmap = kmalloc(sizeof(*vmmap), M_WAITOK); mtx_lock(&vmmaplock); vmmap->vm_size = size; vmmap->vm_addr = addr; diff --git a/sys/compat/linuxkpi/common/src/linux_interrupt.c b/sys/compat/linuxkpi/common/src/linux_interrupt.c index 5602b09c8fb8..886a5d5ad014 100644 --- a/sys/compat/linuxkpi/common/src/linux_interrupt.c +++ b/sys/compat/linuxkpi/common/src/linux_interrupt.c @@ -135,6 +135,9 @@ lkpi_request_irq(struct device *xdev, unsigned int irq, GFP_KERNEL | __GFP_ZERO); else irqe = kzalloc(sizeof(*irqe), GFP_KERNEL); + if (irqe == NULL) + return (-ENOMEM); + irqe->dev = dev; irqe->res = res; irqe->arg = arg; diff --git a/sys/compat/linuxkpi/common/src/linux_pci.c b/sys/compat/linuxkpi/common/src/linux_pci.c index 8386552dcd51..f692b9c3ef09 100644 --- a/sys/compat/linuxkpi/common/src/linux_pci.c +++ b/sys/compat/linuxkpi/common/src/linux_pci.c @@ -305,7 +305,7 @@ lkpifill_pci_dev(device_t dev, struct pci_dev *pdev) pdev->subsystem_device = pci_get_subdevice(dev); pdev->class = pci_get_class(dev); pdev->revision = pci_get_revid(dev); - pdev->path_name = kasprintf(GFP_KERNEL, "%04d:%02d:%02d.%d", + pdev->path_name = kasprintf(M_WAITOK, "%04d:%02d:%02d.%d", pci_get_domain(dev), pci_get_bus(dev), pci_get_slot(dev), pci_get_function(dev)); pdev->bus = malloc(sizeof(*pdev->bus), M_DEVBUF, M_WAITOK | M_ZERO); @@ -1469,7 +1469,7 @@ linux_dma_pool_create(char *name, struct device *dev, size_t size, priv = dev->dma_priv; - pool = kzalloc(sizeof(*pool), GFP_KERNEL); + pool = kzalloc(sizeof(*pool), M_WAITOK); pool->pool_device = dev; pool->pool_entry_size = size; diff --git a/sys/compat/linuxkpi/common/src/linux_shmemfs.c b/sys/compat/linuxkpi/common/src/linux_shmemfs.c index 3c71d6495f4a..bf6a8cd775e9 100644 --- a/sys/compat/linuxkpi/common/src/linux_shmemfs.c +++ b/sys/compat/linuxkpi/common/src/linux_shmemfs.c @@ -48,8 +48,15 @@ linux_shmem_read_mapping_page_gfp(vm_object_t obj, int pindex, gfp_t gfp) struct page *page; int rv; - if ((gfp & GFP_NOWAIT) != 0) - panic("GFP_NOWAIT is unimplemented"); + /* + * Historically, GFP_KERNEL was the equivalent of M_WAITOK. But it was + * changed to a synonym of M_NOWAIT to allow allocations in + * non-sleepable code. + * + * However, there was an assertion here to make sure that `gfp` was + * never set to GFP_NOWAIT/M_NOWAIT. Do we need a specific handling of + * M_NOWAIT here? + */ VM_OBJECT_WLOCK(obj); rv = vm_page_grab_valid(&page, obj, pindex, VM_ALLOC_NORMAL |