From nobody Tue Sep 29 09:09:01 2026 Received: from mail-pj1-f52.google.com (mail-pj1-f52.google.com [209.85.216.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6CD2B23D7FF for ; Mon, 10 Aug 2026 08:18:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786349919; cv=none; b=tdkqTcyqcHRa3KOrpZGvGkjtwqYCSo/Mtyf9U4fF/jvkqtVi7WhUYw9es6FBierq5ROZj5kvGfRv3BJopTcH+UmAM542KO7MAuLkKEJb7hX5jzWyT32JJGSEqZ3ztkFernyu+ieqRO4yX0b3K/BD8/QqJhlDgqpBU6kRYVkNs20= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786349919; c=relaxed/simple; bh=MkaeorZDxOhatVSYZhp+VxqNOBMk437VWx6qzRZKGZU=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=P58WzxIEFAiiMZcfReVL9Zkcemgtlpunq69B8RhL29u2plw9Qi0rD0h7oLSeSonqTmIwx+I0uRipZ7toHU/bDuKGypP3dQkrB3Z8GYLkoQ0Zcn3YkaWYA+TALpud0SO1jG6xFrF5snw4NpCcAY3Kwq7pwJ/zwrltKhP5teP2aAM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=frmft76n; arc=none smtp.client-ip=209.85.216.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="frmft76n" Received: by mail-pj1-f52.google.com with SMTP id 98e67ed59e1d1-3856d6fbcb3so1377776a91.2 for ; Mon, 10 Aug 2026 01:18:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1786349918; x=1786954718; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=xpeCrgXvAid7/y4NBv3qwSowazrhSLa6IPPYvzvehh8=; b=frmft76nLJxhG4V1S9af1xVEYVgXlQz16XcuJn55AAOrVWB3i/RNF8oDqxhW7pQfeB BahWwA+kVVaysrLBQyBA6XnDxVMfcFalsSJWC8v2TZbvw4v9M75iEOfwpYu/z5EZhvN2 m9ZIn953UiTZ6tdPN46LfYc3FSQCjoEpWMWe4vFWUfns+NWMRc0fs7bvS0CKgU1yluj9 rHTtGpM9rgWUnBivxOeZG5peAYyhRozusWwd3kK6VfUUI0Hg/hSLuv+Ro9YZ8KA0hIMk QxUyk3X7a1UhCWlHJBqwVijS3YqJnGVRZryXH4NjIJtnEBWLu2G26aiSpKTkO/rIeTwR 8AGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786349918; x=1786954718; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=xpeCrgXvAid7/y4NBv3qwSowazrhSLa6IPPYvzvehh8=; b=UWjT0U5FfJ56rL2R5eRZy0YlWexvOMIYAnOX6pV5fzxwNWl//km47MqYtt2b2MkPLq R6mC+wE/SQkPB/KEawsYRo/T0sGr0KvAT4gUtANygd7y1kI34u9bZEI+3j1Y1cNCsopo kciLI3GZRtJP5366nl5Tn96fLxLjh6k3dY9jbGGVkZC5Yc0nZcX2bk3DFd6z6tgwUWSI jLGVvdYXlNjoVuzUZIEFWgO3gSiTroNEUzAwCoRX0VhQcw9Zxj9vGwZ0xDaPZjrmuYc9 dwOb9MkOMMFSIxycfyIhUgvQ6O8S6dfXC4QUb9KUDPY99Ck9kYh8z9RQlCKnPrN/kUSS sIAg== X-Forwarded-Encrypted: i=1; AHgh+Rry6+lHC2LT3dWVAOeaetTLA9VgNBtwqBc3/NLgM2XUgxFwp338Uc2lOLuHoAOlcrlnBoQPrYAuXWmy9go=@vger.kernel.org X-Gm-Message-State: AOJu0YzkZxaVJQIre3kg9URMnj2OZcwybvPb4uY64FhLtk4MLT5AdXGC XInKnFPok2e5phschk8fClbV2m4wy3466gpsWo4ZeyY1INdJqpZ2iX21 X-Gm-Gg: AR+sD13cQf/Olmb3kf8X/GBMp8Ho5TP0WcKbsKSlSEw/sWcyt+MuAUgZFe2syHtl/FR wJ1cOYcLhUxEZJccjazAtj9fk3hgj6hZ461IuOEgGjSLPMmVqUAfdMEqN4CdpvdhRfLOeuNRLUR qXWxb2bc14ulmNXzd1yixfSImNnv2zvahtj2Ut4qpJUQSK3Ij94SzYmQp7kXx4oxDg0iNM0dY4A NEuU3z6IBTYGggHIlD295uPExw/zrZrU2Ph0JcR5Ah4gVUMGtMSzEFepgmY9r25fXRE+u7jfIig 9fJy/6HRGIvul/pNvTOdau2S66xeY4lY/qhoP30IjZ2VssA+umK9mEeTxZyDLkX/SD3iWp9rHDk +woz8mALZJet4cvnS32KQeGRHrGhkqrFQ+KTw0zi4YAS+NT/h6Z9kBYS+NfEk1nxQOdJwg+GHoQ sEGbaVD/V7WjN2SEq3xWu2OBmA0bLOppsa0H29g6eGTJ4xKOFc2as0xBg2ULqic6ddgm0AEIb/ X-Received: by 2002:a17:90b:4a09:b0:37f:df85:272f with SMTP id 98e67ed59e1d1-3928242e8e4mr13184898a91.19.1786349917761; Mon, 10 Aug 2026 01:18:37 -0700 (PDT) Received: from osman.mioffice.cn ([43.224.245.178]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-39085f5b98csm13722971a91.13.2026.08.10.01.18.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Aug 2026 01:18:37 -0700 (PDT) From: Zhan Xusheng X-Google-Original-From: Zhan Xusheng To: Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot Cc: Mel Gorman , Dietmar Eggemann , Steven Rostedt , Ben Segall , Valentin Schneider , K Prateek Nayak , zhanxusheng@xiaomi.com, linux-kernel@vger.kernel.org Subject: [PATCH] sched/debug: Validate writes to the scan_size_mb debugfs knob Date: Mon, 10 Aug 2026 16:18:29 +0800 Message-ID: <20260810081829.3149958-1-zhanxusheng@xiaomi.com> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" sysctl_numa_balancing_scan_size is used as a divisor, so two kinds of write to scan_size_mb kill the machine. Zero makes task_scan_min() divide by it: windows =3D MAX_SCAN_WINDOW / scan_size; # echo 0 > /sys/kernel/debug/sched/numa_balancing/scan_size_mb Oops: divide error: 0000 [#1] SMP PTI RIP: 0010:task_scan_max+0x30/0x1a0 RAX: 0000000000000a00 RCX: 0000000000000000 Call Trace: init_numa_balancing+0xe0/0x200 __sched_fork+0x13b/0x180 sched_fork+0x12/0x1d0 copy_process+0xdea/0x2370 kernel_clone+0xd6/0x4a0 __do_sys_clone3+0xf4/0x140 task_scan_min() is inlined there. The trigger is thread creation rather than fork, since init_numa_balancing() returns early for a new address space and only reaches task_scan_max() for a CLONE_VM child. A multiple of 2^24 (with 4K pages) is the second kind. MB_TO_PAGES() shifts an unsigned int, so the result wraps to zero and task_nr_scan_windows() divides by that instead: # echo 16777216 > /sys/kernel/debug/sched/numa_balancing/scan_size_mb Oops: divide error: 0000 [#1] SMP PTI RIP: 0010:task_nr_scan_windows.isra.0+0x5c/0x70 The move to debugfs kept validation where a bad value was harmful: tunable_scaling gained sched_scaling_fops, so an out-of-range write there returns -EINVAL rather than being stored. scan_size_mb went to a plain debugfs_create_u32() instead, and the .procname =3D "numa_balancing_scan_size_mb", .extra1 =3D SYSCTL_ONE, it used to have was not carried over, so zero became storable. No upper bound was ever enforced, so the wrap predates that commit. Follow sched_scaling_fops and refuse both instead of storing them. The knob is described in Documentation/scheduler/sched-debug.rst as one of the files controlling the scan rate, so returning -EINVAL is friendlier than quietly substituting a different value. Checked in a 2-node qemu guest: on an unpatched 7.2.0-rc6 the two writes above panic at the two RIPs shown, and with this patch writes of 0, 2^24 and 3*2^24 all get -EINVAL while a write of 512 still takes effect. Fixes: 8a99b6833c88 ("sched: Move SCHED_DEBUG sysctl to debugfs") Signed-off-by: Zhan Xusheng Reviewed-by: Chen Yu --- kernel/sched/debug.c | 51 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/kernel/sched/debug.c b/kernel/sched/debug.c index 40584b27ea0c..e09fe94e7b9c 100644 --- a/kernel/sched/debug.c +++ b/kernel/sched/debug.c @@ -210,6 +210,55 @@ static const struct file_operations sched_scaling_fops= =3D { .release =3D single_release, }; =20 +#ifdef CONFIG_NUMA_BALANCING +/* + * scan_size_mb is fed to MB_TO_PAGES() and then used as a divisor by + * task_nr_scan_windows() and task_scan_min(). Zero divides by zero, and a + * value big enough to overflow the 32-bit shift in MB_TO_PAGES() wraps to + * zero and does the same, so refuse both rather than store them. + */ +#define NUMA_SCAN_SIZE_MB_MAX (UINT_MAX >> (20 - PAGE_SHIFT)) + +static ssize_t sched_numa_scan_size_write(struct file *filp, + const char __user *ubuf, + size_t cnt, loff_t *ppos) +{ + unsigned int mb; + int ret; + + ret =3D kstrtouint_from_user(ubuf, cnt, 10, &mb); + if (ret) + return ret; + + if (!mb || mb > NUMA_SCAN_SIZE_MB_MAX) + return -EINVAL; + + sysctl_numa_balancing_scan_size =3D mb; + + *ppos +=3D cnt; + return cnt; +} + +static int sched_numa_scan_size_show(struct seq_file *m, void *v) +{ + seq_printf(m, "%u\n", sysctl_numa_balancing_scan_size); + return 0; +} + +static int sched_numa_scan_size_open(struct inode *inode, struct file *fil= p) +{ + return single_open(filp, sched_numa_scan_size_show, NULL); +} + +static const struct file_operations sched_numa_scan_size_fops =3D { + .open =3D sched_numa_scan_size_open, + .write =3D sched_numa_scan_size_write, + .read =3D seq_read, + .llseek =3D seq_lseek, + .release =3D single_release, +}; +#endif /* CONFIG_NUMA_BALANCING */ + #ifdef CONFIG_SCHED_CACHE static ssize_t sched_cache_enable_write(struct file *filp, const char __user *ubuf, @@ -664,7 +713,7 @@ static __init int sched_init_debug(void) debugfs_create_u32("scan_delay_ms", 0644, numa, &sysctl_numa_balancing_sc= an_delay); debugfs_create_u32("scan_period_min_ms", 0644, numa, &sysctl_numa_balanci= ng_scan_period_min); debugfs_create_u32("scan_period_max_ms", 0644, numa, &sysctl_numa_balanci= ng_scan_period_max); - debugfs_create_u32("scan_size_mb", 0644, numa, &sysctl_numa_balancing_sca= n_size); + debugfs_create_file("scan_size_mb", 0644, numa, NULL, &sched_numa_scan_si= ze_fops); debugfs_create_u32("hot_threshold_ms", 0644, numa, &sysctl_numa_balancing= _hot_threshold); #endif /* CONFIG_NUMA_BALANCING */ =20 --=20 2.43.0