Message ID | 20240213184438.16675-3-james.morse@arm.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel+bounces-64093-ouuuleilei=gmail.com@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp738311dyb; Tue, 13 Feb 2024 10:45:50 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWJPMq0ukvsd6ivKsPC6rQJwlxIprV7xu3hs4shbEBeIDtwh6d08YeO++tK9wxZ2j/cqNZHl9kW/nfbdwCHXjrWRhJA0A== X-Google-Smtp-Source: AGHT+IEUKHYtWFUsFglTljDuEyJ81k5biy2nD2X/7mjg0nCaJmE64Xn2dmIDDsvdfug+AMZpUOjF X-Received: by 2002:a05:6402:134f:b0:561:fd3f:d75a with SMTP id y15-20020a056402134f00b00561fd3fd75amr374807edw.28.1707849950388; Tue, 13 Feb 2024 10:45:50 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707849950; cv=pass; d=google.com; s=arc-20160816; b=FK3UzCkKpLU+s9n4lsRBf7YomgXW7KZoJV5t1Mu1+On+UEhyomh3MKrC+j5VF6gejb nBMirps4icTyG7hwGilFXPlrhKzRL5/1MyVrv7ZJz9cXNcUemYXRcHlTmH8z5I0TTATB oK0dTghwC+3jv1emSRYqQ15bZztofd7ufwgtDyMvn938WDlRCp37/u6h+fSbur6P9q8/ LcNw0W/GIAqtqq/yH334Pj6z06t9yJnLLQe6H/KtsW/Q6//BqXloA4MLXYT0fgdCAruN 5F/g/8yH7WEkOT+8gXHWftxmOyi60PgA3Qmyox8La/a1Ek/hyr7sD6eik5Pa+r/E7c47 Q7wQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from; bh=Ubd+KdrHeNuK4PbZyo8WR0IZ9Sea2C/GizjNaf/Dq1Y=; fh=NCE4Kspnr11lETLj+y+ZrRYg+Ch2b440ymcFSKRzp2s=; b=n7R34WQw5Fcatm/zDHzMDsQBWoQxP4XB/1b+Bngq6v0Li0i0oPRKbvr7/DGjGN2rqS KzXZf/w0I8rs3er0n02nQjZG32WkW+F32pn/SqSPQq+CioVGzSGEG3b3PIe4WdQtCkfF /aVCiH4AivDR+afgl0U5mFKhgBPgvaJTcsc1KSNJ6dmwNzbdBTGs4TOqbiwAB33TvWjy eA63zfvssI25wS4C6L2zNH1ITGx3zlxlnvqsmpbEWIQYP7oWSxxJ5PscesY11oeC+F7f P+6I229mNQu2fgUlLTSGf1RAb48rxDNm5x4aKAThFb68oKWm+BftZxdqGqr1GNsY1XlE nFiA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-64093-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-64093-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com X-Forwarded-Encrypted: i=2; AJvYcCVJll34QSB8kcrPHIZuaYjo8VHXWTFoXG1MF+Nc9uUS3E1EtmJ/6yDzF1ohfgKYht7Ugvr6zFaePp2Eo7on9yLOIaZIFg== Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id i1-20020a056402054100b0055ff1384e15si3988904edx.225.2024.02.13.10.45.50 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Feb 2024 10:45:50 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-64093-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-64093-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-64093-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 06FFA1F24D3B for <ouuuleilei@gmail.com>; Tue, 13 Feb 2024 18:45:50 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3B8366087C; Tue, 13 Feb 2024 18:45:12 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 095355812E for <linux-kernel@vger.kernel.org>; Tue, 13 Feb 2024 18:45:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707849910; cv=none; b=rs9R3C9D1RHwGouYsOY88kK9KtE1EvM2ug6CXE8MG/Aje2jXssesgNiwtBryyfT4FTAqh64/IyiDdUfcSQKPfaNrhPXwXQn95ukqhz5NXjdyAQODajg5Y3wMLXwct4Ce0boZ6TQeODL6olLI6pguqpYtZ7SWpkyqIrKvk/C8UUo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707849910; c=relaxed/simple; bh=cnu/fw90MUyfw/nm4T5u6mCM9ebJAuqXfvQQKR65YCQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=E/Jv+atH02zO82X6ucNsWuV+ERpv37M8Pb3CxqRPTPRyNBfIX5e0uiy+Uik1T4orz5sLossukihLe36rd3mTVId14k5ul+35M9qYCr4ZylrjNhD1vCa2n7FDknkZjN9rwmtTFCdywp0BFTG/uYRGstW2WY6TWd8kHnQmEvRBYcs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id B8358FEC; Tue, 13 Feb 2024 10:45:49 -0800 (PST) Received: from merodach.members.linode.com (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 532FC3F766; Tue, 13 Feb 2024 10:45:05 -0800 (PST) From: James Morse <james.morse@arm.com> To: x86@kernel.org, linux-kernel@vger.kernel.org Cc: Fenghua Yu <fenghua.yu@intel.com>, Reinette Chatre <reinette.chatre@intel.com>, Thomas Gleixner <tglx@linutronix.de>, Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>, H Peter Anvin <hpa@zytor.com>, Babu Moger <Babu.Moger@amd.com>, James Morse <james.morse@arm.com>, shameerali.kolothum.thodi@huawei.com, D Scott Phillips OS <scott@os.amperecomputing.com>, carl@os.amperecomputing.com, lcherian@marvell.com, bobo.shaobowang@huawei.com, tan.shaopeng@fujitsu.com, baolin.wang@linux.alibaba.com, Jamie Iles <quic_jiles@quicinc.com>, Xin Hao <xhao@linux.alibaba.com>, peternewman@google.com, dfustini@baylibre.com, amitsinght@marvell.com, David Hildenbrand <david@redhat.com>, Babu Moger <babu.moger@amd.com> Subject: [PATCH v9 02/24] x86/resctrl: kfree() rmid_ptrs from resctrl_exit() Date: Tue, 13 Feb 2024 18:44:16 +0000 Message-Id: <20240213184438.16675-3-james.morse@arm.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20240213184438.16675-1-james.morse@arm.com> References: <20240213184438.16675-1-james.morse@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: <linux-kernel.vger.kernel.org> List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org> List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790810469652452446 X-GMAIL-MSGID: 1790810469652452446 |
Series |
x86/resctrl: monitored closid+rmid together, separate arch/fs locking
|
|
Commit Message
James Morse
Feb. 13, 2024, 6:44 p.m. UTC
rmid_ptrs[] is allocated from dom_data_init() but never free()d. While the exit text ends up in the linker script's DISCARD section, the direction of travel is for resctrl to be/have loadable modules. Add resctrl_put_mon_l3_config() to cleanup any memory allocated by rdt_get_mon_l3_config(). There is no reason to backport this to a stable kernel. Signed-off-by: James Morse <james.morse@arm.com> Tested-by: Babu Moger <babu.moger@amd.com> Tested-by: Carl Worth <carl@os.amperecomputing.com> # arm64 Reviewed-by: Babu Moger <babu.moger@amd.com> --- Changes since v5: * This patch is new Changes since v6: * Removed struct rdt_resource argument, added __exit markers to match the only caller. * Adedd a whole stack of functions to maintain symmetry. Changes since v7: * Moved the eventual kfree() call to be after rdtgroup_exit() Changes since v8: * Removed an unused parameter from some unused code. --- arch/x86/kernel/cpu/resctrl/core.c | 6 ++++++ arch/x86/kernel/cpu/resctrl/internal.h | 1 + arch/x86/kernel/cpu/resctrl/monitor.c | 15 +++++++++++++++ 3 files changed, 22 insertions(+)
Comments
Hi James, On 2/13/2024 10:44 AM, James Morse wrote: > rmid_ptrs[] is allocated from dom_data_init() but never free()d. > > While the exit text ends up in the linker script's DISCARD section, > the direction of travel is for resctrl to be/have loadable modules. > > Add resctrl_put_mon_l3_config() to cleanup any memory allocated > by rdt_get_mon_l3_config(). > > There is no reason to backport this to a stable kernel. > > Signed-off-by: James Morse <james.morse@arm.com> > Tested-by: Babu Moger <babu.moger@amd.com> > Tested-by: Carl Worth <carl@os.amperecomputing.com> # arm64 > Reviewed-by: Babu Moger <babu.moger@amd.com> Thank you. Reviewed-by: Reinette Chatre <reinette.chatre@intel.com> Reinette
Hi Reinette, On 13/02/2024 23:14, Reinette Chatre wrote: > On 2/13/2024 10:44 AM, James Morse wrote: >> rmid_ptrs[] is allocated from dom_data_init() but never free()d. >> >> While the exit text ends up in the linker script's DISCARD section, >> the direction of travel is for resctrl to be/have loadable modules. >> >> Add resctrl_put_mon_l3_config() to cleanup any memory allocated >> by rdt_get_mon_l3_config(). >> >> There is no reason to backport this to a stable kernel. > Reviewed-by: Reinette Chatre <reinette.chatre@intel.com> Thanks! James
On 13.02.24 19:44, James Morse wrote: > rmid_ptrs[] is allocated from dom_data_init() but never free()d. > > While the exit text ends up in the linker script's DISCARD section, > the direction of travel is for resctrl to be/have loadable modules. > > Add resctrl_put_mon_l3_config() to cleanup any memory allocated > by rdt_get_mon_l3_config(). > > There is no reason to backport this to a stable kernel. > > Signed-off-by: James Morse <james.morse@arm.com> > Tested-by: Babu Moger <babu.moger@amd.com> > Tested-by: Carl Worth <carl@os.amperecomputing.com> # arm64 > Reviewed-by: Babu Moger <babu.moger@amd.com> > --- [...] > +static void __exit dom_data_exit(void) > +{ > + mutex_lock(&rdtgroup_mutex); > + > + kfree(rmid_ptrs); > + rmid_ptrs = NULL; > + > + mutex_unlock(&rdtgroup_mutex); Just curious: is grabbing that mutex really required? Against which race are we trying to protect ourselves? I suspect this mutex is not required here: if we could racing with someone else, likely freeing that memory would not be safe either. Apart from that LGTM.
Hi David, On 20/02/2024 15:27, David Hildenbrand wrote: > On 13.02.24 19:44, James Morse wrote: >> rmid_ptrs[] is allocated from dom_data_init() but never free()d. >> >> While the exit text ends up in the linker script's DISCARD section, >> the direction of travel is for resctrl to be/have loadable modules. >> >> Add resctrl_put_mon_l3_config() to cleanup any memory allocated >> by rdt_get_mon_l3_config(). >> >> There is no reason to backport this to a stable kernel. >> +static void __exit dom_data_exit(void) >> +{ >> + mutex_lock(&rdtgroup_mutex); >> + >> + kfree(rmid_ptrs); >> + rmid_ptrs = NULL; >> + >> + mutex_unlock(&rdtgroup_mutex); > > Just curious: is grabbing that mutex really required? > > Against which race are we trying to protect ourselves? Not a race, but its to protect against having to think about memory ordering! > I suspect this mutex is not required here: if we could racing with someone else, likely > freeing that memory would not be safe either. All the accesses to that variable take the mutex, its necessary to take the mutex to ensure the most recently stored values are seen. In this case the array values don't matter, but rmid_ptrs is written under the mutex too. There is almost certainly a control dependency that means the CPU calling dom_data_exit() will see the value of rmid_ptrs from dom_data_init() - but its much simpler to check that all accesses take the mutex. With MPAM this code can be invoked from an error IRQ signalled by the hardware, so it could happen anytime. > Apart from that LGTM. Thanks for taking a look! Thanks, James
On Tue, Feb 20 2024 at 15:46, James Morse wrote: > On 20/02/2024 15:27, David Hildenbrand wrote: >> On 13.02.24 19:44, James Morse wrote: >>> +static void __exit dom_data_exit(void) >>> +{ >>> + mutex_lock(&rdtgroup_mutex); >>> + >>> + kfree(rmid_ptrs); >>> + rmid_ptrs = NULL; >>> + >>> + mutex_unlock(&rdtgroup_mutex); >> >> Just curious: is grabbing that mutex really required? >> >> Against which race are we trying to protect ourselves? > > Not a race, but its to protect against having to think about memory ordering! > >> I suspect this mutex is not required here: if we could racing with someone else, likely >> freeing that memory would not be safe either. > > All the accesses to that variable take the mutex, its necessary to take the mutex to > ensure the most recently stored values are seen. In this case the array values don't > matter, but rmid_ptrs is written under the mutex too. > There is almost certainly a control dependency that means the CPU calling dom_data_exit() > will see the value of rmid_ptrs from dom_data_init() - but its much simpler to check that > all accesses take the mutex. > > With MPAM this code can be invoked from an error IRQ signalled by the hardware, so it > could happen anytime. Which does not work because you can't acquire a mutex from hard interrupt context. Thanks, tglx
On 20/02/2024 15:54, Thomas Gleixner wrote: > On Tue, Feb 20 2024 at 15:46, James Morse wrote: >> On 20/02/2024 15:27, David Hildenbrand wrote: >>> On 13.02.24 19:44, James Morse wrote: >>>> +static void __exit dom_data_exit(void) >>>> +{ >>>> + mutex_lock(&rdtgroup_mutex); >>>> + >>>> + kfree(rmid_ptrs); >>>> + rmid_ptrs = NULL; >>>> + >>>> + mutex_unlock(&rdtgroup_mutex); >>> >>> Just curious: is grabbing that mutex really required? >>> >>> Against which race are we trying to protect ourselves? >> >> Not a race, but its to protect against having to think about memory ordering! >> >>> I suspect this mutex is not required here: if we could racing with someone else, likely >>> freeing that memory would not be safe either. >> >> All the accesses to that variable take the mutex, its necessary to take the mutex to >> ensure the most recently stored values are seen. In this case the array values don't >> matter, but rmid_ptrs is written under the mutex too. >> There is almost certainly a control dependency that means the CPU calling dom_data_exit() >> will see the value of rmid_ptrs from dom_data_init() - but its much simpler to check that >> all accesses take the mutex. >> >> With MPAM this code can be invoked from an error IRQ signalled by the hardware, so it >> could happen anytime. > > Which does not work because you can't acquire a mutex from hard > interrupt context. Indeed - which is why that happens via schedule_work() [0] My point was that its non-obvious where/when this will happen, so taking the lock and forgetting about it is the simplest thing to do. Thanks, James [0] https://git.kernel.org/pub/scm/linux/kernel/git/morse/linux.git/tree/drivers/platform/mpam/mpam_devices.c?h=mpam/snapshot/v6.7-rc2&id=7da1c7f9d9ef723f829bf44ed96e1fc4a46ef29f#n1299
On Tue, Feb 20 2024 at 16:01, James Morse wrote: > On 20/02/2024 15:54, Thomas Gleixner wrote: >>> With MPAM this code can be invoked from an error IRQ signalled by the hardware, so it >>> could happen anytime. >> >> Which does not work because you can't acquire a mutex from hard >> interrupt context. > > Indeed - which is why that happens via schedule_work() [0] > > My point was that its non-obvious where/when this will happen, so taking the lock and > forgetting about it is the simplest thing to do. Makes sense. Thanks, tglx
diff --git a/arch/x86/kernel/cpu/resctrl/core.c b/arch/x86/kernel/cpu/resctrl/core.c index aa9810a64258..9641c42d0f85 100644 --- a/arch/x86/kernel/cpu/resctrl/core.c +++ b/arch/x86/kernel/cpu/resctrl/core.c @@ -990,8 +990,14 @@ late_initcall(resctrl_late_init); static void __exit resctrl_exit(void) { + struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_L3].r_resctrl; + cpuhp_remove_state(rdt_online); + rdtgroup_exit(); + + if (r->mon_capable) + rdt_put_mon_l3_config(); } __exitcall(resctrl_exit); diff --git a/arch/x86/kernel/cpu/resctrl/internal.h b/arch/x86/kernel/cpu/resctrl/internal.h index 52e7e7deee10..61c763604fc9 100644 --- a/arch/x86/kernel/cpu/resctrl/internal.h +++ b/arch/x86/kernel/cpu/resctrl/internal.h @@ -544,6 +544,7 @@ void closid_free(int closid); int alloc_rmid(void); void free_rmid(u32 rmid); int rdt_get_mon_l3_config(struct rdt_resource *r); +void __exit rdt_put_mon_l3_config(void); bool __init rdt_cpu_has(int flag); void mon_event_count(void *info); int rdtgroup_mondata_show(struct seq_file *m, void *arg); diff --git a/arch/x86/kernel/cpu/resctrl/monitor.c b/arch/x86/kernel/cpu/resctrl/monitor.c index 3a6c069614eb..3a73db0579d8 100644 --- a/arch/x86/kernel/cpu/resctrl/monitor.c +++ b/arch/x86/kernel/cpu/resctrl/monitor.c @@ -719,6 +719,16 @@ static int dom_data_init(struct rdt_resource *r) return 0; } +static void __exit dom_data_exit(void) +{ + mutex_lock(&rdtgroup_mutex); + + kfree(rmid_ptrs); + rmid_ptrs = NULL; + + mutex_unlock(&rdtgroup_mutex); +} + static struct mon_evt llc_occupancy_event = { .name = "llc_occupancy", .evtid = QOS_L3_OCCUP_EVENT_ID, @@ -814,6 +824,11 @@ int __init rdt_get_mon_l3_config(struct rdt_resource *r) return 0; } +void __exit rdt_put_mon_l3_config(void) +{ + dom_data_exit(); +} + void __init intel_rdt_mbm_apply_quirk(void) { int cf_index;