Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp695587pxb; Tue, 5 Apr 2022 19:08:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJycsXzuid6d9F1H3Yl24rS/9M/IL7uhIhGKWjFrg3dsrKhMJUK30sLJCEkkQ4jqWeir9DUs X-Received: by 2002:aa7:c946:0:b0:416:226f:f8a6 with SMTP id h6-20020aa7c946000000b00416226ff8a6mr6447664edt.360.1649210896280; Tue, 05 Apr 2022 19:08:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649210896; cv=none; d=google.com; s=arc-20160816; b=XuZwD1RSLpZ36hA+Q22uISBMA2xyZpEa++H6JcjuAUz2tYy127VXIoxM6JQu1yMtyP 239KJUH2+i260jRLsYy2kPXZ9B5BOW1OjLN+w6PRBfUpdrVIlr0OktUHVacDqd7rxPwE WP0LSv4iGUA7HSJeCAzEDcRsTZJxhE0vAQfnyfblDJQLT6SZCoQ8+XNvzRyim1WV0Azd j1Zse/NqvWsTLWa8a+PoS+G/TcOjlLs5x6pYm0kn5gF+K8yKw5z+h7xJui4RVlwfJXiF gJIi3F9nHhlIeCYu1YEikrhAn99Mo8CPXZmjoIfak/wZYxIh4i+yYAUOiDb7lceZqSvn stRw== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=wWHjdTu/P2VKcxfUq7Lw0WP0+ZW6kxtHGPaLe4ypNWo=; b=XaEhbNdTex+P2+mOP31DG6H4pRFlJQt9LW2sgLh8NbusiIJBZVevXh4Wz/CZHt32Kd /W8zTVjJqD3QAS2rc4IQhkanItclasuEujvWDTZ0VFq4ffgq0+XpSj+fydUhRXhvn6vI s4EpUm8qzT+PRjHpCPdtVao+OcgPGPWVsecxFXpqpslZegCg4vwLXAEr6MjOc8OS+SRo pGMdwJIgAKhpmHyIuTQ1bwPQJMWqTbHPRgoHwV+EuFsBcpvaooT9fOhEQGO0nUJpbASt Xyj/BQuZg1De9AxqnA7SFkTOzwdb9vpcQj/ps/rEi1qW2RS0v1m19Nr2mijv7DxgsQUj 4TFw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=s85NPSio; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q2-20020a170906a08200b006df76385f3esi9851176ejy.990.2022.04.05.19.07.47; Tue, 05 Apr 2022 19:08:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=s85NPSio; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243582AbiDEJkB (ORCPT + 99 others); Tue, 5 Apr 2022 05:40:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45416 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239344AbiDEIT7 (ORCPT ); Tue, 5 Apr 2022 04:19:59 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 20337BB917; Tue, 5 Apr 2022 01:11:34 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id D43A6B81B92; Tue, 5 Apr 2022 08:11:32 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 445B6C385A1; Tue, 5 Apr 2022 08:11:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649146291; bh=EKYlKmPt0IA/3YLvn2Y3q/HEN685jP6kWQm/KNHNy1s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=s85NPSiogpwGsrktv9KUZl1xfWYW2keAea4fduMBbRgZfdS+PSnM0Na31boH2e1IF 49GROKc5SnT6dikZjdifzIYAleksX+lh5ytAXAUF+54dmSO4L9cGHLt6NGlhJ1Owhz /l5baNGhOqMY93rqw+WWzkDpOTXhAIgCWrcVxYJM= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Miaohe Lin , David Hildenbrand , Dan Williams , Alistair Popple , Andrew Morton , Linus Torvalds , Sasha Levin Subject: [PATCH 5.17 0710/1126] kernel/resource: fix kfree() of bootmem memory again Date: Tue, 5 Apr 2022 09:24:17 +0200 Message-Id: <20220405070428.440638540@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220405070407.513532867@linuxfoundation.org> References: <20220405070407.513532867@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham 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 From: Miaohe Lin [ Upstream commit 0cbcc92917c5de80f15c24d033566539ad696892 ] Since commit ebff7d8f270d ("mem hotunplug: fix kfree() of bootmem memory"), we could get a resource allocated during boot via alloc_resource(). And it's required to release the resource using free_resource(). Howerver, many people use kfree directly which will result in kernel BUG. In order to fix this without fixing every call site, just leak a couple of bytes in such corner case. Link: https://lkml.kernel.org/r/20220217083619.19305-1-linmiaohe@huawei.com Fixes: ebff7d8f270d ("mem hotunplug: fix kfree() of bootmem memory") Signed-off-by: Miaohe Lin Suggested-by: David Hildenbrand Cc: Dan Williams Cc: Alistair Popple Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin --- kernel/resource.c | 41 ++++++++--------------------------------- 1 file changed, 8 insertions(+), 33 deletions(-) diff --git a/kernel/resource.c b/kernel/resource.c index 9c08d6e9eef2..34eaee179689 100644 --- a/kernel/resource.c +++ b/kernel/resource.c @@ -56,14 +56,6 @@ struct resource_constraint { static DEFINE_RWLOCK(resource_lock); -/* - * For memory hotplug, there is no way to free resource entries allocated - * by boot mem after the system is up. So for reusing the resource entry - * we need to remember the resource. - */ -static struct resource *bootmem_resource_free; -static DEFINE_SPINLOCK(bootmem_resource_lock); - static struct resource *next_resource(struct resource *p) { if (p->child) @@ -160,36 +152,19 @@ __initcall(ioresources_init); static void free_resource(struct resource *res) { - if (!res) - return; - - if (!PageSlab(virt_to_head_page(res))) { - spin_lock(&bootmem_resource_lock); - res->sibling = bootmem_resource_free; - bootmem_resource_free = res; - spin_unlock(&bootmem_resource_lock); - } else { + /** + * If the resource was allocated using memblock early during boot + * we'll leak it here: we can only return full pages back to the + * buddy and trying to be smart and reusing them eventually in + * alloc_resource() overcomplicates resource handling. + */ + if (res && PageSlab(virt_to_head_page(res))) kfree(res); - } } static struct resource *alloc_resource(gfp_t flags) { - struct resource *res = NULL; - - spin_lock(&bootmem_resource_lock); - if (bootmem_resource_free) { - res = bootmem_resource_free; - bootmem_resource_free = res->sibling; - } - spin_unlock(&bootmem_resource_lock); - - if (res) - memset(res, 0, sizeof(struct resource)); - else - res = kzalloc(sizeof(struct resource), flags); - - return res; + return kzalloc(sizeof(struct resource), flags); } /* Return the conflict entry if you can't request it */ -- 2.34.1