From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) (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 951AF1A4F09 for ; Mon, 30 Dec 2024 09:56:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735552608; cv=none; b=mUdbkoqVDagacUU+fji3i00crhFNxXW0iXDHCqkqP/gUziXcG6U7Axmvxce8OTpNNz24FyY/gm67kYny7Lv+06/SwGDH98Z+BTfzQsoM2Vanja8zXSlVUkvoTxy/SaHf1gS6JIVkRu8PtTmywhBhHDSMyzewf90Cv18EyVOyMaE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735552608; c=relaxed/simple; bh=x8H6DVoIMlNDpp4A25ArxzyiMF448S9aPxeDVLI8SLU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=CJ2uLmgUD1yTPb6CqJMnffDkKxHVZoEcUDMb7Je+qtzNYAmLmN6O4vps7D/kkogUH27f7k0ySA/whTW0z+IoriTVG9TQr/V6DlZOvcloEGS8vFQl0giBKAK0ew2Urkpr4nWMqJomA3ydP/HqQNQYEBD2zaliHSk7OEmzn+lXlYY= 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=PmlW4jID; arc=none smtp.client-ip=209.85.214.172 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="PmlW4jID" Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-2161eb95317so119850725ad.1 for ; Mon, 30 Dec 2024 01:56:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1735552606; x=1736157406; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=rmZA4vQufn+h6OnUPeuDpOx8Poi6ZPcPUwnKJ0yEQjk=; b=PmlW4jIDTNB8sYC27OBJwt1M+E2jq0N9vA4ymI7CFusKB6V3eZ3Tdfe/jJdc3zRcjy jSVMz53f7wy0GtLQAclGOoeQoZ5POCaOZixfr0TGxe1UnZlpTHFTZjH2TSAsRS7cCOhq 1Kg5A9dY9BZIoXbv7FwGHjeuLV0upqLGuhbBoLStPFIc6jrMtXay5R6CrfPYneggPJ5T QJpaLZgMcJbM92SDHSNgckJRXTqcXjco6enYTlC/j7XWu9qqUjogNg1jfJsGeLv/KJow AxbS64yKfcVxG0WjbJp9JTSuKt32fFIcCNYEBiNi8twQtx9Wxv8vfiQUT+Jsvp2nSBem 9HTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735552606; x=1736157406; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=rmZA4vQufn+h6OnUPeuDpOx8Poi6ZPcPUwnKJ0yEQjk=; b=L0GXmYSW6rRAt4v3Lgl2140OY6IDqMcMF2DKQk+yodxjVqM5ylqOa6yomxGkGP3jiP xuuksbLfgdarqeKT5f1gfaDaMg0Cg7JRgd07jlNhAXvxgpNU4qPHAhrnd1Zbkbysia3t veOXwdXyHJK/EOEfD+v2jf4Y98TXYWIl8ZoNan++Bz35nZfed+bGl+fwGNrkqvGg2jpT CbZ3N8N/OvcfbiaQg3xVtA7lXLCDGMPuCgQry20u0zrueapk0EUYmPmWqYzNxCbBwp3W 9OGVtlHL9ddVm4iy88Jbu9CDveFRafmqQVg7bAhwLGhuKZMRFX3YrHG1jpuENPD333pq BDfw== X-Forwarded-Encrypted: i=1; AJvYcCUFdGHdEIEFgokvvc51H9sNhJsF9IPSjvLNUlpu2+glXd4D8xgNX4X23Lwb36knhZr9OLnocjQN0r0WHic=@vger.kernel.org X-Gm-Message-State: AOJu0YwvDx85S6HK7DiPawaXuUo2/wJvh48zFZdTkbjgFoYrh2GrqD5o x9hzyjml6f7j5xpA3NSKb+kKETKVnU0vlB1Zitb1cVX3yoFkDF677qHEXw== X-Gm-Gg: ASbGncvoFyRKLhlovr+elyE8ebo/OLYcn1iZvgsV69PBe+1Z4Z/OsRSv0DDmvk8ygWJ sYCcVHUDCiTi5oVKYnTppBuurOtH9zhO1MCJTJ3VMkcc7HfwPjsCDoVjdZ3mgvVuIBGR0cPiVNm xSL1fKDOj4QblP6dZaC5v1ioJnZyWyK3xULnho74qosCD0Ckg++DZJidtnicwISu3kDyjfnkey5 fRxvmpbiriycahCA0xIr59AC67qtryStII1C8yrl24lI/urbKTkF572NHCi X-Google-Smtp-Source: AGHT+IG6q4klahbK+NORht++i7GfPZ9QazKnUpxFF1PsCwFxgtUJlZpFhvYeDljqSfvjWw7SMSoy5w== X-Received: by 2002:a17:903:2310:b0:215:e685:fa25 with SMTP id d9443c01a7336-219e6e9fa7fmr438056645ad.20.1735552605834; Mon, 30 Dec 2024 01:56:45 -0800 (PST) Received: from localhost ([58.29.143.236]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-219dc972b34sm175991835ad.81.2024.12.30.01.56.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Dec 2024 01:56:45 -0800 (PST) From: Changwoo Min X-Google-Original-From: Changwoo Min To: tj@kernel.org, void@manifault.com, arighi@nvidia.com, mingo@redhat.com, peterz@infradead.org Cc: changwoo@igalia.com, kernel-dev@igalia.com, linux-kernel@vger.kernel.org Subject: [PATCH v7 6/6] sched_ext: Use time helpers in BPF schedulers Date: Mon, 30 Dec 2024 18:56:25 +0900 Message-ID: <20241230095625.114363-7-changwoo@igalia.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20241230095625.114363-1-changwoo@igalia.com> References: <20241230095625.114363-1-changwoo@igalia.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Modify the BPF schedulers to use time helpers defined in common.bpf.h Signed-off-by: Changwoo Min --- tools/sched_ext/scx_central.bpf.c | 7 +------ tools/sched_ext/scx_flatcg.bpf.c | 21 ++++++++------------- tools/sched_ext/scx_simple.bpf.c | 9 ++------- 3 files changed, 11 insertions(+), 26 deletions(-) diff --git a/tools/sched_ext/scx_central.bpf.c b/tools/sched_ext/scx_central.bpf.c index 4239034ad593..50bc1737c167 100644 --- a/tools/sched_ext/scx_central.bpf.c +++ b/tools/sched_ext/scx_central.bpf.c @@ -87,11 +87,6 @@ struct { __type(value, struct central_timer); } central_timer SEC(".maps"); -static bool vtime_before(u64 a, u64 b) -{ - return (s64)(a - b) < 0; -} - s32 BPF_STRUCT_OPS(central_select_cpu, struct task_struct *p, s32 prev_cpu, u64 wake_flags) { @@ -279,7 +274,7 @@ static int central_timerfn(void *map, int *key, struct bpf_timer *timer) /* kick iff the current one exhausted its slice */ started_at = ARRAY_ELEM_PTR(cpu_started_at, cpu, nr_cpu_ids); if (started_at && *started_at && - vtime_before(now, *started_at + slice_ns)) + time_before(now, *started_at + slice_ns)) continue; /* and there's something pending */ diff --git a/tools/sched_ext/scx_flatcg.bpf.c b/tools/sched_ext/scx_flatcg.bpf.c index 5f588963fb2f..2c720e3ecad5 100644 --- a/tools/sched_ext/scx_flatcg.bpf.c +++ b/tools/sched_ext/scx_flatcg.bpf.c @@ -137,11 +137,6 @@ static u64 div_round_up(u64 dividend, u64 divisor) return (dividend + divisor - 1) / divisor; } -static bool vtime_before(u64 a, u64 b) -{ - return (s64)(a - b) < 0; -} - static bool cgv_node_less(struct bpf_rb_node *a, const struct bpf_rb_node *b) { struct cgv_node *cgc_a, *cgc_b; @@ -271,7 +266,7 @@ static void cgrp_cap_budget(struct cgv_node *cgv_node, struct fcg_cgrp_ctx *cgc) */ max_budget = (cgrp_slice_ns * nr_cpus * cgc->hweight) / (2 * FCG_HWEIGHT_ONE); - if (vtime_before(cvtime, cvtime_now - max_budget)) + if (time_before(cvtime, cvtime_now - max_budget)) cvtime = cvtime_now - max_budget; cgv_node->cvtime = cvtime; @@ -401,7 +396,7 @@ void BPF_STRUCT_OPS(fcg_enqueue, struct task_struct *p, u64 enq_flags) * Limit the amount of budget that an idling task can accumulate * to one slice. */ - if (vtime_before(tvtime, cgc->tvtime_now - SCX_SLICE_DFL)) + if (time_before(tvtime, cgc->tvtime_now - SCX_SLICE_DFL)) tvtime = cgc->tvtime_now - SCX_SLICE_DFL; scx_bpf_dsq_insert_vtime(p, cgrp->kn->id, SCX_SLICE_DFL, @@ -535,7 +530,7 @@ void BPF_STRUCT_OPS(fcg_running, struct task_struct *p) * from multiple CPUs and thus racy. Any error should be * contained and temporary. Let's just live with it. */ - if (vtime_before(cgc->tvtime_now, p->scx.dsq_vtime)) + if (time_before(cgc->tvtime_now, p->scx.dsq_vtime)) cgc->tvtime_now = p->scx.dsq_vtime; } bpf_cgroup_release(cgrp); @@ -645,7 +640,7 @@ static bool try_pick_next_cgroup(u64 *cgidp) cgv_node = container_of(rb_node, struct cgv_node, rb_node); cgid = cgv_node->cgid; - if (vtime_before(cvtime_now, cgv_node->cvtime)) + if (time_before(cvtime_now, cgv_node->cvtime)) cvtime_now = cgv_node->cvtime; /* @@ -744,7 +739,7 @@ void BPF_STRUCT_OPS(fcg_dispatch, s32 cpu, struct task_struct *prev) if (!cpuc->cur_cgid) goto pick_next_cgroup; - if (vtime_before(now, cpuc->cur_at + cgrp_slice_ns)) { + if (time_before(now, cpuc->cur_at + cgrp_slice_ns)) { if (scx_bpf_dsq_move_to_local(cpuc->cur_cgid)) { stat_inc(FCG_STAT_CNS_KEEP); return; @@ -920,14 +915,14 @@ void BPF_STRUCT_OPS(fcg_cgroup_move, struct task_struct *p, struct cgroup *from, struct cgroup *to) { struct fcg_cgrp_ctx *from_cgc, *to_cgc; - s64 vtime_delta; + s64 delta; /* find_cgrp_ctx() triggers scx_ops_error() on lookup failures */ if (!(from_cgc = find_cgrp_ctx(from)) || !(to_cgc = find_cgrp_ctx(to))) return; - vtime_delta = p->scx.dsq_vtime - from_cgc->tvtime_now; - p->scx.dsq_vtime = to_cgc->tvtime_now + vtime_delta; + delta = time_delta(p->scx.dsq_vtime, from_cgc->tvtime_now); + p->scx.dsq_vtime = to_cgc->tvtime_now + delta; } s32 BPF_STRUCT_OPS_SLEEPABLE(fcg_init) diff --git a/tools/sched_ext/scx_simple.bpf.c b/tools/sched_ext/scx_simple.bpf.c index 31f915b286c6..e6de99dba7db 100644 --- a/tools/sched_ext/scx_simple.bpf.c +++ b/tools/sched_ext/scx_simple.bpf.c @@ -52,11 +52,6 @@ static void stat_inc(u32 idx) (*cnt_p)++; } -static inline bool vtime_before(u64 a, u64 b) -{ - return (s64)(a - b) < 0; -} - s32 BPF_STRUCT_OPS(simple_select_cpu, struct task_struct *p, s32 prev_cpu, u64 wake_flags) { bool is_idle = false; @@ -84,7 +79,7 @@ void BPF_STRUCT_OPS(simple_enqueue, struct task_struct *p, u64 enq_flags) * Limit the amount of budget that an idling task can accumulate * to one slice. */ - if (vtime_before(vtime, vtime_now - SCX_SLICE_DFL)) + if (time_before(vtime, vtime_now - SCX_SLICE_DFL)) vtime = vtime_now - SCX_SLICE_DFL; scx_bpf_dsq_insert_vtime(p, SHARED_DSQ, SCX_SLICE_DFL, vtime, @@ -108,7 +103,7 @@ void BPF_STRUCT_OPS(simple_running, struct task_struct *p) * thus racy. Any error should be contained and temporary. Let's just * live with it. */ - if (vtime_before(vtime_now, p->scx.dsq_vtime)) + if (time_before(vtime_now, p->scx.dsq_vtime)) vtime_now = p->scx.dsq_vtime; } -- 2.47.1