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 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5D116C433EF for ; Thu, 5 May 2022 12:00:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BA07F6B0071; Thu, 5 May 2022 08:00:48 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B28116B0073; Thu, 5 May 2022 08:00:48 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9A1C66B0074; Thu, 5 May 2022 08:00:48 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 858EA6B0071 for ; Thu, 5 May 2022 08:00:48 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay11.hostedemail.com (Postfix) with ESMTP id 5809181B6E for ; Thu, 5 May 2022 12:00:48 +0000 (UTC) X-FDA: 79431547776.18.6482B49 Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) by imf18.hostedemail.com (Postfix) with ESMTP id 929831C008B for ; Thu, 5 May 2022 12:00:38 +0000 (UTC) Received: by mail-pl1-f171.google.com with SMTP id d17so4206851plg.0 for ; Thu, 05 May 2022 05:00:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=WMuPgepdrSazdkz06gFm9JfoECKA/FToWwZtM73EUSA=; b=47qcXKFK26DGItUhbK44A/cIHxysn4HEVVKYmj8mMt0cVmC9IoOinYztuD2TE5V0hd vDtM/SqZP/emiRk74M/vyenYOPMOqYAo3iGTrAaSAj4XjALQCWD6+rkdP0qjrmOl5KAw 39a3XnwdGxt9oLq4VXkObt50rL/cTSCowy6wxi4Ck+GAuk43tw3UXahr556zq8lkv0dq PDxwYWrgAoHJtFcPVrypUPiQwYRNAOQ+VXXkZaJalew03OlmuYxkxpeIkzOH0iuGK4ya /FA2m+3g9ymChKjROcS4+CTEMg3UoVvFPUZ9WN34dcEhInDJn/xxak/3GCSjYhlJCoQ2 Csww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=WMuPgepdrSazdkz06gFm9JfoECKA/FToWwZtM73EUSA=; b=LrxGmDSChdz0L+edNMXAgD1JolgTHnVJp+gnJllsNZpkRBoMwVjCFMg02GAmjw1qd9 KLtCmL86ihCVra1+ndTu+zZWew7FJlw3Zf642bAmqoQF/0bgL1xLcBrzzKDhiu/bn8G4 S2fVWKB1bev3QpPE+3NtaQTsTx6gYqzo9ZUTaR/tk/ct4BThOcTO9280URnvLhCpHQsN 2FXzQote17kyMHLjM80Xq+9R+bV9unGjE4ry/jCKS0w9nupYRMHRI2PyE4n54p2HsWka 75SqybyNev0TeJav1bOzJnrgYo3LHLblE4XsjpOhRmjDzFdRKOSnJ3//V248ujh5Qtmy 0/OA== X-Gm-Message-State: AOAM532nUgLR9sK6heqmnPfDEPj7FFacmQqbKPuBStD9REe5k2hjYGIy fCKH6JnFpqPr5qLdibmg/xWCdA== X-Google-Smtp-Source: ABdhPJwfN+eLUQdAsC9BIDRcRX5eyt2INrIr9SrlW3YeYIvRu6ud5un2xYCS24nZbczA9YRhCoy08w== X-Received: by 2002:a17:90a:c08a:b0:1d9:88de:d192 with SMTP id o10-20020a17090ac08a00b001d988ded192mr5759913pjs.8.1651752045181; Thu, 05 May 2022 05:00:45 -0700 (PDT) Received: from localhost ([139.177.225.234]) by smtp.gmail.com with ESMTPSA id x4-20020a17090300c400b0015e8d4eb237sm1328141plc.129.2022.05.05.05.00.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 May 2022 05:00:44 -0700 (PDT) Date: Thu, 5 May 2022 20:00:41 +0800 From: Muchun Song To: Hyeonggon Yoo <42.hyeyoo@gmail.com> Cc: Alexander Potapenko , Marco Elver , Dmitry Vyukov , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v3] mm/kfence: reset PG_slab and memcg_data before freeing __kfence_pool Message-ID: References: <20220505073920.1880661-1-42.hyeyoo@gmail.com> <20220505101337.1997819-1-42.hyeyoo@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Stat-Signature: 478g9xrke4m9rzxk8id71so74tn9a1nx Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=47qcXKFK; spf=pass (imf18.hostedemail.com: domain of songmuchun@bytedance.com designates 209.85.214.171 as permitted sender) smtp.mailfrom=songmuchun@bytedance.com; dmarc=pass (policy=none) header.from=bytedance.com X-Rspam-User: X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 929831C008B X-HE-Tag: 1651752038-528673 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: On Thu, May 05, 2022 at 08:33:36PM +0900, Hyeonggon Yoo wrote: > On Thu, May 05, 2022 at 06:54:18PM +0800, Muchun Song wrote: > > On Thu, May 05, 2022 at 07:13:37PM +0900, Hyeonggon Yoo wrote: > > > When kfence fails to initialize kfence pool, it frees the pool. > > > But it does not reset PG_slab flag and memcg_data of struct page. > > > > > > Below is a BUG because of this. Let's fix it by resetting PG_slab > > > and memcg_data before free. > > > > > > [ 0.089149] BUG: Bad page state in process swapper/0 pfn:3d8e06 > > > [ 0.089149] page:ffffea46cf638180 refcount:0 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x3d8e06 > > > [ 0.089150] memcg:ffffffff94a475d1 > > > [ 0.089150] flags: 0x17ffffc0000200(slab|node=0|zone=2|lastcpupid=0x1fffff) > > > [ 0.089151] raw: 0017ffffc0000200 ffffea46cf638188 ffffea46cf638188 0000000000000000 > > > [ 0.089152] raw: 0000000000000000 0000000000000000 00000000ffffffff ffffffff94a475d1 > > > [ 0.089152] page dumped because: page still charged to cgroup > > > [ 0.089153] Modules linked in: > > > [ 0.089153] CPU: 0 PID: 0 Comm: swapper/0 Tainted: G B W 5.18.0-rc1+ #965 > > > [ 0.089154] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.14.0-2 04/01/2014 > > > [ 0.089154] Call Trace: > > > [ 0.089155] > > > [ 0.089155] dump_stack_lvl+0x49/0x5f > > > [ 0.089157] dump_stack+0x10/0x12 > > > [ 0.089158] bad_page.cold+0x63/0x94 > > > [ 0.089159] check_free_page_bad+0x66/0x70 > > > [ 0.089160] __free_pages_ok+0x423/0x530 > > > [ 0.089161] __free_pages_core+0x8e/0xa0 > > > [ 0.089162] memblock_free_pages+0x10/0x12 > > > [ 0.089164] memblock_free_late+0x8f/0xb9 > > > [ 0.089165] kfence_init+0x68/0x92 > > > [ 0.089166] start_kernel+0x789/0x992 > > > [ 0.089167] x86_64_start_reservations+0x24/0x26 > > > [ 0.089168] x86_64_start_kernel+0xa9/0xaf > > > [ 0.089170] secondary_startup_64_no_verify+0xd5/0xdb > > > [ 0.089171] > > > > > > Fixes: 0ce20dd84089 ("mm: add Kernel Electric-Fence infrastructure") > > > Fixes: 8f0b36497303 ("mm: kfence: fix objcgs vector allocation") > > > Signed-off-by: Hyeonggon Yoo <42.hyeyoo@gmail.com> > > > Reviewed-by: Marco Elver > > > Reviewed-by: Muchun Song > > > --- > > > > > > v2 -> v3: > > > - Add Reviewed-by: tags from Marco and Muchun. Thanks! > > > - Initialize folio where it is defined. > > > > > > mm/kfence/core.c | 8 ++++++++ > > > 1 file changed, 8 insertions(+) > > > > > > diff --git a/mm/kfence/core.c b/mm/kfence/core.c > > > index a203747ad2c0..b7d3a9667f00 100644 > > > --- a/mm/kfence/core.c > > > +++ b/mm/kfence/core.c > > > @@ -642,6 +642,14 @@ static bool __init kfence_init_pool_early(void) > > > * fails for the first page, and therefore expect addr==__kfence_pool in > > > * most failure cases. > > > */ > > > + for (char *p = (char *)addr; p < __kfence_pool + KFENCE_POOL_SIZE; p += PAGE_SIZE) { > > > + struct folio *folio = virt_to_folio(p); > > > + > > > > After more thinking, I think it is better to use 'struct slab *' > > to define a local variable since we already use this struct > > throughout slab core. What do you think? > > > > I think that may not be better. > > In the code we're freeing folios (so not going to reuse it again in slab/kfence). > And it may not be Slab depending on why kfence_init_pool() failed. > If it it not a Slab, then virt_to_slab() returns NULL in this case, it is unnecessary to clear PG_slab and reset its ->memcg_data. Right? Like the following changes: diff --git a/mm/kfence/core.c b/mm/kfence/core.c index 6e69986c3f0d..d90fe82dc752 100644 --- a/mm/kfence/core.c +++ b/mm/kfence/core.c @@ -627,6 +627,16 @@ static bool __init kfence_init_pool_early(void) * fails for the first page, and therefore expect addr==__kfence_pool in * most failure cases. */ + for (char *p = (char *)addr; p < __kfence_pool + KFENCE_POOL_SIZE; p += PAGE_SIZE) { + struct slab *slab = virt_to_slab(p); + + if (!slab) + continue; + __folio_clear_slab(slab_folio(slab)); +#ifdef CONFIG_MEMCG + slab->memcg_data = 0; +#endif + } memblock_free_late(__pa(addr), KFENCE_POOL_SIZE - (addr - (unsigned long)__kfence_pool)); __kfence_pool = NULL; return false;