From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) (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 2DE6C19C55E for ; Mon, 30 Dec 2024 09:56:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.180 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735552592; cv=none; b=OKbol98wYgE4jDpTHrkt3RgGKxcqT2fRLF+nKw0Si73GeShvNuPuvab0SSHmf5pRkwiCELfWevta9vpqHc9Z0RF48N7kqL07Jo/yQLZjNAMrFPfmYkI9Da1gNU1++45qMPjB+BnO+A9lFx8kmw1QEuhKDji1aYxnuWaT3Ua2Sqs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735552592; c=relaxed/simple; bh=MXJ73plPwPLe7MQ/dKOYgShV+LVoa+U3vqwuEKNE+Ok=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=CQBrPH8uo19Z+9uX1qNEvnGMzPuBXr8gu8KuX2mQMR5Koq5R+VDvf0YT1yldOOP+KFUpUwdt6+hmxrt1svM5V1QWzT57BKysLTlCaJFl6pzzWaaIKzZX7C3f3CFK+QfM/RIfuZ0secyG6O5Rs2LsaLZN+4LjIYv5ZuJtlS51924= 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=T+N+4bB2; arc=none smtp.client-ip=209.85.214.180 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="T+N+4bB2" Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-21683192bf9so117061935ad.3 for ; Mon, 30 Dec 2024 01:56:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1735552590; x=1736157390; 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; bh=GjsL3+9ZvZtBgIjHPonpG9lUfDhfGi/tld8nX4V+N6Y=; b=T+N+4bB2WDgwphyh+6ISDFFY5/5XbWR/EQiHXmAD0RRyMCsKhAXzuzECo7ydY+JJuI IxBTW6FacFxDsCQbTkgql0nU+BzIyyF07tN4HS8pgYhdFO5ap4NFrE8RjPTLaDQCIjBw Wy9dTQsmnmZc9SEP1ogbgu+nmQNdZdWfr/dHDXWrOcoiP98BK8U+KgSeT9oa4wOZ5xlC B//T87B+6nIaFZfyVyzUFCNLChuw3d6uw0Zl/4xZMcd1IyWanK8J63ICH1VWZu/b2vpN mFhRxYRt9ptMV7MxXfqTkewlWL4qF0W/4VPgTXyZ7cZSV8QZmUB0z0r0ci6OE6Xd7qUN BDPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735552590; x=1736157390; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=GjsL3+9ZvZtBgIjHPonpG9lUfDhfGi/tld8nX4V+N6Y=; b=WgYGuJksHCL6FpBFbpt23+qwTuIhZkNS6wRyBy97E0f87uaWqKXsMhgoq/fgaiP+Sn lyNP8B3LV467hYPrnfgpH+n+V4UcjIH/PvfxsNbNsVxQxBfpFXGDfpz+nkkG6jLYjoBw Ol0FWf7xwReqdwhUSeKIIFzfsPmE9tQ9ZKazGf0DgS3jRvHr7ihGR+U9dEq1893xd/mb EMh2iBWf/Ip0KYj0yAgAn6tsW3DGg3DK4RjShTcom5C03ZOjaSFPwnXVTrNMJmrzr7i6 H+0i93kOShno5Da1CzlvANoNOwspGSsmkaGVqIZJQV1/D9nKdAA7KW5LoNZzq8M4/W/w RL8A== X-Forwarded-Encrypted: i=1; AJvYcCWTRbUhQW6hXUQwOw6waW3Iy1H037n2sZHvF3zHCkhhHSY/gy08yHpbeuVMURuflyhIS85rCfvTP9Hcqu8=@vger.kernel.org X-Gm-Message-State: AOJu0YyxITL1bdf7HRw7G4wFCFxwOGnVkB2zbmT8UPSgAhvhnEZihpgQ qhw+2niwsgBtsNu81wb2pisWaE/fA/FzZwX+68PNkH2DHIHVuPdK X-Gm-Gg: ASbGncsum9BPTBSp/Kf4dW9LpBEPBDUsjOGbIC2ompjG0jnT+TjtpKXT2GK1BOIS0wd Ekv2lEPaC1BkVqCYVacQ2sxZn39lDmD3+NFz9JKRTNYW6KgAzULaRR30cjyyM1oAbcTfYY6rhtq ltyEO1NvuRHqjCdlr6MoWThpvFhQEEHkX5w+5b3Csxa+W0LlmH3chZeirjxBlAZ92GE55+AtKy8 ptnKoWizNyGWA3u/jBDjPYe2mJr2F8jI/+O5iQNTEPd5zfB/iuUweUDU3Ok X-Google-Smtp-Source: AGHT+IEA3ZNbbLhJa0DTAs556qUCR9XFxT08syubiAi3mHAuZDynsa3mknHdgtaRGoPy1wnKibl5pw== X-Received: by 2002:a17:902:f70f:b0:211:8404:a957 with SMTP id d9443c01a7336-219e70c01a2mr565763705ad.41.1735552590337; Mon, 30 Dec 2024 01:56:30 -0800 (PST) Received: from localhost ([58.29.143.236]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-219dc9cdf02sm175543015ad.151.2024.12.30.01.56.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Dec 2024 01:56:29 -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 0/6] sched_ext: Support high-performance monotonically non-decreasing clock Date: Mon, 30 Dec 2024 18:56:19 +0900 Message-ID: <20241230095625.114363-1-changwoo@igalia.com> X-Mailer: git-send-email 2.47.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Many BPF schedulers (such as scx_central, scx_lavd, scx_rusty, scx_bpfland, and scx_flash) frequently call bpf_ktime_get_ns() for tracking tasks' runtime properties. If supported, bpf_ktime_get_ns() eventually reads a hardware timestamp counter (TSC). However, reading a hardware TSC is not performant in some hardware platforms, degrading IPC. This patchset addresses the performance problem of reading hardware TSC by leveraging the rq clock in the scheduler core, introducing a scx_bpf_now() function for BPF schedulers. Whenever the rq clock is fresh and valid, scx_bpf_now() provides the rq clock, which is already updated by the scheduler core (update_rq_clock), so it can reduce reading the hardware TSC. When the rq lock is released (rq_unpin_lock), the rq clock is invalidated, so a subsequent scx_bpf_now() call gets the fresh sched_clock for the caller. In addition, scx_bpf_now() guarantees the clock is monotonically non-decreasing for the same CPU, so the clock cannot go backward in the same CPU. Using scx_bpf_now() reduces the number of reading hardware TSC by 50-80% (76% for scx_lavd, 82% for scx_bpfland, and 51% for scx_rusty) for the following benchmark: perf bench -f simple sched messaging -t -g 20 -l 6000 ChangeLog v6 -> v7: - Do not update rq_lock when the rq_clock is invalidated. - Rename scx_bpf_now_ns() to scx_bpf_now(). - Rename vtime_*() helper functions to time_*(). - Fix a typo. ChangeLog v5 -> v6: - Drop prev_clock because a race between a process context and an interrupt context (e.g., timer interrupt) is not observable from a caller (discussed with Tejun offline), so it is not necessary to check prev_clock. ChangeLog v4 -> v5: - Merge patches 2, 3, and 4 into one for readability. - Use a time helper (time_after_eq64) for time comparison at scx_bpf_now_ns(). - Do not validate the rq clock outside the rq critical section for more predictable behavior. - Improve the comment at scx_bpf_now_ns() for readability. - Rename scx_rq_clock_stale() into scx_rq_clock_invalidate(). - Invalidate all the rq clocks upon unloading to prevent getting outdated rq clocks from a previous scx scheduler. - Use READ/WRITE_ONCE() when accessing rq->scx.{clock, prev_clock, flags} to properly handle concurrent accesses from an interrupt contex. - Add time helpers for BPF schedulers. - Update the rdtsc reduction numbers in the cover letter with the latest scx_scheds (1.0.7) and the updated scx_bpf_now_ns(). ChangeLog v3 -> v4: - Separate the code relocation related to scx_enabled() into a separate patch. - Remove scx_rq_clock_stale() after (or before) ops.running() and ops.update_idle() calls - Rename scx_bpf_clock_get_ns() into scx_bpf_now_ns() and revise it to address the comments - Move the per-CPU variable holding a prev clock into scx_rq (rq->scx.prev_clock) - Add a comment describing when the clock could go backward in scx_bpf_now_ns() - Rebase the code to the tip of Tejun's sched_ext repo (for-next branch) ChangeLog v2 -> v3: - To avoid unnecessarily modifying cache lines, scx_rq_clock_update() and scx_rq_clock_stale() update the clock and flags only when a sched_ext scheduler is enabled. ChangeLog v1 -> v2: - Rename SCX_RQ_CLK_UPDATED to SCX_RQ_CLK_VALID to denote the validity of an rq clock clearly. - Rearrange the clock and flags fields in struct scx_rq to make sure they are in the same cacheline to minimize the cache misses - Add an additional explanation to the commit message in the 2/5 patch describing when the rq clock will be reused with an example. - Fix typos - Rebase the code to the tip of Tejun's sched_ext repo Changwoo Min (6): sched_ext: Relocate scx_enabled() related code sched_ext: Implement scx_bpf_now() sched_ext: Add scx_bpf_now() for BPF scheduler sched_ext: Add time helpers for BPF schedulers sched_ext: Replace bpf_ktime_get_ns() to scx_bpf_now() sched_ext: Use time helpers in BPF schedulers kernel/sched/core.c | 6 +- kernel/sched/ext.c | 74 +++++++++++++++++- kernel/sched/sched.h | 51 +++++++++---- tools/sched_ext/include/scx/common.bpf.h | 95 ++++++++++++++++++++++++ tools/sched_ext/include/scx/compat.bpf.h | 5 ++ tools/sched_ext/scx_central.bpf.c | 11 +-- tools/sched_ext/scx_flatcg.bpf.c | 23 +++--- tools/sched_ext/scx_simple.bpf.c | 9 +-- 8 files changed, 228 insertions(+), 46 deletions(-) -- 2.47.1