From patchwork Fri Sep 24 12:53:32 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 12515267 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.4 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, UNWANTED_LANGUAGE_BODY,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 43BA9C433EF for ; Fri, 24 Sep 2021 12:57:00 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id F334660F39 for ; Fri, 24 Sep 2021 12:56:59 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org F334660F39 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:References: Mime-Version:Message-Id:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=WufdCE1G7nMXxUHEiz7brvpJapbF2Gthgi/Y6MHRu7I=; b=ImqjkqV3e3T0ytdwZgknGQDlHM kGx7lKc52jDnY5UBAJg0zdS0dshzVF1RJ6kMpcgQ/ucLP/puJ//B39sVTrXE8zBw7Vs3gLpg1aIu0 DIAmt2xWi7Sh0i6BxB1BQ6Rg667NQy5O5ybo/bTejuslag7aUjkzDlMLKLpY+9VOkimK3oWib/cHC O5Dg7I6z+s1KhezLv087cAQnlakup9qQnZD5I/WxE7cADAwZj3NHnrM0Jknby+HptRhxGSmqQ8FBY 5vjeEamI8CHc8DBfpo+KiN5vS6kkQgnP67hDbT3EoROZHJLVQnPHiPMpA2xhuCRmfhsvfKvu076uP vSM3zQ4w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mTkjC-00EMKl-28; Fri, 24 Sep 2021 12:54:58 +0000 Received: from mail-wr1-x449.google.com ([2a00:1450:4864:20::449]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mTkiQ-00EM1V-De for linux-arm-kernel@lists.infradead.org; Fri, 24 Sep 2021 12:54:13 +0000 Received: by mail-wr1-x449.google.com with SMTP id h5-20020a5d6885000000b0015e21e37523so7979190wru.10 for ; Fri, 24 Sep 2021 05:54:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc:content-transfer-encoding; bh=W+2w1qwCuEn0t2SEv46R5dHRsrtW6N98LhGxQR4ZHMs=; b=fB29omoApOyd7KWBCUSp019JzllSe991ZbrpXIl/8+wD518FgOf68xWf1p080OQzLK 7ef+DcagPMxgJ1sICd9uZ37FbAyEV43bQtw0dqqbG/gJFzVs8A0S/Bb39XrkMckmrqxM 1xQ+TEJK1KB7xTTgmdXlNGKw2wBihF3ZOqPeIzc+8ysz20+GBCRUZot2z/oGTAuMPAoo iN4EUiT0JIIy7df/wYX3JoMf0BR7l/n4skiALWB3CGeAqMwOjDzvwWn/yw0Kt27/4qEP UjBSemQMkNmHZNIT3OiGLvTr/IT7xnhAOOJrXtzxLcGqL285afdsuBvWqD5Jq8CQSmAl 7+wg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc:content-transfer-encoding; bh=W+2w1qwCuEn0t2SEv46R5dHRsrtW6N98LhGxQR4ZHMs=; b=kyJXKyrRxuRBBSWjajJMRwZA7/xhEbguFoMetpVQ5rE31qPvn+4uxAQEZc2Pa4xr03 CUNZ268z0leQrqijgs6fEJ7Bu7V5g8NYOTty4SSyXNkOPvsz6Q+n1fn6nFr3JUOFduuM 4vV5zrTmbWCQAYvIWnybKM7zajAqkXGTotGwq52GFR98Uv/LXOEa/M/MQDDx4lc/LT8B 8esEtBBjZF/6f0nmcFY4oL1r5XXRKPBfemASZ0yVWnr4GMno5ThYBdWAfYKwiQx95pjF A885Yt+FRQiX8k+NRTd+HYHgSuSyG2yCrnswWdZ38Je1bKXmF6oh0HUYmS+ASQZKvtJi ENEg== X-Gm-Message-State: AOAM532ZPKYrkEgTy5THX0QfPxe4qjD46uQ/hp0peegFtSGUrdKDbwGh FSHPZdw4qbsB4QtQ0/M9iTRehM3m/g== X-Google-Smtp-Source: ABdhPJx/AhXU7v63bYO98iWD72IfKcnea82QgZwoY4OwP8dr+1tbs2DMALrsIf7vL8Tvaw9WkugTCl9mXQ== X-Received: from tabba.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:482]) (user=tabba job=sendgmr) by 2002:a1c:4c14:: with SMTP id z20mr1965423wmf.82.1632488048707; Fri, 24 Sep 2021 05:54:08 -0700 (PDT) Date: Fri, 24 Sep 2021 13:53:32 +0100 In-Reply-To: <20210924125359.2587041-1-tabba@google.com> Message-Id: <20210924125359.2587041-4-tabba@google.com> Mime-Version: 1.0 References: <20210924125359.2587041-1-tabba@google.com> X-Mailer: git-send-email 2.33.0.685.g46640cef36-goog Subject: [RFC PATCH v1 03/30] [DONOTMERGE] Coccinelle scripts for refactoring From: Fuad Tabba To: kvmarm@lists.cs.columbia.edu Cc: maz@kernel.org, will@kernel.org, james.morse@arm.com, alexandru.elisei@arm.com, suzuki.poulose@arm.com, mark.rutland@arm.com, christoffer.dall@arm.com, drjones@redhat.com, qperret@google.com, kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kernel-team@android.com, tabba@google.com X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210924_055410_517795_D5BF9848 X-CRM114-Status: GOOD ( 16.71 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org These Coccinelle scripts are used in the coming refactoring patches. Adding them as a commit to keep them as part of the history. For running the scripts please use a recent version of Coccinelle, which has this patch [*]. Signed-off-by: Fuad Tabba [*] Link: https://lore.kernel.org/cocci/alpine.DEB.2.22.394.2104211654020.13358@hadrien/T/#t --- cocci_refactor/add_ctxt.cocci | 169 ++++++++++++++++++++++++ cocci_refactor/add_hypstate.cocci | 125 ++++++++++++++++++ cocci_refactor/hyp_ctxt.cocci | 38 ++++++ cocci_refactor/range.cocci | 50 +++++++ cocci_refactor/remove_unused.cocci | 69 ++++++++++ cocci_refactor/test.cocci | 20 +++ cocci_refactor/use_ctxt.cocci | 32 +++++ cocci_refactor/use_ctxt_access.cocci | 39 ++++++ cocci_refactor/use_hypstate.cocci | 63 +++++++++ cocci_refactor/vcpu_arch_ctxt.cocci | 13 ++ cocci_refactor/vcpu_declr.cocci | 59 +++++++++ cocci_refactor/vcpu_flags.cocci | 10 ++ cocci_refactor/vcpu_hyp_accessors.cocci | 35 +++++ cocci_refactor/vcpu_hyp_state.cocci | 30 +++++ cocci_refactor/vgic3_cpu.cocci | 118 +++++++++++++++++ 15 files changed, 870 insertions(+) create mode 100644 cocci_refactor/add_ctxt.cocci create mode 100644 cocci_refactor/add_hypstate.cocci create mode 100644 cocci_refactor/hyp_ctxt.cocci create mode 100644 cocci_refactor/range.cocci create mode 100644 cocci_refactor/remove_unused.cocci create mode 100644 cocci_refactor/test.cocci create mode 100644 cocci_refactor/use_ctxt.cocci create mode 100644 cocci_refactor/use_ctxt_access.cocci create mode 100644 cocci_refactor/use_hypstate.cocci create mode 100644 cocci_refactor/vcpu_arch_ctxt.cocci create mode 100644 cocci_refactor/vcpu_declr.cocci create mode 100644 cocci_refactor/vcpu_flags.cocci create mode 100644 cocci_refactor/vcpu_hyp_accessors.cocci create mode 100644 cocci_refactor/vcpu_hyp_state.cocci create mode 100644 cocci_refactor/vgic3_cpu.cocci diff --git a/cocci_refactor/add_ctxt.cocci b/cocci_refactor/add_ctxt.cocci new file mode 100644 index 000000000000..203644944ace --- /dev/null +++ b/cocci_refactor/add_ctxt.cocci @@ -0,0 +1,169 @@ +// + +/* +spatch --sp-file add_ctxt.cocci --dir arch/arm64/kvm/hyp --ignore arch/arm64/kvm/hyp/nvhe/debug-sr.c --ignore arch/arm64/kvm/hyp/vhe/debug-sr.c --include-headers --in-place +*/ + + +@exists@ +identifier vcpu; +fresh identifier vcpu_ctxt = vcpu ## "_ctxt"; +identifier fc; +@@ +<... +( + struct kvm_vcpu *vcpu = NULL; ++ struct kvm_cpu_context *vcpu_ctxt; +| + struct kvm_vcpu *vcpu = ...; ++ struct kvm_cpu_context *vcpu_ctxt = &vcpu_ctxt(vcpu); +| + struct kvm_vcpu *vcpu; ++ struct kvm_cpu_context *vcpu_ctxt; +) +<... + vcpu = ...; ++ vcpu_ctxt = &vcpu_ctxt(vcpu); +...> +fc(..., vcpu, ...) +...> + +@exists@ +identifier func != {kvm_arch_vcpu_run_pid_change}; +identifier fc != {vcpu_ctxt}; +identifier vcpu; +fresh identifier vcpu_ctxt = vcpu ## "_ctxt"; +@@ +func(..., struct kvm_vcpu *vcpu, ...) { ++ struct kvm_cpu_context *vcpu_ctxt = &vcpu_ctxt(vcpu); +<+... +fc(..., vcpu, ...) +...+> + } + +@@ +expression a, b; +identifier vcpu; +fresh identifier vcpu_ctxt = vcpu ## "_ctxt"; +iterator name kvm_for_each_vcpu; +identifier fc; +@@ +kvm_for_each_vcpu(a, vcpu, b) + { ++ vcpu_ctxt = &vcpu_ctxt(vcpu); +<+... +fc(..., vcpu, ...) +...+> + } + +@@ +identifier vcpu_ctxt, vcpu; +iterator name kvm_for_each_vcpu; +type T; +identifier x; +statement S1, S2; +@@ +kvm_for_each_vcpu(...) + { +- vcpu_ctxt = &vcpu_ctxt(vcpu); +... when != S1 ++ vcpu_ctxt = &vcpu_ctxt(vcpu); + S2 + ... when any + } + +@ +disable optional_qualifier +exists +@ +identifier vcpu; +identifier vcpu_ctxt; +@@ +<... + const struct kvm_vcpu *vcpu = ...; +- struct kvm_cpu_context *vcpu_ctxt = &vcpu_ctxt(vcpu); ++ const struct kvm_cpu_context *vcpu_ctxt = &vcpu_ctxt(vcpu); +...> + +@disable optional_qualifier@ +identifier func, vcpu; +identifier vcpu_ctxt; +@@ +func(..., const struct kvm_vcpu *vcpu, ...) { +- struct kvm_cpu_context *vcpu_ctxt = &vcpu_ctxt(vcpu); ++ const struct kvm_cpu_context *vcpu_ctxt = &vcpu_ctxt(vcpu); +... + } + +@exists@ +expression r1, r2; +identifier vcpu; +fresh identifier vcpu_ctxt = vcpu ## "_ctxt"; +@@ +( +- vcpu_gp_regs(vcpu) ++ ctxt_gp_regs(vcpu_ctxt) +| +- vcpu_spsr_abt(vcpu) ++ ctxt_spsr_abt(vcpu_ctxt) +| +- vcpu_spsr_und(vcpu) ++ ctxt_spsr_und(vcpu_ctxt) +| +- vcpu_spsr_irq(vcpu) ++ ctxt_spsr_irq(vcpu_ctxt) +| +- vcpu_spsr_fiq(vcpu) ++ ctxt_spsr_fiq(vcpu_ctxt) +| +- vcpu_fp_regs(vcpu) ++ ctxt_fp_regs(vcpu_ctxt) +| +- __vcpu_sys_reg(vcpu, r1) ++ ctxt_sys_reg(vcpu_ctxt, r1) +| +- __vcpu_read_sys_reg(vcpu, r1) ++ __ctxt_read_sys_reg(vcpu_ctxt, r1) +| +- __vcpu_write_sys_reg(vcpu, r1, r2) ++ __ctxt_write_sys_reg(vcpu_ctxt, r1, r2) +| +- __vcpu_write_spsr(vcpu, r1) ++ __ctxt_write_spsr(vcpu_ctxt, r1) +| +- __vcpu_write_spsr_abt(vcpu, r1) ++ __ctxt_write_spsr_abt(vcpu_ctxt, r1) +| +- __vcpu_write_spsr_und(vcpu, r1) ++ __ctxt_write_spsr_und(vcpu_ctxt, r1) +| +- vcpu_pc(vcpu) ++ ctxt_pc(vcpu_ctxt) +| +- vcpu_cpsr(vcpu) ++ ctxt_cpsr(vcpu_ctxt) +| +- vcpu_mode_is_32bit(vcpu) ++ ctxt_mode_is_32bit(vcpu_ctxt) +| +- vcpu_set_thumb(vcpu) ++ ctxt_set_thumb(vcpu_ctxt) +| +- vcpu_get_reg(vcpu, r1) ++ ctxt_get_reg(vcpu_ctxt, r1) +| +- vcpu_set_reg(vcpu, r1, r2) ++ ctxt_set_reg(vcpu_ctxt, r1, r2) +) + + +/* Handles one case of a call within a call. */ +@@ +expression r1, r2; +identifier vcpu; +fresh identifier vcpu_ctxt = vcpu ## "_ctxt"; +@@ +- vcpu_pc(vcpu) ++ ctxt_pc(vcpu_ctxt) + +// diff --git a/cocci_refactor/add_hypstate.cocci b/cocci_refactor/add_hypstate.cocci new file mode 100644 index 000000000000..e8635d0e8f57 --- /dev/null +++ b/cocci_refactor/add_hypstate.cocci @@ -0,0 +1,125 @@ +// + +/* +FILES="$(find arch/arm64/kvm/hyp -name "*.[ch]" ! -name "debug-sr*") arch/arm64/include/asm/kvm_hyp.h" +spatch --sp-file add_hypstate.cocci $FILES --in-place +*/ + +@exists@ +identifier vcpu; +fresh identifier hyps = vcpu ## "_hyps"; +identifier fc; +@@ +<... +( + struct kvm_vcpu *vcpu = NULL; ++ struct vcpu_hyp_state *hyps; +| + struct kvm_vcpu *vcpu = ...; ++ struct vcpu_hyp_state *hyps = &hyp_state(vcpu); +| + struct kvm_vcpu *vcpu; ++ struct vcpu_hyp_state *hyps; +) +<... + vcpu = ...; ++ hyps = &hyp_state(vcpu); +...> +fc(..., vcpu, ...) +...> + +@exists@ +identifier func != {kvm_arch_vcpu_run_pid_change}; +identifier vcpu; +fresh identifier hyps = vcpu ## "_hyps"; +identifier fc; +@@ +func(..., struct kvm_vcpu *vcpu, ...) { ++ struct vcpu_hyp_state *hyps = &hyp_state(vcpu); +<+... +fc(..., vcpu, ...) +...+> + } + +@@ +expression a, b; +identifier vcpu; +fresh identifier hyps = vcpu ## "_hyps"; +iterator name kvm_for_each_vcpu; +identifier fc; +@@ +kvm_for_each_vcpu(a, vcpu, b) + { ++ hyps = &hyp_state(vcpu); +<+... +fc(..., vcpu, ...) +...+> + } + +@@ +identifier hyps, vcpu; +iterator name kvm_for_each_vcpu; +statement S1, S2; +@@ +kvm_for_each_vcpu(...) + { +- hyps = &hyp_state(vcpu); +... when != S1 ++ hyps = &hyp_state(vcpu); + S2 + ... when any + } + +@ +disable optional_qualifier +exists +@ +identifier vcpu, hyps; +@@ +<... + const struct kvm_vcpu *vcpu = ...; +- struct vcpu_hyp_state *hyps = &hyp_state(vcpu); ++ const struct vcpu_hyp_state *hyps = &hyp_state(vcpu); +...> + + +@@ +identifier func, vcpu, hyps; +@@ +func(..., const struct kvm_vcpu *vcpu, ...) { +- struct vcpu_hyp_state *hyps = &hyp_state(vcpu); ++ const struct vcpu_hyp_state *hyps = &hyp_state(vcpu); +... + } + +@exists@ +identifier vcpu; +fresh identifier hyps = vcpu ## "_hyps"; +@@ +( +- vcpu_hcr_el2(vcpu) ++ hyp_state_hcr_el2(hyps) +| +- vcpu_mdcr_el2(vcpu) ++ hyp_state_mdcr_el2(hyps) +| +- vcpu_vsesr_el2(vcpu) ++ hyp_state_vsesr_el2(hyps) +| +- vcpu_fault(vcpu) ++ hyp_state_fault(hyps) +| +- vcpu_flags(vcpu) ++ hyp_state_flags(hyps) +| +- vcpu_has_sve(vcpu) ++ hyp_state_has_sve(hyps) +| +- vcpu_has_ptrauth(vcpu) ++ hyp_state_has_ptrauth(hyps) +| +- kvm_arm_vcpu_sve_finalized(vcpu) ++ kvm_arm_hyp_state_sve_finalized(hyps) +) + +// diff --git a/cocci_refactor/hyp_ctxt.cocci b/cocci_refactor/hyp_ctxt.cocci new file mode 100644 index 000000000000..af7974e3a502 --- /dev/null +++ b/cocci_refactor/hyp_ctxt.cocci @@ -0,0 +1,38 @@ +// Remove vcpu if all we're using is hypstate and ctxt + +/* +FILES="$(find arch/arm64/kvm/hyp -name "*.[ch]")" +spatch --sp-file hyp_ctxt.cocci $FILES --in-place; +*/ + +// + +@remove@ +identifier func !~ "^trap_|^access_|dbg_to_reg|check_pmu_access_disabled|match_mpidr|get_ctr_el0|emulate_cp|unhandled_cp_access|index_to_sys_reg_desc|kvm_pmu_|pmu_counter_idx_valid|reset_|read_from_write_only|write_to_read_only|undef_access|vgic_|kvm_handle_|handle_sve|handle_smc|handle_no_fpsimd|id_visibility|reg_to_dbg|ptrauth_visibility|sve_visibility|kvm_arch_sched_in|kvm_arch_vcpu_|kvm_vcpu_pmu_|kvm_psci_|kvm_arm_copy_fw_reg_indices|kvm_arm_pvtime_|kvm_trng_|kvm_arm_timer_"; +identifier vcpu; +fresh identifier vcpu_ctxt = vcpu ## "_ctxt"; +fresh identifier vcpu_hyps = vcpu ## "_hyps"; +identifier hyps_remove; +identifier ctxt_remove; +@@ +func(..., +- struct kvm_vcpu *vcpu ++ struct kvm_cpu_context *vcpu_ctxt, struct vcpu_hyp_state *vcpu_hyps +,...) { +?- struct vcpu_hyp_state *hyps_remove = ...; +?- struct kvm_cpu_context *ctxt_remove = ...; +... when != vcpu + } + +@@ +identifier vcpu; +fresh identifier vcpu_ctxt = vcpu ## "_ctxt"; +fresh identifier vcpu_hyps = vcpu ## "_hyps"; +identifier remove.func; +@@ + func( +- vcpu ++ vcpu_ctxt, vcpu_hyps + , ...) + +// \ No newline at end of file diff --git a/cocci_refactor/range.cocci b/cocci_refactor/range.cocci new file mode 100644 index 000000000000..d99b9ee30657 --- /dev/null +++ b/cocci_refactor/range.cocci @@ -0,0 +1,50 @@ + + +// + +/* + FILES="$(find arch/arm64 -name "*.[ch]") include/kvm/arm_hypercalls.h"; spatch --sp-file range.cocci $FILES +*/ + +@initialize:python@ +@@ +starts = ("start", "begin", "from", "floor", "addr", "kaddr") +ends = ("size", "length", "len") + +//ends = ("end", "to", "ceiling", "size", "length", "len") + + +@start_end@ +identifier f; +type A, B; +identifier start, end; +parameter list[n] ps; +@@ +f(ps, A start, B end, ...) { +... +} + +@script:python@ +start << start_end.start; +end << start_end.end; +ta << start_end.A; +tb << start_end.B; +@@ + +if ta != tb and tb != "size_t": + cocci.include_match(False) +elif not any(x in start for x in starts) and not any(x in end for x in ends): + cocci.include_match(False) + +@@ +identifier f = start_end.f; +expression list[start_end.n] xs; +expression a, b; +@@ +( +* f(xs, a, a, ...) +| +* f(xs, a, a - b, ...) +) + +// \ No newline at end of file diff --git a/cocci_refactor/remove_unused.cocci b/cocci_refactor/remove_unused.cocci new file mode 100644 index 000000000000..c06278398198 --- /dev/null +++ b/cocci_refactor/remove_unused.cocci @@ -0,0 +1,69 @@ +// + +/* +spatch --sp-file remove_unused.cocci --dir arch/arm64/kvm/hyp --in-place --include-headers --force-diff +*/ + +@@ +identifier hyps; +@@ +{ +... +( +- struct vcpu_hyp_state *hyps = ...; +| +- struct vcpu_hyp_state *hyps; +) +... when != hyps + when != if (...) { <+...hyps...+> } +?- hyps = ...; +... when != hyps + when != if (...) { <+...hyps...+> } +} + +@@ +identifier vcpu_ctxt; +@@ +{ +... +( +- struct kvm_cpu_context *vcpu_ctxt = ...; +| +- struct kvm_cpu_context *vcpu_ctxt; +) +... when != vcpu_ctxt + when != if (...) { <+...vcpu_ctxt...+> } +?- vcpu_ctxt = ...; +... when != vcpu_ctxt + when != if (...) { <+...vcpu_ctxt...+> } +} + +@@ +identifier x; +identifier func; +statement S; +@@ +func(...) + { +... +struct kvm_cpu_context *x = ...; ++ +S +... + } + +@@ +identifier x; +identifier func; +statement S; +@@ +func(...) + { +... +struct vcpu_hyp_state *x = ...; ++ +S +... + } + +// diff --git a/cocci_refactor/test.cocci b/cocci_refactor/test.cocci new file mode 100644 index 000000000000..5eb685240ce7 --- /dev/null +++ b/cocci_refactor/test.cocci @@ -0,0 +1,20 @@ +/* + FILES="$(find arch/arm64 -name "*.[ch]") include/kvm/arm_hypercalls.h"; spatch --sp-file test.cocci $FILES + +*/ + +@r@ +identifier fn; +@@ +fn(...) { + hello; + ... +} + +@@ +identifier r.fn; +@@ +static fn(...) { ++ world; + ... +} diff --git a/cocci_refactor/use_ctxt.cocci b/cocci_refactor/use_ctxt.cocci new file mode 100644 index 000000000000..f3f961f567fd --- /dev/null +++ b/cocci_refactor/use_ctxt.cocci @@ -0,0 +1,32 @@ +// +/* +spatch --sp-file use_ctxt.cocci --dir arch/arm64/kvm/hyp --ignore debug-sr --include-headers --in-place +spatch --sp-file use_ctxt.cocci --dir arch/arm64/kvm/hyp --ignore debug-sr --include-headers --in-place +*/ + +@remove_vcpu@ +identifier vcpu; +fresh identifier vcpu_ctxt = vcpu ## "_ctxt"; +identifier ctxt_remove; +identifier func !~ "(reset_unknown|reset_val|kvm_pmu_valid_counter_mask|reset_pmcr|kvm_arch_vcpu_in_kernel|__vgic_v3_)"; +@@ +func( +- struct kvm_vcpu *vcpu ++ struct kvm_cpu_context *vcpu_ctxt +, ...) { +- struct kvm_cpu_context *ctxt_remove = ...; +... when != vcpu + when != if (...) { <+...vcpu...+> } +} + +@@ +identifier vcpu; +fresh identifier vcpu_ctxt = vcpu ## "_ctxt"; +identifier func = remove_vcpu.func; +@@ +func( +- vcpu ++ vcpu_ctxt + , ...) + +// diff --git a/cocci_refactor/use_ctxt_access.cocci b/cocci_refactor/use_ctxt_access.cocci new file mode 100644 index 000000000000..74f94141e662 --- /dev/null +++ b/cocci_refactor/use_ctxt_access.cocci @@ -0,0 +1,39 @@ +// + +/* +spatch --sp-file use_ctxt_access.cocci --dir arch/arm64/kvm/ --include-headers --in-place +*/ + +@@ +constant r; +@@ +- __ctxt_sys_reg(&vcpu->arch.ctxt, r) ++ &__vcpu_sys_reg(vcpu, r) + +@@ +identifier r; +@@ +- vcpu->arch.ctxt.regs.r ++ vcpu_gp_regs(vcpu)->r + +@@ +identifier r; +@@ +- vcpu->arch.ctxt.fp_regs.r ++ vcpu_fp_regs(vcpu)->r + +@@ +identifier r; +fresh identifier accessor = "vcpu_" ## r; +@@ +- &vcpu->arch.ctxt.r ++ accessor(vcpu) + +@@ +identifier r; +fresh identifier accessor = "vcpu_" ## r; +@@ +- vcpu->arch.ctxt.r ++ *accessor(vcpu) + +// \ No newline at end of file diff --git a/cocci_refactor/use_hypstate.cocci b/cocci_refactor/use_hypstate.cocci new file mode 100644 index 000000000000..f685149de748 --- /dev/null +++ b/cocci_refactor/use_hypstate.cocci @@ -0,0 +1,63 @@ +// + +/* +FILES="$(find arch/arm64/kvm/hyp -name "*.[ch]" ! -name "debug-sr*") arch/arm64/include/asm/kvm_hyp.h" +spatch --sp-file use_hypstate.cocci $FILES --in-place +*/ + + +@remove_vcpu_hyps@ +identifier vcpu; +fresh identifier hyps = vcpu ## "_hyps"; +identifier hyps_remove; +identifier func; +@@ +func( +- struct kvm_vcpu *vcpu ++ struct vcpu_hyp_state *hyps +, ...) { +- struct vcpu_hyp_state *hyps_remove = ...; +... when != vcpu + when != if (...) { <+...vcpu...+> } +} + +@@ +identifier vcpu; +fresh identifier hyps = vcpu ## "_hyps"; +identifier func = remove_vcpu_hyps.func; +@@ +func( +- vcpu ++ hyps + , ...) + +@remove_vcpu_hyps_ctxt@ +identifier vcpu; +fresh identifier hyps = vcpu ## "_hyps"; +identifier hyps_remove; +identifier ctxt_remove; +identifier func; +@@ +func( +- struct kvm_vcpu *vcpu ++ struct vcpu_hyp_state *hyps +, ...) { +- struct vcpu_hyp_state *hyps_remove = ...; +- struct kvm_cpu_context *ctxt_remove = ...; +... when != vcpu + when != if (...) { <+...vcpu...+> } + when != ctxt_remove + when != if (...) { <+...ctxt_remove...+> } +} + +@@ +identifier vcpu; +fresh identifier hyps = vcpu ## "_hyps"; +identifier func = remove_vcpu_hyps_ctxt.func; +@@ +func( +- vcpu ++ hyps + , ...) + +// diff --git a/cocci_refactor/vcpu_arch_ctxt.cocci b/cocci_refactor/vcpu_arch_ctxt.cocci new file mode 100644 index 000000000000..69b3a000de4e --- /dev/null +++ b/cocci_refactor/vcpu_arch_ctxt.cocci @@ -0,0 +1,13 @@ +// spatch --sp-file vcpu_arch_ctxt.cocci --no-includes --include-headers --dir arch/arm64 + +// +@@ +identifier vcpu; +@@ +( +- vcpu->arch.ctxt.regs ++ vcpu_gp_regs(vcpu) +| +- vcpu->arch.ctxt.fp_regs ++ vcpu_fp_regs(vcpu) +) diff --git a/cocci_refactor/vcpu_declr.cocci b/cocci_refactor/vcpu_declr.cocci new file mode 100644 index 000000000000..59cd46bd6b2d --- /dev/null +++ b/cocci_refactor/vcpu_declr.cocci @@ -0,0 +1,59 @@ + +/* +FILES="$(find arch/arm64 -name "*.[ch]") include/kvm/arm_hypercalls.h"; spatch --sp-file vcpu_declr.cocci $FILES --in-place +*/ + +// + +@@ +identifier vcpu; +expression E; +@@ +<... +- struct kvm_vcpu *vcpu; ++ struct kvm_vcpu *vcpu = E; + +- vcpu = E; +...> + + +/* +@@ +identifier vcpu; +identifier f1, f2; +@@ +f1(...) +{ +- struct kvm_vcpu *vcpu = NULL; ++ struct kvm_vcpu *vcpu; +... when != f2(..., vcpu, ...) +} +*/ + +/* +@find_after@ +identifier vcpu; +position p; +identifier f; +@@ +<... + struct kvm_vcpu *vcpu@p; + ... when != vcpu = ...; + f(..., vcpu, ...); +...> + +@@ +identifier vcpu; +expression E; +position p != find_after.p; +@@ +<... +- struct kvm_vcpu *vcpu@p; ++ struct kvm_vcpu *vcpu = E; + ... +- vcpu = E; +...> + +*/ + +// diff --git a/cocci_refactor/vcpu_flags.cocci b/cocci_refactor/vcpu_flags.cocci new file mode 100644 index 000000000000..609bb7bd7bd0 --- /dev/null +++ b/cocci_refactor/vcpu_flags.cocci @@ -0,0 +1,10 @@ +// spatch --sp-file el2_def_flags.cocci --no-includes --include-headers --dir arch/arm64 + +// +@@ +expression vcpu; +@@ + +- vcpu->arch.flags ++ vcpu_flags(vcpu) +// \ No newline at end of file diff --git a/cocci_refactor/vcpu_hyp_accessors.cocci b/cocci_refactor/vcpu_hyp_accessors.cocci new file mode 100644 index 000000000000..506b56f7216f --- /dev/null +++ b/cocci_refactor/vcpu_hyp_accessors.cocci @@ -0,0 +1,35 @@ +// + +/* +spatch --sp-file vcpu_hyp_accessors.cocci --dir arch/arm64 --include-headers --in-place +*/ + +@find_defines@ +identifier macro; +identifier vcpu; +position p; +@@ +#define macro(vcpu) vcpu@p + +@@ +identifier vcpu; +position p != find_defines.p; +@@ +( +- vcpu@p->arch.hcr_el2 ++ vcpu_hcr_el2(vcpu) +| +- vcpu@p->arch.mdcr_el2 ++ vcpu_mdcr_el2(vcpu) +| +- vcpu@p->arch.vsesr_el2 ++ vcpu_vsesr_el2(vcpu) +| +- vcpu@p->arch.fault ++ vcpu_fault(vcpu) +| +- vcpu@p->arch.flags ++ vcpu_flags(vcpu) +) + +// diff --git a/cocci_refactor/vcpu_hyp_state.cocci b/cocci_refactor/vcpu_hyp_state.cocci new file mode 100644 index 000000000000..3005a6f11871 --- /dev/null +++ b/cocci_refactor/vcpu_hyp_state.cocci @@ -0,0 +1,30 @@ +// + +// spatch --sp-file vcpu_hyp_state.cocci --no-includes --include-headers --dir arch/arm64 --very-quiet --in-place + +@@ +expression vcpu; +@@ +- vcpu->arch. ++ vcpu->arch.hyp_state. +( + hcr_el2 +| + mdcr_el2 +| + vsesr_el2 +| + fault +| + flags +| + sysregs_loaded_on_cpu +) + +@@ +identifier arch; +@@ +- arch.fault ++ arch.hyp_state.fault + +// \ No newline at end of file diff --git a/cocci_refactor/vgic3_cpu.cocci b/cocci_refactor/vgic3_cpu.cocci new file mode 100644 index 000000000000..f7495b2e49cb --- /dev/null +++ b/cocci_refactor/vgic3_cpu.cocci @@ -0,0 +1,118 @@ +// + +/* +spatch --sp-file vgic3_cpu.cocci arch/arm64/kvm/hyp/vgic-v3-sr.c --in-place +*/ + + +@@ +identifier vcpu; +fresh identifier vcpu_hyps = vcpu ## "_hyps"; +@@ +( +- kvm_vcpu_sys_get_rt ++ kvm_hyp_state_sys_get_rt +| +- kvm_vcpu_get_esr ++ kvm_hyp_state_get_esr +) +- (vcpu) ++ (vcpu_hyps) + +@add_cpu_if@ +identifier func; +identifier c; +@@ +int func( +- struct kvm_vcpu *vcpu ++ struct vgic_v3_cpu_if *cpu_if + , ...) +{ +<+... +- vcpu->arch.vgic_cpu.vgic_v3.c ++ cpu_if->c +...+> +} + +@@ +identifier func = add_cpu_if.func; +@@ + func( +- vcpu ++ cpu_if + , ... + ) + + +@add_vgic_ctxt_hyps@ +identifier func; +@@ +void func( +- struct kvm_vcpu *vcpu ++ struct vgic_v3_cpu_if *cpu_if, struct kvm_cpu_context *vcpu_ctxt, struct vcpu_hyp_state *vcpu_hyps + , ...) { +?- struct vcpu_hyp_state *vcpu_hyps = ...; +?- struct kvm_cpu_context *vcpu_ctxt = ...; + ... + } + +@@ +identifier func = add_vgic_ctxt_hyps.func; +@@ + func( +- vcpu, ++ cpu_if, vcpu_ctxt, vcpu_hyps, + ... + ) + + +@find_calls@ +identifier fn; +type a, b; +@@ +- void (*fn)(struct kvm_vcpu *, a, b); ++ void (*fn)(struct vgic_v3_cpu_if *, struct kvm_cpu_context *, struct vcpu_hyp_state *, a, b); + +@@ +identifier fn = find_calls.fn; +identifier a, b; +@@ +- fn(vcpu, a, b); ++ fn(cpu_if, vcpu_ctxt, vcpu_hyps, a, b); + +@@ +@@ +int __vgic_v3_perform_cpuif_access(struct kvm_vcpu *vcpu) { ++ struct vgic_v3_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v3; +... +} + +@remove@ +identifier func; +identifier vcpu; +fresh identifier vcpu_ctxt = vcpu ## "_ctxt"; +fresh identifier vcpu_hyps = vcpu ## "_hyps"; +identifier hyps_remove; +identifier ctxt_remove; +@@ +func(..., +- struct kvm_vcpu *vcpu ++ struct kvm_cpu_context *vcpu_ctxt, struct vcpu_hyp_state *vcpu_hyps +,...) { +?- struct vcpu_hyp_state *hyps_remove = ...; +?- struct kvm_cpu_context *ctxt_remove = ...; +... when != vcpu + } + +@@ +identifier vcpu; +fresh identifier vcpu_ctxt = vcpu ## "_ctxt"; +fresh identifier vcpu_hyps = vcpu ## "_hyps"; +identifier remove.func; +@@ + func( +- vcpu ++ vcpu_ctxt, vcpu_hyps + , ...) + +//