Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp4804770iob; Mon, 9 May 2022 01:57:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyjdpYfiWDGpMgzXVUHZJH91cVPFiItI3xaPlOrwymXfbV+0NLgBcvByfKfXTGM6PCrlpHd X-Received: by 2002:a63:505a:0:b0:3c6:d5d1:eb2 with SMTP id q26-20020a63505a000000b003c6d5d10eb2mr1383949pgl.122.1652086631021; Mon, 09 May 2022 01:57:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652086631; cv=none; d=google.com; s=arc-20160816; b=ZAMVJnmhH+/Z3D1Da3+YvhKv9w4byO7+JH1uzZvYrNNzZuilX60TXHq6boZZNOh+g8 ND5OmmToQ7N10qD7TW465x1TV89ZKkKuQjojQrqRvI/4ALXqK0z8WMoYbc4TPzWR/t8G LTCO+X1m0dP76b8jyeuH7GEHj7e+WvSQSf8VwPAb7Y5NDiLO6w37W44dTW/fueqTSYsb Yj/JLCjNZVeb+hxnEGos4jDEYDseJbj98i8okvuONwKceqO7k+M2ldW4t8JnLE19uQSB 7+o+9MpeCGGtNqQ6k/odWX2XqkyZ4e03sHJB26M29KgpdxncuhTMNoh0UqS1POr0p+iK fHgw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=GOAcDGlSWe2Jb33NzDcQaYtU804KKEt7OooYW1HA4U4=; b=CofE+E89oD1pQE6JbX5bBBnFCM7rhXodVvMDpUt3+2d67Y2He4IwjTyrk1MS6uZa7Z uTUcI6/XG6uA9/nAGiQeq+xl+ov4auN+gJ0V7rLO+5X4dcUkPQwil2yxvxWVwPpGyy0d rr6Z7SgoiNTDbFK2B/twBDPuT93Cppq0ydhOVV9WPjfvbf/OOhV0z3VRSFrp4IAeETVe cTz3HnESCy3IWLHWkD5weTq5RIfX/6g4F6voKVCbFVMSjJ/BIL5W5Cmdp6WfvoJlNp9H ZZ4UfBvKhqHSgsnpLZv0m47S4rKn2JB47UEcKcVRktQpBaE7iqr9UgevtkSSNFZOk5ft Sj8w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=Fgh9cX+m; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id l185-20020a6391c2000000b003c16c84a15csi13155245pge.188.2022.05.09.01.57.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 May 2022 01:57:11 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=Fgh9cX+m; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id A03CF15D314; Mon, 9 May 2022 01:42:37 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1355337AbiEEKT4 (ORCPT + 99 others); Thu, 5 May 2022 06:19:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57426 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1355315AbiEEKTj (ORCPT ); Thu, 5 May 2022 06:19:39 -0400 Received: from mail-pf1-x429.google.com (mail-pf1-x429.google.com [IPv6:2607:f8b0:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E419351E68 for ; Thu, 5 May 2022 03:16:00 -0700 (PDT) Received: by mail-pf1-x429.google.com with SMTP id g8so3288123pfh.5 for ; Thu, 05 May 2022 03:16:00 -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=GOAcDGlSWe2Jb33NzDcQaYtU804KKEt7OooYW1HA4U4=; b=Fgh9cX+m5xcyqwOOpw5Jxn2Co881HgTaFW8hqLvN6T9iFRp6wJxkdwOH9WMnlguJ2B STHoH/FFDYncrqxYbclQyprPLjc4cEQOxZC/21bpPmLblsfF4kHHuIHYd1Y9jDeEnmt2 r+Ygl25+//nu/1sfxpYdaCRv8oqzDVlqqyfdGxynS5IhwTbpbGsKiP6h7D38TakshvkX J8xjRUmfLNowcTQsdX2YJt2leWTJHgOitQuFqz765cEPcMZmXFN2Y1D79t4ykHQCc+/I Fg/e3eDPuu6H18cDUoOKpp4UZpOLEhnEpSJPlEu7BsbdHe0sJ/1MwSv1Ggz10GWgn7Lm etzw== 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=GOAcDGlSWe2Jb33NzDcQaYtU804KKEt7OooYW1HA4U4=; b=7kQ2MXs8Mb+FVgTxJtTcigQTeL1uKmlqtL9KImQPCOQKNqoO0iljDEW+wajPMEVlLn S8CWIwDY3QmHTH1+zx8VKd9X2IOpOIOyjmvP1bDWh+iPrh/gH5UmNmpMdWLFTCAM8iEl 6Oq4A1fXLSTsdQvtqu3Fqo607B9G6xLsG/+IKXR7RqY/y3aUD/h78getW+euw/kNgfZg 2MSmbn8hE15a80KNyILfbjcb9NloecsIdZ8G1ev1ktVknjkrJ26tkfbDxYOJaGDpTsKf Qt+fLUCkHxGiff9evZe+tl/kP2yXy5z2OT6s/1ayS3vYpQR8qF0G6hVawlc/gCtmG9wz 8OoQ== X-Gm-Message-State: AOAM5305yBiKHS8jurht9SZZw/BmN87yC3VPxnqnilNQdXGdmi7iELYM yeUVa+1W1vos3lFtggsssLc= X-Received: by 2002:a63:682:0:b0:3c1:5bb1:85ee with SMTP id 124-20020a630682000000b003c15bb185eemr21776271pgg.146.1651745760387; Thu, 05 May 2022 03:16:00 -0700 (PDT) Received: from hyeyoo.. ([114.29.24.243]) by smtp.gmail.com with ESMTPSA id c4-20020aa78804000000b0050dc76281a7sm970648pfo.129.2022.05.05.03.15.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 May 2022 03:15:59 -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, Muchun Song , Hyeonggon Yoo <42.hyeyoo@gmail.com> Subject: [PATCH v3] mm/kfence: reset PG_slab and memcg_data before freeing __kfence_pool Date: Thu, 5 May 2022 19:13:37 +0900 Message-Id: <20220505101337.1997819-1-42.hyeyoo@gmail.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220505073920.1880661-1-42.hyeyoo@gmail.com> References: <20220505073920.1880661-1-42.hyeyoo@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,HK_RANDOM_FROM,MAILING_LIST_MULTI, RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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); + + __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; -- 2.32.0