From patchwork Sat Jul 4 03:50:27 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Long Li X-Patchwork-Id: 11643365 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 5F7A1618 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 20226207D4 for ; Sat, 4 Jul 2020 03:50:41 +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 20226207D4 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) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 013C16B00BC; Fri, 3 Jul 2020 23:50:39 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org 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-Original-To: linux-mm@kvack.org 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-Spam-Summary: 1,0,0,9f9f9f2e389e0ef7,d41d8cd98f00b204,lonuxli.64@gmail.com,,RULES_HIT:41:152:355:379:421:560:800:960:973:988:989:1260:1277:1312:1313:1314:1345:1431:1434:1437:1516:1518:1519:1535:1544:1593:1594:1595:1596:1605:1711:1730:1747:1777:1792:2198:2199:2393:2553:2559:2562:2693:2731:3138:3139:3140:3141:3142:3865:3866:3867:3868:3870:3871:3872:4118:4321:4605:5007:6261:6653:7514:7903:8603:8660:9040:9413:10004:11026:11473:11658:11914:12043:12048:12296:12297:12438:12517:12519:12555:12895:12986:13095:13148:13181:13229:13230:13255:13895:14181:14394:14687:14721:21080:21433:21444:21451:21627:21666:21796:21939:21990:30012:30029:30036:30054:30070:30090,0,RBL:209.85.216.65:@gmail.com:.lbl8.mailshell.net-66.100.201.100 62.50.0.100;04y8hmzfhs8bkg4qibftzo3goe6wpycmk8ks8xo9x5j8rz4c77d51uftbgxsjk9.6wjwom1sc1acaskqgr3pnwrpcd4tyqjnho8kioh73f5aoxocu7jku9z8ux3wt4s.k-lbl8.mailshell.net-223.238.255.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,D NSBL:non 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-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 Acked-by: David Rientjes --- 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);