Skip to contentSkip to conversation

sched/numa: Fix divide by zero for sysctl_numa_balancing_scan_size.

PATCH Superseded chris hyser 2 participants 4 messages Mar 30, 2023 – Apr 4, 2023 Scheduler

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 sched/numa: Fix divide by zero for sysctl_numa_balancing_scan_size.

Commit 6419265899d9 ("sched/fair: Fix division by zero sysctl_numa_balancing_scan_size") prevented a divide by zero by using sysctl mechanisms to return EINVAL for a sysctl_numa_balancing_scan_size value of zero. When moved from a sysctl to a debugfs file, this checking was lost.

This patch puts zero checking back in place.

---
 kernel/sched/debug.c | 52 +++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 51 insertions(+), 1 deletion(-)
kernel/sched/debug.c +51 -1
index 1637b65ba07a..b5fc7e250128 100644
@@ -278,6 +278,56 @@ static const struct file_operations sched_dynamic_fops = {
 
 #endif /* CONFIG_PREEMPT_DYNAMIC */
 
+#ifdef CONFIG_NUMA_BALANCING
+
+static ssize_t sched_numa_scan_write(struct file *filp, const char __user *ubuf,
+				     size_t cnt, loff_t *ppos)
+{
+	int err;
+	char buf[16];
+	unsigned int scan_size;
+
+	if (cnt > 15)
+		cnt = 15;
+
+	if (copy_from_user(&buf, ubuf, cnt))
+		return -EFAULT;
+	buf[cnt] = '\0';
+
+	err = kstrtouint(buf, 10, &scan_size);
+	if (err)
+		return err;
+
+	if (!scan_size)
+		return -EINVAL;
+
+	sysctl_numa_balancing_scan_size = scan_size;
+
+	*ppos += cnt;
+	return cnt;
+}
+
+static int sched_numa_scan_show(struct seq_file *m, void *v)
+{
+	seq_printf(m, "%d\n", sysctl_numa_balancing_scan_size);
+	return 0;
+}
+
+static int sched_numa_scan_open(struct inode *inode, struct file *filp)
+{
+	return single_open(filp, sched_numa_scan_show, NULL);
+}
+
+static const struct file_operations sched_numa_scan_fops = {
+	.open		= sched_numa_scan_open,
+	.write		= sched_numa_scan_write,
+	.read		= seq_read,
+	.llseek		= seq_lseek,
+	.release	= single_release,
+};
+
+#endif /* CONFIG_NUMA_BALANCING */
+
 __read_mostly bool sched_debug_verbose;
 
 static const struct seq_operations sched_debug_sops;
@@ -332,7 +382,7 @@ static __init int sched_init_debug(void)
 	debugfs_create_u32("scan_delay_ms", 0644, numa, &sysctl_numa_balancing_scan_delay);
 	debugfs_create_u32("scan_period_min_ms", 0644, numa, &sysctl_numa_balancing_scan_period_min);
 	debugfs_create_u32("scan_period_max_ms", 0644, numa, &sysctl_numa_balancing_scan_period_max);
-	debugfs_create_u32("scan_size_mb", 0644, numa, &sysctl_numa_balancing_scan_size);
+	debugfs_create_file("scan_size_mb", 0644, numa, NULL, &sched_numa_scan_fops);
 	debugfs_create_u32("hot_threshold_ms", 0644, numa, &sysctl_numa_balancing_hot_threshold);
 #endif
 
Signature · 1 line
2.31.1
#1 lore raw
Quoted 28 lines · chris hyser

Commit 6419265899d9 ("sched/fair: Fix division by zero sysctl_numa_balancing_scan_size") prevented a divide by zero by using sysctl mechanisms to return EINVAL for a sysctl_numa_balancing_scan_size value of zero. When moved from a sysctl to a debugfs file, this checking was lost.

This patch puts zero checking back in place.

---
 kernel/sched/debug.c | 52 +++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 51 insertions(+), 1 deletion(-)
kernel/sched/debug.c +7 -0
index 1637b65ba07a..b5fc7e250128 100644
@@ -278,6 +278,56 @@ static const struct file_operations sched_dynamic_fops = {
 
 #endif /* CONFIG_PREEMPT_DYNAMIC */
 
+#ifdef CONFIG_NUMA_BALANCING
+
+static ssize_t sched_numa_scan_write(struct file *filp, const char __user *ubuf,
+				     size_t cnt, loff_t *ppos)
+{
+	int err;
+	char buf[16];

kernel/sched/debug.c static const struct file_operations sched_dynamic_fops = { · line 297

Quoted 10 lines
kernel/sched/debug.c +10 -0
+	unsigned int scan_size;
+
+	if (cnt > 15)
+		cnt = 15;
+
+	if (copy_from_user(&buf, ubuf, cnt))
+		return -EFAULT;
+	buf[cnt] = '\0';
+
+	err = kstrtouint(buf, 10, &scan_size);

While testing the code, it is found that there is a kstrtouint_from_user() could do the above. Not sure if it is worthy to use that?

thanks,
Chenyu

#2 lore raw
Quoted 34 lines · Chen Yu
Quoted 33 lines · chris hyser

Commit 6419265899d9 ("sched/fair: Fix division by zero sysctl_numa_balancing_scan_size") prevented a divide by zero by using sysctl mechanisms to return EINVAL for a sysctl_numa_balancing_scan_size value of zero. When moved from a sysctl to a debugfs file, this checking was lost.

This patch puts zero checking back in place.

---
  kernel/sched/debug.c | 52 +++++++++++++++++++++++++++++++++++++++++++-
  1 file changed, 51 insertions(+), 1 deletion(-)
kernel/sched/debug.c +12 -0
index 1637b65ba07a..b5fc7e250128 100644
@@ -278,6 +278,56 @@ static const struct file_operations sched_dynamic_fops = {
 
  #endif /* CONFIG_PREEMPT_DYNAMIC */
 
+#ifdef CONFIG_NUMA_BALANCING
+
+static ssize_t sched_numa_scan_write(struct file *filp, const char __user *ubuf,
+				     size_t cnt, loff_t *ppos)
+{
+	int err;
+	char buf[16];
+	unsigned int scan_size;
+
+	if (cnt > 15)
+		cnt = 15;
+
Quoted 8 lines

kernel/sched/debug.c static const struct file_operations sched_dynamic_fops = { · line 297

Quoted 5 lines
kernel/sched/debug.c +5 -0
+	if (copy_from_user(&buf, ubuf, cnt))
+		return -EFAULT;
+	buf[cnt] = '\0';
+
+	err = kstrtouint(buf, 10, &scan_size);

While testing the code, it is found that there is a kstrtouint_from_user() could do the above. Not sure if it is worthy to use that?

Let me look at that. I think in general, replacing a block of code in a non-perf path with an existing function is better.

And thank you for looking at this.

-chrish

#3 lore raw
In reply to Chen Yu
Quoted 34 lines · Chen Yu
Quoted 33 lines · chris hyser

Commit 6419265899d9 ("sched/fair: Fix division by zero sysctl_numa_balancing_scan_size") prevented a divide by zero by using sysctl mechanisms to return EINVAL for a sysctl_numa_balancing_scan_size value of zero. When moved from a sysctl to a debugfs file, this checking was lost.

This patch puts zero checking back in place.

---
  kernel/sched/debug.c | 52 +++++++++++++++++++++++++++++++++++++++++++-
  1 file changed, 51 insertions(+), 1 deletion(-)
kernel/sched/debug.c +12 -0
index 1637b65ba07a..b5fc7e250128 100644
@@ -278,6 +278,56 @@ static const struct file_operations sched_dynamic_fops = {
 
  #endif /* CONFIG_PREEMPT_DYNAMIC */
 
+#ifdef CONFIG_NUMA_BALANCING
+
+static ssize_t sched_numa_scan_write(struct file *filp, const char __user *ubuf,
+				     size_t cnt, loff_t *ppos)
+{
+	int err;
+	char buf[16];
+	unsigned int scan_size;
+
+	if (cnt > 15)
+		cnt = 15;
+
Quoted 8 lines

kernel/sched/debug.c static const struct file_operations sched_dynamic_fops = { · line 297

Quoted 5 lines
kernel/sched/debug.c +5 -0
+	if (copy_from_user(&buf, ubuf, cnt))
+		return -EFAULT;
+	buf[cnt] = '\0';
+
+	err = kstrtouint(buf, 10, &scan_size);

While testing the code, it is found that there is a kstrtouint_from_user() could do the above. Not sure if it is worthy to use that?

Yes, putting this in results in cleaner code. Thanks.

I just sent a v3 with this change.

-chrish

Quoted 3 lines

thanks,
Chenyu