Skip to contentSkip to conversation

sched/psi: Allow unprivileged PSI polling

PATCH Superseded Domenico Cerasuolo 2 participants 10 messages Mar 29, 2023 1 message not in the archive

Subsystems (4)

v4 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v4 cover sched/psi: Allow unprivileged PSI polling

PSI offers 2 mechanisms to get information about a specific resource pressure. One is reading from /proc/pressure/<resource>, which gives average pressures aggregated every 2s. The other is creating a pollable fd for a specific resource and cgroup.

The trigger creation requires CAP_SYS_RESOURCE, and gives the possibility to pick specific time window and threshold, spawing an RT thread to aggregate the data.

Systemd would like to provide containers the option to monitor pressure on their own cgroup and sub-cgroups. For example, if systemd launches a container that itself then launches services, the container should have the ability to poll() for pressure in individual services. But neither the container nor the services are privileged.

The series is implemented in 4 steps in order to reduce the noise of the change.

V4:
- fixed psi_open leftover usage in IRQ accounting (patch 4/4)

V3:
- restored renaming patch (#2 of 4) as suggested in review
- rebased #3 and #4 on the renaming commit

V2:
- removed renaming patch (previous 2/4) and applied suggested solution
- changed update_triggers side effect removal as suggested in review
- rebased core patch on other V2 changes

Domenico Cerasuolo (4):
sched/psi: rearrange polling code in preparation sched/psi: rename existing poll members in preparation sched/psi: extract update_triggers side effect sched/psi: allow unprivileged polling of N*2s period

 Documentation/accounting/psi.rst |   4 +
 include/linux/psi.h              |   2 +-
 include/linux/psi_types.h        |  43 +--
 kernel/cgroup/cgroup.c           |   2 +-
 kernel/sched/psi.c               | 434 +++++++++++++++++--------------
 5 files changed, 263 insertions(+), 222 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v4 1/4 sched/psi: rearrange polling code in preparation

Move a few functions up in the file to avoid forward declaration needed in the patch implementing unprivileged PSI triggers.

---
 kernel/sched/psi.c | 196 ++++++++++++++++++++++-----------------------
 1 file changed, 98 insertions(+), 98 deletions(-)
kernel/sched/psi.c +98 -98
index 02e011cabe91..fe9269f1d2a4 100644
@@ -384,92 +384,6 @@ static void collect_percpu_times(struct psi_group *group,
 		*pchanged_states = changed_states;
 }
 
-static u64 update_averages(struct psi_group *group, u64 now)
-{
-	unsigned long missed_periods = 0;
-	u64 expires, period;
-	u64 avg_next_update;
-	int s;
-
-	/* avgX= */
-	expires = group->avg_next_update;
-	if (now - expires >= psi_period)
-		missed_periods = div_u64(now - expires, psi_period);
-
-	/*
-	 * The periodic clock tick can get delayed for various
-	 * reasons, especially on loaded systems. To avoid clock
-	 * drift, we schedule the clock in fixed psi_period intervals.
-	 * But the deltas we sample out of the per-cpu buckets above
-	 * are based on the actual time elapsing between clock ticks.
-	 */
-	avg_next_update = expires + ((1 + missed_periods) * psi_period);
-	period = now - (group->avg_last_update + (missed_periods * psi_period));
-	group->avg_last_update = now;
-
-	for (s = 0; s < NR_PSI_STATES - 1; s++) {
-		u32 sample;
-
-		sample = group->total[PSI_AVGS][s] - group->avg_total[s];
-		/*
-		 * Due to the lockless sampling of the time buckets,
-		 * recorded time deltas can slip into the next period,
-		 * which under full pressure can result in samples in
-		 * excess of the period length.
-		 *
-		 * We don't want to report non-sensical pressures in
-		 * excess of 100%, nor do we want to drop such events
-		 * on the floor. Instead we punt any overage into the
-		 * future until pressure subsides. By doing this we
-		 * don't underreport the occurring pressure curve, we
-		 * just report it delayed by one period length.
-		 *
-		 * The error isn't cumulative. As soon as another
-		 * delta slips from a period P to P+1, by definition
-		 * it frees up its time T in P.
-		 */
-		if (sample > period)
-			sample = period;
-		group->avg_total[s] += sample;
-		calc_avgs(group->avg[s], missed_periods, sample, period);
-	}
-
-	return avg_next_update;
-}
-
-static void psi_avgs_work(struct work_struct *work)
-{
-	struct delayed_work *dwork;
-	struct psi_group *group;
-	u32 changed_states;
-	u64 now;
-
-	dwork = to_delayed_work(work);
-	group = container_of(dwork, struct psi_group, avgs_work);
-
-	mutex_lock(&group->avgs_lock);
-
-	now = sched_clock();
-
-	collect_percpu_times(group, PSI_AVGS, &changed_states);
-	/*
-	 * If there is task activity, periodically fold the per-cpu
-	 * times and feed samples into the running averages. If things
-	 * are idle and there is no data to process, stop the clock.
-	 * Once restarted, we'll catch up the running averages in one
-	 * go - see calc_avgs() and missed_periods.
-	 */
-	if (now >= group->avg_next_update)
-		group->avg_next_update = update_averages(group, now);
-
-	if (changed_states & PSI_STATE_RESCHEDULE) {
-		schedule_delayed_work(dwork, nsecs_to_jiffies(
-				group->avg_next_update - now) + 1);
-	}
-
-	mutex_unlock(&group->avgs_lock);
-}
-
 /* Trigger tracking window manipulations */
 static void window_reset(struct psi_window *win, u64 now, u64 value,
 			 u64 prev_growth)
@@ -516,18 +430,6 @@ static u64 window_update(struct psi_window *win, u64 now, u64 value)
 	return growth;
 }
 
