From patchwork Thu May 5 07:39:20 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hyeonggon Yoo <42.hyeyoo@gmail.com> X-Patchwork-Id: 12839139 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 CC6D2C433FE for ; Thu, 5 May 2022 07:40:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 65EFA6B0071; Thu, 5 May 2022 03:40:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 60E306B0073; Thu, 5 May 2022 03:40:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4FDB76B0074; Thu, 5 May 2022 03:40:40 -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 41B446B0071 for ; Thu, 5 May 2022 03:40:40 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay11.hostedemail.com (Postfix) with ESMTP id 132CA80DE0 for ; Thu, 5 May 2022 07:40:40 +0000 (UTC) X-FDA: 79430892240.02.F59EA61 Received: from mail-pf1-f170.google.com (mail-pf1-f170.google.com [209.85.210.170]) by imf06.hostedemail.com (Postfix) with ESMTP id D0862180094 for ; Thu, 5 May 2022 07:40:37 +0000 (UTC) Received: by mail-pf1-f170.google.com with SMTP id j6so2999740pfe.13 for ; Thu, 05 May 2022 00:40:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=g5+cU6rju5q4PixcXKa5q0u5g3HxteJZ5Y7imLW6zec=; b=aC1SKi4r1y8gQCHUieTZ02NfjxR/NT7PnVvgPER49hsL0t10jaJr0/EOvqWfXWWZbi hui3ac+un3NS7SeDvijuxGNLKCFTPZ0nIGSJrX8WOBSC8RKLxx10nTmR9wmBMwkk0Ahi pDRstdJIC4m7UfS73Z3CESXtKP116DxrNOTFNDtnRkZZFDe4HQ0KlWyN2ahpRwj75ZMp 0jjvUstov+9usyDNkTkyoFTMlL4VUEM4V5LMW5VkaaYQIQxeDOD60kh00nGp2sxg1MfB pADNqPC4D2W/tHZfunItlxaXoJKfBQ0PFVRHV7WR01soQBeOpeQG4nQXd6srhyskD7Kw 1trA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=g5+cU6rju5q4PixcXKa5q0u5g3HxteJZ5Y7imLW6zec=; b=ueULJeGrXcj8Y0eUHSpQV1QSnEJlNks60v3So+L3juls5JvxTvDwskl9CXXzMhzkJc VdjneQfX97PihX2Ix7bQKHu0iJCRC52axJJnUWTUVL6OMJcFweNVf3F7PXUGQpxC+QLY OLqGlfjJSaEuM2rpe1aOcj82VwMyeMxb7dPJd0mRrCZIvSra/elidvQjq1Okhlv/ShLM /1AU5+YV1/0cIVvxox0Emi5yciCu9fbVpEJG5UZO5Sx2O6+qIsg/+xIzNa3AHe8UbUoG QfI/TzUNF1o3q2AeQ08Rh1UJ3y5iaRUUkM5/Q17UkbOngIKxKq/Y/WoYVDozNQJgL/Dq ybnQ== X-Gm-Message-State: AOAM530zTb6KE+O+Ire1hu3FlisyYQKzO+XcRta7t3eU11JdjwNBuQS6 mCNGCkQ4etyGzwCa1DQmRt8= X-Google-Smtp-Source: ABdhPJw20k22tZv728rddyr0QfZY9ymgsPLiRKtRYR7lT+jEnb5pM2AP2kMYj3hTQiSrMXTAZIqQlQ== X-Received: by 2002:a63:1d26:0:b0:3c1:eb3f:9daf with SMTP id d38-20020a631d26000000b003c1eb3f9dafmr18535970pgd.284.1651736438698; Thu, 05 May 2022 00:40:38 -0700 (PDT) Received: from hyeyoo.. ([114.29.24.243]) by smtp.gmail.com with ESMTPSA id t68-20020a625f47000000b0050dc76281dcsm655084pfb.182.2022.05.05.00.40.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 May 2022 00:40:38 -0700 (PDT) From: Hyeonggon Yoo <42.hyeyoo@gmail.com> To: Alexander Potapenko , Marco Elver , Dmitry Vyukov , Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Hyeonggon Yoo <42.hyeyoo@gmail.com> Subject: [PATCH v2] mm/kfence: reset PG_slab and memcg_data before freeing __kfence_pool Date: Thu, 5 May 2022 16:39:20 +0900 Message-Id: <20220505073920.1880661-1-42.hyeyoo@gmail.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220505070105.1835745-1-42.hyeyoo@gmail.com> References: <20220505070105.1835745-1-42.hyeyoo@gmail.com> MIME-Version: 1.0 Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=aC1SKi4r; spf=pass (imf06.hostedemail.com: domain of 42.hyeyoo@gmail.com designates 209.85.210.170 as permitted sender) smtp.mailfrom=42.hyeyoo@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-Rspam-User: X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: D0862180094 X-Stat-Signature: a7qu34tbejwfkg6jny3udbrkqeubykdp X-HE-Tag: 1651736437-671892 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 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 --- v1 -> v2: - Use folio instead of page - Add Fixes: tags - Wrap #ifdef ~ #endif around folio->memcg_data = 0; mm/kfence/core.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/mm/kfence/core.c b/mm/kfence/core.c index a203747ad2c0..bb1c6c489d0a 100644 --- a/mm/kfence/core.c +++ b/mm/kfence/core.c @@ -642,6 +642,15 @@ 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; + + folio = virt_to_folio(p); + __folio_clear_slab(folio); +#ifdef CONFIG_MEMCG + folio->memcg_data = 0; +#endif + } memblock_free_late(__pa(addr), KFENCE_POOL_SIZE - (addr - (unsigned long)__kfence_pool)); __kfence_pool = NULL; return false;