Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp1202987pxy; Thu, 29 Apr 2021 01:47:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy3a35/ragYu0ZCpU6HyHsZasFwnWhpANd/9esUgEWo8cZp6fsD4yeneB2xi3A1lyCIhVJa X-Received: by 2002:a17:906:b850:: with SMTP id ga16mr21490040ejb.161.1619686030041; Thu, 29 Apr 2021 01:47:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619686030; cv=none; d=google.com; s=arc-20160816; b=UFtVqIMr81WNZBu4dHkx4t4MxqpV14Q+Y5ysEoMzdZLjeJCf6wMuU9SuLXQmr/eY7S 5ysOgV+gUfw0Wxy+33xMyVoqM706Z+4Wu8JUBkxLgw+X14dWcd9C+kLoLfOM8uYnsiDf kYkp4sg7VyNkA4UN2Az+LjkvEfW6N12daS+n//uqLOGzvk6Rd7c9KIctPcQUxA9iVk1k R8GBHNa3dJnv5KvosUxZ1jaeHFwk4f7BiJ9gVWakrsa9H6QojZJuGdpS0cbWDC792Ham EPHJLOnofllc10TtCX3Zs570m4+RXr0BUacdMRfKSFws+g0lIZZq0wkOb5IqisLTUpbW 4ZWQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=jczHCwrsUz4nxGytYAbOYxiwVzhOvP/hHxvtUgEKyfY=; b=DW97O1wb1M3xuzP6fhzUfDc6ISFmhTR21yh3ltzUwGXBMfpwEvNqkW2iA2QVf8Vs24 fRmG22tcr+T1MkWsGTM9/TejU9CvPb/3JvSK7eGX0XhWvyfuvOn53/Bd8YxP49sJPfRG 8CZqcCOwC91mxzDI+xcW8KN98XFVztF1L68/w+WmUw0A2BNdRMaaYMuK+jx//RomimmW iO+GgoMyMxSpBQYHrRsetJWR/QZUfDsjD7emGVgZvtoX+PcjuacZryrs7ZfMGhiUSyPe Cjs9LAuRWlg8J3Uo3MezQM+eSmHAdMw5OW1F4UT7bwHflK1Ed5tUIfe5wnwV0G0EWa+B 6sUg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=gF2qYHrD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id cx25si2293173edb.167.2021.04.29.01.46.46; Thu, 29 Apr 2021 01:47:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=gF2qYHrD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239966AbhD2IqK (ORCPT + 99 others); Thu, 29 Apr 2021 04:46:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48886 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239952AbhD2IqJ (ORCPT ); Thu, 29 Apr 2021 04:46:09 -0400 Received: from mail-qk1-x749.google.com (mail-qk1-x749.google.com [IPv6:2607:f8b0:4864:20::749]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5EE30C06138D for ; Thu, 29 Apr 2021 01:45:21 -0700 (PDT) Received: by mail-qk1-x749.google.com with SMTP id h190-20020a3785c70000b02902e022511825so27298133qkd.7 for ; Thu, 29 Apr 2021 01:45:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=jczHCwrsUz4nxGytYAbOYxiwVzhOvP/hHxvtUgEKyfY=; b=gF2qYHrDn89JrVsHKfB0auMYhxo5tdLoOGPcZ6s8bOXQ0rnJoYf2p8vyqIEDyUOMNx uBMjP2jiho5Djuvtg8p0rVR1hjrI2W/TwZuyWJhKehBBFrpAoSkW2s9/yWyDXpJuXmkH GdplhNz8B6ObULbxmKPzDpiLLkOEsttTrqrxXdrLhyif+LGxqSvgBLeeBfbiPHMheV+n YO1Q002eC6G4L7pVhvJTzbLWa+Y4aRWS+R7wRnb/IVK+9J6VjtDsDkpz1r3mwu3sXIMM dtMOdzWVtGdaKmyMzxMRhDFO8zIsfDUN9IGhjvHzdVAWJOLsGew4m7E1oGPYNywe2HA8 Adug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=jczHCwrsUz4nxGytYAbOYxiwVzhOvP/hHxvtUgEKyfY=; b=NmXGtfOy/vhB4ZoZs8sdlaC7mpkLj6LAw7HE8yqOOUU2xB75L7wQxdsRLVV+cZnuQs IFSDHBh45UGx0M8OdLXq2v/wlxcdmo76gJNKSPjvb9S8jsMG5/4hp4lW/S7RaCWQQXt2 k7W9y6sjvVBezxBayta7nEtQPh7ae96BQijEI8gbyxzPdpAQOJKDa8te4EO5HxbKSTyY Fv3B7kAJ3Y9bwR7ge+QpbCbq+N/kcg3csZw8wPnbMXHIWjCwSsmIMdQcgvSajJWo6JiE +r8O5wYnvloQK1XRnUsBcJbC3tpnmKFOip/XF5R8kg6XreBrbU6qBn+wI7stau3pHOWV f0fQ== X-Gm-Message-State: AOAM531tYV5uWFlzD17GUex5GgbtZxo1Wkh+cxH/miZCEyPDQyFkFmaH 6Qgg1gaPTPnWtsoTTE2UYHYzAQ6CJle3 X-Received: from nandos.syd.corp.google.com ([2401:fa00:9:14:30c1:7a5a:2ec9:29e5]) (user=amistry job=sendgmr) by 2002:ad4:4c11:: with SMTP id bz17mr17371179qvb.42.1619685920462; Thu, 29 Apr 2021 01:45:20 -0700 (PDT) Date: Thu, 29 Apr 2021 18:44:10 +1000 In-Reply-To: <20210429084410.783998-1-amistry@google.com> Message-Id: <20210429184101.RFC.v2.2.I0d44ed3cc06ff2924c1b5e418e0599e1c1731c3c@changeid> Mime-Version: 1.0 References: <20210429084410.783998-1-amistry@google.com> X-Mailer: git-send-email 2.31.1.498.g6c1eba8ee3d-goog Subject: [RFC PATCH v2 2/2] selftests: Benchmark for the cost of disabling IB speculation From: Anand K Mistry To: x86@kernel.org Cc: joelaf@google.com, asteinhauser@google.com, bp@alien8.de, tglx@linutronix.de, Anand K Mistry , Shuah Khan , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This is a simple benchmark for determining the cost of disabling IB speculation. It forks a child process and does a simple ping-pong using pipes between the parent and child. The child process can have IB speculation disabled by running with 'd' as the first argument. The test increases the number of iterations until the iterations take at least 1 second, to minimise noise. This file is NOT intended for inclusion in the kernel source. It is presented here as a patch for reference and for others to replicate results. The binary should be run with 'taskset' and pinned to a single core, since the goal is to benchmark process switching on a single core. Signed-off-by: Anand K Mistry --- (no changes since v1) .../testing/selftests/ib_spec/ib_spec_bench.c | 109 ++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 tools/testing/selftests/ib_spec/ib_spec_bench.c diff --git a/tools/testing/selftests/ib_spec/ib_spec_bench.c b/tools/testing/selftests/ib_spec/ib_spec_bench.c new file mode 100644 index 000000000000..e8eab910a9d0 --- /dev/null +++ b/tools/testing/selftests/ib_spec/ib_spec_bench.c @@ -0,0 +1,109 @@ +#include +#include +#include +#include +#include +#include + +#define PR_SPEC_IBPB_MODE 2 +#define PR_SPEC_IBPB_MODE_DEFAULT 0 +#define PR_SPEC_IBPB_MODE_SANDBOX 1 +#define PR_SPEC_IBPB_MODE_PROTECT 2 + +int64_t get_time_us() { + struct timespec ts = {0}; + assert(clock_gettime(CLOCK_MONOTONIC, &ts) == 0); + return (ts.tv_sec * 1000000) + (ts.tv_nsec/1000); +} + +void pong(int read_fd, int write_fd) { + int ret; + char buf; + + while (1) { + ret = read(read_fd, &buf, 1); + if (ret == 0) + return; + assert(ret == 1); + + assert(write(write_fd, &buf, 1) == 1); + } +} + +void ping_once(int write_fd, int read_fd) { + char buf = 42; + assert(write(write_fd, &buf, 1) == 1); + assert(read(read_fd, &buf, 1) == 1); +} + +int64_t ping_multi(int iters, int write_fd, int read_fd) { + int64_t start_time = get_time_us(); + for (int i = 0; i < iters; i++) + ping_once(write_fd, read_fd); + return get_time_us() - start_time; +} + +void run_test(int write_fd, int read_fd) { + int64_t iters = 1; + int64_t t; + for (int i = 0; i < 60; i++) { + t = ping_multi(iters, write_fd, read_fd); + printf("iters: %d, t: %dus, iter/sec: %d, us/iter: %d\n", + iters, t, (iters * 1000000LL) / t, t/iters); + + if (t > 1000000) + break; + iters <<= 1; + } +} + +int main(int argc, char* argv[]) { + int fds_ping[2], fds_pong[2]; + assert(pipe(fds_ping) == 0); + assert(pipe(fds_pong) == 0); + + int disable_ib = 0; + int spec_ibpb_mode = 0; + + if (argc > 1) { + int done = 0; + for (int i = 0; !done; i++) { + switch (argv[1][i]) { + case 0: + done = 1; + break; + case 'd': + disable_ib = 1; + break; + case 's': + spec_ibpb_mode = PR_SPEC_IBPB_MODE_SANDBOX; + break; + case 'p': + spec_ibpb_mode = PR_SPEC_IBPB_MODE_PROTECT; + break; + } + } + } + + pid_t pid = fork(); + assert(pid >= 0); + if (!pid) { + if (prctl(PR_SET_SPECULATION_CTRL, + PR_SPEC_IBPB_MODE, spec_ibpb_mode, 0, 0)) { + perror("Unable to set IBPB mode"); + } + + if (disable_ib) + assert(prctl(PR_SET_SPECULATION_CTRL, + PR_SPEC_INDIRECT_BRANCH, + PR_SPEC_DISABLE, 0, 0) == 0); + + close(fds_ping[1]); + pong(fds_ping[0], fds_pong[1]); + } else { + run_test(fds_ping[1], fds_pong[0]); + close(fds_ping[1]); + } + + return 0; +} -- 2.31.1.498.g6c1eba8ee3d-goog