Re: [PATCH v2 2/3] mm/vmap: Add a notifier for when we run out of vmap address space

From: Joonas Lahtinen
Date: Tue Apr 05 2016 - 04:00:32 EST


On ma, 2016-04-04 at 14:46 +0100, Chris Wilson wrote:
> vmaps are temporary kernel mappings that may be of long duration.
> Reusing a vmap on an object is preferrable for a driver as the cost of
> setting up the vmap can otherwise dominate the operation on the object.
> However, the vmap address space is rather limited on 32bit systems and
> so we add a notification for vmap pressure in order for the driver to
> release any cached vmappings.
>
> The interface is styled after the oom-notifier where the callees are
> passed a pointer to an unsigned long counter for them to indicate if they
> have freed any space.
>
> v2: Guard the blocking notifier call with gfpflags_allow_blocking()
> v3: Correct typo in forward declaration and move to head of file
>
> Signed-off-by: Chris Wilson <chris@xxxxxxxxxxxxxxxxxx>
> Cc: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
> Cc: David Rientjes <rientjes@xxxxxxxxxx>
> Cc: Roman Peniaev <r.peniaev@xxxxxxxxx>
> Cc: Mel Gorman <mgorman@xxxxxxxxxxxxxxxxxxx>
> Cc: linux-mm@xxxxxxxxx
> Cc: linux-kernel@xxxxxxxxxxxxxxx
> Acked-by: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx> # for inclusion via DRM
> Cc: Joonas Lahtinen <joonas.lahtinen@xxxxxxxxxxxxxxx>

Reviewed-by: Joonas Lahtinen <joonas.lahtinen@xxxxxxxxxxxxxxx>

> Cc: Tvrtko Ursulin <tvrtko.ursulin@xxxxxxxxx>
> ---
> Âinclude/linux/vmalloc.h |ÂÂ4 ++++
> Âmm/vmalloc.cÂÂÂÂÂÂÂÂÂÂÂÂ| 27 +++++++++++++++++++++++++++
> Â2 files changed, 31 insertions(+)
>
> diff --git a/include/linux/vmalloc.h b/include/linux/vmalloc.h
> index d1f1d338af20..8b51df3ab334 100644
> --- a/include/linux/vmalloc.h
> +++ b/include/linux/vmalloc.h
> @@ -8,6 +8,7 @@
> Â#include
> Â
> Âstruct vm_area_struct; /* vma defining user mapping in mm_types.h */
> +struct notifier_block; /* in notifier.h */
> Â
> Â/* bits in flags of vmalloc's vm_struct below */
> Â#define VM_IOREMAP 0x00000001 /* ioremap() and friends */
> @@ -187,4 +188,7 @@ pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms)
> Â#define VMALLOC_TOTAL 0UL
> Â#endif
> Â
> +int register_vmap_purge_notifier(struct notifier_block *nb);
> +int unregister_vmap_purge_notifier(struct notifier_block *nb);
> +
> Â#endif /* _LINUX_VMALLOC_H */
> diff --git a/mm/vmalloc.c b/mm/vmalloc.c
> index ae7d20b447ff..293889d7f482 100644
> --- a/mm/vmalloc.c
> +++ b/mm/vmalloc.c
> @@ -21,6 +21,7 @@
> Â#include
> Â#include
> Â#include
> +#include
> Â#include
> Â#include
> Â#include
> @@ -344,6 +345,8 @@ static void __insert_vmap_area(struct vmap_area *va)
> Â
> Âstatic void purge_vmap_area_lazy(void);
> Â
> +static BLOCKING_NOTIFIER_HEAD(vmap_notify_list);
> +
> Â/*
> Â * Allocate a region of KVA of the specified size and alignment, within the
> Â * vstart and vend.
> @@ -363,6 +366,8 @@ static struct vmap_area *alloc_vmap_area(unsigned long size,
> Â BUG_ON(offset_in_page(size));
> Â BUG_ON(!is_power_of_2(align));
> Â
> + might_sleep_if(gfpflags_allow_blocking(gfp_mask));
> +
> Â va = kmalloc_node(sizeof(struct vmap_area),
> Â gfp_mask & GFP_RECLAIM_MASK, node);
> Â if (unlikely(!va))
> @@ -468,6 +473,16 @@ overflow:
> Â purged = 1;
> Â goto retry;
> Â }
> +
> + if (gfpflags_allow_blocking(gfp_mask)) {
> + unsigned long freed = 0;
> + blocking_notifier_call_chain(&vmap_notify_list, 0, &freed);
> + if (freed > 0) {
> + purged = 0;
> + goto retry;
> + }
> + }
> +
> Â if (printk_ratelimit())
> Â pr_warn("vmap allocation for size %lu failed: use vmalloc= to increase size\n",
> Â size);
> @@ -475,6 +490,18 @@ overflow:
> Â return ERR_PTR(-EBUSY);
> Â}
> Â
> +int register_vmap_purge_notifier(struct notifier_block *nb)
> +{
> + return blocking_notifier_chain_register(&vmap_notify_list, nb);
> +}
> +EXPORT_SYMBOL_GPL(register_vmap_purge_notifier);
> +
> +int unregister_vmap_purge_notifier(struct notifier_block *nb)
> +{
> + return blocking_notifier_chain_unregister(&vmap_notify_list, nb);
> +}
> +EXPORT_SYMBOL_GPL(unregister_vmap_purge_notifier);
> +
> Âstatic void __free_vmap_area(struct vmap_area *va)
> Â{
> Â BUG_ON(RB_EMPTY_NODE(&va->rb_node));
--
Joonas Lahtinen
Open Source Technology Center
Intel Corporation