Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp963764pxb; Wed, 6 Apr 2022 05:28:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyjAorQ3oOi350pR4AjFiEM91lpVSnYrBHG+WiRi2UIgZ0nPC48uZa5NCgjkcHfXQhebbrn X-Received: by 2002:a17:903:110c:b0:153:1293:5624 with SMTP id n12-20020a170903110c00b0015312935624mr8321477plh.149.1649248080823; Wed, 06 Apr 2022 05:28:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649248080; cv=none; d=google.com; s=arc-20160816; b=pJw4ez6IrTodABeysVV4+1FBvthN+BJrvMWRaXBs9NTXKwz0hrUR5sD8nd17/m/jVF 9+M9hhPO3tXL2x8YE26DMcvz5dHIq5+EhWi6lDukNoSplH/tdvtjr8ybRDFbOAKIwuHg vpBjT+M0RiVUXTczEy4Sun2s9tognE6+fios6qcZ/LCMr5nxvTrbHFLoHRgP5W3JuzpO XWZg+SbEbkrLNJDR+E2BATjZ2MGkDdbjNsY5O7nUAAkerZQTRIN8/vBXKLMUITfpsBko fGI9aPbg21Q8gp9N67ywbIpdQ0R5MxnmSXkgEF9OefBQKbQHbvLTC8ZVtljjiNLW7AeC ikoQ== 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=YBkkGGw4iKwa+BGdj1Z78jnw6CkEMzKFJIdv0e7xEiE=; b=gN+hGiUJYtqCgNbxs4tqtLNkxVdoAuj7cRBjxXDaE66Dl6Agm3GS5lUOukuFjeqisY aSQVhdXp5Q3tNy+heND6fXv2jGJTiVpJIWGwqTsuBUVyDR/xIcsNJu1xMK6csqGlGlvG /M6AiGbzkcTq7qnWEW5XLcWsdGDfUJNsEhEn0Tx6swT4/+hWGg5vHVYc4V2+VS+MbnUi 1gk2rPIvuBBf7Iq24x9kKENDF5ulMSZ5GUPfxZ/1k8536frPaEaexytUraErlGWF7iDj 1bYcFnYLjxS1AnZ7JssnFQBUfCc+20DAY63khEtsuvUjTadmqJv9vrVY48ldq8kqk75z aeeA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=OxTzfoCr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id w3-20020a634743000000b003816043eff7si15899223pgk.492.2022.04.06.05.28.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Apr 2022 05:28:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=OxTzfoCr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 3C5426F235A; Wed, 6 Apr 2022 04:38:35 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243105AbiDFAmT (ORCPT + 99 others); Tue, 5 Apr 2022 20:42:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49232 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349413AbiDEJtt (ORCPT ); Tue, 5 Apr 2022 05:49:49 -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 1371519C32; Tue, 5 Apr 2022 02:45:05 -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 AB41BB817D3; Tue, 5 Apr 2022 09:45:04 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1B72CC385A2; Tue, 5 Apr 2022 09:45:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649151903; bh=ZoRe7guTkoNEAclCUVLRBKzRkuDaJ6fCr6jIj2JxNFc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OxTzfoCrNixuaC22DsmsARby8fVQAAmpfdpu0ofVjMyMkWp0mUsk/u1SvF0TcgUS8 YeVD89issOGv/qTlbp9/LsUIik1TWEUGZn8htsS6TbFAXE/QPYZEn+k0K2uF7OeRQl 7hfcJKL57paIZCZDpZUZ7Nd8EfQmhaS12UFhspTU= 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.15 576/913] kernel/resource: fix kfree() of bootmem memory again Date: Tue, 5 Apr 2022 09:27:18 +0200 Message-Id: <20220405070357.112421057@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220405070339.801210740@linuxfoundation.org> References: <20220405070339.801210740@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=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, 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 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 ca9f5198a01f..20e10e48f052 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) @@ -148,36 +140,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