From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.6 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 44CE2C433DF for ; Sat, 4 Jul 2020 03:50:41 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id CABBA20675 for ; Sat, 4 Jul 2020 03:50:40 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="b37FjW/8" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org CABBA20675 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 088FF6B00BB; Fri, 3 Jul 2020 23:50:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 013C16B00BC; Fri, 3 Jul 2020 23:50:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E1CEE6B00BD; Fri, 3 Jul 2020 23:50:39 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id C7D216B00BB for ; Fri, 3 Jul 2020 23:50:39 -0400 (EDT) Received: from smtpin29.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 65F6A2C8F for ; Sat, 4 Jul 2020 03:50:39 +0000 (UTC) X-FDA: 76999016598.29.van74_350fc4b26e97 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin29.hostedemail.com (Postfix) with ESMTP id 446CC18086E2F for ; Sat, 4 Jul 2020 03:50:39 +0000 (UTC) X-HE-Tag: van74_350fc4b26e97 X-Filterd-Recvd-Size: 7354 Received: from mail-pj1-f65.google.com (mail-pj1-f65.google.com [209.85.216.65]) by imf06.hostedemail.com (Postfix) with ESMTP for ; Sat, 4 Jul 2020 03:50:38 +0000 (UTC) Received: by mail-pj1-f65.google.com with SMTP id gc15so5293739pjb.0 for ; Fri, 03 Jul 2020 20:50:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:mime-version:content-disposition :user-agent; bh=q1XynUfsc2eM5ihuhlQetlsbC+dUMHlSTMRXkwQtFo0=; b=b37FjW/8orW8HP/T+mL2UAW+xgIRIknUEYz/Znm7S1gpOWRQfw/aqTVX0VPMjfIzEb gx4T0PN2v86iKDbiLraSujTw7LwvLdKprv1VoJS2TiE8xI6LkOwlDE/XUOrqqTpeTFws a28ZpdHUwOOB9hZ2l86Cqpi3m8Ewjfp8KFHZyFHysM8/z4eKXZfjjNZ/MohFtKDtn1/1 1J733Hru6GAeJlBMa2zErZ25wT9JT7JmvA4WaznGOdR0cT7e5N0g8v8SMTBMsV8dz/aZ 6wyHctjpBGP6uNh/3lmCqDCl67KzXxVFzHNmTPQhtm2l4aU+/E4Fn8/nIyd7Tl/8AxYU 7+dg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition:user-agent; bh=q1XynUfsc2eM5ihuhlQetlsbC+dUMHlSTMRXkwQtFo0=; b=grAp/43zo46CPbBOOXObsajYNJwpmQBFmqVG8iFn+nwClLdnDOsOE7pYZq0AK9K9EF PwKqhET8CBJ8keY8SF9rmgPjAgNFJCUcbwfG3/cf38iGaIl4nri91ynZqLq+pwow2FgJ oJfXn3uqCiLGKOsFn0TSOvUiCAM0bgr4llIC7LQq/BuHiQjPGzfG34Kx/r7OZYwgRKic NdBQ9UZTncv7rpkqMQQ7gON8ol5aittAZ85WPcxYxJ+huXJoe5B9PmYk4EV2r+KGlO9u ED2UnuQ8pqhM3KcaUQPDL2bbz+AB4XrUuPn5+76BBmADQLV2VKbj+BTHBTHbcPgNe1AB uDVw== X-Gm-Message-State: AOAM531Y/cbrhbqVulBaD6fuZsmkeeUdFttrKKq7o7qs9Ecg1URJbyZt HHF2LQ2m8XM7Xe6XYhzpn3I= X-Google-Smtp-Source: ABdhPJwIymfQjaSCqC91R1uFDTfYHYWYbhAEXdgocisoXOaEon9mDUugGTC80ZkS4KHUYRuOKmqMkA== X-Received: by 2002:a17:902:d896:: with SMTP id b22mr33503325plz.87.1593834637952; Fri, 03 Jul 2020 20:50:37 -0700 (PDT) Received: from localhost ([144.34.187.180]) by smtp.gmail.com with ESMTPSA id u74sm13072745pgc.58.2020.07.03.20.50.35 (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Fri, 03 Jul 2020 20:50:37 -0700 (PDT) Date: Sat, 4 Jul 2020 03:50:27 +0000 From: Long Li To: willy@infradead.org, cl@linux.com, penberg@kernel.org, rientjes@google.com, iamjoonsoo.kim@lge.com, akpm@linux-foundation.org Cc: linux-mm@kvack.org Subject: [PATCH v6] mm, slab: Check GFP_SLAB_BUG_MASK before alloc_pages in kmalloc_order Message-ID: <20200704035027.GA62481@lilong> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline User-Agent: Mutt/1.9.4 (2018-02-28) X-Rspamd-Queue-Id: 446CC18086E2F X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam03 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: kmalloc cannot allocate memory from HIGHMEM. Allocating large amounts of memory currently bypasses the check and will simply leak the memory when page_address() returns NULL. To fix this, factor the GFP_SLAB_BUG_MASK check out of slab & slub, and call it from kmalloc_order() as well. In order to make the code clear, the warning message is put in one place. Reviewed-by: Matthew Wilcox (Oracle) Reviewed-by: Pekka Enberg Cc: Christoph Lameter Cc: David Rientjes Cc: Joonsoo Kim Cc: Andrew Morton Signed-off-by: Long Li --- changes in v6: -Move flags check out of kmalloc_fix_flags(), thus saving a function call in the normal case. changes in v5: -Change the falg check function name to kmalloc_fix_flags(), This name may be more appropriate. changes in V4: -Change the check function name to kmalloc_check_flags() -Put the flags check into the kmalloc_check_flags() changes in V3: -Put the warning message in one place -updage the change log to be clear mm/slab.c | 10 +++------- mm/slab.h | 1 + mm/slab_common.c | 17 +++++++++++++++++ mm/slub.c | 9 ++------- 4 files changed, 23 insertions(+), 14 deletions(-) diff --git a/mm/slab.c b/mm/slab.c index ac7a223d9ac3..559fc1c12a2e 100644 --- a/mm/slab.c +++ b/mm/slab.c @@ -2573,13 +2573,9 @@ static struct page *cache_grow_begin(struct kmem_cache *cachep, * Be lazy and only check for valid flags here, keeping it out of the * critical path in kmem_cache_alloc(). */ - if (unlikely(flags & GFP_SLAB_BUG_MASK)) { - gfp_t invalid_mask = flags & GFP_SLAB_BUG_MASK; - flags &= ~GFP_SLAB_BUG_MASK; - pr_warn("Unexpected gfp: %#x (%pGg). Fixing up to gfp: %#x (%pGg). Fix your code!\n", - invalid_mask, &invalid_mask, flags, &flags); - dump_stack(); - } + if (unlikely(flags & GFP_SLAB_BUG_MASK)) + flags = kmalloc_fix_flags(flags); + WARN_ON_ONCE(cachep->ctor && (flags & __GFP_ZERO)); local_flags = flags & (GFP_CONSTRAINT_MASK|GFP_RECLAIM_MASK); diff --git a/mm/slab.h b/mm/slab.h index a06f3313e4a0..8cd2bf391725 100644 --- a/mm/slab.h +++ b/mm/slab.h @@ -90,6 +90,7 @@ void create_kmalloc_caches(slab_flags_t); struct kmem_cache *kmalloc_slab(size_t, gfp_t); #endif +gfp_t kmalloc_fix_flags(gfp_t flags); /* Functions provided by the slab allocators */ int __kmem_cache_create(struct kmem_cache *, slab_flags_t flags); diff --git a/mm/slab_common.c b/mm/slab_common.c index a143a8c8f874..a513f3237155 100644 --- a/mm/slab_common.c +++ b/mm/slab_common.c @@ -26,6 +26,8 @@ #define CREATE_TRACE_POINTS #include +#include "internal.h" + #include "slab.h" enum slab_state slab_state; @@ -805,6 +807,18 @@ void __init create_kmalloc_caches(slab_flags_t flags) } #endif /* !CONFIG_SLOB */ +gfp_t kmalloc_fix_flags(gfp_t flags) +{ + gfp_t invalid_mask = flags & GFP_SLAB_BUG_MASK; + + flags &= ~GFP_SLAB_BUG_MASK; + pr_warn("Unexpected gfp: %#x (%pGg). Fixing up to gfp: %#x (%pGg). Fix your code!\n", + invalid_mask, &invalid_mask, flags, &flags); + dump_stack(); + + return flags; +} + /* * To avoid unnecessary overhead, we pass through large allocation requests * directly to the page allocator. We use __GFP_COMP, because we will need to @@ -815,6 +829,9 @@ void *kmalloc_order(size_t size, gfp_t flags, unsigned int order) void *ret = NULL; struct page *page; + if (unlikely(flags & GFP_SLAB_BUG_MASK)) + flags = kmalloc_fix_flags(flags); + flags |= __GFP_COMP; page = alloc_pages(flags, order); if (likely(page)) { diff --git a/mm/slub.c b/mm/slub.c index 62d2de56549e..29a650f89e8d 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -1817,13 +1817,8 @@ static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node) static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node) { - if (unlikely(flags & GFP_SLAB_BUG_MASK)) { - gfp_t invalid_mask = flags & GFP_SLAB_BUG_MASK; - flags &= ~GFP_SLAB_BUG_MASK; - pr_warn("Unexpected gfp: %#x (%pGg). Fixing up to gfp: %#x (%pGg). Fix your code!\n", - invalid_mask, &invalid_mask, flags, &flags); - dump_stack(); - } + if (unlikely(flags & GFP_SLAB_BUG_MASK)) + flags = kmalloc_fix_flags(flags); return allocate_slab(s, flags & (GFP_RECLAIM_MASK | GFP_CONSTRAINT_MASK), node); -- 2.17.1