From patchwork Fri Oct 21 21:18:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vipin Sharma X-Patchwork-Id: 7008 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4242:0:0:0:0:0 with SMTP id s2csp907771wrr; Fri, 21 Oct 2022 14:21:35 -0700 (PDT) X-Google-Smtp-Source: AMsMyM60tD0j3Nh7nEBk9u6gYc8PselqQp8GLc/gY+Oe7zyiPuO98Bld5FV4PLqHQE83Yd8zCFlQ X-Received: by 2002:a17:906:9b92:b0:791:9355:cb89 with SMTP id dd18-20020a1709069b9200b007919355cb89mr16904124ejc.498.1666387295393; Fri, 21 Oct 2022 14:21:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666387295; cv=none; d=google.com; s=arc-20160816; b=ec3+c+wY79AWmg/tPf1FMokdbkaooXTSpqkclqiKCHsNGq/v3+zCWX1zuLA5wlGYWZ BvwHFH6ArERRBkUSu7/dN7hk71HBEV5DC3ngruQlM3DwY29V3RFqw5xWPHeQQKuZSHvZ 3ttJSziln6g8DityqxE025Iu+teHmZQmyDRpO1Bgb2VIQX+qIPO1Oex20D4ZklAHI6RV mIPvQ+0WRg5Xkqvqz7QEXeHGR1EKrC+KOvRO45Y8uZOIbu59QknRFGcnX38lbvBNBFmL Jhy2QhU5pKOr4kMldhkpUctFxzcOym1ONiJbVqyW29CVAr10s5EhmyZlhvOQ8UJAJFCL HXxw== 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:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=2nZIMOkxkPqMdJjizNd12NlButg+wAhOHsOCFogicCg=; b=oM/g5B8Qy9zAzQ/LIPQkxE2S2L8dpQJkdh1WAR/bj8mLNSvhoBtzzY/yNp6NREiy/4 m8iXaZvEKzJ4OCtODPrwkbLbWKWLJBcR1jD76ZUVMBQt0obzSmZV8v/NBqk1Dkm5qGuP f3aKz+a1GCULgFkJG+dhW5IpWy6axLl0yg49EdMsqVNoQT/pGRaRfBf7tD7cHTaf+Y0d 3C61bKdhqFOx21bpblUatXsFeSVHj7fxKFMmzKlz2VDfH8pw5h3BDrc8zu6CrixMmRG7 9o7P3kzevmttSGo5901m86JxFmRuObRiGW+pd8dh3OC5fxEu9d+4gbn6uP3h9s6dNEw8 bCmg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=WcPSPUKF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id p21-20020a170906785500b0077bc1269f8esi18007808ejm.424.2022.10.21.14.21.11; Fri, 21 Oct 2022 14:21:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=WcPSPUKF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S230224AbiJUVSm (ORCPT + 99 others); Fri, 21 Oct 2022 17:18:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40092 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230038AbiJUVS1 (ORCPT ); Fri, 21 Oct 2022 17:18:27 -0400 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B48982A4E11 for ; Fri, 21 Oct 2022 14:18:26 -0700 (PDT) Received: by mail-pl1-x64a.google.com with SMTP id c10-20020a170902d48a00b001825736b382so2320763plg.15 for ; Fri, 21 Oct 2022 14:18:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=2nZIMOkxkPqMdJjizNd12NlButg+wAhOHsOCFogicCg=; b=WcPSPUKF7ggBl0Y2IXHy1DMp3RWNL9m8VHXZLCw+DdLzcdtF1JNMgKQ4+7na6BJAIO AW0w9JlOFe31ljI0u5nBDWCR37+o2mlUwocMEAqlXjxOFu3JOXkgDD+L7q3ytVQ0j/d0 UP6haL7G1S52AUDMJEYSVcTRzCj5FokhTEhpN4n5Munvo7sDoC1885BRAhDqnqDwLtC9 dof/6ipmS0UF3J3LivuOP9ww1qTXAHQ0nYkCdlgv5JnnQNjCbZpfv2DMclRVGtRirlmi d3k13p9vzhP6XRidDpiN63cvd8szHaO9Ra9A2xCCBM+I8GXMEtMCp5Kc00ArOhXzxz0l cEoQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=2nZIMOkxkPqMdJjizNd12NlButg+wAhOHsOCFogicCg=; b=YCyqlXBjjADU5jTaANl9Sp0EHgRaTSBg7w/0am5FRjtENUcTgNAWInXNuOOQ8FJye/ ZZ7CGomzeB6MSDeILQH0oS88Fv//8gO7HMyW0Ja1+Rdjg4qkL2hb+uuUeaZxkSwWH940 Bpck29o+jJ3mSREcd/ZRxmnu96APIzFklIqvP+pHR2cKgEJbe5001AZyXRJ0FOM16MTj HiaxOE83+dS6YiL+ykNfnzsQROluLhbZksP0sonbcl4hQj7L2FP090IG5MfHHbb2fHFj VrK5jt9lH2sP6lVOEr8aDjgx5ou1ZsXqwwnBo/LyRnIXKw5xsuEqRPU+5OggHGcfqlum mXjw== X-Gm-Message-State: ACrzQf1xcvQ4rkTHUzMa59IpW1r6wt00RIeSyaHhV4LXbgJvX8FVC8HN fww3rOsnd/ANoNkHTH8ephv5AbT5jLjp X-Received: from vipin.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:479f]) (user=vipinsh job=sendgmr) by 2002:a63:91c7:0:b0:460:156c:ded7 with SMTP id l190-20020a6391c7000000b00460156cded7mr17952216pge.298.1666387106255; Fri, 21 Oct 2022 14:18:26 -0700 (PDT) Date: Fri, 21 Oct 2022 14:18:12 -0700 In-Reply-To: <20221021211816.1525201-1-vipinsh@google.com> Mime-Version: 1.0 References: <20221021211816.1525201-1-vipinsh@google.com> X-Mailer: git-send-email 2.38.0.135.g90850a2211-goog Message-ID: <20221021211816.1525201-2-vipinsh@google.com> Subject: [PATCH v6 1/5] KVM: selftests: Add missing break between -e and -g option in dirty_log_perf_test From: Vipin Sharma To: seanjc@google.com, pbonzini@redhat.com, dmatlack@google.com Cc: andrew.jones@linux.dev, wei.w.wang@intel.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Vipin Sharma X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747333724982282990?= X-GMAIL-MSGID: =?utf-8?q?1747333724982282990?= Passing -e option (Run VCPUs while dirty logging is being disabled) in dirty_log_perf_test also unintentionally enables -g (Do not enable KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2). Add break between two switch case logic. Fixes: cfe12e64b065 ("KVM: selftests: Add an option to run vCPUs while disabling dirty logging") Signed-off-by: Vipin Sharma --- tools/testing/selftests/kvm/dirty_log_perf_test.c | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/testing/selftests/kvm/dirty_log_perf_test.c b/tools/testing/selftests/kvm/dirty_log_perf_test.c index f99e39a672d3..56e08da3a87f 100644 --- a/tools/testing/selftests/kvm/dirty_log_perf_test.c +++ b/tools/testing/selftests/kvm/dirty_log_perf_test.c @@ -411,6 +411,7 @@ int main(int argc, char *argv[]) case 'e': /* 'e' is for evil. */ run_vcpus_while_disabling_dirty_logging = true; + break; case 'g': dirty_log_manual_caps = 0; break; From patchwork Fri Oct 21 21:18:13 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vipin Sharma X-Patchwork-Id: 7010 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4242:0:0:0:0:0 with SMTP id s2csp907817wrr; Fri, 21 Oct 2022 14:21:43 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6Ds8FD8VWyS3qkgGgpoJGEzsALL0kDZAJNfgxWRZG1lKuIFExvcBNqr2AikfmTM9kAMqNS X-Received: by 2002:a17:907:1689:b0:78d:e460:1d98 with SMTP id hc9-20020a170907168900b0078de4601d98mr16827013ejc.451.1666387303294; Fri, 21 Oct 2022 14:21:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666387303; cv=none; d=google.com; s=arc-20160816; b=dF9Odiew6WJKsrH2C2Yg09ykysiNoXAw40f8QBTKMmolPaTgeBKS2g7JYfG4Z+l4lx jjCEQDPApuXgb9Pdf+HVR4uMct5ZsIzocRJIQc1fktPNj+g9NKHjXg171Vfrf3tgrr18 GtyDZMLb4/iBu1vJnrZinY//9OKKGaVOF0pWjNNqSglOs6yb+U14SD86xQpfygfnDAGk +D0vy4xrIaSwFQmqUZTNif3qHkSDMRsR9DeVFIN7gw/b9PJuOvTuXioIvMYjxE1d5JHx 8phAPeChBAzjjAPNyAZ3c5xM3bB+RVUW7cYgmQfmf83hST6lGNbrcW0bfvm7WG3WQLXD pl+w== 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:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=vMVJJoBCzsMQyaPTMVzkUL5zwK0EC1PhaZ6zsLS80Z8=; b=MnNisuc6+W4zxCsDiMZQesw5x6i10R+D1vMPmySFcHUUFKPnBnrZBY9GQW8LNuh3B7 gkCkGxQ5IK15PmvyBT1TF+6dYKeYGK5OZKAxJirpWjNsre/vXt+LFxg5iqkQjVjlvIDL r2FnVNAVlpLfkbOBFflnuQ5Kx8JQTje0+Ojhee40rr2fvfAYE7rLYXd9liOky5Rh7U1z beiYEoKbu8F07SCL5hA9XF7i1AWMw5qFD7uofxeBUXYGP3P8/dEizoQxyvwYkTE9XMSZ cRoTYb5rqsRI8XfJGBwFKZ7SM1nnlLg2xvMGPq+DAO3R2pbdKakkJK54Lk5zlEH505d9 TtlA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=HJ9djxnn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l17-20020a056402255100b0045d7d14baefsi14175255edb.386.2022.10.21.14.21.18; Fri, 21 Oct 2022 14:21:43 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=HJ9djxnn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S230217AbiJUVSr (ORCPT + 99 others); Fri, 21 Oct 2022 17:18:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40164 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230207AbiJUVSa (ORCPT ); Fri, 21 Oct 2022 17:18:30 -0400 Received: from mail-pf1-x449.google.com (mail-pf1-x449.google.com [IPv6:2607:f8b0:4864:20::449]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B93E72A4E0D for ; Fri, 21 Oct 2022 14:18:28 -0700 (PDT) Received: by mail-pf1-x449.google.com with SMTP id a11-20020a056a001d0b00b005635c581a24so1904869pfx.17 for ; Fri, 21 Oct 2022 14:18:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=vMVJJoBCzsMQyaPTMVzkUL5zwK0EC1PhaZ6zsLS80Z8=; b=HJ9djxnnBohMH2BuprRhqnTKlyK1QQaQQXtmQoanmuQoqUMDQMYGc9BSE002jjc0Fg DXHA1NrvZ90Vx0Glb1IZHVqqv5aPUe/3FdfYJxiuPWrI03UNtpkP0nS5o36wpmdQV6pf X5T5T7j6X1VRYDo+/BCdUkiM6ydddIS4Iw/VRrYJqljLPLzI1gwN0EnFx9cKBddI+89F +NVywaVi0bTZqZf4HCCwUrqDXjMJzdy1hu+yOpcnO22t4ecRkNrjuBWdccFZzlJmAb9H sKLwBYRi4ODZCSnVQBhSxMtJC1HB/hs1CvqxRcnzUjdoq6MWB9hbqGRsgI0ARWqSZagv 53ZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=vMVJJoBCzsMQyaPTMVzkUL5zwK0EC1PhaZ6zsLS80Z8=; b=QSmF9gAaHjEVOuuNLFWTAMWloOi1/5sqJjJrsutCtPdIJk2RMbNvtmL2urmrEcBtZz JqErexjdAG+dJaikfSi+xI7SYwNFW+F+eO/Jh63Jb5c3yy4qcZpiKAZY0LY+tN8Ox/CD rFprUaCaFqKyl44HicgUdYhDwK32ioIy2qmKuTBB5XT4Y2teXOehut/QbKc6M+cxgR/E avK6Zpy3vlBJV38S3zH9L/ulcvsmeCRUoPmeK5vivUKK0AMXLA8zU1W82uZcpo3w4t2u 4eNy4z3K+XMfjsE/xU4LvXghfPygnUL0N5Bgt8ZF2EyreyTpudDguUqIW+DHqDh3wdQ3 iDdA== X-Gm-Message-State: ACrzQf04XyNmA6QdzzhiRi0FxdEhbsoFx+sMVs5TNZbf7TuHUQCcnrdo aYwHFkVHwNQan2n5xFREiv4G5uvqBFXL X-Received: from vipin.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:479f]) (user=vipinsh job=sendgmr) by 2002:a05:6a00:1306:b0:555:6d3f:11ed with SMTP id j6-20020a056a00130600b005556d3f11edmr21113463pfu.55.1666387107996; Fri, 21 Oct 2022 14:18:27 -0700 (PDT) Date: Fri, 21 Oct 2022 14:18:13 -0700 In-Reply-To: <20221021211816.1525201-1-vipinsh@google.com> Mime-Version: 1.0 References: <20221021211816.1525201-1-vipinsh@google.com> X-Mailer: git-send-email 2.38.0.135.g90850a2211-goog Message-ID: <20221021211816.1525201-3-vipinsh@google.com> Subject: [PATCH v6 2/5] KVM: selftests: Put command line options in alphabetical order in dirty_log_perf_test From: Vipin Sharma To: seanjc@google.com, pbonzini@redhat.com, dmatlack@google.com Cc: andrew.jones@linux.dev, wei.w.wang@intel.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Vipin Sharma X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747333732949266068?= X-GMAIL-MSGID: =?utf-8?q?1747333732949266068?= There are 13 command line options and they are not in any order. Put them in alphabetical order to make it easy to add new options. No functional change intended. Signed-off-by: Vipin Sharma Reviewed-by: Wei Wang --- .../selftests/kvm/dirty_log_perf_test.c | 36 ++++++++++--------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/tools/testing/selftests/kvm/dirty_log_perf_test.c b/tools/testing/selftests/kvm/dirty_log_perf_test.c index 56e08da3a87f..5bb6954b2358 100644 --- a/tools/testing/selftests/kvm/dirty_log_perf_test.c +++ b/tools/testing/selftests/kvm/dirty_log_perf_test.c @@ -406,50 +406,52 @@ int main(int argc, char *argv[]) guest_modes_append_default(); - while ((opt = getopt(argc, argv, "eghi:p:m:nb:f:v:os:x:")) != -1) { + while ((opt = getopt(argc, argv, "b:ef:ghi:m:nop:s:v:x:")) != -1) { switch (opt) { + case 'b': + guest_percpu_mem_size = parse_size(optarg); + break; case 'e': /* 'e' is for evil. */ run_vcpus_while_disabling_dirty_logging = true; break; + case 'f': + p.wr_fract = atoi(optarg); + TEST_ASSERT(p.wr_fract >= 1, + "Write fraction cannot be less than one"); + break; case 'g': dirty_log_manual_caps = 0; break; + case 'h': + help(argv[0]); + break; case 'i': p.iterations = atoi(optarg); break; - case 'p': - p.phys_offset = strtoull(optarg, NULL, 0); - break; case 'm': guest_modes_cmdline(optarg); break; case 'n': perf_test_args.nested = true; break; - case 'b': - guest_percpu_mem_size = parse_size(optarg); + case 'o': + p.partition_vcpu_memory_access = false; break; - case 'f': - p.wr_fract = atoi(optarg); - TEST_ASSERT(p.wr_fract >= 1, - "Write fraction cannot be less than one"); + case 'p': + p.phys_offset = strtoull(optarg, NULL, 0); + break; + case 's': + p.backing_src = parse_backing_src_type(optarg); break; case 'v': nr_vcpus = atoi(optarg); TEST_ASSERT(nr_vcpus > 0 && nr_vcpus <= max_vcpus, "Invalid number of vcpus, must be between 1 and %d", max_vcpus); break; - case 'o': - p.partition_vcpu_memory_access = false; - break; - case 's': - p.backing_src = parse_backing_src_type(optarg); - break; case 'x': p.slots = atoi(optarg); break; - case 'h': default: help(argv[0]); break; From patchwork Fri Oct 21 21:18:14 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vipin Sharma X-Patchwork-Id: 7007 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4242:0:0:0:0:0 with SMTP id s2csp907671wrr; Fri, 21 Oct 2022 14:21:18 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4YfXOZfeMVcMAAGzv8fCJu0FLnW3YCp0IwzYoKDf6Yn3P/s+GSF4kNgwOYod2KR4fPfFDO X-Received: by 2002:a05:6402:278f:b0:45d:5503:8acd with SMTP id b15-20020a056402278f00b0045d55038acdmr18943287ede.314.1666387278479; Fri, 21 Oct 2022 14:21:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666387278; cv=none; d=google.com; s=arc-20160816; b=gYrYZsIQyGwhIoEcFpnd6dgUFqkaIr0pVUTMIOuANiMu/Ut263UxjUGegE5sPNgkc9 YKuoadxXlXZ0BJwvsrrVzBYsQf86eUATUkiMUzXp8w+NtMPaREz31ha29csLscNKL7sQ xdz2NxdaTr21ATcu5+DJm0/NAlOKArQtehhCYKbXVHzzA876P0SFZpwZalCRyDzqXUdz NQRZMw8RRMhnlCRDGsu8IyD1F6ZiF2HbWn5jv6lUoA5j1RGhGdYByjeDQ8YgfzcEVyOp MMVb0F9qyLYA+QIRzjQeZ7pSwNkiDn+4p6zsahkWfKhDm7XEqPqxzvKk6vRjkZRETJBw rBAw== 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:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=rMws9ezkK6n1R9JTRBXWTh4yY8uZLjcEb1w7BIkwhN4=; b=Jl30fVrrMTbEQReIz84ORJPOYUqddQbNOQDlBBZlWqNqV6+RaQzRoeEpEz0JvxTBp3 6kgw906vHLu67PQrhUo/KEy7Zm0KgdYMmkoWcSCzKvuQECdjdtB9YTJ0VV+2LeYeolWZ QUcj8LKjknoFkdw2Zmxy5eN6Khx+I7cIpGIcn3WAjsRZ3YuEznmDyHD5brqMjpGGfUmx cj+67+dLhBzYzC2Y5CFoQsdiamQMxeCuAbCvbzP2T+rDhezJ4GE1yIWWoJA1lQigaJpZ mnd7gQXyZu2xFsq4T5py8qjEUGEk2mP7/3yQkkfHDwQXQaCIYSIFZl8xqqfGo78+JfvX 1X6w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=K3FzZtqF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b8-20020a170906708800b0078df946ea14si18598827ejk.419.2022.10.21.14.20.54; Fri, 21 Oct 2022 14:21:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=K3FzZtqF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S230381AbiJUVTH (ORCPT + 99 others); Fri, 21 Oct 2022 17:19:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40596 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230244AbiJUVSq (ORCPT ); Fri, 21 Oct 2022 17:18:46 -0400 Received: from mail-pj1-x104a.google.com (mail-pj1-x104a.google.com [IPv6:2607:f8b0:4864:20::104a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4E7E22A4E1E for ; Fri, 21 Oct 2022 14:18:31 -0700 (PDT) Received: by mail-pj1-x104a.google.com with SMTP id p1-20020a17090a2c4100b00212733d7aaaso1941985pjm.4 for ; Fri, 21 Oct 2022 14:18:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=rMws9ezkK6n1R9JTRBXWTh4yY8uZLjcEb1w7BIkwhN4=; b=K3FzZtqFuHALXau+Q7a2QLf8984i87r/M+XQTxHJmdNdaE4UgR1bCiRCZcrhArP2CA nW+GIRRgNc5itOdavMVVlDljWwbIgph0Qhri/Ccn3JZIwumAFENWrW0/+gHwalER/BTF Dp2YWqIQ6E1iA3KAe/hatBKimCGTuHq404Tvip57C0tY5LdIkPzcDPNZvMfQ5UAwkBd0 BT6d5caMLL956Q3JufvTe7DtGiOXYQOTCoiduvy+v9KyByD5CbEtDjbaFrnUhIMNXtVh +icdBVVlUrFb6nrp/Vdl/qkSdkULbeVsl0C9IM6EueV2z1/W5OZPQlqJTT0lgjXEXKuD qSKA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=rMws9ezkK6n1R9JTRBXWTh4yY8uZLjcEb1w7BIkwhN4=; b=oyjQxH5v9zz3P1F5zOmk7rmLVVFv390jp9AlaBaHwANNjQxsOQtNyUCLAOVs1uYb8z /D1ztn7t1Shav0pSYjb7QP4DWKm+CeHcUqV7n6g1cgv3l8wHJMQA02+IqnOfyoKFJQmI k4LKHxapXhLJBkt4GAlk6UQK4EjdktlVWwwndEByAD6SVyE3gAeAZLEN37G8ZFvpHmdn ZYny42yGnxF/rA5N6KfND9bzRoTUZ1UtJHDKQ3HRi5E1+006IbA3kU2OYzkFRRnMrx+p K2quyFexnF5H5EiqMZcVFDmXAa3IXRFERTnXLd5Q4FzBBnQttDFJEBBC/BZEuAisEf/6 k65A== X-Gm-Message-State: ACrzQf03g7NUPPk5IwZ/+zSgPJA9GUa4KTgj2Rwb4ljwAS3fj4TcV0dv flPCVakmAmak3Eh74yL49wcKChNJkWol X-Received: from vipin.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:479f]) (user=vipinsh job=sendgmr) by 2002:a17:90b:2243:b0:20b:42a:4c0d with SMTP id hk3-20020a17090b224300b0020b042a4c0dmr58105348pjb.123.1666387109925; Fri, 21 Oct 2022 14:18:29 -0700 (PDT) Date: Fri, 21 Oct 2022 14:18:14 -0700 In-Reply-To: <20221021211816.1525201-1-vipinsh@google.com> Mime-Version: 1.0 References: <20221021211816.1525201-1-vipinsh@google.com> X-Mailer: git-send-email 2.38.0.135.g90850a2211-goog Message-ID: <20221021211816.1525201-4-vipinsh@google.com> Subject: [PATCH v6 3/5] KVM: selftests: Add atoi_paranoid() to catch errors missed by atoi() From: Vipin Sharma To: seanjc@google.com, pbonzini@redhat.com, dmatlack@google.com Cc: andrew.jones@linux.dev, wei.w.wang@intel.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Vipin Sharma X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747333706768839719?= X-GMAIL-MSGID: =?utf-8?q?1747333706768839719?= atoi() doesn't detect errors. There is no way to know that a 0 return is correct conversion or due to an error. Introduce atoi_paranoid() to detect errors and provide correct conversion. Replace all atoi() calls with atoi_paranoid(). Signed-off-by: Vipin Sharma Suggested-by: David Matlack Suggested-by: Sean Christopherson --- .../selftests/kvm/aarch64/arch_timer.c | 8 ++++---- .../testing/selftests/kvm/aarch64/vgic_irq.c | 6 +++--- .../selftests/kvm/access_tracking_perf_test.c | 2 +- .../selftests/kvm/demand_paging_test.c | 2 +- .../selftests/kvm/dirty_log_perf_test.c | 8 ++++---- .../testing/selftests/kvm/include/test_util.h | 2 ++ .../selftests/kvm/kvm_page_table_test.c | 2 +- tools/testing/selftests/kvm/lib/test_util.c | 19 +++++++++++++++++++ .../selftests/kvm/max_guest_memory_test.c | 6 +++--- .../kvm/memslot_modification_stress_test.c | 4 ++-- .../testing/selftests/kvm/memslot_perf_test.c | 10 +++++----- .../selftests/kvm/set_memory_region_test.c | 2 +- .../selftests/kvm/x86_64/nx_huge_pages_test.c | 4 ++-- 13 files changed, 48 insertions(+), 27 deletions(-) diff --git a/tools/testing/selftests/kvm/aarch64/arch_timer.c b/tools/testing/selftests/kvm/aarch64/arch_timer.c index 574eb73f0e90..251e7ff04883 100644 --- a/tools/testing/selftests/kvm/aarch64/arch_timer.c +++ b/tools/testing/selftests/kvm/aarch64/arch_timer.c @@ -414,7 +414,7 @@ static bool parse_args(int argc, char *argv[]) while ((opt = getopt(argc, argv, "hn:i:p:m:")) != -1) { switch (opt) { case 'n': - test_args.nr_vcpus = atoi(optarg); + test_args.nr_vcpus = atoi_paranoid(optarg); if (test_args.nr_vcpus <= 0) { pr_info("Positive value needed for -n\n"); goto err; @@ -425,21 +425,21 @@ static bool parse_args(int argc, char *argv[]) } break; case 'i': - test_args.nr_iter = atoi(optarg); + test_args.nr_iter = atoi_paranoid(optarg); if (test_args.nr_iter <= 0) { pr_info("Positive value needed for -i\n"); goto err; } break; case 'p': - test_args.timer_period_ms = atoi(optarg); + test_args.timer_period_ms = atoi_paranoid(optarg); if (test_args.timer_period_ms <= 0) { pr_info("Positive value needed for -p\n"); goto err; } break; case 'm': - test_args.migration_freq_ms = atoi(optarg); + test_args.migration_freq_ms = atoi_paranoid(optarg); if (test_args.migration_freq_ms < 0) { pr_info("0 or positive value needed for -m\n"); goto err; diff --git a/tools/testing/selftests/kvm/aarch64/vgic_irq.c b/tools/testing/selftests/kvm/aarch64/vgic_irq.c index 17417220a083..ae90b718070a 100644 --- a/tools/testing/selftests/kvm/aarch64/vgic_irq.c +++ b/tools/testing/selftests/kvm/aarch64/vgic_irq.c @@ -824,16 +824,16 @@ int main(int argc, char **argv) while ((opt = getopt(argc, argv, "hn:e:l:")) != -1) { switch (opt) { case 'n': - nr_irqs = atoi(optarg); + nr_irqs = atoi_paranoid(optarg); if (nr_irqs > 1024 || nr_irqs % 32) help(argv[0]); break; case 'e': - eoi_split = (bool)atoi(optarg); + eoi_split = (bool)atoi_paranoid(optarg); default_args = false; break; case 'l': - level_sensitive = (bool)atoi(optarg); + level_sensitive = (bool)atoi_paranoid(optarg); default_args = false; break; case 'h': diff --git a/tools/testing/selftests/kvm/access_tracking_perf_test.c b/tools/testing/selftests/kvm/access_tracking_perf_test.c index 76c583a07ea2..c6bcc5301e2c 100644 --- a/tools/testing/selftests/kvm/access_tracking_perf_test.c +++ b/tools/testing/selftests/kvm/access_tracking_perf_test.c @@ -368,7 +368,7 @@ int main(int argc, char *argv[]) params.vcpu_memory_bytes = parse_size(optarg); break; case 'v': - params.nr_vcpus = atoi(optarg); + params.nr_vcpus = atoi_paranoid(optarg); break; case 'o': overlap_memory_access = true; diff --git a/tools/testing/selftests/kvm/demand_paging_test.c b/tools/testing/selftests/kvm/demand_paging_test.c index 779ae54f89c4..82597fb04146 100644 --- a/tools/testing/selftests/kvm/demand_paging_test.c +++ b/tools/testing/selftests/kvm/demand_paging_test.c @@ -427,7 +427,7 @@ int main(int argc, char *argv[]) p.src_type = parse_backing_src_type(optarg); break; case 'v': - nr_vcpus = atoi(optarg); + nr_vcpus = atoi_paranoid(optarg); TEST_ASSERT(nr_vcpus > 0 && nr_vcpus <= max_vcpus, "Invalid number of vcpus, must be between 1 and %d", max_vcpus); break; diff --git a/tools/testing/selftests/kvm/dirty_log_perf_test.c b/tools/testing/selftests/kvm/dirty_log_perf_test.c index 5bb6954b2358..ecda802b78ff 100644 --- a/tools/testing/selftests/kvm/dirty_log_perf_test.c +++ b/tools/testing/selftests/kvm/dirty_log_perf_test.c @@ -416,7 +416,7 @@ int main(int argc, char *argv[]) run_vcpus_while_disabling_dirty_logging = true; break; case 'f': - p.wr_fract = atoi(optarg); + p.wr_fract = atoi_paranoid(optarg); TEST_ASSERT(p.wr_fract >= 1, "Write fraction cannot be less than one"); break; @@ -427,7 +427,7 @@ int main(int argc, char *argv[]) help(argv[0]); break; case 'i': - p.iterations = atoi(optarg); + p.iterations = atoi_paranoid(optarg); break; case 'm': guest_modes_cmdline(optarg); @@ -445,12 +445,12 @@ int main(int argc, char *argv[]) p.backing_src = parse_backing_src_type(optarg); break; case 'v': - nr_vcpus = atoi(optarg); + nr_vcpus = atoi_paranoid(optarg); TEST_ASSERT(nr_vcpus > 0 && nr_vcpus <= max_vcpus, "Invalid number of vcpus, must be between 1 and %d", max_vcpus); break; case 'x': - p.slots = atoi(optarg); + p.slots = atoi_paranoid(optarg); break; default: help(argv[0]); diff --git a/tools/testing/selftests/kvm/include/test_util.h b/tools/testing/selftests/kvm/include/test_util.h index befc754ce9b3..feae42863759 100644 --- a/tools/testing/selftests/kvm/include/test_util.h +++ b/tools/testing/selftests/kvm/include/test_util.h @@ -152,4 +152,6 @@ static inline void *align_ptr_up(void *x, size_t size) return (void *)align_up((unsigned long)x, size); } +int atoi_paranoid(const char *num_str); + #endif /* SELFTEST_KVM_TEST_UTIL_H */ diff --git a/tools/testing/selftests/kvm/kvm_page_table_test.c b/tools/testing/selftests/kvm/kvm_page_table_test.c index f42c6ac6d71d..ea7feb69bb88 100644 --- a/tools/testing/selftests/kvm/kvm_page_table_test.c +++ b/tools/testing/selftests/kvm/kvm_page_table_test.c @@ -461,7 +461,7 @@ int main(int argc, char *argv[]) p.test_mem_size = parse_size(optarg); break; case 'v': - nr_vcpus = atoi(optarg); + nr_vcpus = atoi_paranoid(optarg); TEST_ASSERT(nr_vcpus > 0 && nr_vcpus <= max_vcpus, "Invalid number of vcpus, must be between 1 and %d", max_vcpus); break; diff --git a/tools/testing/selftests/kvm/lib/test_util.c b/tools/testing/selftests/kvm/lib/test_util.c index 6d23878bbfe1..ec0f070a6f21 100644 --- a/tools/testing/selftests/kvm/lib/test_util.c +++ b/tools/testing/selftests/kvm/lib/test_util.c @@ -334,3 +334,22 @@ long get_run_delay(void) return val[1]; } + +int atoi_paranoid(const char *num_str) +{ + char *end_ptr; + long num; + + errno = 0; + num = strtol(num_str, &end_ptr, 10); + TEST_ASSERT(!errno, "strtol(\"%s\") failed", num_str); + TEST_ASSERT(num_str != end_ptr, + "strtol(\"%s\") didn't find a valid integer.\n", num_str); + TEST_ASSERT(*end_ptr == '\0', + "strtol(\"%s\") failed to parse trailing characters \"%s\".\n", + num_str, end_ptr); + TEST_ASSERT(num >= INT_MIN && num <= INT_MAX, + "%ld not in range of [%d, %d]", num, INT_MIN, INT_MAX); + + return num; +} diff --git a/tools/testing/selftests/kvm/max_guest_memory_test.c b/tools/testing/selftests/kvm/max_guest_memory_test.c index 9a6e4f3ad6b5..1595b73dc09a 100644 --- a/tools/testing/selftests/kvm/max_guest_memory_test.c +++ b/tools/testing/selftests/kvm/max_guest_memory_test.c @@ -193,15 +193,15 @@ int main(int argc, char *argv[]) while ((opt = getopt(argc, argv, "c:h:m:s:H")) != -1) { switch (opt) { case 'c': - nr_vcpus = atoi(optarg); + nr_vcpus = atoi_paranoid(optarg); TEST_ASSERT(nr_vcpus > 0, "number of vcpus must be >0"); break; case 'm': - max_mem = atoi(optarg) * size_1gb; + max_mem = atoi_paranoid(optarg) * size_1gb; TEST_ASSERT(max_mem > 0, "memory size must be >0"); break; case 's': - slot_size = atoi(optarg) * size_1gb; + slot_size = atoi_paranoid(optarg) * size_1gb; TEST_ASSERT(slot_size > 0, "slot size must be >0"); break; case 'H': diff --git a/tools/testing/selftests/kvm/memslot_modification_stress_test.c b/tools/testing/selftests/kvm/memslot_modification_stress_test.c index 6ee7e1dde404..865276993ffb 100644 --- a/tools/testing/selftests/kvm/memslot_modification_stress_test.c +++ b/tools/testing/selftests/kvm/memslot_modification_stress_test.c @@ -166,7 +166,7 @@ int main(int argc, char *argv[]) guest_percpu_mem_size = parse_size(optarg); break; case 'v': - nr_vcpus = atoi(optarg); + nr_vcpus = atoi_paranoid(optarg); TEST_ASSERT(nr_vcpus > 0 && nr_vcpus <= max_vcpus, "Invalid number of vcpus, must be between 1 and %d", max_vcpus); @@ -175,7 +175,7 @@ int main(int argc, char *argv[]) p.partition_vcpu_memory_access = false; break; case 'i': - p.nr_memslot_modifications = atoi(optarg); + p.nr_memslot_modifications = atoi_paranoid(optarg); break; case 'h': default: diff --git a/tools/testing/selftests/kvm/memslot_perf_test.c b/tools/testing/selftests/kvm/memslot_perf_test.c index 44995446d942..4bae9e3f5ca1 100644 --- a/tools/testing/selftests/kvm/memslot_perf_test.c +++ b/tools/testing/selftests/kvm/memslot_perf_test.c @@ -885,21 +885,21 @@ static bool parse_args(int argc, char *argv[], map_unmap_verify = true; break; case 's': - targs->nslots = atoi(optarg); + targs->nslots = atoi_paranoid(optarg); if (targs->nslots <= 0 && targs->nslots != -1) { pr_info("Slot count cap has to be positive or -1 for no cap\n"); return false; } break; case 'f': - targs->tfirst = atoi(optarg); + targs->tfirst = atoi_paranoid(optarg); if (targs->tfirst < 0) { pr_info("First test to run has to be non-negative\n"); return false; } break; case 'e': - targs->tlast = atoi(optarg); + targs->tlast = atoi_paranoid(optarg); if (targs->tlast < 0 || targs->tlast >= NTESTS) { pr_info("Last test to run has to be non-negative and less than %zu\n", NTESTS); @@ -907,14 +907,14 @@ static bool parse_args(int argc, char *argv[], } break; case 'l': - targs->seconds = atoi(optarg); + targs->seconds = atoi_paranoid(optarg); if (targs->seconds < 0) { pr_info("Test length in seconds has to be non-negative\n"); return false; } break; case 'r': - targs->runs = atoi(optarg); + targs->runs = atoi_paranoid(optarg); if (targs->runs <= 0) { pr_info("Runs per test has to be positive\n"); return false; diff --git a/tools/testing/selftests/kvm/set_memory_region_test.c b/tools/testing/selftests/kvm/set_memory_region_test.c index 0d55f508d595..c366949c8362 100644 --- a/tools/testing/selftests/kvm/set_memory_region_test.c +++ b/tools/testing/selftests/kvm/set_memory_region_test.c @@ -407,7 +407,7 @@ int main(int argc, char *argv[]) #ifdef __x86_64__ if (argc > 1) - loops = atoi(argv[1]); + loops = atoi_paranoid(argv[1]); else loops = 10; diff --git a/tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.c b/tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.c index 59ffe7fd354f..354b6902849c 100644 --- a/tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.c +++ b/tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.c @@ -241,10 +241,10 @@ int main(int argc, char **argv) while ((opt = getopt(argc, argv, "hp:t:r")) != -1) { switch (opt) { case 'p': - reclaim_period_ms = atoi(optarg); + reclaim_period_ms = atoi_paranoid(optarg); break; case 't': - token = atoi(optarg); + token = atoi_paranoid(optarg); break; case 'r': reboot_permissions = true; From patchwork Fri Oct 21 21:18:15 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vipin Sharma X-Patchwork-Id: 7009 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4242:0:0:0:0:0 with SMTP id s2csp907807wrr; Fri, 21 Oct 2022 14:21:42 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6dtiFyLAi59uUJ4hyD5MF+wHclbCx1ZUzCUbOmLIhv74JWH2O69sUyX7oQS93ZKvHPGP3K X-Received: by 2002:a17:907:7635:b0:78d:c5e9:3e57 with SMTP id jy21-20020a170907763500b0078dc5e93e57mr17485773ejc.204.1666387302194; Fri, 21 Oct 2022 14:21:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666387302; cv=none; d=google.com; s=arc-20160816; b=N+2NkRYa7YTst2vfkVLtUtB3xcEXBjp+oBa4PfxtRnNYw90ZavCVRS9VwulPs556S0 Rtq3niCfBQMv16sg5EffgEHhAli5o0o71It90EWzHTCMEeP4wZLWcWAA6yFtkh8c7W/T eLDUEwqEn2ydj1mrxLkYLENkKrJUU90YHtZUV3Y2VShiQVUzkI4KXwiPxJHjH7NO48Rs CfPj3BYQpbecORQuqYQ/a/5+NKBwzxkrfdWUhLVV46TbnQ7kF5eVbPT4Y3LW/2MJevon q6F6BbcxI21Jf29/EC9WUUfB7Tuf+RkBQ24gqpNyBXb4AaiY/oTPHX7lNtP3CBUqdINW UP/g== 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:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=N/3+UYD1iUfTuL1WJoe+LwMhCBr0Vrvp2n9eDEXYU1Y=; b=zBg9ZNmmzS4Ce/QwQLHIRzJRFPIVQ0tZiI9uiXeXarGWoBRacGAGh7ws2wgBEiWZ5f kdFi5plNf4cl65grg5tMRso5elBhdInRus8Hb7+3sumJn40/wSA7179DtN7nRb9NXNnH nv0b9KwjeaJgEp8gjqd0vZVPo35Yl1Gkbnxin9lt2vwOe2mrtbR5JaNfWeSON9MMTOX7 LAmo2J60gdry66LrbVjhQzgJwhA0rUP7DHbUfJ+lMsQzRsx7+oPG7jh5ROOIa855ec48 aATCj7GSRVjnR3ZHMDqpVnxCOnaTi8D7D70gTbgZJN/8DnYvFS17LJP/bhKjTBXy4/9P 0Xww== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b="CWZh/ieQ"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i7-20020a1709064fc700b0078d6418dcb8si23620605ejw.456.2022.10.21.14.21.18; Fri, 21 Oct 2022 14:21:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b="CWZh/ieQ"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S230314AbiJUVTO (ORCPT + 99 others); Fri, 21 Oct 2022 17:19:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41000 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230307AbiJUVS6 (ORCPT ); Fri, 21 Oct 2022 17:18:58 -0400 Received: from mail-oa1-x49.google.com (mail-oa1-x49.google.com [IPv6:2001:4860:4864:20::49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1D0EA2A58FE for ; Fri, 21 Oct 2022 14:18:42 -0700 (PDT) Received: by mail-oa1-x49.google.com with SMTP id 586e51a60fabf-1329a5d3272so2378867fac.17 for ; Fri, 21 Oct 2022 14:18:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=N/3+UYD1iUfTuL1WJoe+LwMhCBr0Vrvp2n9eDEXYU1Y=; b=CWZh/ieQaLQbrhUPf7VOcTmvYjLRxfMK92pQymM/lfPuAnJMGuIRVd9hy7Hy8djO8W siUbUPnL4FG4rmvuEmw6fO7TK8Legy5PKSRuUgVamKKfuxYYkrBrfz4uylNVSkqpCyIi Y/8gSYpNecBKAaLrY617Aab0EVdkORQGe8b4CnYfhZkxfFdoUIOjt9HcSVIpKD1eaNNR kX3Qn3wf3vKOnhfp6RT3Zjfw4PHJKJfqA6xr2q/3hW9F6XlYvv3TUB63uFon45sQWesC 3hTi8NBSqLyODe32wOdyRohxKBL+hB9Y/9BKPOFzrjlFm6H6j8f1HmR3uRJVEljTRtpK e5Lg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=N/3+UYD1iUfTuL1WJoe+LwMhCBr0Vrvp2n9eDEXYU1Y=; b=Y7X8ZQiO98/uuNv5G9He1YegAA6C/u3hzDPnD6IjT/4oKFox8dL0cv5GQKx9hc5UWC VYrEMEyyPGVBWFC2o5UL100X6covTZ66f0LvSenHi643tk1Fuz1RzjvtZlJGdFjWQU66 +tjtSOInEiYmVSIHI/Ce4ql8pHOaNdggnRoV2ZoVCOPhSAwxbAh6C7/eq/NSftrf4UD1 QLwwC+brnzGsn2d6zAcody3kP4VaMyqot+eyrVcersjaPuUJfcMOCXi2VPXbss5+xqqJ nNXUCihL7p0eQKr2RA2kfhsLniBEy75gJ29d2m5ssAPfif3UmJYwfWIEvoB5/46xwEeq dI1A== X-Gm-Message-State: ACrzQf074R34GvyTrLAri9wHLFdW+h3gkPNgHchApmV/t9bJ7Ls+6p76 c55ou6gh1n3thhfGCIPUPL6e7Ie5W/wz X-Received: from vipin.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:479f]) (user=vipinsh job=sendgmr) by 2002:a17:90b:3507:b0:20a:9d07:1656 with SMTP id ls7-20020a17090b350700b0020a9d071656mr24208245pjb.188.1666387111948; Fri, 21 Oct 2022 14:18:31 -0700 (PDT) Date: Fri, 21 Oct 2022 14:18:15 -0700 In-Reply-To: <20221021211816.1525201-1-vipinsh@google.com> Mime-Version: 1.0 References: <20221021211816.1525201-1-vipinsh@google.com> X-Mailer: git-send-email 2.38.0.135.g90850a2211-goog Message-ID: <20221021211816.1525201-5-vipinsh@google.com> Subject: [PATCH v6 4/5] KVM: selftests: Add atoi_positive() and atoi_non_negative() for input validation From: Vipin Sharma To: seanjc@google.com, pbonzini@redhat.com, dmatlack@google.com Cc: andrew.jones@linux.dev, wei.w.wang@intel.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Vipin Sharma X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747333731653738441?= X-GMAIL-MSGID: =?utf-8?q?1747333731653738441?= Many KVM selftests take command line arguments which are supposed to be positive (>0) or non-negative (>=0). Some tests do these validation and some missed adding the check. Add atoi_positive() and atoi_non_negative() to validate inputs in selftests before proceeding to use those values. Signed-off-by: Vipin Sharma --- .../selftests/kvm/aarch64/arch_timer.c | 25 ++++--------------- .../testing/selftests/kvm/aarch64/vgic_irq.c | 2 +- .../selftests/kvm/access_tracking_perf_test.c | 2 +- .../selftests/kvm/demand_paging_test.c | 4 +-- .../selftests/kvm/dirty_log_perf_test.c | 12 ++++----- .../testing/selftests/kvm/include/test_util.h | 2 ++ .../selftests/kvm/kvm_page_table_test.c | 4 +-- tools/testing/selftests/kvm/lib/test_util.c | 16 ++++++++++++ .../selftests/kvm/max_guest_memory_test.c | 7 +++--- .../kvm/memslot_modification_stress_test.c | 6 ++--- .../testing/selftests/kvm/memslot_perf_test.c | 22 ++++------------ .../selftests/kvm/set_memory_region_test.c | 2 +- 12 files changed, 46 insertions(+), 58 deletions(-) diff --git a/tools/testing/selftests/kvm/aarch64/arch_timer.c b/tools/testing/selftests/kvm/aarch64/arch_timer.c index 251e7ff04883..24dffcaf7a9f 100644 --- a/tools/testing/selftests/kvm/aarch64/arch_timer.c +++ b/tools/testing/selftests/kvm/aarch64/arch_timer.c @@ -414,36 +414,21 @@ static bool parse_args(int argc, char *argv[]) while ((opt = getopt(argc, argv, "hn:i:p:m:")) != -1) { switch (opt) { case 'n': - test_args.nr_vcpus = atoi_paranoid(optarg); - if (test_args.nr_vcpus <= 0) { - pr_info("Positive value needed for -n\n"); - goto err; - } else if (test_args.nr_vcpus > KVM_MAX_VCPUS) { + test_args.nr_vcpus = atoi_positive(optarg); + if (test_args.nr_vcpus > KVM_MAX_VCPUS) { pr_info("Max allowed vCPUs: %u\n", KVM_MAX_VCPUS); goto err; } break; case 'i': - test_args.nr_iter = atoi_paranoid(optarg); - if (test_args.nr_iter <= 0) { - pr_info("Positive value needed for -i\n"); - goto err; - } + test_args.nr_iter = atoi_positive(optarg); break; case 'p': - test_args.timer_period_ms = atoi_paranoid(optarg); - if (test_args.timer_period_ms <= 0) { - pr_info("Positive value needed for -p\n"); - goto err; - } + test_args.timer_period_ms = atoi_positive(optarg); break; case 'm': - test_args.migration_freq_ms = atoi_paranoid(optarg); - if (test_args.migration_freq_ms < 0) { - pr_info("0 or positive value needed for -m\n"); - goto err; - } + test_args.migration_freq_ms = atoi_non_negative(optarg); break; case 'h': default: diff --git a/tools/testing/selftests/kvm/aarch64/vgic_irq.c b/tools/testing/selftests/kvm/aarch64/vgic_irq.c index ae90b718070a..d7da5f24db35 100644 --- a/tools/testing/selftests/kvm/aarch64/vgic_irq.c +++ b/tools/testing/selftests/kvm/aarch64/vgic_irq.c @@ -824,7 +824,7 @@ int main(int argc, char **argv) while ((opt = getopt(argc, argv, "hn:e:l:")) != -1) { switch (opt) { case 'n': - nr_irqs = atoi_paranoid(optarg); + nr_irqs = atoi_non_negative(optarg); if (nr_irqs > 1024 || nr_irqs % 32) help(argv[0]); break; diff --git a/tools/testing/selftests/kvm/access_tracking_perf_test.c b/tools/testing/selftests/kvm/access_tracking_perf_test.c index c6bcc5301e2c..b30500cc197e 100644 --- a/tools/testing/selftests/kvm/access_tracking_perf_test.c +++ b/tools/testing/selftests/kvm/access_tracking_perf_test.c @@ -368,7 +368,7 @@ int main(int argc, char *argv[]) params.vcpu_memory_bytes = parse_size(optarg); break; case 'v': - params.nr_vcpus = atoi_paranoid(optarg); + params.nr_vcpus = atoi_positive(optarg); break; case 'o': overlap_memory_access = true; diff --git a/tools/testing/selftests/kvm/demand_paging_test.c b/tools/testing/selftests/kvm/demand_paging_test.c index 82597fb04146..dcdb6964b1dc 100644 --- a/tools/testing/selftests/kvm/demand_paging_test.c +++ b/tools/testing/selftests/kvm/demand_paging_test.c @@ -427,8 +427,8 @@ int main(int argc, char *argv[]) p.src_type = parse_backing_src_type(optarg); break; case 'v': - nr_vcpus = atoi_paranoid(optarg); - TEST_ASSERT(nr_vcpus > 0 && nr_vcpus <= max_vcpus, + nr_vcpus = atoi_positive(optarg); + TEST_ASSERT(nr_vcpus <= max_vcpus, "Invalid number of vcpus, must be between 1 and %d", max_vcpus); break; case 'o': diff --git a/tools/testing/selftests/kvm/dirty_log_perf_test.c b/tools/testing/selftests/kvm/dirty_log_perf_test.c index ecda802b78ff..618598ddd993 100644 --- a/tools/testing/selftests/kvm/dirty_log_perf_test.c +++ b/tools/testing/selftests/kvm/dirty_log_perf_test.c @@ -416,9 +416,7 @@ int main(int argc, char *argv[]) run_vcpus_while_disabling_dirty_logging = true; break; case 'f': - p.wr_fract = atoi_paranoid(optarg); - TEST_ASSERT(p.wr_fract >= 1, - "Write fraction cannot be less than one"); + p.wr_fract = atoi_positive(optarg); break; case 'g': dirty_log_manual_caps = 0; @@ -427,7 +425,7 @@ int main(int argc, char *argv[]) help(argv[0]); break; case 'i': - p.iterations = atoi_paranoid(optarg); + p.iterations = atoi_positive(optarg); break; case 'm': guest_modes_cmdline(optarg); @@ -445,12 +443,12 @@ int main(int argc, char *argv[]) p.backing_src = parse_backing_src_type(optarg); break; case 'v': - nr_vcpus = atoi_paranoid(optarg); - TEST_ASSERT(nr_vcpus > 0 && nr_vcpus <= max_vcpus, + nr_vcpus = atoi_positive(optarg); + TEST_ASSERT(nr_vcpus <= max_vcpus, "Invalid number of vcpus, must be between 1 and %d", max_vcpus); break; case 'x': - p.slots = atoi_paranoid(optarg); + p.slots = atoi_positive(optarg); break; default: help(argv[0]); diff --git a/tools/testing/selftests/kvm/include/test_util.h b/tools/testing/selftests/kvm/include/test_util.h index feae42863759..9c7b2c186a48 100644 --- a/tools/testing/selftests/kvm/include/test_util.h +++ b/tools/testing/selftests/kvm/include/test_util.h @@ -153,5 +153,7 @@ static inline void *align_ptr_up(void *x, size_t size) } int atoi_paranoid(const char *num_str); +uint32_t atoi_positive(const char *num_str); +uint32_t atoi_non_negative(const char *num_str); #endif /* SELFTEST_KVM_TEST_UTIL_H */ diff --git a/tools/testing/selftests/kvm/kvm_page_table_test.c b/tools/testing/selftests/kvm/kvm_page_table_test.c index ea7feb69bb88..2f62e19976fd 100644 --- a/tools/testing/selftests/kvm/kvm_page_table_test.c +++ b/tools/testing/selftests/kvm/kvm_page_table_test.c @@ -461,8 +461,8 @@ int main(int argc, char *argv[]) p.test_mem_size = parse_size(optarg); break; case 'v': - nr_vcpus = atoi_paranoid(optarg); - TEST_ASSERT(nr_vcpus > 0 && nr_vcpus <= max_vcpus, + nr_vcpus = atoi_positive(optarg); + TEST_ASSERT(nr_vcpus <= max_vcpus, "Invalid number of vcpus, must be between 1 and %d", max_vcpus); break; case 's': diff --git a/tools/testing/selftests/kvm/lib/test_util.c b/tools/testing/selftests/kvm/lib/test_util.c index ec0f070a6f21..210e98a49a83 100644 --- a/tools/testing/selftests/kvm/lib/test_util.c +++ b/tools/testing/selftests/kvm/lib/test_util.c @@ -353,3 +353,19 @@ int atoi_paranoid(const char *num_str) return num; } + +uint32_t atoi_positive(const char *num_str) +{ + int num = atoi_paranoid(num_str); + + TEST_ASSERT(num > 0, "%s is not a positive integer.\n", num_str); + return num; +} + +uint32_t atoi_non_negative(const char *num_str) +{ + int num = atoi_paranoid(num_str); + + TEST_ASSERT(num >= 0, "%s is not a non-negative integer.\n", num_str); + return num; +} diff --git a/tools/testing/selftests/kvm/max_guest_memory_test.c b/tools/testing/selftests/kvm/max_guest_memory_test.c index 1595b73dc09a..20015de3b91c 100644 --- a/tools/testing/selftests/kvm/max_guest_memory_test.c +++ b/tools/testing/selftests/kvm/max_guest_memory_test.c @@ -193,15 +193,14 @@ int main(int argc, char *argv[]) while ((opt = getopt(argc, argv, "c:h:m:s:H")) != -1) { switch (opt) { case 'c': - nr_vcpus = atoi_paranoid(optarg); - TEST_ASSERT(nr_vcpus > 0, "number of vcpus must be >0"); + nr_vcpus = atoi_positive(optarg); break; case 'm': - max_mem = atoi_paranoid(optarg) * size_1gb; + max_mem = atoi_positive(optarg) * size_1gb; TEST_ASSERT(max_mem > 0, "memory size must be >0"); break; case 's': - slot_size = atoi_paranoid(optarg) * size_1gb; + slot_size = atoi_positive(optarg) * size_1gb; TEST_ASSERT(slot_size > 0, "slot size must be >0"); break; case 'H': diff --git a/tools/testing/selftests/kvm/memslot_modification_stress_test.c b/tools/testing/selftests/kvm/memslot_modification_stress_test.c index 865276993ffb..7539ee7b6e95 100644 --- a/tools/testing/selftests/kvm/memslot_modification_stress_test.c +++ b/tools/testing/selftests/kvm/memslot_modification_stress_test.c @@ -166,8 +166,8 @@ int main(int argc, char *argv[]) guest_percpu_mem_size = parse_size(optarg); break; case 'v': - nr_vcpus = atoi_paranoid(optarg); - TEST_ASSERT(nr_vcpus > 0 && nr_vcpus <= max_vcpus, + nr_vcpus = atoi_positive(optarg); + TEST_ASSERT(nr_vcpus <= max_vcpus, "Invalid number of vcpus, must be between 1 and %d", max_vcpus); break; @@ -175,7 +175,7 @@ int main(int argc, char *argv[]) p.partition_vcpu_memory_access = false; break; case 'i': - p.nr_memslot_modifications = atoi_paranoid(optarg); + p.nr_memslot_modifications = atoi_positive(optarg); break; case 'h': default: diff --git a/tools/testing/selftests/kvm/memslot_perf_test.c b/tools/testing/selftests/kvm/memslot_perf_test.c index 4bae9e3f5ca1..8e6e2d44d002 100644 --- a/tools/testing/selftests/kvm/memslot_perf_test.c +++ b/tools/testing/selftests/kvm/memslot_perf_test.c @@ -892,33 +892,21 @@ static bool parse_args(int argc, char *argv[], } break; case 'f': - targs->tfirst = atoi_paranoid(optarg); - if (targs->tfirst < 0) { - pr_info("First test to run has to be non-negative\n"); - return false; - } + targs->tfirst = atoi_non_negative(optarg); break; case 'e': - targs->tlast = atoi_paranoid(optarg); - if (targs->tlast < 0 || targs->tlast >= NTESTS) { + targs->tlast = atoi_non_negative(optarg); + if (targs->tlast >= NTESTS) { pr_info("Last test to run has to be non-negative and less than %zu\n", NTESTS); return false; } break; case 'l': - targs->seconds = atoi_paranoid(optarg); - if (targs->seconds < 0) { - pr_info("Test length in seconds has to be non-negative\n"); - return false; - } + targs->seconds = atoi_non_negative(optarg); break; case 'r': - targs->runs = atoi_paranoid(optarg); - if (targs->runs <= 0) { - pr_info("Runs per test has to be positive\n"); - return false; - } + targs->runs = atoi_positive(optarg); break; } } diff --git a/tools/testing/selftests/kvm/set_memory_region_test.c b/tools/testing/selftests/kvm/set_memory_region_test.c index c366949c8362..e09092110121 100644 --- a/tools/testing/selftests/kvm/set_memory_region_test.c +++ b/tools/testing/selftests/kvm/set_memory_region_test.c @@ -407,7 +407,7 @@ int main(int argc, char *argv[]) #ifdef __x86_64__ if (argc > 1) - loops = atoi_paranoid(argv[1]); + loops = atoi_positive(argv[1]); else loops = 10; From patchwork Fri Oct 21 21:18:16 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vipin Sharma X-Patchwork-Id: 7011 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4242:0:0:0:0:0 with SMTP id s2csp908149wrr; Fri, 21 Oct 2022 14:22:37 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4xB7aBjtzOgZaUnebr12i+HT2Srhq0dhQgVR2M/iRPJ5NnwBnk0hAxMOctXv8W3oZevrf2 X-Received: by 2002:aa7:800a:0:b0:565:af23:f5a4 with SMTP id j10-20020aa7800a000000b00565af23f5a4mr21287169pfi.42.1666387357086; Fri, 21 Oct 2022 14:22:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666387357; cv=none; d=google.com; s=arc-20160816; b=qYydH4gIOu+rekOCIpFkNrEVQtbwsCVLWu1Oja+WsCesNgSq3Jky7PS6pjggO3Jo9x PvStEePbwxaawAmRvUMcakPA2N+QmOC6MCm95qnRGexMC4ZA6zX2Qr7/fLvRoZZc1pra Id1p5oriQWuTJwuBUHqnAbc0RFfO77wAfgwt8V5G7wQOurJkGhjHKFwAZtxk1rPFScmp f0ZNxTW7QUEYyP/JWbzcuKqawaD6cUlNHToJxxhwAPF298i9oizjz7/Nn0kukn72/xoC rBxGUenPpKlRaF6ByKZHO/8nzikVy/x/z1hy85NY/7H+tdWsxzihkkTRl3hjM2v9k48X y74Q== 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:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=T7kxPR9BlRIjzoKc+0ht99Q2Dk0EHdE778/74XTTiPw=; b=aNiKBEuRF6YoQshhO4zJ40bIv9xUaerhgGYD6mpUDk2O+Fk2f+h+Iu+hyiWV5FozfQ LynEjq3egM1L90dG0Np/YNAzjzH3rna6APkm8o0WP61A8OImN2RtOZ1uHyfkzcSPDVfn CC6HADhJq6dqspfYUw7/6eeyAl+h5H7sGSYTBTO6Fl/stJr4B4QSIvXtYJ1I4ZXWq4OP 0BrogF3BSifFFKtFDfX1Yu+oxU71Q+7upztUOHvupPlcVmtFjIIFAKzIlH2dog43kpdW 21LPr4aqRGecxTjc7sNxpiYbppiCTbpHi/JMmi2707JXWEW82JsPFC0QypL6/tq4oYdJ /EKA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=quSmY0wZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 71-20020a63054a000000b0045c16005914si26895772pgf.423.2022.10.21.14.22.24; Fri, 21 Oct 2022 14:22:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=quSmY0wZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S230268AbiJUVTC (ORCPT + 99 others); Fri, 21 Oct 2022 17:19:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40614 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230016AbiJUVSr (ORCPT ); Fri, 21 Oct 2022 17:18:47 -0400 Received: from mail-pj1-x1049.google.com (mail-pj1-x1049.google.com [IPv6:2607:f8b0:4864:20::1049]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7BA742A4E29 for ; Fri, 21 Oct 2022 14:18:34 -0700 (PDT) Received: by mail-pj1-x1049.google.com with SMTP id il7-20020a17090b164700b0020d1029ceaaso4749763pjb.8 for ; Fri, 21 Oct 2022 14:18:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=T7kxPR9BlRIjzoKc+0ht99Q2Dk0EHdE778/74XTTiPw=; b=quSmY0wZwBh/jf1uHg2pkg0cdZIJpr+v1oL1KCCGxim+ySz2rvH1x+s1e5h7yNB1JW Im03C1t7sBQ1fLv3aWJQDhOD1aweJ+/JE9IRHQNCH9hrzM09Giovi36NSc/Rhnipv2Ru rIIBWL+VXcmZzZnxoVxiwrDh7DC0+TCLfiRopHVg8EkL3OKnWAObKg8suH7Ad24R8xNg 7WgXkg5ikphJvhQlJjUs6jSvLsIKE86gE81UMMsSPWJmUr03kTtZHxHLg0JocMakx/Ir FyIPlZ4B6xK/MOvc7rXPp1GdPwvGyHOYIH+9jJ4jZGIkjJE66RyLJjkRO163UosZMsuX fJtg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=T7kxPR9BlRIjzoKc+0ht99Q2Dk0EHdE778/74XTTiPw=; b=47gEOgOGN5r1Pow9j02p6PvUMl9eJgmS+yrluMnAh3eMsXnjLh0lRxksrszytmmEXX +x2FHhk/9DCLMCUoQaeh3VpDM+cL/yg8x5I0uNYArlOiDfo1JVGDDKpN3JP6UyaFXwHR I3lsO4rHfBmIONdV9Aw4ojvrfB9ey24rBnxPgj+y1ZTFMQacX/HbT2R2+TlptmuG+fnz /IRxED+GlDbG/klh5nAb0W66J5bGsCPDYZ5Kfbmx3c56K/Ken9AU8Q5ZisoxTSMGKzdf yTENd6Y71mC32k6aOtQH+/G/3kxB3TOxJyD8Cf7XqvutixZpf8QjEQIjH73iqDN9WPY7 o3zQ== X-Gm-Message-State: ACrzQf1HzvUdDa6JuX0lXO/jcN0vcQccOwhVDa8jztZ3opnN1QEboNiE vZA/4fEshgQhZZ13c+G9nQ5sb1eDR/NK X-Received: from vipin.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:479f]) (user=vipinsh job=sendgmr) by 2002:a17:903:247:b0:179:b5e1:54b7 with SMTP id j7-20020a170903024700b00179b5e154b7mr21116838plh.84.1666387113402; Fri, 21 Oct 2022 14:18:33 -0700 (PDT) Date: Fri, 21 Oct 2022 14:18:16 -0700 In-Reply-To: <20221021211816.1525201-1-vipinsh@google.com> Mime-Version: 1.0 References: <20221021211816.1525201-1-vipinsh@google.com> X-Mailer: git-send-email 2.38.0.135.g90850a2211-goog Message-ID: <20221021211816.1525201-6-vipinsh@google.com> Subject: [PATCH v6 5/5] KVM: selftests: Allowing running dirty_log_perf_test on specific CPUs From: Vipin Sharma To: seanjc@google.com, pbonzini@redhat.com, dmatlack@google.com Cc: andrew.jones@linux.dev, wei.w.wang@intel.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Vipin Sharma X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747333789226400914?= X-GMAIL-MSGID: =?utf-8?q?1747333789226400914?= Add a command line option, -c, to pin vCPUs to physical CPUs (pCPUs), i.e. to force vCPUs to run on specific pCPUs. Requirement to implement this feature came in discussion on the patch "Make page tables for eager page splitting NUMA aware" https://lore.kernel.org/lkml/YuhPT2drgqL+osLl@google.com/ This feature is useful as it provides a way to analyze performance based on the vCPUs and dirty log worker locations, like on the different NUMA nodes or on the same NUMA nodes. To keep things simple, implementation is intentionally very limited, either all of the vCPUs will be pinned followed by an optional main thread or nothing will be pinned. Signed-off-by: Vipin Sharma Suggested-by: David Matlack --- .../selftests/kvm/dirty_log_perf_test.c | 22 ++++++- .../selftests/kvm/include/perf_test_util.h | 6 ++ .../selftests/kvm/lib/perf_test_util.c | 65 ++++++++++++++++++- 3 files changed, 90 insertions(+), 3 deletions(-) diff --git a/tools/testing/selftests/kvm/dirty_log_perf_test.c b/tools/testing/selftests/kvm/dirty_log_perf_test.c index 618598ddd993..35504b36b126 100644 --- a/tools/testing/selftests/kvm/dirty_log_perf_test.c +++ b/tools/testing/selftests/kvm/dirty_log_perf_test.c @@ -353,7 +353,7 @@ static void help(char *name) puts(""); printf("usage: %s [-h] [-i iterations] [-p offset] [-g] " "[-m mode] [-n] [-b vcpu bytes] [-v vcpus] [-o] [-s mem type]" - "[-x memslots]\n", name); + "[-x memslots] [-c physical cpus to run test on]\n", name); puts(""); printf(" -i: specify iteration counts (default: %"PRIu64")\n", TEST_HOST_LOOP_N); @@ -383,6 +383,17 @@ static void help(char *name) backing_src_help("-s"); printf(" -x: Split the memory region into this number of memslots.\n" " (default: 1)\n"); + printf(" -c: Pin tasks to physical CPUs. Takes a list of comma separated\n" + " values (target pCPU), one for each vCPU, plus an optional\n" + " entry for the main application task (specified via entry\n" + " ). If used, entries must be provided for all\n" + " vCPUs, i.e. pinning vCPUs is all or nothing.\n\n" + " E.g. to create 3 vCPUs, pin vCPU0=>pCPU22, vCPU1=>pCPU23,\n" + " vCPU2=>pCPU24, and pin the application task to pCPU50:\n\n" + " ./dirty_log_perf_test -v 3 -c 22,23,24,50\n\n" + " To leave the application task unpinned, drop the final entry:\n\n" + " ./dirty_log_perf_test -v 3 -c 22,23,24\n\n" + " (default: no pinning)\n"); puts(""); exit(0); } @@ -390,6 +401,7 @@ static void help(char *name) int main(int argc, char *argv[]) { int max_vcpus = kvm_check_cap(KVM_CAP_MAX_VCPUS); + const char *pcpu_list = NULL; struct test_params p = { .iterations = TEST_HOST_LOOP_N, .wr_fract = 1, @@ -406,11 +418,14 @@ int main(int argc, char *argv[]) guest_modes_append_default(); - while ((opt = getopt(argc, argv, "b:ef:ghi:m:nop:s:v:x:")) != -1) { + while ((opt = getopt(argc, argv, "b:c:ef:ghi:m:nop:s:v:x:")) != -1) { switch (opt) { case 'b': guest_percpu_mem_size = parse_size(optarg); break; + case 'c': + pcpu_list = optarg; + break; case 'e': /* 'e' is for evil. */ run_vcpus_while_disabling_dirty_logging = true; @@ -456,6 +471,9 @@ int main(int argc, char *argv[]) } } + if (pcpu_list) + perf_test_setup_pinning(pcpu_list, nr_vcpus); + TEST_ASSERT(p.iterations >= 2, "The test should have at least two iterations"); pr_info("Test iterations: %"PRIu64"\n", p.iterations); diff --git a/tools/testing/selftests/kvm/include/perf_test_util.h b/tools/testing/selftests/kvm/include/perf_test_util.h index eaa88df0555a..ccfe3b9dc6bd 100644 --- a/tools/testing/selftests/kvm/include/perf_test_util.h +++ b/tools/testing/selftests/kvm/include/perf_test_util.h @@ -27,6 +27,8 @@ struct perf_test_vcpu_args { /* Only used by the host userspace part of the vCPU thread */ struct kvm_vcpu *vcpu; int vcpu_idx; + /* The pCPU to which this vCPU is pinned. Only valid if pin_vcpus is true. */ + uint32_t pcpu; }; struct perf_test_args { @@ -39,6 +41,8 @@ struct perf_test_args { /* Run vCPUs in L2 instead of L1, if the architecture supports it. */ bool nested; + /* True if all vCPUs are pinned to pCPUs */ + bool pin_vcpus; struct perf_test_vcpu_args vcpu_args[KVM_MAX_VCPUS]; }; @@ -60,4 +64,6 @@ void perf_test_guest_code(uint32_t vcpu_id); uint64_t perf_test_nested_pages(int nr_vcpus); void perf_test_setup_nested(struct kvm_vm *vm, int nr_vcpus, struct kvm_vcpu *vcpus[]); +void perf_test_setup_pinning(const char *pcpus_string, int nr_vcpus); + #endif /* SELFTEST_KVM_PERF_TEST_UTIL_H */ diff --git a/tools/testing/selftests/kvm/lib/perf_test_util.c b/tools/testing/selftests/kvm/lib/perf_test_util.c index 9618b37c66f7..520d1f896d61 100644 --- a/tools/testing/selftests/kvm/lib/perf_test_util.c +++ b/tools/testing/selftests/kvm/lib/perf_test_util.c @@ -2,7 +2,10 @@ /* * Copyright (C) 2020, Google LLC. */ +#define _GNU_SOURCE + #include +#include #include "kvm_util.h" #include "perf_test_util.h" @@ -240,10 +243,27 @@ void __weak perf_test_setup_nested(struct kvm_vm *vm, int nr_vcpus, struct kvm_v exit(KSFT_SKIP); } +static void pin_this_task_to_pcpu(uint32_t pcpu) +{ + cpu_set_t mask; + int r; + + CPU_ZERO(&mask); + CPU_SET(pcpu, &mask); + r = sched_setaffinity(0, sizeof(mask), &mask); + TEST_ASSERT(!r, "sched_setaffinity() failed for pCPU '%u'.\n", pcpu); +} + static void *vcpu_thread_main(void *data) { + struct perf_test_vcpu_args *vcpu_args; struct vcpu_thread *vcpu = data; + vcpu_args = &perf_test_args.vcpu_args[vcpu->vcpu_idx]; + + if (perf_test_args.pin_vcpus) + pin_this_task_to_pcpu(vcpu_args->pcpu); + WRITE_ONCE(vcpu->running, true); /* @@ -255,7 +275,7 @@ static void *vcpu_thread_main(void *data) while (!READ_ONCE(all_vcpu_threads_running)) ; - vcpu_thread_fn(&perf_test_args.vcpu_args[vcpu->vcpu_idx]); + vcpu_thread_fn(vcpu_args); return NULL; } @@ -292,3 +312,46 @@ void perf_test_join_vcpu_threads(int nr_vcpus) for (i = 0; i < nr_vcpus; i++) pthread_join(vcpu_threads[i].thread, NULL); } + +static uint32_t parse_pcpu(const char *cpu_str, const cpu_set_t *allowed_mask) +{ + uint32_t pcpu = atoi_non_negative(cpu_str); + + TEST_ASSERT(CPU_ISSET(pcpu, allowed_mask), + "Not allowed to run on pCPU '%d', check cgroups?\n", pcpu); + return pcpu; +} + +void perf_test_setup_pinning(const char *pcpus_string, int nr_vcpus) +{ + cpu_set_t allowed_mask; + char *cpu, *cpu_list; + char delim[2] = ","; + int i, r; + + cpu_list = strdup(pcpus_string); + TEST_ASSERT(cpu_list, "strdup() allocation failed.\n"); + + r = sched_getaffinity(0, sizeof(allowed_mask), &allowed_mask); + TEST_ASSERT(!r, "sched_getaffinity() failed"); + + cpu = strtok(cpu_list, delim); + + /* 1. Get all pcpus for vcpus. */ + for (i = 0; i < nr_vcpus; i++) { + TEST_ASSERT(cpu, "pCPU not provided for vCPU '%d'\n", i); + perf_test_args.vcpu_args[i].pcpu = parse_pcpu(cpu, &allowed_mask); + cpu = strtok(NULL, delim); + } + + perf_test_args.pin_vcpus = true; + + /* 2. Check if the main worker needs to be pinned. */ + if (cpu) { + pin_this_task_to_pcpu(parse_pcpu(cpu, &allowed_mask)); + cpu = strtok(NULL, delim); + } + + TEST_ASSERT(!cpu, "pCPU list contains trailing garbage characters '%s'", cpu); + free(cpu_list); +}