From patchwork Tue Feb 6 14:12:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 197467 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:168b:b0:106:860b:bbdd with SMTP id ma11csp1564647dyb; Tue, 6 Feb 2024 06:13:10 -0800 (PST) X-Google-Smtp-Source: AGHT+IG6mTEANwxQZ4tZyd7omS2RR3LtlXKDoo7CEnkTdUp3DWxmS3aQErZjGt3Y1S0O+6ogF8WU X-Received: by 2002:a05:6a00:2d92:b0:6e0:4a04:cd1 with SMTP id fb18-20020a056a002d9200b006e04a040cd1mr3330687pfb.17.1707228790425; Tue, 06 Feb 2024 06:13:10 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707228790; cv=pass; d=google.com; s=arc-20160816; b=FY/Yzfnqooc6P5L3bOfCOYDrJ/ATnYwJcAnVPDq2EF74wZHtxLGg/pyKjdwQLTGnIo Xc5gZwlD4zq+tb48WDi/lzd2wa4vadeyH5VPy5XI5Wnuggrrql+R4kDw3p2E3Sk5yLip j/HurGqhU5MWQviELsIgEBw4mtKUZAQ5xUO9QFsfnRCNiQ+W2Zq0YWoQW9ZuSsy5Ylxs SbbxYpSJaIN3dnvp3v1DvHxYZJkZFvUTaAo8Ue9q+Re5+a/l2O3S9OedqaEZ+HsrGg+g V4Cb+0zA2Hhd6nCOcPBgs45lw1Zvw0vbDfQGtm2tAyksBEMIF407xTnL6BPje2/C8QfA q3qQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=7SA4faVVJYDMfy/r36fEUOihZ2CAD3tpPWMLG1ykrqg=; fh=yvFMvReqR1/iqKyFYMJnwqjkoMWgrVmiTU5g+HGw5AQ=; b=dHOhKkjai/r3iGZTZZAR8rxIH4g/A8LxjQQc89yhSLqgLMudkXVvV+0CoAdbupb9H1 WWiDjt/LLICApDGgMouGQMODCcAJSJ27g39NS19BXd46HuicqBGjGNTX2s2KoL89+5t2 E+On/y0PP7B0DQdW+x8t+NNaOQ3vu0BAGM6Q9ZCxdYG7vrxwJ2oP807ZyA7W8njIRDh8 DAIGJpCab+afcqLxDoBUMwAjDf5/g5aMI27v2lK3YShb/hZ+CJqSKKnqmBAt/g502vFU O0FCX+L8MqJRKH+YDnprannwg2o7jaM1bmFiZNahmBxvf2+YfZsF41LPRSgFmH0ypCiz YQrg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=AHmmrBUN; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-55087-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-55087-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org X-Forwarded-Encrypted: i=1; AJvYcCWJqGXmAxXuBXVUtiCPMCOnNglsJwQWQYC55W5t74Qz+Xt4TUI9dujyohjEQtNmXSqtyOCw7gHUhQ8Ja59MtZA622ptXA== Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id a36-20020a631a24000000b005cf8b7a0629si1696630pga.663.2024.02.06.06.13.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 06:13:10 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-55087-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=AHmmrBUN; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-55087-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-55087-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 1D232287FFC for ; Tue, 6 Feb 2024 14:12:59 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 49DD4131E57; Tue, 6 Feb 2024 14:12:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="AHmmrBUN" Received: from mail-pf1-f180.google.com (mail-pf1-f180.google.com [209.85.210.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 A1A21131752 for ; Tue, 6 Feb 2024 14:12:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707228763; cv=none; b=HlZKKx1qO+dLZF4TC5LlGkcg853kCdMKTfp9xTquAA6XRhLubQ9KdhMfsL8UO29mHSAXIo5I0UTOAbNbJJ9sNdV1KGYZFmBTUrAClGxYCkYIbYXOpPRDjSgcDh9cTCV0zuhUm69FbR8t264fVxcilVr7WU/O8iC6F+OlSxN0ZbI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707228763; c=relaxed/simple; bh=Wt1T7etqlQIEFK5ye8kbDen9dDtuHaTDaBefpNZivFc=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=goFsLBZ5nZBqWtjLkbJCmZWOh1gOqWLgexG5DpJQZoThGFM6GMsfSiWQMsHQMnkS9NvTkGmeQdgEMA5Ko1nt+kXMZG6lNp73VBFf9DxMVZCDslTT7WLm2aC37qszjD20WP2/VfNWdy5ZW1AfbW1oFMdJbzfyg5CiyuRK0rLVtgw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=AHmmrBUN; arc=none smtp.client-ip=209.85.210.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Received: by mail-pf1-f180.google.com with SMTP id d2e1a72fcca58-6da4a923b1bso3750007b3a.2 for ; Tue, 06 Feb 2024 06:12:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1707228761; x=1707833561; 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=7SA4faVVJYDMfy/r36fEUOihZ2CAD3tpPWMLG1ykrqg=; b=AHmmrBUNQbpQBUNTNGMp4onxodtEw1ZYMd8Z89tho4tDr3F3rA1D2BHCQojlgbCZTv voJb+ixLHF2RBEdcTuFIwEAB6SXfv4PnUSbYSXRhw+OSeooEo4p3JGFgY06PgHbIgUGG 6uECBK4Kt9YjqsL9rICiwTJLANX0H7ZLd8SYc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707228761; x=1707833561; 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=7SA4faVVJYDMfy/r36fEUOihZ2CAD3tpPWMLG1ykrqg=; b=VrZH24nrzrUl7s5A6E1kaWKE8pjnrNWRW4uGbj6mT5w9D60tzJ2J9DpBz2gKGSMr9s J5lZ2AE8AeQFjTeHeuSVnjnG0hH4xn/iPS1TuAzz/mZi4qTtN0vGzZdIqhwptXkDW7R3 vJm6IjzaVj9qnPhq/yP/VmfTTj7QMiOQfJUt1tmG/0EovmON4Wde9cJnk3WZKJ5alhIr Yedllp4bqWluOYAh1o72Yj2kITAfOhA4Ig5MdV2NGw+si28vmjvqWX7UFJswyX6zGfY2 a6k6z8PUKtrcx2d1huAaeiIWcp0YeHWIZ5AyVQ2pKEGRTf80s9t82g632YG1m2uy1YEn XomA== X-Gm-Message-State: AOJu0Yy/6CQ4G5zr3IclUI5YrHXGLfVvavPYTHp43K/DbC6aPolB7ASP wbWduTy7CxpoxHGZ+1PVtEiDb/SBa/VTfnqkGoDUWnkFFAJgjReypNQK+BafcA== X-Received: by 2002:a05:6a00:938d:b0:6df:f634:4f83 with SMTP id ka13-20020a056a00938d00b006dff6344f83mr3185288pfb.2.1707228760934; Tue, 06 Feb 2024 06:12:40 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCWIazxDHvc79fqGX/yKrNSWou2+R1ZUiqoDY3X9PldKpTEv1Buai/LBFC1rxMdTEClTGBPyS9UXo1TbQPDov/pY4NIG97DF8PsVhgxuupuIRcwqKOihklIN97P2bPpvksyOfhcpc2OnmIpdWoXcSFem/2d/Jv3PiU2y5PltgyI0uLDhIJCQpQAVvsayTdZH3DmlzBRXXuwDiv3Wnnxh2jT8A4WzcrVuj2kY3tZD1mLLyhBGvsPJK7aeLf4T/uJ1tJXZo4eCWnug8/gsFuOgoKbM5BzOgdVqS6zg2IYUZ3c= Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id i27-20020a056a00005b00b006ddcf5d5b0bsm1939165pfk.153.2024.02.06.06.12.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 06:12:40 -0800 (PST) From: Kees Cook To: Mark Brown Cc: Kees Cook , kernel test robot , Andy Lutomirski , Will Drewry , Shuah Khan , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-hardening@vger.kernel.org Subject: [PATCH v2] selftests/seccomp: Pin benchmark to single CPU Date: Tue, 6 Feb 2024 06:12:38 -0800 Message-Id: <20240206141234.it.656-kees@kernel.org> X-Mailer: git-send-email 2.34.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3071; i=keescook@chromium.org; h=from:subject:message-id; bh=Wt1T7etqlQIEFK5ye8kbDen9dDtuHaTDaBefpNZivFc=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBlwj5WFrkcgZ8hALvXjbxNjhFAKG7jNJ00NXZx4 ZMCIEaIQp2JAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZcI+VgAKCRCJcvTf3G3A Jj8PD/0RojU69usxAsxYgxt3MI1BZ+ud7tXZFqO7D6VqR1yC1k33YfPK8CB0aIfflDa9URbWbKt UPHooy6DVrSjD4M+JTp7ulrAm+M6i5r/ZLjowZz3dd09rUoe2v776gu8lUxlkugnR33mNwz63Du okWgEALg9W7IhpRRrTG2p9d54Chwc6Ztr9DXueR+XyeSiN1JmTGO7NP2Um1NcaXQZKj07ea135h i65RDTEz9fjIAgbjjzmXPr/agb6H29o36M/CMc9UFfm2+uBV3OTSEG8c1mM/7ZNHuS6Wv5PN9bW aKzCnwyDequy3FkbCVmi81f9SKoME+VUTcKD4PTlcrJuSSxnJ1Ef9v2VdZk2A3Y8LBsH/Vd0yPL SFlcUB/fA2p0f6s2M0XYKyT1z1+1mBPC680zuWdNoMZHb0A9RZ/0QpOzAehag3j88cLjyorG6CD ejXuSDPLXaTzd6SE8ly0UMU4W89GfmE+zHS4P69nK6ECGe8yIjwvctektdgRADC1zglg9yCamKC W0e1rjaLHQQwkIkThNmkh9PE0LewY94M2yia91t0/EX9hLcL2qW3xzRDMcRDOGEXMOx9AmOKQqD rWLNo55g+I70dTA8MqlSwR/RlaYOoDcaPrHj5pKz238be1bNDPcbI45Oh8N0xOiq9IaToYXnls6 nQC2Vfm cMCiWoxw== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790159136360403378 X-GMAIL-MSGID: 1790159136360403378 The seccomp benchmark test (for validating the benefit of bitmaps) can be sensitive to scheduling speed, so pin the process to a single CPU, which appears to significantly improve reliability, and loosen the "close enough" checking to allow up to 10% variance instead of 1%. Reported-by: kernel test robot Closes: https://lore.kernel.org/oe-lkp/202402061002.3a8722fd-oliver.sang@intel.com Cc: Mark Brown Cc: Andy Lutomirski Cc: Will Drewry Signed-off-by: Kees Cook Reviewed-by: Mark Brown --- v2: - improve comment about selecting CPU (broonie) - loosen variance check from 1% to 10% v1: https://lore.kernel.org/all/20240206095642.work.502-kees@kernel.org/ --- .../selftests/seccomp/seccomp_benchmark.c | 38 ++++++++++++++++++- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/seccomp/seccomp_benchmark.c b/tools/testing/selftests/seccomp/seccomp_benchmark.c index 5b5c9d558dee..9d7aa5a730e0 100644 --- a/tools/testing/selftests/seccomp/seccomp_benchmark.c +++ b/tools/testing/selftests/seccomp/seccomp_benchmark.c @@ -4,7 +4,9 @@ */ #define _GNU_SOURCE #include +#include #include +#include #include #include #include @@ -76,8 +78,12 @@ unsigned long long calibrate(void) bool approx(int i_one, int i_two) { - double one = i_one, one_bump = one * 0.01; - double two = i_two, two_bump = two * 0.01; + /* + * This continues to be a noisy test. Instead of a 1% comparison + * go with 10%. + */ + double one = i_one, one_bump = one * 0.1; + double two = i_two, two_bump = two * 0.1; one_bump = one + MAX(one_bump, 2.0); two_bump = two + MAX(two_bump, 2.0); @@ -119,6 +125,32 @@ long compare(const char *name_one, const char *name_eval, const char *name_two, return good ? 0 : 1; } +/* Pin to a single CPU so the benchmark won't bounce around the system. */ +void affinity(void) +{ + long cpu; + ulong ncores = sysconf(_SC_NPROCESSORS_CONF); + cpu_set_t *setp = CPU_ALLOC(ncores); + ulong setsz = CPU_ALLOC_SIZE(ncores); + + /* + * Totally unscientific way to avoid CPUs that might be busier: + * choose the highest CPU instead of the lowest. + */ + for (cpu = ncores - 1; cpu >= 0; cpu--) { + CPU_ZERO_S(setsz, setp); + CPU_SET_S(cpu, setsz, setp); + if (sched_setaffinity(getpid(), setsz, setp) == -1) + continue; + printf("Pinned to CPU %lu of %lu\n", cpu + 1, ncores); + goto out; + } + fprintf(stderr, "Could not set CPU affinity -- calibration may not work well"); + +out: + CPU_FREE(setp); +} + int main(int argc, char *argv[]) { struct sock_filter bitmap_filter[] = { @@ -153,6 +185,8 @@ int main(int argc, char *argv[]) system("grep -H . /proc/sys/net/core/bpf_jit_enable"); system("grep -H . /proc/sys/net/core/bpf_jit_harden"); + affinity(); + if (argc > 1) samples = strtoull(argv[1], NULL, 0); else