From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) (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 135831411DE for ; Fri, 20 Dec 2024 06:20:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.179 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734675633; cv=none; b=V4zmQtLIM4tmKc+qyh+EKP92b0++5Z8pIl3xOxnxXS1f7hyO8R18BEFRPmVkRxCDUo4Cp7H0SFiz7BiHNCoH6uuog+5z0Ytg0ESoziCBnSJDJ2g5/6Qu0a/avKsVdh2umehj8l2fMTHDj847AArnaKeB7fYv4IdU+gJ9uHPWghQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734675633; c=relaxed/simple; bh=Y0MdXBrLkUVfI3wZBkVBfg4stCx8C3DVqYWS0NtA7Hc=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=DdqCh51HSt9WJM6MJv1KY74vlXX+QQQFB92i9MvMSHZDuhhsXaBeY+PTE6ZfEp7zCxM+LU08RPdAfs7E94OvLl6tYT16Ba5+yM2fi0PlpLgMPgCZeUtyJWiMgWwYyXUbi1X3NAQy/zfGvNe/zCzid+rdabAiPssoAalnBRZf3+w= 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=Gulyj3LE; arc=none smtp.client-ip=209.85.210.179 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="Gulyj3LE" Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-725f4623df7so1514205b3a.2 for ; Thu, 19 Dec 2024 22:20:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1734675631; x=1735280431; 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=A1YOKOFEsDJsbpYfEP5u7z60puUQ1gX2RHvcPrhdK3g=; b=Gulyj3LExLXDuRYKx0t/s8w6agCWv8AZkNYi8sDpxcCaBik+wnWqKcZHj6F+rZAFaY 1iHpbUuvtIRopCLUWlL+nSDO7+P066+sdnDo82c3FIKz4Nyz0pI8ewFnENu9MQPchSJG YxvHepNMk8+0Aqhpk5GMN6Yz0cExtwl12gsIH3Dd7ovFnr920W8T8y2pQ051fjdiWvE1 pq0qY0QdLReoGVUdLME3vU2om8er31rxkAmxmCNPHCKzglwK72sHrIDJXpkRLPV5MFni bpmpQ+VW8vm1VGL20pFUp3wyoD4T10XYElblp21eV3cVIlnlx/ojERWuyfKfkoWhiznO Z/DQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734675631; x=1735280431; 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=A1YOKOFEsDJsbpYfEP5u7z60puUQ1gX2RHvcPrhdK3g=; b=g+4h1Yo7wDTYFQAo/NvDa+xmuVQZnyba7aMhnHNNxYqz7QGteTsUqkk4aupvca9v0J bL2Lk/bhillVJnSSa5KEZZLqFCrJ62U8X8T+xUkhbglKivsVil2kVYb3+1LBI4ROZIsT 9EP6IBqjfDYgZYSYHtsLtwYHBTnPCrIJjK/Mp1MeK+iapKEjDZxMWmJ/cCuqL7Cp/vZb DDTtMBeY35C1iGN5DzNW7rY+N9l1zm1tLBRLLGQ/ZfKPPxIyudR70ywLuTxpCWAqb8R2 UIadx7/qHlKQzF4kAcdUr2a/sla/uMup07gDLzdriUmQ2yF5/9Aehd5poyK5zHXPSF5k 40AQ== X-Forwarded-Encrypted: i=1; AJvYcCUDZCsdGODWNWrxTG4kId6p/2tylpX68Gsci0+EVrsnlO9jZsmdBWg+nYb7KV1mUR1242z0kC+RD/APy7U=@vger.kernel.org X-Gm-Message-State: AOJu0Yx7UDwLSBEUxBaX4XMhknwRuSURI7k+U/QyZHmPqrZBq5ImqR7I hVA7PS0OfnxOC+f0Pk8E0iFuyRNy1AU0FmhWMWNgG6J7+3cibwmEybkJ0g== X-Gm-Gg: ASbGncvSTxSMJ/8Y4mExDajA+1ITQZfGcDBy+uripJcMhbVAP2LNWu5JBEiA/cfarFC Pti9H/L1iWRaCEEe4E+FyvgwUf0vWZZWqiORUBKBx2GiqyHNn0QU4r07/WlFikb6smETrOTOAje Xo6/Z74Ph3Era6wTQrAbHGz4PkFk8SyfU1j6HEG41AjXW7UgEfk9IR/khCbxNWNg49fG10prSrf BXbsOryOWVlIRuOOgYzyaQJGRrV3BTO+wdk85GmuRqcbQp6Q3HXDMu3i1eRMvudyXA3juicTPRr a9sO4qnqqZ0c X-Google-Smtp-Source: AGHT+IFHfsh1Ne74518TJex5Mmt6UO80fz17LSnSfZjmgBvxsDbxZalkkH4ZGPJCGU2qOuDoHdKLhA== X-Received: by 2002:a05:6a21:9017:b0:1e5:ddac:1eff with SMTP id adf61e73a8af0-1e5e04a0c7cmr3313367637.20.1734675631260; Thu, 19 Dec 2024 22:20:31 -0800 (PST) Received: from localhost ([58.29.143.236]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-842aba7319asm2162191a12.2.2024.12.19.22.20.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Dec 2024 22:20:30 -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 v6 0/6] sched_ext: Support high-performance monotonically non-decreasing clock Date: Fri, 20 Dec 2024 15:20:19 +0900 Message-ID: <20241220062025.27724-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_ns() function for BPF schedulers. Whenever the rq clock is fresh and valid, scx_bpf_now_ns() 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_ns() call gets the fresh sched_clock for the caller. In addition, scx_bpf_now_ns() 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_ns() 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 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_ns() sched_ext: Add scx_bpf_now_ns() for BPF scheduler sched_ext: Add time helpers for BPF schedulers sched_ext: Replace bpf_ktime_get_ns() to scx_bpf_now_ns() sched_ext: Use time helpers in BPF schedulers kernel/sched/core.c | 6 +- kernel/sched/ext.c | 75 ++++++++++++++++++- kernel/sched/sched.h | 52 +++++++++---- 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 | 9 +-- tools/sched_ext/scx_flatcg.bpf.c | 13 +--- tools/sched_ext/scx_simple.bpf.c | 5 -- 8 files changed, 222 insertions(+), 38 deletions(-) -- 2.47.1