Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp2163366iof; Tue, 7 Jun 2022 21:52:03 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwJwoNrm9bFK4OiHB00IMDgR9pqLjRU1pZFMLWubdTsUXSaNn05nMGOGyvzpnRq5dj7XoU9 X-Received: by 2002:a05:6a00:1305:b0:512:ebab:3b20 with SMTP id j5-20020a056a00130500b00512ebab3b20mr32592287pfu.82.1654663923487; Tue, 07 Jun 2022 21:52:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654663923; cv=none; d=google.com; s=arc-20160816; b=k4FkLfjdmPewJ5Pblvk9IyKzooipWo7r0wjBMR1SMgvZtLNUUnAOiBz7xmzgK0SpQY 2mQevpH2h3xmFek4r933AagCfTp5b/DS0x5V452DoTF49wMEA5NKJ+szi1MU6+SoDW3q ap3DkD3NEyx798MfXGeIGWXyNvsmueD6gYDZ/UptjECIyNs+PgONVapHeq+8WeCeYjZh 75itRqhftQtTObyz4wEtVRCYCrVwUh0lAj74epqzsgNDW78S7DEqSgdt/mSenc8NtcGl lZspHX0D/IwOAQPQJTCD07krmoF3MsAtUoY6iNvcjYvDj97y7IBjtNc5+MYl+r9ZcjuZ 8yew== 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=CGkiRRNfpuZhtiuSvbQs2fvsf4SSHCsvYPHQAnKbKzE=; b=kHrJMIMbgq2kYh/1K70I63+Xt8cUQr+GZtFnr3+/7eQ0DWCZi9t8Zup7hyHFIsZ15U 7IAGGNxXD2wKztdjijYSMqVWzJWzQlA1DBQMEkGWM7ebNu/zadPwGf1Gqk5j8ZqRgs37 mEYN8cjEu/sl8t3lIEPG4CUpbu57C4xC8AceYNniLExQa3Bb1yX6j2Ci4Oe0Ws0AJpw5 wSmsRVSGucpVTQN8mdyk9jJ84SsTdout7kXb3jJsvQu/a27+ri0IGTURZO32bVqMi1Uh pxfhcbhWXK81uVjAv1jYp+Pyw+soErUoOjp+LhCCPRUXOEP1EtobK24bXJhY7x3VloHP GjRg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=LhLQ1mma; 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 ix15-20020a170902f80f00b001638d6105cesi24704878plb.495.2022.06.07.21.52.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jun 2022 21:52:03 -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=LhLQ1mma; 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 B45A73151FE; Tue, 7 Jun 2022 21:21:45 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345604AbiFGRTA (ORCPT + 99 others); Tue, 7 Jun 2022 13:19:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37838 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345597AbiFGRSo (ORCPT ); Tue, 7 Jun 2022 13:18:44 -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 319601053C9; Tue, 7 Jun 2022 10:18:40 -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 D92C1B822AF; Tue, 7 Jun 2022 17:18:38 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 44630C34115; Tue, 7 Jun 2022 17:18:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1654622317; bh=KZaq+5wpG3X41Uo87q90vm1FixZ/eyT9ZhY/vkIndL0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LhLQ1mma/DiACBb6QZvmjlLlFdcBrLIeLdPUpozVRMM8/8lqgMPv1JKXmg+U3FDaC RQ4EGsWXkkJKpTjwsXlVEBY84PnrOf/K9lCwKGKWZ2WX16RtoiWeEmNU2ymnh/BkR4 ZYxaFaHKyUAzvREH03IVBhI0/Bb+NGSxolQiAAiM= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Alviro Iskandar Setiawan , Yazen Ghannam , Ammar Faizi , Borislav Petkov Subject: [PATCH 5.10 017/452] x86/MCE/AMD: Fix memory leak when threshold_create_bank() fails Date: Tue, 7 Jun 2022 18:57:54 +0200 Message-Id: <20220607164909.055097234@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220607164908.521895282@linuxfoundation.org> References: <20220607164908.521895282@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=-3.1 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=unavailable 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: Ammar Faizi commit e5f28623ceb103e13fc3d7bd45edf9818b227fd0 upstream. In mce_threshold_create_device(), if threshold_create_bank() fails, the previously allocated threshold banks array @bp will be leaked because the call to mce_threshold_remove_device() will not free it. This happens because mce_threshold_remove_device() fetches the pointer through the threshold_banks per-CPU variable but bp is written there only after the bank creation is successful, and not before, when threshold_create_bank() fails. Add a helper which unwinds all the bank creation work previously done and pass into it the previously allocated threshold banks array for freeing. [ bp: Massage. ] Fixes: 6458de97fc15 ("x86/mce/amd: Straighten CPU hotplug path") Co-developed-by: Alviro Iskandar Setiawan Signed-off-by: Alviro Iskandar Setiawan Co-developed-by: Yazen Ghannam Signed-off-by: Yazen Ghannam Signed-off-by: Ammar Faizi Signed-off-by: Borislav Petkov Cc: Link: https://lore.kernel.org/r/20220329104705.65256-3-ammarfaizi2@gnuweeb.org Signed-off-by: Greg Kroah-Hartman --- arch/x86/kernel/cpu/mce/amd.c | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) --- a/arch/x86/kernel/cpu/mce/amd.c +++ b/arch/x86/kernel/cpu/mce/amd.c @@ -1457,10 +1457,23 @@ out_free: kfree(bank); } +static void __threshold_remove_device(struct threshold_bank **bp) +{ + unsigned int bank, numbanks = this_cpu_read(mce_num_banks); + + for (bank = 0; bank < numbanks; bank++) { + if (!bp[bank]) + continue; + + threshold_remove_bank(bp[bank]); + bp[bank] = NULL; + } + kfree(bp); +} + int mce_threshold_remove_device(unsigned int cpu) { struct threshold_bank **bp = this_cpu_read(threshold_banks); - unsigned int bank, numbanks = this_cpu_read(mce_num_banks); if (!bp) return 0; @@ -1471,13 +1484,7 @@ int mce_threshold_remove_device(unsigned */ this_cpu_write(threshold_banks, NULL); - for (bank = 0; bank < numbanks; bank++) { - if (bp[bank]) { - threshold_remove_bank(bp[bank]); - bp[bank] = NULL; - } - } - kfree(bp); + __threshold_remove_device(bp); return 0; } @@ -1514,15 +1521,14 @@ int mce_threshold_create_device(unsigned if (!(this_cpu_read(bank_map) & (1 << bank))) continue; err = threshold_create_bank(bp, cpu, bank); - if (err) - goto out_err; + if (err) { + __threshold_remove_device(bp); + return err; + } } this_cpu_write(threshold_banks, bp); if (thresholding_irq_en) mce_threshold_vector = amd_threshold_interrupt; return 0; -out_err: - mce_threshold_remove_device(cpu); - return err; }