From patchwork Thu Jul 2 15:27:59 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Long Li X-Patchwork-Id: 11639291 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 17F27618 for ; Thu, 2 Jul 2020 15:28:17 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id D7030208B6 for ; Thu, 2 Jul 2020 15:28:16 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="sHVI+Z2+" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org D7030208B6 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 DA5496B00A6; Thu, 2 Jul 2020 11:28:15 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id D55426B00A8; Thu, 2 Jul 2020 11:28:15 -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 C44C46B00A9; Thu, 2 Jul 2020 11:28:15 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0095.hostedemail.com [216.40.44.95]) by kanga.kvack.org (Postfix) with ESMTP id AED1B6B00A6 for ; Thu, 2 Jul 2020 11:28:15 -0400 (EDT) Received: from smtpin21.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 551B6180AD80F for ; Thu, 2 Jul 2020 15:28:15 +0000 (UTC) X-FDA: 76993516950.21.grass83_540388c26e8a Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin21.hostedemail.com (Postfix) with ESMTP id 28600180442C3 for ; Thu, 2 Jul 2020 15:28:15 +0000 (UTC) X-Spam-Summary: 1,0,0,6313336660e008cf,d41d8cd98f00b204,lonuxli.64@gmail.com,,RULES_HIT:41:152:355:379:421:560:800:960:973:988:989:1260:1277:1312:1313:1314:1345:1434:1437:1516:1518:1519:1535:1543:1593:1594:1595:1596:1711:1730:1747:1777:1792:2198:2199:2393:2559:2562:2693:2731:3138:3139:3140:3141:3142:3355:3865:3866:3867:3868:3870:3871:3872:4117:4321:4605:5007:6261:6653:7514:7903:8603:8660:9040:9413:10004:10400:10450:10455: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:19904:19999:21080:21433:21444:21451:21627:21666:21796:21939:21990:30012:30029:30036:30054:30070,0,RBL:209.85.214.193:@gmail.com:.lbl8.mailshell.net-66.100.201.100 62.50.0.100;04yfi55gjpoeprakpgo7n61rojnm1ock5x35s93ajwgba6qtyhqhx4duc99dcw5.mc8c8wj7doatzuwrkz5f6c9jx87doebb1f3hxd7hp5w1oumdb4u5arbix37gynm.y-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,M X-HE-Tag: grass83_540388c26e8a X-Filterd-Recvd-Size: 6846 Received: from mail-pl1-f193.google.com (mail-pl1-f193.google.com [209.85.214.193]) by imf29.hostedemail.com (Postfix) with ESMTP for ; Thu, 2 Jul 2020 15:28:14 +0000 (UTC) Received: by mail-pl1-f193.google.com with SMTP id g17so11440083plq.12 for ; Thu, 02 Jul 2020 08:28:14 -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=f6NHiKBOJxQx2yu25TfleraIEvN0rvdlanwLkWkQo5A=; b=sHVI+Z2+QnOPqFlk3cV7wUqBpsuT8U4d+eJmFckHcxG7gqqQkN+g7rfCTrAWhH7zNE NvrXfIlEHszrhIITJ9+pPVwQqvE2YB8HeAb5tFoHjVkEMaqRYA20kfE7+gyS6W5lzmuc YsuZ2NpjIhjzaXTIf+J4IR/TKAhwbKdV0R7WNx2zWAnIH+S7TuyUbnR6pQHYuT+T5G2T gWpdWk20+PiJFXXgXFqQn1nXjVzBONJN00cdX30q+cO0KiRnf72jqzZx45Ic6JBfS2wf Evu8EseZLptk03ZJbPI2HN8JhJgxb44mkjn+oZ5x2rf+DCWhEPdsk69097DF0w3fQ0Q7 hkdg== 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=f6NHiKBOJxQx2yu25TfleraIEvN0rvdlanwLkWkQo5A=; b=lwj0C/8Ik6bxD80OPP1h/iiqxGJc2z5HpbCJqpwCWH+KDfunjk0h8SzRPU3CrQtkhr QCvhxEIRCwtUwR0se7MpvVRXRvDRCkPwQiP/7XHQnwo4ACGo8ba2RYJ5vASjekYNLb7q 12KO6PzkN556hikcROXb8lcU8TGhT3+5jk+swzu7Hdq82HTcFMpVQt5TmRCOTEfSlvcF x+zU1M27mxpX4wfWMRuTAvyEhHiivVEFMkh/uz4VwNJMb5pFPccskaj0nKggmSVVOuMQ 6of5uLAW9qePMexZHwOv4JuvqFZzVHdahdczZNTIRrVM6+sLRG4XItgKTQfA0S3kjzni pF1Q== X-Gm-Message-State: AOAM532KCuwFnE/EKUB5oQ5Nl4H/vxYC5uo5EnGxO4kYY09UqbYZ46CU lihLfzDfFhQHbcgjiH8FUm8= X-Google-Smtp-Source: ABdhPJxfhDWpMb0DIFZ7cnoKC1zFDSgfvqqwtdrZliYAbrZoOBhClp2cLdpKYFc9Nnhr40pnaonwCw== X-Received: by 2002:a17:902:8bc7:: with SMTP id r7mr27861805plo.57.1593703693653; Thu, 02 Jul 2020 08:28:13 -0700 (PDT) Received: from localhost ([144.34.187.180]) by smtp.gmail.com with ESMTPSA id s194sm9248546pgs.24.2020.07.02.08.28.11 (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 02 Jul 2020 08:28:13 -0700 (PDT) Date: Thu, 2 Jul 2020 15:27:59 +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, linux-kernel@vger.kernel.org Subject: [PATCH v4] mm, slab: Check GFP_SLAB_BUG_MASK before alloc_pages in kmalloc_order Message-ID: <20200702152759.GA32931@lilong> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.9.4 (2018-02-28) X-Rspamd-Queue-Id: 28600180442C3 X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam05 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. Signed-off-by: Long Li --- 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 | 8 +------- mm/slab.h | 1 + mm/slab_common.c | 18 +++++++++++++++++- mm/slub.c | 8 +------- 4 files changed, 20 insertions(+), 15 deletions(-) diff --git a/mm/slab.c b/mm/slab.c index ac7a223d9ac3..755f33f96f04 100644 --- a/mm/slab.c +++ b/mm/slab.c @@ -2573,13 +2573,7 @@ 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(); - } + flags = kmalloc_check_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..48df5660764c 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_check_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..9184e4575d6d 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,20 @@ void __init create_kmalloc_caches(slab_flags_t flags) } #endif /* !CONFIG_SLOB */ +gfp_t kmalloc_check_flags(gfp_t flags) +{ + 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(); + } + + 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,7 +831,7 @@ void *kmalloc_order(size_t size, gfp_t flags, unsigned int order) void *ret = NULL; struct page *page; - flags |= __GFP_COMP; + flags = kmalloc_check_flags(flags) | __GFP_COMP; page = alloc_pages(flags, order); if (likely(page)) { ret = page_address(page); diff --git a/mm/slub.c b/mm/slub.c index 62d2de56549e..8e787767850f 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -1817,13 +1817,7 @@ 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(); - } + flags = kmalloc_check_flags(flags); return allocate_slab(s, flags & (GFP_RECLAIM_MASK | GFP_CONSTRAINT_MASK), node);