From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from pdx-out-003.esa.us-west-2.outbound.mail-perimeter.amazon.com (pdx-out-003.esa.us-west-2.outbound.mail-perimeter.amazon.com [44.246.68.102]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AB03818050 for ; Mon, 25 May 2026 19:37:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=44.246.68.102 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779737843; cv=none; b=dQJG3CPz3TCEFbBaai2L/gNSl6qV/yBg6GZqSvqCFfw8su9M7Ndqdmcxns349/Eq3loxleUnPvsSDMU9Zdre1haDprbTfvpeOqvCmWYxvtq+Yz4y8zqQUk+tinJGUr6QIul5fpH/wUJvWQbyzpnfSlPqpxdn9hhGtaO5wfcKqsk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779737843; c=relaxed/simple; bh=H5+dngu4A/VNWEuJ6M+Cmq67XZKZIiuFiubc4dGamgg=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=dfODK6VdwhqUUbvlCDBUzhWGST0FVTOu/dzP6BxIf5xK82a2pebVo2ynKnTLKSpwXS7+7niO8oycYFNU/d9uhK1Ji820az51soZG8MafmYF7UKEnvEEpvKU/5rMVeW1cpTtKr2naX6WYvBOxzSZATj4bUmH3p9zapz+GSDk1LOo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com; spf=pass smtp.mailfrom=amazon.com; dkim=pass (2048-bit key) header.d=amazon.com header.i=@amazon.com header.b=OprrRBxb; arc=none smtp.client-ip=44.246.68.102 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=amazon.com header.i=@amazon.com header.b="OprrRBxb" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazoncorp2; t=1779737841; x=1811273841; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=qf114ivLzRcC55l5gllRol+HgRt1cswU4/yJKKjssGI=; b=OprrRBxbqeFWdytMPmOTHM4VSYaIcpQF3ODk02IyE2fkOVGMnvs3DBEX WSDKdcIMW3V4bnlGKFq5JHMRRJMkZ+mIagm97YxKIZOAhU7zARfV65gqk r9I/7R7NtOpfRIKYBCfLesRm3VSGasaQOSfOtlh2Xj9MlQxT5q3vVnVAS hSnusUS69QESfgcRjtm2pjSl8MJvBhR2mOvEWXRQarx8q4ez8xU2Maota UhaB5IAuMPRUEkUNoaKD3RLgV/tahxnTHcD86vCdYuUrtF/OYG2tvc3gk h8O7aXXR+KEy6jBBBEQS5+N2zUkm93maNBDEzXIAH//Rtfut6GajWHsYz Q==; X-CSE-ConnectionGUID: Zj0Ln1VIT/2qDCqnFqwpRQ== X-CSE-MsgGUID: ptsA4z8qSLmtytzotrWyag== X-IronPort-AV: E=Sophos;i="6.24,168,1774310400"; d="scan'208";a="20452615" Received: from ip-10-5-12-219.us-west-2.compute.internal (HELO smtpout.naws.us-west-2.prod.farcaster.email.amazon.dev) ([10.5.12.219]) by internal-pdx-out-003.esa.us-west-2.outbound.mail-perimeter.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 May 2026 19:37:18 +0000 Received: from EX19MTAUWB002.ant.amazon.com [205.251.233.111:8059] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.48.135:2525] with esmtp (Farcaster) id 9f5e6316-895d-40f4-8143-6a58146c79a2; Mon, 25 May 2026 19:37:18 +0000 (UTC) X-Farcaster-Flow-ID: 9f5e6316-895d-40f4-8143-6a58146c79a2 Received: from EX19D001UWA001.ant.amazon.com (10.13.138.214) by EX19MTAUWB002.ant.amazon.com (10.250.64.231) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.37; Mon, 25 May 2026 19:37:18 +0000 Received: from dev-dsk-sieberf-metal-1a-7543e84d.eu-west-1.amazon.com (172.19.116.227) by EX19D001UWA001.ant.amazon.com (10.13.138.214) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.37; Mon, 25 May 2026 19:37:15 +0000 From: Fernand Sieber To: Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot CC: Tejun Heo , David Vernet , Andrea Righi , Changwoo Min , Dietmar Eggemann , Ben Segall , Mel Gorman , , , Fahad Mubeen , "Hendrik Borghorst" , David Woodhouse , Fernand Sieber Subject: [PATCH 1/2] sched/fair: expose cpu.max.runtime to set bandwidth runtime directly Date: Mon, 25 May 2026 21:36:21 +0200 Message-ID: <20260525193622.70282-2-sieberf@amazon.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260525193622.70282-1-sieberf@amazon.com> References: <20260525193622.70282-1-sieberf@amazon.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: EX19D038UWC002.ant.amazon.com (10.13.139.238) To EX19D001UWA001.ant.amazon.com (10.13.138.214) Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Add a cpu.max.runtime cgroup v2 interface that allows userspace to set the CFS bandwidth controller's runtime directly. This enables CPU credit injection: an orchestrator writes a runtime budget which the cgroup consumes naturally through the existing bandwidth enforcement mechanism. The write sets cfs_b->runtime directly. Each period, the task consumes runtime and the refill restores only quota (capped at quota + burst), so the injected credits drain until runtime falls below the cap, after which the cgroup returns to its steady-state quota allocation. Writes are rejected if the value exceeds quota + burst (the per-period runtime cap) or exceeds the maximum bandwidth limit. Also relax the burst validation: remove the burst <= quota constraint, requiring only that burst + quota does not overflow. This allows configuring burst > quota so that the runtime cap (quota + burst) can reach up to one full period, enabling 100% utilization while credits last. The interface uses microseconds, consistent with cpu.max quota/period. Signed-off-by: Fernand Sieber --- kernel/sched/core.c | 44 +++++++++++++++- tools/testing/selftests/cgroup/test_cpu.c | 62 +++++++++++++++++++++++ 2 files changed, 104 insertions(+), 2 deletions(-) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index b8871449d..d92e5840b 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -10085,8 +10085,7 @@ static int tg_set_bandwidth(struct task_group *tg, if (quota_us != RUNTIME_INF && quota_us > max_bw_runtime_us) return -EINVAL; - if (quota_us != RUNTIME_INF && (burst_us > quota_us || - burst_us + quota_us > max_bw_runtime_us)) + if (quota_us != RUNTIME_INF && (burst_us + quota_us > max_bw_runtime_us)) return -EINVAL; #ifdef CONFIG_CFS_BANDWIDTH @@ -10147,6 +10146,41 @@ static int cpu_burst_write_u64(struct cgroup_subsys_state *css, tg_bandwidth(tg, &period_us, "a_us, NULL); return tg_set_bandwidth(tg, period_us, quota_us, burst_us); } + +static int cpu_runtime_write_u64(struct cgroup_subsys_state *css, + struct cftype *cftype, u64 runtime_us) +{ + struct task_group *tg = css_tg(css); + struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth; + + if (runtime_us > max_bw_runtime_us) + return -EINVAL; + + raw_spin_lock_irq(&cfs_b->lock); + if (cfs_b->quota != RUNTIME_INF && + (u64)runtime_us * NSEC_PER_USEC > cfs_b->quota + cfs_b->burst) { + raw_spin_unlock_irq(&cfs_b->lock); + return -EINVAL; + } + cfs_b->runtime = (u64)runtime_us * NSEC_PER_USEC; + raw_spin_unlock_irq(&cfs_b->lock); + + return 0; +} + +static u64 cpu_runtime_read_u64(struct cgroup_subsys_state *css, + struct cftype *cftype) +{ + struct task_group *tg = css_tg(css); + struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth; + u64 runtime_ns; + + raw_spin_lock_irq(&cfs_b->lock); + runtime_ns = cfs_b->runtime; + raw_spin_unlock_irq(&cfs_b->lock); + + return runtime_ns / NSEC_PER_USEC; +} #endif /* CONFIG_GROUP_SCHED_BANDWIDTH */ #ifdef CONFIG_RT_GROUP_SCHED @@ -10498,6 +10532,12 @@ static struct cftype cpu_files[] = { .read_u64 = cpu_burst_read_u64, .write_u64 = cpu_burst_write_u64, }, + { + .name = "max.runtime", + .flags = CFTYPE_NOT_ON_ROOT, + .read_u64 = cpu_runtime_read_u64, + .write_u64 = cpu_runtime_write_u64, + }, #endif /* CONFIG_CFS_BANDWIDTH */ #ifdef CONFIG_UCLAMP_TASK_GROUP { diff --git a/tools/testing/selftests/cgroup/test_cpu.c b/tools/testing/selftests/cgroup/test_cpu.c index c83f05438..df151702b 100644 --- a/tools/testing/selftests/cgroup/test_cpu.c +++ b/tools/testing/selftests/cgroup/test_cpu.c @@ -776,6 +776,67 @@ static int test_cpucg_max_nested(const char *root) return ret; } +static int test_cpucg_max_runtime(const char *root) +{ + int ret = KSFT_FAIL; + long quota_usec = 1000; /* 1ms (minimum) */ + long period_usec = 100000; /* 100ms */ + long burst_usec = 5000000; /* 5s, so cap = 5001ms */ + long runtime_usec = 2500000; /* 2500ms = half of 5s run */ + long duration_sec = 5; + long expected_usec = duration_sec * USEC_PER_SEC / 2; /* 50% */ + long usage_usec; + char *cpucg; + char buf[64]; + int pid; + + cpucg = cg_name(root, "cpucg_runtime_test"); + if (!cpucg) + goto cleanup; + + if (cg_create(cpucg)) + goto cleanup; + + snprintf(buf, sizeof(buf), "%ld %ld", quota_usec, period_usec); + if (cg_write(cpucg, "cpu.max", buf)) + goto cleanup; + if (cg_write_numeric(cpucg, "cpu.max.burst", burst_usec)) + goto cleanup; + + /* Start burner, let it settle, then inject credits */ + struct cpu_hog_func_param param = { + .nprocs = 1, + .ts = { .tv_sec = duration_sec, .tv_nsec = 0 }, + .clock_type = CPU_HOG_CLOCK_WALL, + }; + pid = cg_run_nowait(cpucg, hog_cpus_timed, (void *)¶m); + if (pid < 0) + goto cleanup; + + usleep(100000); + if (cg_write_numeric(cpucg, "cpu.max.runtime", runtime_usec)) { + kill(pid, SIGKILL); + waitpid(pid, NULL, 0); + goto cleanup; + } + + waitpid(pid, NULL, 0); + + usage_usec = cg_read_key_long(cpucg, "cpu.stat", "usage_usec"); + if (usage_usec <= 0) + goto cleanup; + + if (!values_close_report(usage_usec, expected_usec, 10)) + goto cleanup; + + ret = KSFT_PASS; + +cleanup: + cg_destroy(cpucg); + free(cpucg); + return ret; +} + #define T(x) { x, #x } struct cpucg_test { int (*fn)(const char *root); @@ -790,6 +851,7 @@ struct cpucg_test { T(test_cpucg_nested_weight_underprovisioned), T(test_cpucg_max), T(test_cpucg_max_nested), + T(test_cpucg_max_runtime), }; #undef T -- 2.47.3 Amazon Development Centre (South Africa) (Proprietary) Limited 29 Gogosoa Street, Observatory, Cape Town, Western Cape, 7925, South Africa Registration Number: 2004 / 034463 / 07