-static void init_triggers(struct psi_group *group, u64 now)
-{
-	struct psi_trigger *t;
-
-	list_for_each_entry(t, &group->triggers, node)
-		window_reset(&t->win, now,
-				group->total[PSI_POLL][t->state], 0);
-	memcpy(group->polling_total, group->total[PSI_POLL],
-		   sizeof(group->polling_total));
-	group->polling_next_update = now + group->poll_min_period;
-}
-
 static u64 update_triggers(struct psi_group *group, u64 now)
 {
 	struct psi_trigger *t;
@@ -590,6 +492,104 @@ static u64 update_triggers(struct psi_group *group, u64 now)
 	return now + group->poll_min_period;
 }
 
+static u64 update_averages(struct psi_group *group, u64 now)
+{
+	unsigned long missed_periods = 0;
+	u64 expires, period;
+	u64 avg_next_update;
+	int s;
+
+	/* avgX= */
+	expires = group->avg_next_update;
+	if (now - expires >= psi_period)
+		missed_periods = div_u64(now - expires, psi_period);
+
+	/*
+	 * The periodic clock tick can get delayed for various
+	 * reasons, especially on loaded systems. To avoid clock
+	 * drift, we schedule the clock in fixed psi_period intervals.
+	 * But the deltas we sample out of the per-cpu buckets above
+	 * are based on the actual time elapsing between clock ticks.
+	 */
+	avg_next_update = expires + ((1 + missed_periods) * psi_period);
+	period = now - (group->avg_last_update + (missed_periods * psi_period));
+	group->avg_last_update = now;
+
+	for (s = 0; s < NR_PSI_STATES - 1; s++) {
+		u32 sample;
+
+		sample = group->total[PSI_AVGS][s] - group->avg_total[s];
+		/*
+		 * Due to the lockless sampling of the time buckets,
+		 * recorded time deltas can slip into the next period,
+		 * which under full pressure can result in samples in
+		 * excess of the period length.
+		 *
+		 * We don't want to report non-sensical pressures in
+		 * excess of 100%, nor do we want to drop such events
+		 * on the floor. Instead we punt any overage into the
+		 * future until pressure subsides. By doing this we
+		 * don't underreport the occurring pressure curve, we
+		 * just report it delayed by one period length.
+		 *
+		 * The error isn't cumulative. As soon as another
+		 * delta slips from a period P to P+1, by definition
+		 * it frees up its time T in P.
+		 */
+		if (sample > period)
+			sample = period;
+		group->avg_total[s] += sample;
+		calc_avgs(group->avg[s], missed_periods, sample, period);
+	}
+
+	return avg_next_update;
+}
+
+static void psi_avgs_work(struct work_struct *work)
+{
+	struct delayed_work *dwork;
+	struct psi_group *group;
+	u32 changed_states;
+	u64 now;
+
+	dwork = to_delayed_work(work);
+	group = container_of(dwork, struct psi_group, avgs_work);
+
+	mutex_lock(&group->avgs_lock);
+
+	now = sched_clock();
+
+	collect_percpu_times(group, PSI_AVGS, &changed_states);
+	/*
+	 * If there is task activity, periodically fold the per-cpu
+	 * times and feed samples into the running averages. If things
+	 * are idle and there is no data to process, stop the clock.
+	 * Once restarted, we'll catch up the running averages in one
+	 * go - see calc_avgs() and missed_periods.
+	 */
+	if (now >= group->avg_next_update)
+		group->avg_next_update = update_averages(group, now);
+
+	if (changed_states & PSI_STATE_RESCHEDULE) {
+		schedule_delayed_work(dwork, nsecs_to_jiffies(
+				group->avg_next_update - now) + 1);
+	}
+
+	mutex_unlock(&group->avgs_lock);
+}
+
+static void init_triggers(struct psi_group *group, u64 now)
+{
+	struct psi_trigger *t;
+
+	list_for_each_entry(t, &group->triggers, node)
+		window_reset(&t->win, now,
+				group->total[PSI_POLL][t->state], 0);
+	memcpy(group->polling_total, group->total[PSI_POLL],
+		   sizeof(group->polling_total));
+	group->polling_next_update = now + group->poll_min_period;
+}
+
 /* Schedule polling if it's not already scheduled or forced. */
 static void psi_schedule_poll_work(struct psi_group *group, unsigned long delay,
 				   bool force)
