From patchwork Mon Mar 20 03:00:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muchun Song X-Patchwork-Id: 13180694 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 98EC1C6FD1F for ; Mon, 20 Mar 2023 03:01:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EACA3900003; Sun, 19 Mar 2023 23:01:15 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E350C900002; Sun, 19 Mar 2023 23:01:15 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C8754900003; Sun, 19 Mar 2023 23:01:15 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id B6848900002 for ; Sun, 19 Mar 2023 23:01:15 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 8AB30C01A9 for ; Mon, 20 Mar 2023 03:01:15 +0000 (UTC) X-FDA: 80587775310.28.BA733E3 Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) by imf12.hostedemail.com (Postfix) with ESMTP id C6B324001A for ; Mon, 20 Mar 2023 03:01:12 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=kXzMdTPb; dmarc=pass (policy=none) header.from=bytedance.com; spf=pass (imf12.hostedemail.com: domain of songmuchun@bytedance.com designates 209.85.214.179 as permitted sender) smtp.mailfrom=songmuchun@bytedance.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1679281273; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=1F7l7Tv9LSQ3FV0WZzvP63T8ZRGBiz+hyWwDqQtTFrk=; b=J/wzUmz2d581C06tNj0eSOY+Jl/PQmz0LQOrD5rBT1uB8C+gCViflXnGQKXbH577LQpup8 tld1/tnqADzizFtJQAnlLKf6LcHr2MdiG/dmixfy5ALaWvYVa+iTzi5Ut1G1KACO6v9JZM kDYAjXM7RbzBBx4lH6cBvMkV2ZS7Amw= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=kXzMdTPb; dmarc=pass (policy=none) header.from=bytedance.com; spf=pass (imf12.hostedemail.com: domain of songmuchun@bytedance.com designates 209.85.214.179 as permitted sender) smtp.mailfrom=songmuchun@bytedance.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1679281273; a=rsa-sha256; cv=none; b=p+42sKvJrd3Xiq5BkJHQa6xJMSo0BbyzNpQ/m5lmVdwG99IK1rZZR2V1DvzV2i4geSmdgX 2IYYRet43coCxLKDzeT8rj4pRXcmBm9uqc7WlwL2lNkvNHYXvl6WHLIX3My+fA/eeOU9Bo XL8x+nK7l2tJ2eWCMAntMpGKG6XauZA= Received: by mail-pl1-f179.google.com with SMTP id z19so640365plo.2 for ; Sun, 19 Mar 2023 20:01:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1679281271; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=1F7l7Tv9LSQ3FV0WZzvP63T8ZRGBiz+hyWwDqQtTFrk=; b=kXzMdTPb5N8Ul0z7u6rxOEloBxiKexaFqUxNDrFd30XOo5ezApsGP1HtNT7RTMyW2d u5frjVpkc8cDUx76Kc8r8EpcinCtNqlafHj6/p9HKvk2t3DgV6TdfJr+VH8COGR5jfS5 1+l/SaT7tsU3hPnrjdAJYLtnnotXCs/CM+sdAsUFrUGltPDwWgcJhddB3iVNddxtgy0m XhIJY6eo263R8BypN1jp9hSISDpEiUE44siXWRE+mSDg5T+LU0zxkFicOQ9nkmJJIdCj J4tZx0uVCakPk3ryFGSR0Xe15X23pKRt3i8CJYiD0BuIUIOiT+H12/SDA6NKxBeORhlS l/Pw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679281271; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=1F7l7Tv9LSQ3FV0WZzvP63T8ZRGBiz+hyWwDqQtTFrk=; b=7Yif/lsXLfd89dezwwsMfCWKaeuNWEs8NkW0X/ExxY1ADPQysSLcE9YI+eylY25gVK Nq/0axW4yLUuTP6DKXdSEzbczdSRxTauKoCsw8B6D4KB3CwlIjwaDCrrH+gNJlqCGfi1 vnqe+FzrvqNfGnWjT+xJZvaBhd32qvRanATq0kSedpaXyAcNGKcF8NkSPsmEuO80rezH CV+XjP2GiJbUwAjc1WAvdqv700VASGW6z8yQiHl5Wo7Yn7gKUgEMWJYJQGTwMuxG+tlY ooij7tjLXALhNpMst3IYRvnKFeXrXtzNuZIIruLB3M00XW1f9xNMM9IJwCkuGcWMI+yJ dzHA== X-Gm-Message-State: AO0yUKVEO+OvxqRPvXEvhHemdlHAWoiuQGzR9C3nOaK5Ans+5mnJjsSy wNlGqXoTTevb+W1XyuGw4nNv0g== X-Google-Smtp-Source: AK7set+Uhy6In9jfIYEi7amqY8WsA4FKNiLfkNDTI7EQ+zRzGXAZfdS6XBLxKl8ikkYQ+MmFukE+Yg== X-Received: by 2002:a17:90a:56:b0:23d:4e9d:2eb0 with SMTP id 22-20020a17090a005600b0023d4e9d2eb0mr16866142pjb.36.1679281271272; Sun, 19 Mar 2023 20:01:11 -0700 (PDT) Received: from PXLDJ45XCM.bytedance.net ([61.213.176.5]) by smtp.gmail.com with ESMTPSA id y17-20020a170902d65100b001a1c69cc0besm1844972plh.200.2023.03.19.20.01.05 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sun, 19 Mar 2023 20:01:10 -0700 (PDT) From: Muchun Song To: glider@google.com, elver@google.com, dvyukov@google.com, akpm@linux-foundation.org, sjpark@amazon.de, jannh@google.com, muchun.song@linux.dev, roman.gushchin@linux.dev Cc: kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Muchun Song Subject: [PATCH] mm: kfence: fix PG_slab and memcg_data clearing Date: Mon, 20 Mar 2023 11:00:59 +0800 Message-Id: <20230320030059.20189-1-songmuchun@bytedance.com> X-Mailer: git-send-email 2.37.1 (Apple Git-137.1) MIME-Version: 1.0 X-Rspamd-Queue-Id: C6B324001A X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: j9w8mqft54gtby5pdusqghs6s5rcjy1t X-HE-Tag: 1679281272-635579 X-HE-Meta: U2FsdGVkX1+nhB+MKte68avF5CT/AaFMT/bmwTvHbPxfcgvpk5tYv6lxcKcdtwsPI4sNBPeW9DHEMChEta1/V5ASTUPTVFHQkDkoF5a/JMPLKob/FS2KMSgKDyyL1Yz8hsAt/ON9PJn12p++/tzxCMJzkUWAZkrp2JZ9EMSwxP/Kv205Hst2xXTX6uUk+w+GFPOicE2Mh6seG0F4qzv7w5j3lDf9X5pmBvyhBjVRL09aCLKXNyAmM3LpnzKB2Q1y9gcJT6HbQuidyfOXS/+iMpbsBptXG30YJUSVBqFdOcTo7xaRT6q5U4MeL0L+A83lmZhOOvmVADh3kwo1IhTVjwkP2RLuRRFRbPZX+Dklzprl5Cz7BANmtu0ePHNlarurOaMj2DIYad7QZqBNccVg2K7+HWriPBsXp+W+ohWbiFI0UuHBPn/VBt27uGFwOc/3z54o/NYhHa2Yt92k9ZSwhq6J5oc+hwq7IErgIre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op: owner-majordomo@kvack.org List-ID: It does not reset PG_slab and memcg_data when KFENCE fails to initialize kfence pool at runtime. It is reporting a "Bad page state" message when kfence pool is freed to buddy. The checking of whether it is a compound head page seems unnecessary sicne we already guarantee this when allocating kfence pool, removing the check to simplify the code. Fixes: 0ce20dd84089 ("mm: add Kernel Electric-Fence infrastructure") Fixes: 8f0b36497303 ("mm: kfence: fix objcgs vector allocation") Signed-off-by: Muchun Song --- mm/kfence/core.c | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/mm/kfence/core.c b/mm/kfence/core.c index 79c94ee55f97..d66092dd187c 100644 --- a/mm/kfence/core.c +++ b/mm/kfence/core.c @@ -561,10 +561,6 @@ static unsigned long kfence_init_pool(void) if (!i || (i % 2)) continue; - /* Verify we do not have a compound head page. */ - if (WARN_ON(compound_head(&pages[i]) != &pages[i])) - return addr; - __folio_set_slab(slab_folio(slab)); #ifdef CONFIG_MEMCG slab->memcg_data = (unsigned long)&kfence_metadata[i / 2 - 1].objcg | @@ -597,12 +593,26 @@ static unsigned long kfence_init_pool(void) /* Protect the right redzone. */ if (unlikely(!kfence_protect(addr + PAGE_SIZE))) - return addr; + goto reset_slab; addr += 2 * PAGE_SIZE; } return 0; + +reset_slab: + for (i = 0; i < KFENCE_POOL_SIZE / PAGE_SIZE; i++) { + struct slab *slab = page_slab(&pages[i]); + + if (!i || (i % 2)) + continue; +#ifdef CONFIG_MEMCG + slab->memcg_data = 0; +#endif + __folio_clear_slab(slab_folio(slab)); + } + + return addr; } static bool __init kfence_init_pool_early(void) @@ -632,16 +642,6 @@ 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; -#ifdef CONFIG_MEMCG - slab->memcg_data = 0; -#endif - __folio_clear_slab(slab_folio(slab)); - } memblock_free_late(__pa(addr), KFENCE_POOL_SIZE - (addr - (unsigned long)__kfence_pool)); __kfence_pool = NULL; return false;