From nobody Tue Sep 29 08:26:17 2026 Received: from mail-pj1-f54.google.com (mail-pj1-f54.google.com [209.85.216.54]) (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 E21123F9F25 for ; Mon, 10 Aug 2026 14:20:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786371661; cv=none; b=a/GXnyp2CBZjdpe2y1nSjU0ViObk790zKYwjMhPof3uNtzNToCAbW/Gb7E+d2xouur1O/sw0Gst+n6ebfaBQj6wrfIkuEcXBIvk5HkgjOFHKAiiWsVZ2AAnwifbpYFPP5Wk7x5N69yK3ZUYhE+c9IxNS55GYXn7EuzzIperryso= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786371661; c=relaxed/simple; bh=5VLk4Jyt3q+VMHsh/W9vUlsqNhjRby+cy624OgzYnTQ=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=Ra264VrqTa7/o1GqbstPAuNS2L/SphZsUCjfAW3h4p7HquexdbVqwLyTt6zynHd3cAU28iZEwakpspduHpGY3RD548UZv1+8hIMUnUbqMwRGN9cHBPUJm3klLiHKED+VqptR+O+/Rgfd4or2Pm7Mi3SoPgFS7bgxQV4vhT+dniU= 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=L3Zzzlzf; arc=none smtp.client-ip=209.85.216.54 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="L3Zzzlzf" Received: by mail-pj1-f54.google.com with SMTP id 98e67ed59e1d1-39266382df6so2040666a91.3 for ; Mon, 10 Aug 2026 07:20:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1786371659; x=1786976459; 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=V+ntJ4+egBWM7EeiTvcsYvwuOLGC1330DIGlpAGxXpE=; b=L3ZzzlzfP+/d9i9jomFf83qzagoV/complIHg2C2CkvR2+KOcxq82WFqtTm2j9TErb 3pd1dGfShxPHNOHDwm26DGaYTTXqbghSvEiLlwPY1OyaI6lhSgfrykxc0D2ezO6T9gSc f+OaxYTlpQ9Il80SV+8rJcclncT5VTiodvYf6rFyQuf85DjcdASaRvLiE62fM4zNBjne +XxH6+wSqrgj9tozpm3yn8W++/AOLsn/XH1xDt67hwYd5LEZuixU422I7h7iUSOxrTnJ 9Vs0LZYVsOm8aSm9j0XS/wTPA2gRvbGrlBaDt9FzD9aaivYrr8ryy6a+R4tlkBVkztT3 ea7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786371659; x=1786976459; 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=V+ntJ4+egBWM7EeiTvcsYvwuOLGC1330DIGlpAGxXpE=; b=SMgaLiK9xWmMOe62XxnZmubCoqlg3MdGb5cirHKKD5PI4zUvHr05Cf9gyzzGOEfhzT KYx3PfT3/aS1g147tJBVN3CPDxr8GVUUi7U/gmgmM19uaCsVUKTd3PSGnvlxzLQa3MWG 9ro3EmQg5Y92DTlWxoq3t6t27cRSBqMidv9yMTWrmJD/zsYwfIEA+aGPhHPKTzvSBoNM sPqvme+Y0tl23o8NXQsok/5KVE4L+UhlS/OlqSi1w/OfJ10/h5/Z6lIiyDPht4VUvL+/ 0ePy1AnMXwXWrQi4oWhgWPgE+ugYVT0n3yFg3XHCfA78XYvyAGcmSTitOrL9QcHPvgGg aVOw== X-Forwarded-Encrypted: i=1; AHgh+RoNquSrdifVMNZtdrmCqE+1/EuxM5SDef8ZdzBR0CGvEB81UHKrBC0CNKRqc9jAlV07ex7x9hXRXPTh5Eo=@vger.kernel.org X-Gm-Message-State: AOJu0YwSWPN12voQeLYFNUU2pRDmByV/V2dWStBojw9XjFq63ukJRWhD C+BHdAtGF3x4p5LhvCxNuOcLK2dg7UfPswczs1KRUCuyQWcRo3I+N+4nGMAWmvNq X-Gm-Gg: AR+sD11P+gMDApfJUqe/pMHdiS/XTrIg8fafeT/aywfXaC+WmBMeaLwhivX2SDVNFlW SY/qLuHyBhv/R91U9060i3eoTEURkjmFV2vDOdXM6L2dmf8fsoWDh9zVS5BpdWtHu+KF5YRKPVM 26eby8oTFshm7IfFW2Y6z720w8GBKg4pQnmLXOkTsslM2Kg3Nixxip+alEKr403p3xxZFZAUxbK JNgIjvsqQ45xGjnQzyo3U0bf39IX9HD6KA5j6dJHoHxk9yVFYAAZUGNfHg2LWkg2F+Gbg5Lp4aV dj8/QQnvdrED/1vXTYQnQ7LK3w0+xJ4SNPa6Uk3AugQArX3M/EVSwu8MzbaTWLm1QRoMApe5YZ2 Z92hwv+JIgctfktmb6KbVi50SJSt3RYMXgl4InsCb0K3OkAB99qrcHHaafE3aTn3vYQuYBNk4Cm zN2zjBgzTTuyr3JJEbvWqQN3m0BwhIU+W/7zKpKKqwvE7sp9G0gnFUlMfoq1owV70svi+itGfW X-Received: by 2002:a17:90b:4d84:b0:38e:6bd7:fbbf with SMTP id 98e67ed59e1d1-3903c697453mr38197592a91.20.1786371659186; Mon, 10 Aug 2026 07:20:59 -0700 (PDT) Received: from osman.mioffice.cn ([43.224.245.178]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3925ff69eabsm13014310a91.15.2026.08.10.07.20.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Aug 2026 07:20:58 -0700 (PDT) From: Zhan Xusheng X-Google-Original-From: Zhan Xusheng To: Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot Cc: Chen Yu , Mel Gorman , Dietmar Eggemann , Steven Rostedt , Ben Segall , Valentin Schneider , K Prateek Nayak , zhanxusheng@xiaomi.com, linux-kernel@vger.kernel.org Subject: [PATCH v2] sched/debug: Validate writes to the scan_size_mb debugfs knob Date: Mon, 10 Aug 2026 22:20:50 +0800 Message-ID: <20260810142050.3828587-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" scan_size_mb ends up as a divisor and debugfs_create_u32() stores whatever is written, so two values panic the kernel. Zero, through MAX_SCAN_WINDOW / scan_size in task_scan_min(): # 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 task_scan_min() is inlined into task_scan_max() there. It takes a CLONE_VM child rather than a fork, since init_numa_balancing() returns before that call for a new address space. And a multiple of 2^24 with 4K pages: MB_TO_PAGES() shifts an unsigned int, so it wraps to zero and task_nr_scan_windows() divides by that. # echo 16777216 > /sys/kernel/debug/sched/numa_balancing/scan_size_mb RIP: 0010:task_nr_scan_windows.isra.0+0x5c/0x70 The sysctl this knob replaced enforced the lower bound with .extra1 =3D SYSCTL_ONE. The move to debugfs kept validation for tunable_scaling, which got sched_scaling_fops, but scan_size_mb became a plain u32 and lost it. The upper bound was never enforced. Reject both the way sched_scaling_fops does. scan_size_mb is documented in Documentation/scheduler/sched-debug.rst, so -EINVAL is better than storing a value that panics on the next clone. Hex input keeps working, as simple_attr_write_xsigned() allowed. Verified in a 2-node qemu guest: both writes panic v7.2-rc6 at the RIPs above; with this patch 0, 2^24 and 3*2^24 get -EINVAL and 512 still takes effect. Fixes: 8a99b6833c88 ("sched: Move SCHED_DEBUG sysctl to debugfs") Signed-off-by: Zhan Xusheng Reviewed-by: Chen Yu --- v2: - parse with base 0 so hex writes keep working, matching the kstrtoull(attr->set_buf, 0, &val) in simple_attr_write_xsigned() that debugfs_create_u32() used (Chen Yu) - name that helper precisely in the changelog - Link to v1: https://lore.kernel.org/lkml/20260810081829.3149958-1-zhanxu= sheng@xiaomi.com/ 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..01e74671e439 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 +/* + * task_scan_min() divides MAX_SCAN_WINDOW by this, and + * task_nr_scan_windows() divides by MB_TO_PAGES() of it. Zero breaks the + * first; a value that overflows the 32-bit shift in MB_TO_PAGES() wraps to + * zero and breaks the second. + */ +#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, 0, &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