mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Reinette Chatre <reinette.chatre@intel.com>
To: James Morse <james.morse@arm.com>, <x86@kernel.org>,
	<linux-kernel@vger.kernel.org>
Cc: Fenghua Yu <fenghua.yu@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>,
	<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>,
	<xingxin.hx@openanolis.org>, <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>
Subject: Re: [PATCH v6 24/24] x86/resctrl: Separate arch and fs resctrl locks
Date: Tue, 3 Oct 2023 14:28:42 -0700	[thread overview]
Message-ID: <9d4282af-7237-0c26-7c48-e2b9e2fb7795@intel.com> (raw)
In-Reply-To: <20230914172138.11977-25-james.morse@arm.com>

Hi James,

On 9/14/2023 10:21 AM, James Morse wrote:
> resctrl has one mutex that is taken by the architecture specific code,
> and the filesystem parts. The two interact via cpuhp, where the
> architecture code updates the domain list. Filesystem handlers that
> walk the domains list should not run concurrently with the cpuhp
> callback modifying the list.
> 
> Exposing a lock from the filesystem code means the interface is not
> cleanly defined, and creates the possibility of cross-architecture
> lock ordering headaches. The interaction only exists so that certain
> filesystem paths are serialised against CPU hotplug. The CPU hotplug
> code already has a mechanism to do this using cpus_read_lock().
> 
> MPAM's monitors have an overflow interrupt, so it needs to be possible
> to walk the domains list in irq context. RCU is ideal for this,
> but some paths need to be able to sleep to allocate memory.
> 
> Because resctrl_{on,off}line_cpu() take the rdtgroup_mutex as part
> of a cpuhp callback, cpus_read_lock() must always be taken first.
> rdtgroup_schemata_write() already does this.
> 
> Most of the filesystem code's domain list walkers are currently
> protected by the rdtgroup_mutex taken in rdtgroup_kn_lock_live().
> The exceptions are rdt_bit_usage_show() and the mon_config helpers
> which take the lock directly.
> 
> Make the domain list protected by RCU. An architecture-specific
> lock prevents concurrent writers. rdt_bit_usage_show() could
> walk the domain list using RCU, but to keep all the filesystem
> operations the same, this is changed to call cpus_read_lock().
> The mon_config helpers send multiple IPIs, take the cpus_read_lock()
> in these cases.
> 
> The other filesystem list walkers need to be able to sleep.
> Add cpus_read_lock() to rdtgroup_kn_lock_live() so that the
> cpuhp callbacks can't be invoked when file system operations are
> occurring.
> 
> Add lockdep_assert_cpus_held() in the cases where the
> rdtgroup_kn_lock_live() call isn't obvious.

One place that does not seem to have this annotation that
I think is needed is within get_domain_from_cpu(). Starting
with this series it is called from resctrl_offline_cpu()
called via CPU hotplug code. From now on extra care needs to be
taken when trying to call it from anywhere else.

> 
> Resctrl's domain online/offline calls now need to take the
> rdtgroup_mutex themselves.
> 
> Reviewed-by: Shaopeng Tan <tan.shaopeng@fujitsu.com>
> Tested-by: Shaopeng Tan <tan.shaopeng@fujitsu.com>
> Tested-By: Peter Newman <peternewman@google.com>
> Signed-off-by: James Morse <james.morse@arm.com>
> ---

...

> diff --git a/arch/x86/kernel/cpu/resctrl/core.c b/arch/x86/kernel/cpu/resctrl/core.c
> index 1a10f567bbe5..8fd0510d767b 100644
> --- a/arch/x86/kernel/cpu/resctrl/core.c
> +++ b/arch/x86/kernel/cpu/resctrl/core.c
> @@ -25,8 +25,15 @@
>  #include <asm/resctrl.h>
>  #include "internal.h"
>  
> -/* Mutex to protect rdtgroup access. */
> -DEFINE_MUTEX(rdtgroup_mutex);
> +/*
> + * rdt_domain structures are kfree()d when their last CPU goes offline,
> + * and allocated when the first CPU in a new domain comes online.
> + * The rdt_resource's domain list is updated when this happens. Readers of
> + * the domain list must either take cpus_read_lock(), or rely on an RCU
> + * read-side critical section, to avoid observing concurrent modification.
> + * All writers take this mutex:
> + */
> +static DEFINE_MUTEX(domain_list_lock);
>  

I assume that you have not followed the SNC work. Please note that in 
that work the domain list is split between a monitoring domain list and
control domain list. I expect this lock would cover both and both would
be rcu lists?


...

