Re: UBSAN: Undefined behaviour in mm/page_alloc.c

From: Andrew Morton
Date: Tue Nov 13 2018 - 18:15:09 EST


On Tue, 13 Nov 2018 10:43:05 +0100 Michal Hocko <mhocko@xxxxxxxxxx> wrote:

> Konstantin has noticed that kvmalloc might trigger the following warning
> [Thu Nov 1 08:43:56 2018] WARNING: CPU: 0 PID: 6676 at mm/vmstat.c:986 __fragmentation_index+0x54/0x60
> [...]
> [Thu Nov 1 08:43:56 2018] Call Trace:
> [Thu Nov 1 08:43:56 2018] fragmentation_index+0x76/0x90
> [Thu Nov 1 08:43:56 2018] compaction_suitable+0x4f/0xf0
> [Thu Nov 1 08:43:56 2018] shrink_node+0x295/0x310
> [Thu Nov 1 08:43:56 2018] node_reclaim+0x205/0x250
> [Thu Nov 1 08:43:56 2018] get_page_from_freelist+0x649/0xad0
> [Thu Nov 1 08:43:56 2018] ? get_page_from_freelist+0x2d4/0xad0
> [Thu Nov 1 08:43:56 2018] ? release_sock+0x19/0x90
> [Thu Nov 1 08:43:56 2018] ? do_ipv6_setsockopt.isra.5+0x10da/0x1290
> [Thu Nov 1 08:43:56 2018] __alloc_pages_nodemask+0x12a/0x2a0
> [Thu Nov 1 08:43:56 2018] kmalloc_large_node+0x47/0x90
> [Thu Nov 1 08:43:56 2018] __kmalloc_node+0x22b/0x2e0
> [Thu Nov 1 08:43:56 2018] kvmalloc_node+0x3e/0x70
> [Thu Nov 1 08:43:56 2018] xt_alloc_table_info+0x3a/0x80 [x_tables]
> [Thu Nov 1 08:43:56 2018] do_ip6t_set_ctl+0xcd/0x1c0 [ip6_tables]
> [Thu Nov 1 08:43:56 2018] nf_setsockopt+0x44/0x60
> [Thu Nov 1 08:43:56 2018] SyS_setsockopt+0x6f/0xc0
> [Thu Nov 1 08:43:56 2018] do_syscall_64+0x67/0x120
> [Thu Nov 1 08:43:56 2018] entry_SYSCALL_64_after_hwframe+0x3d/0xa2
>
> ...
>
> --- a/mm/page_alloc.c
> +++ b/mm/page_alloc.c
> @@ -4364,6 +4353,15 @@ __alloc_pages_nodemask(gfp_t gfp_mask, unsigned int order, int preferred_nid,
> gfp_t alloc_mask; /* The gfp_t that was actually used for allocation */
> struct alloc_context ac = { };
>
> + /*
> + * There are several places where we assume that the order value is sane
> + * so bail out early if the request is out of bound.
> + */
> + if (unlikely(order >= MAX_ORDER)) {
> + WARN_ON_ONCE(!(gfp_mask & __GFP_NOWARN));
> + return NULL;
> + }
> +

I know "everybody enables CONFIG_DEBUG_VM", but given this is fastpath,
we could help those who choose not to enable it by using

#ifdef CONFIG_DEBUG_VM
if (WARN_ON_ONCE(order >= MAX_ORDER && !(gfp_mask & __GFP_NOWARN)))
return NULL;
#endif

(Again curses 91241681c62 ("include/linux/mmdebug.h: make VM_WARN* non-rvals"))