All of lore.kernel.org
 help / color / mirror / Atom feed
From: Rasmus Villemoes <linux@rasmusvillemoes.dk>
To: Tejun Heo <tj@kernel.org>, Lai Jiangshan <jiangshanlai@gmail.com>
Cc: "André Pribil" <a.pribil@beck-ipc.com>,
	"Steven Walter" <stevenrwalter@gmail.com>,
	"Oleksij Rempel" <o.rempel@pengutronix.de>,
	"Esben Haabendal" <esben@geanix.com>,
	"Jiri Slaby" <jirislaby@kernel.org>,
	"Pengutronix Kernel Team" <kernel@pengutronix.de>,
	"Peter Hurley" <peter@hurleysoftware.com>,
	linux-rt-users@vger.kernel.org,
	"Rasmus Villemoes" <linux@rasmusvillemoes.dk>,
	linux-kernel@vger.kernel.org
Subject: [RFC PATCH 2/2] workqueue: update sysfs handlers, allow setting RT policies
Date: Wed, 23 Mar 2022 15:56:00 +0100	[thread overview]
Message-ID: <20220323145600.2156689-3-linux@rasmusvillemoes.dk> (raw)
In-Reply-To: <20220323145600.2156689-1-linux@rasmusvillemoes.dk>

For this POC, this (ab)uses the "nice" attribute.

It's of course not exactly pretty, but it avoids the problem of "what
to show in the xyz file when using SCHED_NORMAL, and what to show in
the nice file when using SCHED_RR/SCHED_FIFO".

It's backwards-compatible, in that a bare integer is interpreted as a
nice value, while an integer prefixed by "fifo:" or "rr:" chooses that
RT scheduling policy with the associated value as the priority. The
show method of course reflects what the store method accepts.

Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
---
 kernel/workqueue.c | 54 ++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 47 insertions(+), 7 deletions(-)

diff --git a/kernel/workqueue.c b/kernel/workqueue.c
index 9eb2ff7bcc04..a97f1aff809e 100644
--- a/kernel/workqueue.c
+++ b/kernel/workqueue.c
@@ -5538,13 +5538,34 @@ static ssize_t wq_nice_show(struct device *dev, struct device_attribute *attr,
 			    char *buf)
 {
 	struct workqueue_struct *wq = dev_to_wq(dev);
-	int written;
+	struct workqueue_attrs *wqattrs;
+	const char *pfx;
+	int val;
 
 	mutex_lock(&wq->mutex);
-	written = scnprintf(buf, PAGE_SIZE, "%d\n", wq->unbound_attrs->nice);
+	wqattrs = wq->unbound_attrs;
+	switch (wqattrs->policy) {
+	case SCHED_NORMAL:
+		pfx = "";
+		val = wqattrs->nice;
+		break;
+	case SCHED_FIFO:
+		pfx = "fifo:";
+		val = wqattrs->priority;
+		break;
+	case SCHED_RR:
+		pfx = "rr:";
+		val = wqattrs->priority;
+		break;
+	default:
+		/* Shouldn't happen. */
+		pfx = NULL;
+	}
 	mutex_unlock(&wq->mutex);
 
-	return written;
+	if (!pfx)
+		return -EIO;
+	return scnprintf(buf, PAGE_SIZE, "%s%d\n", pfx, val);
 }
 
 /* prepare workqueue_attrs for sysfs store operations */
@@ -5568,6 +5589,24 @@ static ssize_t wq_nice_store(struct device *dev, struct device_attribute *attr,
 	struct workqueue_struct *wq = dev_to_wq(dev);
 	struct workqueue_attrs *attrs;
 	int ret = -ENOMEM;
+	int policy, val;
+
+	if (sscanf(buf, "fifo:%d", &val) == 1)
+		policy = SCHED_FIFO;
+	else if (sscanf(buf, "rr:%d", &val) == 1)
+		policy = SCHED_RR;
+	else if (sscanf(buf, "%d", &val) == 1)
+		policy = SCHED_NORMAL;
+	else
+		return -EINVAL;
+
+	if (policy == SCHED_NORMAL) {
+		if (val < MIN_NICE || val > MAX_NICE)
+			return -EINVAL;
+	} else {
+		if (val <= 0 || val >= MAX_RT_PRIO)
+			return -EINVAL;
+	}
 
 	apply_wqattrs_lock();
 
@@ -5575,11 +5614,12 @@ static ssize_t wq_nice_store(struct device *dev, struct device_attribute *attr,
 	if (!attrs)
 		goto out_unlock;
 
-	if (sscanf(buf, "%d", &attrs->nice) == 1 &&
-	    attrs->nice >= MIN_NICE && attrs->nice <= MAX_NICE)
-		ret = apply_workqueue_attrs_locked(wq, attrs);
+	attrs->policy = policy;
+	if (policy == SCHED_NORMAL)
+		attrs->nice = val;
 	else
-		ret = -EINVAL;
+		attrs->priority = val;
+	ret = apply_workqueue_attrs_locked(wq, attrs);
 
 out_unlock:
 	apply_wqattrs_unlock();
-- 
2.31.1


  parent reply	other threads:[~2022-03-23 14:56 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-23 14:55 [RFC PATCH 0/2] RT scheduling policies for workqueues Rasmus Villemoes
2022-03-23 14:55 ` [RFC PATCH 1/2] workqueue: allow use of realtime scheduling policies Rasmus Villemoes
2022-03-23 14:56 ` Rasmus Villemoes [this message]
2022-03-28 10:05 ` [RFC PATCH 0/2] RT scheduling policies for workqueues Sebastian Andrzej Siewior
2022-03-28 10:09   ` Marc Kleine-Budde
2022-03-28 17:39     ` Tejun Heo
2022-03-28 18:07       ` Marc Kleine-Budde
2022-03-29  6:30       ` Sebastian Andrzej Siewior
2022-03-29  8:33         ` Rasmus Villemoes
2022-03-29 16:57           ` Tejun Heo
2022-04-01  9:21           ` Sebastian Andrzej Siewior
2022-04-06 10:00             ` Rasmus Villemoes

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=20220323145600.2156689-3-linux@rasmusvillemoes.dk \
    --to=linux@rasmusvillemoes.dk \
    --cc=a.pribil@beck-ipc.com \
    --cc=esben@geanix.com \
    --cc=jiangshanlai@gmail.com \
    --cc=jirislaby@kernel.org \
    --cc=kernel@pengutronix.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-rt-users@vger.kernel.org \
    --cc=o.rempel@pengutronix.de \
    --cc=peter@hurleysoftware.com \
    --cc=stevenrwalter@gmail.com \
    --cc=tj@kernel.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.