> diff --git a/arch/x86/kernel/cpu/resctrl/ctrlmondata.c b/arch/x86/kernel/cpu/resctrl/ctrlmondata.c
> index b4ed4e1b4938..0620dfc72036 100644
> --- a/arch/x86/kernel/cpu/resctrl/ctrlmondata.c
> +++ b/arch/x86/kernel/cpu/resctrl/ctrlmondata.c
> @@ -209,6 +209,9 @@ static int parse_line(char *line, struct resctrl_schema *s,
>  	struct rdt_domain *d;
>  	unsigned long dom_id;
>  
> +	/* Walking r->domains, ensure it can't race with cpuhp */
> +	lockdep_assert_cpus_held();
> +
>  	if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP &&
>  	    (r->rid == RDT_RESOURCE_MBA || r->rid == RDT_RESOURCE_SMBA)) {
>  		rdt_last_cmd_puts("Cannot pseudo-lock MBA resource\n");
> @@ -313,6 +316,9 @@ int resctrl_arch_update_domains(struct rdt_resource *r, u32 closid)
>  	struct rdt_domain *d;
>  	u32 idx;
>  
> +	/* Walking r->domains, ensure it can't race with cpuhp */
> +	lockdep_assert_cpus_held();
> +
>  	if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
>  		return -ENOMEM;
>  
> @@ -378,11 +384,9 @@ ssize_t rdtgroup_schemata_write(struct kernfs_open_file *of,
>  		return -EINVAL;
>  	buf[nbytes - 1] = '\0';
>  
> -	cpus_read_lock();
>  	rdtgrp = rdtgroup_kn_lock_live(of->kn);
>  	if (!rdtgrp) {
>  		rdtgroup_kn_unlock(of->kn);
> -		cpus_read_unlock();
>  		return -ENOENT;
>  	}
>  	rdt_last_cmd_clear();
> @@ -444,7 +448,6 @@ ssize_t rdtgroup_schemata_write(struct kernfs_open_file *of,
>  out:
>  	rdt_staged_configs_clear();
>  	rdtgroup_kn_unlock(of->kn);
> -	cpus_read_unlock();
>  	return ret ?: nbytes;
>  }
>  
> @@ -464,6 +467,9 @@ static void show_doms(struct seq_file *s, struct resctrl_schema *schema, int clo
>  	bool sep = false;
>  	u32 ctrl_val;
>  
> +	/* Walking r->domains, ensure it can't race with cpuhp */
> +	lockdep_assert_cpus_held();
> +
>  	seq_printf(s, "%*s:", max_name_width, schema->name);
>  	list_for_each_entry(dom, &r->domains, list) {
>  		if (sep)
> @@ -535,7 +541,7 @@ void mon_event_read(struct rmid_read *rr, struct rdt_resource *r,
>  	int cpu;
>  
>  	/* When picking a CPU from cpu_mask, ensure it can't race with cpuhp */
> -	lockdep_assert_held(&rdtgroup_mutex);
> +	lockdep_assert_cpus_held();
>  

Only now is that comment accurate. Could it be moved to this patch?

...

> @@ -2948,6 +2975,9 @@ static int mkdir_mondata_subdir_alldom(struct kernfs_node *parent_kn,
>  	struct rdt_domain *dom;
>  	int ret;
>  
> +	/* Walking r->domains, ensure it can't race with cpuhp */
> +	lockdep_assert_cpus_held();
> +
>  	list_for_each_entry(dom, &r->domains, list) {
>  		ret = mkdir_mondata_subdir(parent_kn, dom, r, prgrp);
>  		if (ret)
> @@ -3766,7 +3796,8 @@ static void domain_destroy_mon_state(struct rdt_domain *d)
>  	kfree(d->mbm_local);
>  }
>  
> -void resctrl_offline_domain(struct rdt_resource *r, struct rdt_domain *d)
> +static void _resctrl_offline_domain(struct rdt_resource *r,
> +				    struct rdt_domain *d)
>  {
>  	lockdep_assert_held(&rdtgroup_mutex);
>  
> @@ -3801,6 +3832,13 @@ void resctrl_offline_domain(struct rdt_resource *r, struct rdt_domain *d)
>  	domain_destroy_mon_state(d);
>  }
>  
> +void resctrl_offline_domain(struct rdt_resource *r, struct rdt_domain *d)
> +{
> +	mutex_lock(&rdtgroup_mutex);
> +	_resctrl_offline_domain(r, d);
> +	mutex_unlock(&rdtgroup_mutex);
> +}
> +

This seems unnecessary. Why not keep resctrl_offline_domain() as-is and just
take the lock within it?

>  static int domain_setup_mon_state(struct rdt_resource *r, struct rdt_domain *d)
>  {
>  	u32 idx_limit = resctrl_arch_system_num_rmid_idx();
> @@ -3832,7 +3870,7 @@ static int domain_setup_mon_state(struct rdt_resource *r, struct rdt_domain *d)
>  	return 0;
>  }
>  
> -int resctrl_online_domain(struct rdt_resource *r, struct rdt_domain *d)
> +static int _resctrl_online_domain(struct rdt_resource *r, struct rdt_domain *d)
>  {
>  	int err;
>  
> @@ -3870,12 +3908,23 @@ int resctrl_online_domain(struct rdt_resource *r, struct rdt_domain *d)
>  	return 0;
>  }
>  
> +int resctrl_online_domain(struct rdt_resource *r, struct rdt_domain *d)
> +{
> +	int err;
> +
> +	mutex_lock(&rdtgroup_mutex);
> +	err = _resctrl_online_domain(r, d);
> +	mutex_unlock(&rdtgroup_mutex);
> +
> +	return err;
> +}
> +

Same here.

>  void resctrl_online_cpu(unsigned int cpu)
>  {
> -	lockdep_assert_held(&rdtgroup_mutex);
> -
> +	mutex_lock(&rdtgroup_mutex);
>  	/* The CPU is set in default rdtgroup after online. */
>  	cpumask_set_cpu(cpu, &rdtgroup_default.cpu_mask);
> +	mutex_unlock(&rdtgroup_mutex);
>  }
>  
>  static void clear_childcpus(struct rdtgroup *r, unsigned int cpu)

...

Reinette

  reply	other threads:[~2023-10-03 21:29 UTC|newest]

Thread overview: 80+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-14 17:21 [PATCH v6 00/24] x86/resctrl: monitored closid+rmid together, separate arch/fs locking James Morse
2023-09-14 17:21 ` [PATCH v6 01/24] tick/nohz: Move tick_nohz_full_mask declaration outside the #ifdef James Morse
2023-09-26 14:31   ` Fenghua Yu
2023-10-03 21:05   ` Reinette Chatre
2023-09-14 17:21 ` [PATCH v6 02/24] x86/resctrl: kfree() rmid_ptrs from rdtgroup_exit() James Morse
2023-10-02 17:00   ` Reinette Chatre
2023-10-05 17:05     ` James Morse
2023-10-05 18:04       ` Reinette Chatre
2023-10-25 17:56         ` James Morse
2023-10-04 18:00   ` Moger, Babu
2023-10-05 17:06     ` James Morse
2023-09-14 17:21 ` [PATCH v6 03/24] x86/resctrl: Create helper for RMID allocation and mondata dir creation James Morse
2023-10-03 21:07   ` Reinette Chatre
2023-09-14 17:21 ` [PATCH v6 04/24] x86/resctrl: Move rmid allocation out of mkdir_rdt_prepare() James Morse
2023-10-03 21:07   ` Reinette Chatre
2023-10-04 18:01   ` Moger, Babu
2023-10-05 17:06     ` James Morse
2023-09-14 17:21 ` [PATCH v6 05/24] x86/resctrl: Track the closid with the rmid James Morse
2023-10-03 21:11   ` Reinette Chatre
2023-09-14 17:21 ` [PATCH v6 06/24] x86/resctrl: Access per-rmid structures by index James Morse
2023-10-03 21:12   ` Reinette Chatre
2023-10-24  9:28   ` Maciej Wieczór-Retman
2023-09-14 17:21 ` [PATCH v6 07/24] x86/resctrl: Allow RMID allocation to be scoped by CLOSID James Morse
2023-10-03 21:12   ` Reinette Chatre
2023-09-14 17:21 ` [PATCH v6 08/24] x86/resctrl: Track the number of dirty RMID a CLOSID has James Morse
2023-10-03 21:13   ` Reinette Chatre
2023-10-05 17:07     ` James Morse
2023-09-14 17:21 ` [PATCH v6 09/24] x86/resctrl: Use set_bit()/clear_bit() instead of open coding James Morse
2023-09-17 21:00   ` David Laight
2023-09-29 16:13     ` James Morse
2023-10-03 21:14   ` Reinette Chatre
2023-10-04 20:38   ` Moger, Babu
2023-10-05 17:07     ` James Morse
2023-09-14 17:21 ` [PATCH v6 10/24] x86/resctrl: Allocate the cleanest CLOSID by searching closid_num_dirty_rmid James Morse
2023-10-03 21:14   ` Reinette Chatre
2023-10-05 20:13   ` Moger, Babu
2023-10-25 17:56     ` James Morse
2023-10-05 20:26   ` Moger, Babu
2023-10-25 17:56     ` James Morse
2023-10-24 12:06   ` Maciej Wieczór-Retman
2023-09-14 17:21 ` [PATCH v6 11/24] x86/resctrl: Move CLOSID/RMID matching and setting to use helpers James Morse
2023-10-03 21:15   ` Reinette Chatre
2023-09-14 17:21 ` [PATCH v6 12/24] x86/resctrl: Add cpumask_any_housekeeping() for limbo/overflow James Morse
2023-10-03 21:15   ` Reinette Chatre
2023-10-05 17:07     ` James Morse
2023-09-14 17:21 ` [PATCH v6 13/24] x86/resctrl: Queue mon_event_read() instead of sending an IPI James Morse
2023-10-03 21:17   ` Reinette Chatre
2023-10-25 17:56     ` James Morse
2023-09-14 17:21 ` [PATCH v6 14/24] x86/resctrl: Allow resctrl_arch_rmid_read() to sleep James Morse
2023-10-03 21:18   ` Reinette Chatre
2023-10-25 17:57     ` James Morse
2023-10-05 21:33   ` Moger, Babu
2023-09-14 17:21 ` [PATCH v6 15/24] x86/resctrl: Allow arch to allocate memory needed in resctrl_arch_rmid_read() James Morse
2023-10-03 21:18   ` Reinette Chatre
2023-10-05 21:46   ` Moger, Babu
2023-10-25 17:58     ` James Morse
2023-09-14 17:21 ` [PATCH v6 16/24] x86/resctrl: Make resctrl_mounted checks explicit James Morse
2023-10-03 21:19   ` Reinette Chatre
2023-09-14 17:21 ` [PATCH v6 17/24] x86/resctrl: Move alloc/mon static keys into helpers James Morse
2023-10-03 21:19   ` Reinette Chatre
2023-09-14 17:21 ` [PATCH v6 18/24] x86/resctrl: Make rdt_enable_key the arch's decision to switch James Morse
2023-10-03 21:19   ` Reinette Chatre
2023-09-14 17:21 ` [PATCH v6 19/24] x86/resctrl: Add helpers for system wide mon/alloc capable James Morse
2023-10-03 21:19   ` Reinette Chatre
2023-09-14 17:21 ` [PATCH v6 20/24] x86/resctrl: Add CPU online callback for resctrl work James Morse
2023-10-03 21:20   ` Reinette Chatre
2023-09-14 17:21 ` [PATCH v6 21/24] x86/resctrl: Allow overflow/limbo handlers to be scheduled on any-but cpu James Morse
2023-10-03 21:22   ` Reinette Chatre
2023-10-25 17:57     ` James Morse
2023-10-27 21:20       ` Reinette Chatre
2023-09-14 17:21 ` [PATCH v6 22/24] x86/resctrl: Add cpu offline callback for resctrl work James Morse
2023-10-03 21:23   ` Reinette Chatre
2023-10-25 17:57     ` James Morse
2023-09-14 17:21 ` [PATCH v6 23/24] x86/resctrl: Move domain helper migration into resctrl_offline_cpu() James Morse
2023-10-03 21:23   ` Reinette Chatre
2023-09-14 17:21 ` [PATCH v6 24/24] x86/resctrl: Separate arch and fs resctrl locks James Morse
2023-10-03 21:28   ` Reinette Chatre [this message]
2023-10-25 17:55     ` James Morse
2023-09-27  7:38 ` [PATCH v6 00/24] x86/resctrl: monitored closid+rmid together, separate arch/fs locking Shaopeng Tan (Fujitsu)
2023-09-29 16:13   ` James Morse

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=9d4282af-7237-0c26-7c48-e2b9e2fb7795@intel.com \
    --to=reinette.chatre@intel.com \
    --cc=Babu.Moger@amd.com \
    --cc=amitsinght@marvell.com \
    --cc=baolin.wang@linux.alibaba.com \
    --cc=bobo.shaobowang@huawei.com \
    --cc=bp@alien8.de \
    --cc=carl@os.amperecomputing.com \
    --cc=dfustini@baylibre.com \
    --cc=fenghua.yu@intel.com \
    --cc=hpa@zytor.com \
    --cc=james.morse@arm.com \
    --cc=lcherian@marvell.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=peternewman@google.com \
    --cc=quic_jiles@quicinc.com \
    --cc=scott@os.amperecomputing.com \
    --cc=shameerali.kolothum.thodi@huawei.com \
    --cc=tan.shaopeng@fujitsu.com \
    --cc=tglx@linutronix.de \
    --cc=x86@kernel.org \
    --cc=xhao@linux.alibaba.com \
    --cc=xingxin.hx@openanolis.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®