Signature · 1 line
2.34.1
#3 lore raw

PATCH v4 2/4 sched/psi: rename existing poll members in preparation

In reply to Domenico Cerasuolo

Renaming in PSI implementation to make a clear distinction between privileged and unprivileged triggers code to be implemented in the next patch.

---
 include/linux/psi_types.h |  36 ++++-----
 kernel/sched/psi.c        | 163 +++++++++++++++++++-------------------
 2 files changed, 100 insertions(+), 99 deletions(-)
include/linux/psi_types.h +18 -18
index 1e0a0d7ace3a..1819afa8b198 100644
@@ -175,26 +175,26 @@ struct psi_group {
 	u64 total[NR_PSI_AGGREGATORS][NR_PSI_STATES - 1];
 	unsigned long avg[NR_PSI_STATES - 1][3];
 
-	/* Monitor work control */
-	struct task_struct __rcu *poll_task;
-	struct timer_list poll_timer;
-	wait_queue_head_t poll_wait;
-	atomic_t poll_wakeup;
-	atomic_t poll_scheduled;
+	/* Monitor RT polling work control */
+	struct task_struct __rcu *rtpoll_task;
+	struct timer_list rtpoll_timer;
+	wait_queue_head_t rtpoll_wait;
+	atomic_t rtpoll_wakeup;
+	atomic_t rtpoll_scheduled;
 
 	/* Protects data used by the monitor */
-	struct mutex trigger_lock;
-
-	/* Configured polling triggers */
-	struct list_head triggers;
-	u32 nr_triggers[NR_PSI_STATES - 1];
-	u32 poll_states;
-	u64 poll_min_period;
-
-	/* Total stall times at the start of monitor activation */
-	u64 polling_total[NR_PSI_STATES - 1];
-	u64 polling_next_update;
-	u64 polling_until;
+	struct mutex rtpoll_trigger_lock;
+
+	/* Configured RT polling triggers */
+	struct list_head rtpoll_triggers;
+	u32 rtpoll_nr_triggers[NR_PSI_STATES - 1];
+	u32 rtpoll_states;
+	u64 rtpoll_min_period;
+
+	/* Total stall times at the start of RT polling monitor activation */
+	u64 rtpoll_total[NR_PSI_STATES - 1];
+	u64 rtpoll_next_update;
+	u64 rtpoll_until;
 };
 
 #else /* CONFIG_PSI */
kernel/sched/psi.c +82 -81

339 lines not on this page · raw message

Signature · 1 line
2.34.1
#5 lore raw

PATCH v4 3/4 sched/psi: extract update_triggers side effect

In reply to Domenico Cerasuolo

This change moves update_total flag out of update_triggers function, currently called only in psi_poll_work.
In the next patch, update_triggers will be called also in psi_avgs_work, but the total update information is specific to psi_poll_work. Returning update_total value to the caller let us avoid differentiating the implementation of update_triggers for different aggregators.

---
 kernel/sched/psi.c | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)
kernel/sched/psi.c +10 -9
index a3d0b5cf797a..f3df6a8ff493 100644
@@ -430,11 +430,11 @@ static u64 window_update(struct psi_window *win, u64 now, u64 value)
 	return growth;
 }
 
-static u64 update_triggers(struct psi_group *group, u64 now)
+static u64 update_triggers(struct psi_group *group, u64 now, bool *update_total)
 {
 	struct psi_trigger *t;
-	bool update_total = false;
 	u64 *total = group->total[PSI_POLL];
+	*update_total = false;
 
 	/*
 	 * On subsequent updates, calculate growth deltas and let
@@ -462,7 +462,7 @@ static u64 update_triggers(struct psi_group *group, u64 now)
 			 * been through all of them. Also remember to extend the
 			 * polling time if we see new stall activity.
 			 */
-			update_total = true;
+			*update_total = true;
 
 			/* Calculate growth since last update */
 			growth = window_update(&t->win, now, total[t->state]);
@@ -485,10 +485,6 @@ static u64 update_triggers(struct psi_group *group, u64 now)
 		t->pending_event = false;
 	}
 
