From patchwork Sun Jun 14 06:38:58 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muchun Song X-Patchwork-Id: 11603187 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 F0542618 for ; Sun, 14 Jun 2020 06:39:38 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id B1FE0207C4 for ; Sun, 14 Jun 2020 06:39:38 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=bytedance-com.20150623.gappssmtp.com header.i=@bytedance-com.20150623.gappssmtp.com header.b="zEhIacWz" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org B1FE0207C4 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=bytedance.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id A1F666B0006; Sun, 14 Jun 2020 02:39:37 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 9CF846B0007; Sun, 14 Jun 2020 02:39:37 -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 8E4A06B0008; Sun, 14 Jun 2020 02:39:37 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0113.hostedemail.com [216.40.44.113]) by kanga.kvack.org (Postfix) with ESMTP id 74C746B0006 for ; Sun, 14 Jun 2020 02:39:37 -0400 (EDT) Received: from smtpin17.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 25578357B for ; Sun, 14 Jun 2020 06:39:37 +0000 (UTC) X-FDA: 76926866394.17.use79_1312b8d26deb Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin17.hostedemail.com (Postfix) with ESMTP id 7EFA5180D0180 for ; Sun, 14 Jun 2020 06:39:36 +0000 (UTC) X-Spam-Summary: 2,0,0,196fc1bd9c3a2324,d41d8cd98f00b204,songmuchun@bytedance.com,,RULES_HIT:41:355:379:421:541:800:960:973:988:989:1260:1311:1314:1345:1434:1437:1515:1535:1543:1711:1730:1747:1777:1792:2198:2199:2393:2559:2562:2731:3138:3139:3140:3141:3142:3354:3865:3866:3867:3868:3870:3871:3872:3874:4117:4321:5007:6119:6120:6261:6653:7901:8660:9040:9592:10004:11026:11473:11658:11914:12043:12048:12291:12296:12297:12517:12519:12555:12895:12986:13148:13230:13894:14093:14181:14394:14721:21080:21444:21450:21451:21627:21939:21990:30012:30045:30054:30070,0,RBL:209.85.215.193:@bytedance.com:.lbl8.mailshell.net-66.201.201.201 62.14.0.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:24,LUA_SUMMARY:none X-HE-Tag: use79_1312b8d26deb X-Filterd-Recvd-Size: 6313 Received: from mail-pg1-f193.google.com (mail-pg1-f193.google.com [209.85.215.193]) by imf37.hostedemail.com (Postfix) with ESMTP for ; Sun, 14 Jun 2020 06:39:35 +0000 (UTC) Received: by mail-pg1-f193.google.com with SMTP id l63so3929517pge.12 for ; Sat, 13 Jun 2020 23:39:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=WBuxpUA3UgXQeJ6yjt0LyKTJBSZPG5D7qi17zQVgy78=; b=zEhIacWz/ZuRcfT9j2C+weG+5nGXWYqbYPGmpkBacnxp/9N3Pt2pMxp8/FS1Zn4gPk QIs7ZHjRX4UOEhWFJy9aQ2+uhmSD6h/sPZnhlrq43O/1Bok+/BCNgUGeZln0gPPqIkQN eiH/NBD9vaXwWm243zf34rX0XxsJi7ynxaXz9mac0YAL2Ir6CEy4NID+sL6QMjvmJx0r +3dO49QF0hMac8ZZqiNEb6X9S5RCtwAik3HSHogcyD/yqyh0XGTtkzWmZCL5IjsHgOz8 XjuPrrcca1qVJr57M6+qyk710FS5GwYNeL5mFGmCtPK6eagTkT+7AVcePY+hdRa629Z6 nYzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=WBuxpUA3UgXQeJ6yjt0LyKTJBSZPG5D7qi17zQVgy78=; b=CCG01WzkIsEkASzJ3T1J5C08ceDN1fSLmd4zpYEvwPLhJdv9/oVnpIP1kjW+zSzZn+ Wm8vJBTWTCX7yrHboy9tt72+cMNwaCNfYpzfOlsfLHpi1s+o4kMPzX/ncFYgj0uVBdbf 2s52O9cuXx5qAKOjwHUnI57fh/wJ+5eEQz9oJymxq86Dx0rPhwoJnSACyPx25HLBawp1 RQOK2sN6Ew2wVv9XvY1HZU3He+6Rj1wTdlaRXr8xnrp1Vaua2mU9CsFm6M9wxz7ZI+Mj wKR3qe/pPn+4NBw+ASPTzO/F2vhHi8k1EHhpG8ssHQlF/hrismwGBX5m/Dwhoxtr9CYI pT2w== X-Gm-Message-State: AOAM5308i1dmna4GZpt+nasLPk5CHZS0c1UpOAFsVvW9TIW7PbPeKn54 ggVcSd4vZ2yMEYNyxpgaIMCkVQ== X-Google-Smtp-Source: ABdhPJxfP1CRHnOtjXV37HOXDbESM8vh3+kuv+xWhizYvtbR3j7CtYbBNicmPe4jRaQ3z7Bb5Cs0Sw== X-Received: by 2002:a62:7a89:: with SMTP id v131mr18257746pfc.38.1592116772902; Sat, 13 Jun 2020 23:39:32 -0700 (PDT) Received: from Smcdef-MBP.lan ([103.136.221.68]) by smtp.gmail.com with ESMTPSA id 191sm10453067pfy.161.2020.06.13.23.39.28 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 13 Jun 2020 23:39:32 -0700 (PDT) From: Muchun Song To: 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, Muchun Song Subject: [PATCH] mm/slab: Add a __GFP_ACCOUNT GFP flag check for slab allocation Date: Sun, 14 Jun 2020 14:38:58 +0800 Message-Id: <20200614063858.85118-1-songmuchun@bytedance.com> X-Mailer: git-send-email 2.21.0 (Apple Git-122) MIME-Version: 1.0 X-Rspamd-Queue-Id: 7EFA5180D0180 X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam01 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: When a kmem_cache is initialized with SLAB_ACCOUNT slab flag, we must not call kmem_cache_alloc with __GFP_ACCOUNT GFP flag. In this case, we can be accounted to kmemcg twice. This is not correct. So we add a __GFP_ACCOUNT GFP flag check for slab allocation. We also introduce a new helper named fixup_gfp_flags to do that check. We can reuse the fixup_gfp_flags for SLAB/SLUB. Signed-off-by: Muchun Song Reported-by: kernel test robot --- mm/slab.c | 10 +--------- mm/slab.h | 21 +++++++++++++++++++++ mm/slub.c | 10 +--------- 3 files changed, 23 insertions(+), 18 deletions(-) diff --git a/mm/slab.c b/mm/slab.c index 9350062ffc1a..6e0110bef2d6 100644 --- a/mm/slab.c +++ b/mm/slab.c @@ -126,8 +126,6 @@ #include -#include "internal.h" - #include "slab.h" /* @@ -2579,13 +2577,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 = fixup_gfp_flags(cachep, 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 815e4e9a94cd..0b91f2a7b033 100644 --- a/mm/slab.h +++ b/mm/slab.h @@ -109,6 +109,7 @@ struct memcg_cache_params { #include #include #include +#include "internal.h" /* * State of the slab allocator. @@ -627,6 +628,26 @@ struct kmem_cache_node { }; +static inline gfp_t fixup_gfp_flags(struct kmem_cache *s, gfp_t flags) +{ + gfp_t invalid_mask = 0; + + if (unlikely(flags & GFP_SLAB_BUG_MASK)) + invalid_mask |= flags & GFP_SLAB_BUG_MASK; + + if (unlikely(flags & __GFP_ACCOUNT && s->flags & SLAB_ACCOUNT)) + invalid_mask |= __GFP_ACCOUNT; + + if (unlikely(invalid_mask)) { + flags &= ~invalid_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; +} + static inline struct kmem_cache_node *get_node(struct kmem_cache *s, int node) { return s->node[node]; diff --git a/mm/slub.c b/mm/slub.c index b8f798b50d44..49b5cb7da318 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -37,8 +37,6 @@ #include -#include "internal.h" - /* * Lock order: * 1. slab_mutex (Global Mutex) @@ -1745,13 +1743,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 = fixup_gfp_flags(s, flags); return allocate_slab(s, flags & (GFP_RECLAIM_MASK | GFP_CONSTRAINT_MASK), node);