-	if (update_total)
-		memcpy(group->rtpoll_total, total,
-				sizeof(group->rtpoll_total));
-
 	return now + group->rtpoll_min_period;
 }
 
@@ -622,6 +618,7 @@ static void psi_rtpoll_work(struct psi_group *group)
 {
 	bool force_reschedule = false;
 	u32 changed_states;
+	bool update_total;
 	u64 now;
 
 	mutex_lock(&group->rtpoll_trigger_lock);
@@ -686,8 +683,12 @@ static void psi_rtpoll_work(struct psi_group *group)
 		goto out;
 	}
 
-	if (now >= group->rtpoll_next_update)
-		group->rtpoll_next_update = update_triggers(group, now);
+	if (now >= group->rtpoll_next_update) {
+		group->rtpoll_next_update = update_triggers(group, now, &update_total);
+		if (update_total)
+			memcpy(group->rtpoll_total, group->total[PSI_POLL],
+				   sizeof(group->rtpoll_total));
+	}
 
 	psi_schedule_rtpoll_work(group,
 		nsecs_to_jiffies(group->rtpoll_next_update - now) + 1,
Signature · 1 line
2.34.1
maintainer Acked-by on v4 3/4
#6 lore raw

Re: [PATCH v4 3/4] sched/psi: extract update_triggers side effect

Quoted 9 lines · Domenico Cerasuolo

This change moves update_total flag out of update_triggers function, currently called only in psi_poll_work.
In the next patch, update_triggers will be called also in psi_avgs_work, but the total update information is specific to psi_poll_work. Returning update_total value to the caller let us avoid differentiating the implementation of update_triggers for different aggregators.

#7 lore raw

PATCH v4 4/4 sched/psi: allow unprivileged polling of N*2s period

In reply to Domenico Cerasuolo

PSI offers 2 mechanisms to get information about a specific resource pressure. One is reading from /proc/pressure/<resource>, which gives average pressures aggregated every 2s. The other is creating a pollable fd for a specific resource and cgroup.

The trigger creation requires CAP_SYS_RESOURCE, and gives the possibility to pick specific time window and threshold, spawing an RT thread to aggregate the data.

Systemd would like to provide containers the option to monitor pressure on their own cgroup and sub-cgroups. For example, if systemd launches a container that itself then launches services, the container should have the ability to poll() for pressure in individual services. But neither the container nor the services are privileged.

This patch implements a mechanism to allow unprivileged users to create pressure triggers. The difference with privileged triggers creation is that unprivileged ones must have a time window that's a multiple of 2s. This is so that we can avoid unrestricted spawning of rt threads, and use instead the same aggregation mechanism done for the averages, which runs independently of any triggers.

---
 Documentation/accounting/psi.rst |   4 ++
 include/linux/psi.h              |   2 +-
 include/linux/psi_types.h        |   7 ++
 kernel/cgroup/cgroup.c           |   2 +-
 kernel/sched/psi.c               | 120 +++++++++++++++++++------------
 5 files changed, 87 insertions(+), 48 deletions(-)
Documentation/accounting/psi.rst +4 -0
index 5e40b3f437f9..df6062eb3abb 100644
@@ -105,6 +105,10 @@ prevent overly frequent polling. Max limit is chosen as a high enough number
 after which monitors are most likely not needed and psi averages can be used
 instead.
 
+Unprivileged users can also create monitors, with the only limitation that the
+window size must be a multiple of 2s, in order to prevent excessive resource
+usage.
+
 When activated, psi monitor stays active for at least the duration of one
 tracking window to avoid repeated activations/deactivations when system is
 bouncing in and out of the stall state.
include/linux/psi.h +1 -1
index b029a847def1..ab26200c2803 100644
@@ -24,7 +24,7 @@ void psi_memstall_leave(unsigned long *flags);
 
 int psi_show(struct seq_file *s, struct psi_group *group, enum psi_res res);
 struct psi_trigger *psi_trigger_create(struct psi_group *group,
-			char *buf, enum psi_res res);
+			char *buf, enum psi_res res, struct file *file);
 void psi_trigger_destroy(struct psi_trigger *t);
 
 __poll_t psi_trigger_poll(void **trigger_ptr, struct file *file,
include/linux/psi_types.h +7 -0
index 1819afa8b198..bc998471976a 100644
@@ -151,6 +151,9 @@ struct psi_trigger {
 
 	/* Deferred event(s) from previous ratelimit window */
 	bool pending_event;
+
+	/* Used to differentiate destruction action*/
+	enum psi_aggregators aggregator;
 };
 
 struct psi_group {
@@ -171,6 +174,10 @@ struct psi_group {
 	/* Aggregator work control */
 	struct delayed_work avgs_work;
 
+	/* Unprivileged triggers against N*PSI_FREQ windows */
+	struct list_head avg_triggers;
+	u32 avg_nr_triggers[NR_PSI_STATES - 1];
+
 	/* Total stall times and sampled pressure averages */
 	u64 total[NR_PSI_AGGREGATORS][NR_PSI_STATES - 1];
 	unsigned long avg[NR_PSI_STATES - 1][3];
kernel/cgroup/cgroup.c +1 -1
index 935e8121b21e..dead36969bba 100644
@@ -3761,7 +3761,7 @@ static ssize_t pressure_write(struct kernfs_open_file *of, char *buf,
 	}
 
 	psi = cgroup_psi(cgrp);
-	new = psi_trigger_create(psi, buf, res);
+	new = psi_trigger_create(psi, buf, res, of->file);
 	if (IS_ERR(new)) {
 		cgroup_put(cgrp);
 		return PTR_ERR(new);
kernel/sched/psi.c +74 -46

251 lines not on this page · raw message

Signature · 1 line
2.34.1
maintainer Acked-by on v4 4/4
#8 lore raw

Re: [PATCH v4 4/4] sched/psi: allow unprivileged polling of N*2s period

Quoted 22 lines · Domenico Cerasuolo

PSI offers 2 mechanisms to get information about a specific resource pressure. One is reading from /proc/pressure/<resource>, which gives average pressures aggregated every 2s. The other is creating a pollable fd for a specific resource and cgroup.

The trigger creation requires CAP_SYS_RESOURCE, and gives the possibility to pick specific time window and threshold, spawing an RT thread to aggregate the data.

Systemd would like to provide containers the option to monitor pressure on their own cgroup and sub-cgroups. For example, if systemd launches a container that itself then launches services, the container should have the ability to poll() for pressure in individual services. But neither the container nor the services are privileged.

This patch implements a mechanism to allow unprivileged users to create pressure triggers. The difference with privileged triggers creation is that unprivileged ones must have a time window that's a multiple of 2s. This is so that we can avoid unrestricted spawning of rt threads, and use instead the same aggregation mechanism done for the averages, which runs independently of any triggers.

Quoted 2 lines

Overall it looks good to me. Thanks for adding the comment on the privilege check, it's much easier to understand now.

A few nitpicks below:

include/linux/psi_types.h struct psi_trigger { · line 157

Quoted 8 lines
include/linux/psi_types.h +3 -0
@@ -151,6 +151,9 @@ struct psi_trigger {
 
 	/* Deferred event(s) from previous ratelimit window */
 	bool pending_event;
+
+	/* Used to differentiate destruction action*/
+	enum psi_aggregators aggregator;
 };

The comment is a bit mysterious. How about

	/* Trigger type - PSI_AVGS for unprivileged, PSI_POLL for RT */
Quoted 13 lines
kernel/sched/psi.c +2 -2
@@ -186,9 +186,9 @@ static void group_init(struct psi_group *group)
 		seqcount_init(&per_cpu_ptr(group->pcpu, cpu)->seq);
 	group->avg_last_update = sched_clock();
 	group->avg_next_update = group->avg_last_update + psi_period;
-	INIT_DELAYED_WORK(&group->avgs_work, psi_avgs_work);
 	mutex_init(&group->avgs_lock);
-	/* Init trigger-related members */
+
+	/* Init rtpoll trigger-related members */
 	atomic_set(&group->rtpoll_scheduled, 0);
 	mutex_init(&group->rtpoll_trigger_lock);
 	INIT_LIST_HEAD(&group->rtpoll_triggers);

kernel/sched/psi.c static void group_init(struct psi_group *group) · line 205

Quoted 9 lines
kernel/sched/psi.c +5 -0
@@ -197,6 +197,11 @@ static void group_init(struct psi_group *group)
 	init_waitqueue_head(&group->rtpoll_wait);
 	timer_setup(&group->rtpoll_timer, poll_timer_fn, 0);
 	rcu_assign_pointer(group->rtpoll_task, NULL);
+
+	/* Init avg trigger-related members */
+	INIT_LIST_HEAD(&group->avg_triggers);
+	memset(group->avg_nr_triggers, 0, sizeof(group->avg_nr_triggers));
+	INIT_DELAYED_WORK(&group->avgs_work, psi_avgs_work);
 }

Can you move those above the rtpoll inits?

It helps with navigating the code and spotting missing inits when the init sequence follows the order of the struct members.

Quoted 5 lines
kernel/sched/psi.c +0 -1
@@ -430,21 +435,25 @@ static u64 window_update(struct psi_window *win, u64 now, u64 value)
 	return growth;
 }
 
-static u64 update_triggers(struct psi_group *group, u64 now, bool *update_total)

kernel/sched/psi.c static u64 window_update(struct psi_window *win, u64 now, u64 value) · line 446

Quoted 10 lines
kernel/sched/psi.c +6 -1
+static u64 update_triggers(struct psi_group *group, u64 now, bool *update_total,
+						   enum psi_aggregators aggregator)
 {
 	struct psi_trigger *t;
-	u64 *total = group->total[PSI_POLL];
+	u64 *total = group->total[aggregator];
+	struct list_head *triggers = aggregator == PSI_AVGS ? &group->avg_triggers
+			: &group->rtpoll_triggers;
+	u64 *aggregator_total = aggregator == PSI_AVGS ? group->avg_total : group->rtpoll_total;
 	*update_total = false;

These lines are a bit too long. When the init part gets that long, it's preferable to move it outside of the decl block:

	if (aggregator == PSI_AVGS) {
		triggers = &group->avg_triggers;
		aggregator_total = group->avg_total;
	} else {
		triggers = &group->rtpoll_triggers;
		aggregator_total = group->rtpoll_total;
	}
Quoted 29 lines
kernel/sched/psi.c +9 -16
@@ -1357,22 +1389,26 @@ void psi_trigger_destroy(struct psi_trigger *t)
 		u64 period = ULLONG_MAX;
 
 		list_del(&t->node);
-		group->rtpoll_nr_triggers[t->state]--;
-		if (!group->rtpoll_nr_triggers[t->state])
-			group->rtpoll_states &= ~(1 << t->state);
-		/* reset min update period for the remaining triggers */
-		list_for_each_entry(tmp, &group->rtpoll_triggers, node)
-			period = min(period, div_u64(tmp->win.size,
-					UPDATES_PER_WINDOW));
-		group->rtpoll_min_period = period;
-		/* Destroy rtpoll_task when the last trigger is destroyed */
-		if (group->rtpoll_states == 0) {
-			group->rtpoll_until = 0;
-			task_to_destroy = rcu_dereference_protected(
-					group->rtpoll_task,
-					lockdep_is_held(&group->rtpoll_trigger_lock));
-			rcu_assign_pointer(group->rtpoll_task, NULL);
-			del_timer(&group->rtpoll_timer);
+		if (t->aggregator == PSI_AVGS) {
+			group->avg_nr_triggers[t->state]--;
+		} else {
+			group->rtpoll_nr_triggers[t->state]--;
+			if (!group->rtpoll_nr_triggers[t->state])
+				group->rtpoll_states &= ~(1 << t->state);
+			/* reset min update period for the remaining triggers */
+			list_for_each_entry(tmp, &group->rtpoll_triggers, node)
+				period = min(period, div_u64(tmp->win.size,

kernel/sched/psi.c void psi_trigger_destroy(struct psi_trigger *t) · line 1410

Quoted 10 lines
kernel/sched/psi.c +10 -0
+						UPDATES_PER_WINDOW));
+			group->rtpoll_min_period = period;
+			/* Destroy rtpoll_task when the last trigger is destroyed */
+			if (group->rtpoll_states == 0) {
+				group->rtpoll_until = 0;
+				task_to_destroy = rcu_dereference_protected(
+						group->rtpoll_task,
+						lockdep_is_held(&group->rtpoll_trigger_lock));
+				rcu_assign_pointer(group->rtpoll_task, NULL);
+				del_timer(&group->rtpoll_timer);

These lines are quite long too, we usually shoot for a line length of 80 characters. Can you do

		if (t->aggregator == PSI_AVGS) {
			group->avg_nr_triggers[t->state]--;
			return;
		}

		/* Else, it's an rtpoll trigger */
		group->rtpoll_nr_triggers[t->state]--;
		...

With that, please add

A message that is not in the archive (sent off-list or not yet received): CA+CLi1hupf1t52GR-GgvAxhfJeUs6Z59Pmgd4NLmkwiEKTyACQ@mail.gmail.com

maintainer
#10 lore raw

Re: [PATCH v4 4/4] sched/psi: allow unprivileged polling of N*2s period

Quoted 163 lines · Domenico Cerasuolo
Quoted 161 lines · Johannes Weiner
Quoted 22 lines · Domenico Cerasuolo

PSI offers 2 mechanisms to get information about a specific resource pressure. One is reading from /proc/pressure/<resource>, which gives average pressures aggregated every 2s. The other is creating a pollable fd for a specific resource and cgroup.

The trigger creation requires CAP_SYS_RESOURCE, and gives the possibility to pick specific time window and threshold, spawing an RT thread to aggregate the data.

Systemd would like to provide containers the option to monitor pressure on their own cgroup and sub-cgroups. For example, if systemd launches a container that itself then launches services, the container should have the ability to poll() for pressure in individual services. But neither the container nor the services are privileged.

This patch implements a mechanism to allow unprivileged users to create pressure triggers. The difference with privileged triggers creation is that unprivileged ones must have a time window that's a multiple of 2s. This is so that we can avoid unrestricted spawning of rt threads, and use instead the same aggregation mechanism done for the averages, which runs independently of any triggers.

Quoted 2 lines

Overall it looks good to me. Thanks for adding the comment on the privilege check, it's much easier to understand now.

A few nitpicks below:

include/linux/psi_types.h struct psi_trigger { · line 157

Quoted 8 lines
include/linux/psi_types.h +3 -0
@@ -151,6 +151,9 @@ struct psi_trigger {
 
      /* Deferred event(s) from previous ratelimit window */
      bool pending_event;
+
+     /* Used to differentiate destruction action*/
+     enum psi_aggregators aggregator;
 };

The comment is a bit mysterious. How about

        /* Trigger type - PSI_AVGS for unprivileged, PSI_POLL for RT */
Quoted 13 lines
kernel/sched/psi.c +2 -2
@@ -186,9 +186,9 @@ static void group_init(struct psi_group *group)
              seqcount_init(&per_cpu_ptr(group->pcpu, cpu)->seq);
      group->avg_last_update = sched_clock();
      group->avg_next_update = group->avg_last_update + psi_period;
-     INIT_DELAYED_WORK(&group->avgs_work, psi_avgs_work);
      mutex_init(&group->avgs_lock);
-     /* Init trigger-related members */
+
+     /* Init rtpoll trigger-related members */
      atomic_set(&group->rtpoll_scheduled, 0);
      mutex_init(&group->rtpoll_trigger_lock);
      INIT_LIST_HEAD(&group->rtpoll_triggers);

kernel/sched/psi.c static void group_init(struct psi_group *group) · line 205

Quoted 9 lines
kernel/sched/psi.c +5 -0
@@ -197,6 +197,11 @@ static void group_init(struct psi_group *group)
      init_waitqueue_head(&group->rtpoll_wait);
      timer_setup(&group->rtpoll_timer, poll_timer_fn, 0);
      rcu_assign_pointer(group->rtpoll_task, NULL);
+
+     /* Init avg trigger-related members */
+     INIT_LIST_HEAD(&group->avg_triggers);
+     memset(group->avg_nr_triggers, 0, sizeof(group->avg_nr_triggers));
+     INIT_DELAYED_WORK(&group->avgs_work, psi_avgs_work);
 }

Can you move those above the rtpoll inits?

It helps with navigating the code and spotting missing inits when the init sequence follows the order of the struct members.

Review static u64 window_update(struct psi_window *win,

Quoted 1 line
(quoted diff) +0 -0
@@ -430,21 +435,25 @@ static u64 window_update(struct psi_window *win,

u64 now, u64 value)

kernel/sched/psi.c static u64 window_update(struct psi_window *win, u64 now, u64 value)

Quoted 4 lines
kernel/sched/psi.c +0 -1
      return growth;
 }
 
-static u64 update_triggers(struct psi_group *group, u64 now, bool

*update_total)

Review static u64 window_update(struct psi_window *win, · line 438

Quoted 1 line
(quoted diff) +1 -0
+static u64 update_triggers(struct psi_group *group, u64 now, bool

*update_total,

Review static u64 window_update(struct psi_window *win, · line 439

Quoted 1 line
(quoted diff) +1 -0
+                                                enum psi_aggregators

aggregator)

kernel/sched/psi.c static u64 window_update(struct psi_window *win, u64 now, u64 value)

Quoted 5 lines
kernel/sched/psi.c +2 -1
 {
      struct psi_trigger *t;
-     u64 *total = group->total[PSI_POLL];
+     u64 *total = group->total[aggregator];
+     struct list_head *triggers = aggregator == PSI_AVGS ?

&group->avg_triggers

Review static u64 window_update(struct psi_window *win, · line 445

Quoted 2 lines
(quoted diff) +2 -0
+                     : &group->rtpoll_triggers;
+     u64 *aggregator_total = aggregator == PSI_AVGS ? group->avg_total

: group->rtpoll_total;

kernel/sched/psi.c static u64 window_update(struct psi_window *win, u64 now, u64 value) · line 446

Quoted 1 line
kernel/sched/psi.c +0 -0
      *update_total = false;

These lines are a bit too long. When the init part gets that long, it's preferable to move it outside of the decl block:

        if (aggregator == PSI_AVGS) {
                triggers = &group->avg_triggers;
                aggregator_total = group->avg_total;
        } else {
                triggers = &group->rtpoll_triggers;
                aggregator_total = group->rtpoll_total;
        }

kernel/sched/psi.c void psi_trigger_destroy(struct psi_trigger *t) · line 1368

Quoted 13 lines
kernel/sched/psi.c +0 -9
@@ -1357,22 +1389,26 @@ void psi_trigger_destroy(struct psi_trigger *t)
              u64 period = ULLONG_MAX;
 
              list_del(&t->node);
-             group->rtpoll_nr_triggers[t->state]--;
-             if (!group->rtpoll_nr_triggers[t->state])
-                     group->rtpoll_states &= ~(1 << t->state);
-             /* reset min update period for the remaining triggers */
-             list_for_each_entry(tmp, &group->rtpoll_triggers, node)
-                     period = min(period, div_u64(tmp->win.size,
-                                     UPDATES_PER_WINDOW));
-             group->rtpoll_min_period = period;
-             /* Destroy rtpoll_task when the last trigger is destroyed

*/

kernel/sched/psi.c void psi_trigger_destroy(struct psi_trigger *t)

Quoted 5 lines
kernel/sched/psi.c +0 -5
-             if (group->rtpoll_states == 0) {
-                     group->rtpoll_until = 0;
-                     task_to_destroy = rcu_dereference_protected(
-                                     group->rtpoll_task,
-

lockdep_is_held(&group->rtpoll_trigger_lock));

kernel/sched/psi.c void psi_trigger_destroy(struct psi_trigger *t)

Quoted 9 lines
kernel/sched/psi.c +7 -2
-                     rcu_assign_pointer(group->rtpoll_task, NULL);
-                     del_timer(&group->rtpoll_timer);
+             if (t->aggregator == PSI_AVGS) {
+                     group->avg_nr_triggers[t->state]--;
+             } else {
+                     group->rtpoll_nr_triggers[t->state]--;
+                     if (!group->rtpoll_nr_triggers[t->state])
+                             group->rtpoll_states &= ~(1 << t->state);
+                     /* reset min update period for the remaining

triggers */

Review void psi_trigger_destroy(struct psi_trigger *t) · line 1399

Quoted 1 line
(quoted diff) +1 -0
+                     list_for_each_entry(tmp, &group->rtpoll_triggers,

node)

kernel/sched/psi.c void psi_trigger_destroy(struct psi_trigger *t)

Quoted 4 lines
kernel/sched/psi.c +4 -0
+                             period = min(period, div_u64(tmp->win.size,
+                                             UPDATES_PER_WINDOW));
+                     group->rtpoll_min_period = period;
+                     /* Destroy rtpoll_task when the last trigger is

destroyed */

kernel/sched/psi.c void psi_trigger_destroy(struct psi_trigger *t)

Quoted 3 lines
kernel/sched/psi.c +3 -0
+                     if (group->rtpoll_states == 0) {
+                             group->rtpoll_until = 0;
+                             task_to_destroy =

rcu_dereference_protected(

Review void psi_trigger_destroy(struct psi_trigger *t) · line 1408

Quoted 2 lines
(quoted diff) +2 -0
+                                             group->rtpoll_task,
+

lockdep_is_held(&group->rtpoll_trigger_lock));

Review void psi_trigger_destroy(struct psi_trigger *t) · line 1409

Quoted 1 line
(quoted diff) +1 -0
+                             rcu_assign_pointer(group->rtpoll_task,

NULL);

kernel/sched/psi.c void psi_trigger_destroy(struct psi_trigger *t) · line 1410

Quoted 1 line
kernel/sched/psi.c +1 -0
+                             del_timer(&group->rtpoll_timer);

These lines are quite long too, we usually shoot for a line length of 80 characters. Can you do

                if (t->aggregator == PSI_AVGS) {
                        group->avg_nr_triggers[t->state]--;
                        return;
                }
Quoted 6 lines
Quoted 4 lines
                /* Else, it's an rtpoll trigger */
                group->rtpoll_nr_triggers[t->state]--;
                ...

Can't return there I think, the function doesn't end after the else branch, should I put a goto out instead to jump the rtpoll code?

You're right, I missed the bottom part beyond the diff.

Looking closer, I think trigger_create and trigger_destroy are actually buggy. They have to protect against update_trigger(), so both creation and destruction must take the right lock - avgs_lock or rtpoll_trigger_lock - before modifying the list. They're both taking only the rtpoll_trigger_lock right now.

IOW the trigger type distinction needs to be higher up in general.

	if (t->aggregator == PSI_AVGS) {
		mutex_lock(&group->avgs_lock);
		...
	} else {
		mutex_lock(&group->rtpoll_trigger_lock);
		...
	}