From nobody Sat Nov 30 12:49:16 2024 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) (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 5B9121B78FA for ; Mon, 9 Sep 2024 12:47:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725886049; cv=none; b=GcN+nfd2drF7xnkf7XoTjXfwBAOQ0Z6QitqwL6OI980eVtXjDTWlwr9ZFil32kXobaniVP8e5j0U7tCilT2TKonjIC6u3OE8/ADs0XC9lfBBGv38N4tHWuEguBT9D993Js1cxZGTpXDFhiVxflUhFn/e6zSXvIlE3wpJjMyq7lI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725886049; c=relaxed/simple; bh=HPr21emQzpEuVlDmp/qQjpu5BVkYPBhIboCvezWrCNI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=iYp7iQHvOTgkPdV9NIpTAFboQQu0CzCDwEv4s3eN/VUC43iD9Sgx0KgZ0bK7yB+ORvOdYiZUsLwnFGgubvO9ESle2XUjEjoPmvSN8FVPlLbWmsf8yMeTKJa/0xPy+G95iVXfHJtjTSBNN2urMA5C4y69lKC/me138u21cgqKxcQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--sebastianene.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=bBrV+HtL; arc=none smtp.client-ip=209.85.128.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--sebastianene.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="bBrV+HtL" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-42cb827e9b6so7465295e9.0 for ; Mon, 09 Sep 2024 05:47:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1725886047; x=1726490847; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=SKJ1NP3DrAm45Ot6yL2cReAMG8qo73bbwYgKiOjz384=; b=bBrV+HtLAKiBnJYjNYS2770a543tqIFLYF64nZDbZd6wVrnEQxHRmWtmbkPAEyiLdm JeskoS8JhKggVYJGS6q3o9wA0cxmPuMkljIf4KcOdDtZ+OCoqRvNBdTa0ajApTePHXda RJQHQOw/Rp4NkYqqlN61bh0w2kEyc5lt1C78oOzbepG3eZADNncpkw4wEwTh6qvHEVKy mqFYjnK2uXqmzFElSEXJj0eYErN4DUWxIysYo2YfPVsOP9o/1DaguMBvepYA8Tnrln2d FIJ/imdE9ZlC7nNRiuPl1a3yjFXvik9l0S8/XLAaHUSr77r5gudiDZUOuBgTh+IIgOaC sPTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725886047; x=1726490847; 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=SKJ1NP3DrAm45Ot6yL2cReAMG8qo73bbwYgKiOjz384=; b=cqnP6UyfJDytU5YwVSgTH+E8ER0lSEPioesD/9LKP+V6UJxIsP3Gbe2NeQbM0VO2es W3ifG8Gj9zTHVKZJ1A2yMdG+DBw2QsTrxn4IQGZh1aADfeR76T/mL1348DdnTCPMoyT0 osb3+xn9sQqlGKuFdEZeuJmB4+2swjFNnRQ8MiIZz54YmKooQYYNHtUDJQxDXDN0ess4 yuXHK4pVeQfdJvORA43Fp3V3FoKbt4bGgBbQhuKSouULOJwlS315AGE/WqroX8zNbkKj cKqt6VZwY9nseJfxYpgStgEhniNmUB8jGuOr/PGL1surx53poDKleVNECJY+s9lIg3nJ YiVg== X-Forwarded-Encrypted: i=1; AJvYcCVQZBjagDxWc4Lk8JIfDawIa6EuO6DKEvblFBQg6Xd8CTBzlz6loNYat4TLNSF83gG7NxhAScT//QGKgbs=@vger.kernel.org X-Gm-Message-State: AOJu0Yz9LXEoAmQGIcmzeryWtQ5ektkjkK5RYxyvoIEgvrZH4vkbSloz BLu/AbrfBS0YckbyIcgEeaU5Wbie17kqKU6PQUrB3qpFF081+pTTMvpbEw/d6O1N2skz/tCM6u/ QG9l8sZ8v3PgOC/kMZxcgo/cqEw== X-Google-Smtp-Source: AGHT+IGB9bwzA+fwuWPOhmU2PE2PuSrdb0vA8nxugJEsLMz4E+4/m65aP47k2xrfDB7zI8uLZNKmhk8HfA0/HNK+S/I= X-Received: from sebkvm.c.googlers.com ([fda3:e722:ac3:cc00:68:fe9:ac10:f1b7]) (user=sebastianene job=sendgmr) by 2002:a05:600c:2d8c:b0:42c:ae30:fc33 with SMTP id 5b1f17b1804b1-42cae30fd4fmr872025e9.4.1725886046868; Mon, 09 Sep 2024 05:47:26 -0700 (PDT) Date: Mon, 9 Sep 2024 12:47:17 +0000 In-Reply-To: <20240909124721.1672199-1-sebastianene@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240909124721.1672199-1-sebastianene@google.com> X-Mailer: git-send-email 2.46.0.469.g59c65b2a67-goog Message-ID: <20240909124721.1672199-2-sebastianene@google.com> Subject: [PATCH v10 1/5] KVM: arm64: Move pagetable definitions to common header From: Sebastian Ene To: akpm@linux-foundation.org, alexghiti@rivosinc.com, ankita@nvidia.com, ardb@kernel.org, catalin.marinas@arm.com, christophe.leroy@csgroup.eu, james.morse@arm.com, vdonnefort@google.com, mark.rutland@arm.com, maz@kernel.org, oliver.upton@linux.dev, rananta@google.com, ryan.roberts@arm.com, sebastianene@google.com, shahuang@redhat.com, suzuki.poulose@arm.com, will@kernel.org, yuzenghui@huawei.com Cc: kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, kernel-team@android.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In preparation for using the stage-2 definitions in ptdump, move some of these macros in the common header. Signed-off-by: Sebastian Ene --- arch/arm64/include/asm/kvm_pgtable.h | 42 ++++++++++++++++++++++++++++ arch/arm64/kvm/hyp/pgtable.c | 42 ---------------------------- 2 files changed, 42 insertions(+), 42 deletions(-) diff --git a/arch/arm64/include/asm/kvm_pgtable.h b/arch/arm64/include/asm/= kvm_pgtable.h index 19278dfe7978..03f4c3d7839c 100644 --- a/arch/arm64/include/asm/kvm_pgtable.h +++ b/arch/arm64/include/asm/kvm_pgtable.h @@ -59,6 +59,48 @@ typedef u64 kvm_pte_t; =20 #define KVM_PHYS_INVALID (-1ULL) =20 +#define KVM_PTE_LEAF_ATTR_LO GENMASK(11, 2) + +#define KVM_PTE_LEAF_ATTR_LO_S1_ATTRIDX GENMASK(4, 2) +#define KVM_PTE_LEAF_ATTR_LO_S1_AP GENMASK(7, 6) +#define KVM_PTE_LEAF_ATTR_LO_S1_AP_RO \ + ({ cpus_have_final_cap(ARM64_KVM_HVHE) ? 2 : 3; }) +#define KVM_PTE_LEAF_ATTR_LO_S1_AP_RW \ + ({ cpus_have_final_cap(ARM64_KVM_HVHE) ? 0 : 1; }) +#define KVM_PTE_LEAF_ATTR_LO_S1_SH GENMASK(9, 8) +#define KVM_PTE_LEAF_ATTR_LO_S1_SH_IS 3 +#define KVM_PTE_LEAF_ATTR_LO_S1_AF BIT(10) + +#define KVM_PTE_LEAF_ATTR_LO_S2_MEMATTR GENMASK(5, 2) +#define KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R BIT(6) +#define KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W BIT(7) +#define KVM_PTE_LEAF_ATTR_LO_S2_SH GENMASK(9, 8) +#define KVM_PTE_LEAF_ATTR_LO_S2_SH_IS 3 +#define KVM_PTE_LEAF_ATTR_LO_S2_AF BIT(10) + +#define KVM_PTE_LEAF_ATTR_HI GENMASK(63, 50) + +#define KVM_PTE_LEAF_ATTR_HI_SW GENMASK(58, 55) + +#define KVM_PTE_LEAF_ATTR_HI_S1_XN BIT(54) + +#define KVM_PTE_LEAF_ATTR_HI_S2_XN BIT(54) + +#define KVM_PTE_LEAF_ATTR_HI_S1_GP BIT(50) + +#define KVM_PTE_LEAF_ATTR_S2_PERMS (KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R | \ + KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W | \ + KVM_PTE_LEAF_ATTR_HI_S2_XN) + +#define KVM_INVALID_PTE_OWNER_MASK GENMASK(9, 2) +#define KVM_MAX_OWNER_ID 1 + +/* + * Used to indicate a pte for which a 'break-before-make' sequence is in + * progress. + */ +#define KVM_INVALID_PTE_LOCKED BIT(10) + static inline bool kvm_pte_valid(kvm_pte_t pte) { return pte & KVM_PTE_VALID; diff --git a/arch/arm64/kvm/hyp/pgtable.c b/arch/arm64/kvm/hyp/pgtable.c index 9e2bbee77491..c3e9d77bba23 100644 --- a/arch/arm64/kvm/hyp/pgtable.c +++ b/arch/arm64/kvm/hyp/pgtable.c @@ -17,48 +17,6 @@ #define KVM_PTE_TYPE_PAGE 1 #define KVM_PTE_TYPE_TABLE 1 =20 -#define KVM_PTE_LEAF_ATTR_LO GENMASK(11, 2) - -#define KVM_PTE_LEAF_ATTR_LO_S1_ATTRIDX GENMASK(4, 2) -#define KVM_PTE_LEAF_ATTR_LO_S1_AP GENMASK(7, 6) -#define KVM_PTE_LEAF_ATTR_LO_S1_AP_RO \ - ({ cpus_have_final_cap(ARM64_KVM_HVHE) ? 2 : 3; }) -#define KVM_PTE_LEAF_ATTR_LO_S1_AP_RW \ - ({ cpus_have_final_cap(ARM64_KVM_HVHE) ? 0 : 1; }) -#define KVM_PTE_LEAF_ATTR_LO_S1_SH GENMASK(9, 8) -#define KVM_PTE_LEAF_ATTR_LO_S1_SH_IS 3 -#define KVM_PTE_LEAF_ATTR_LO_S1_AF BIT(10) - -#define KVM_PTE_LEAF_ATTR_LO_S2_MEMATTR GENMASK(5, 2) -#define KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R BIT(6) -#define KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W BIT(7) -#define KVM_PTE_LEAF_ATTR_LO_S2_SH GENMASK(9, 8) -#define KVM_PTE_LEAF_ATTR_LO_S2_SH_IS 3 -#define KVM_PTE_LEAF_ATTR_LO_S2_AF BIT(10) - -#define KVM_PTE_LEAF_ATTR_HI GENMASK(63, 50) - -#define KVM_PTE_LEAF_ATTR_HI_SW GENMASK(58, 55) - -#define KVM_PTE_LEAF_ATTR_HI_S1_XN BIT(54) - -#define KVM_PTE_LEAF_ATTR_HI_S2_XN BIT(54) - -#define KVM_PTE_LEAF_ATTR_HI_S1_GP BIT(50) - -#define KVM_PTE_LEAF_ATTR_S2_PERMS (KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R | \ - KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W | \ - KVM_PTE_LEAF_ATTR_HI_S2_XN) - -#define KVM_INVALID_PTE_OWNER_MASK GENMASK(9, 2) -#define KVM_MAX_OWNER_ID 1 - -/* - * Used to indicate a pte for which a 'break-before-make' sequence is in - * progress. - */ -#define KVM_INVALID_PTE_LOCKED BIT(10) - struct kvm_pgtable_walk_data { struct kvm_pgtable_walker *walker; =20 --=20 2.46.0.469.g59c65b2a67-goog From nobody Sat Nov 30 12:49:16 2024 Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.74]) (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 2F8551B86CF for ; Mon, 9 Sep 2024 12:47:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725886053; cv=none; b=LS3awr/31U+f2bN70vVCKnc6/EF43GH2PxVKt88DKPUPsMQnNRYj9R63kUvfT+KhHNZeG1VCVTC0C5mF99aBEaYSV/RtHWV+5CPWMM6JE7+ABKC8idZU4uGEhBPbprMVkfmEwfAOS0ZDltDbGzrFScKJpuefV3Qc31lssTsFnn4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725886053; c=relaxed/simple; bh=VgYapO8i1qKDRRRsUo0MTuUblS7PQzfXlK6ZkBqLlsI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=kfT0urQdqpnH7z8vavQKQ3rtxnZLF5ZmhUaBhNwahkI0UaAlVf5nrtOptkIBQiDV4kpx4aiJRAhME/GlxLl2ZpwJE1oNv9tLcj5egwE+VZXdVh7NUFIkiNnw2FC4sM26cCp1+HCaj/9Xo3AVbxaHyiGAKNBvfWIBGM7ecKkhCdE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--sebastianene.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=e2y2wVsf; arc=none smtp.client-ip=209.85.221.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--sebastianene.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="e2y2wVsf" Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-374b981dd62so2108221f8f.3 for ; Mon, 09 Sep 2024 05:47:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1725886049; x=1726490849; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=rOyTNrjRYeC5V1YoJMilLcLgHHeqHqjXzH9wfdwY9sw=; b=e2y2wVsfXgUG62smecUmPZQOn1wigwpwCaBC5qjGE9V/EHSraUY+7Ijr87OIpLsxv4 XltXV2axxngwuQMAfq5CfKF7q8t02rvDlU59whIgQN9Kra/YHfpuDs0D6VJ0rudEnULC 43GSzWgRBx75BFyEJqJnt+8zDfZVuOrPwbuA1rqy2yvyVlvG0XJ6d8tHex+E+ozWs+Gz gb/nRmgXevm/2cSibQEBjidwGJdgTVIxgQZMGLfSBguflhVwax47EKcbkAj4rHc6u/72 /quWEGiA8BERRYqhXSM8lJPuptIDH0aHkoMOtxVkizvtkAgrgCafkNDcZao7WI7K6xgw ZUnQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725886049; x=1726490849; 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=rOyTNrjRYeC5V1YoJMilLcLgHHeqHqjXzH9wfdwY9sw=; b=Uwkt4HoP0QngNTzhemqyh6fbdVb5t+aAxKlWgM2/VcPWI91BH3qGcAUmcUr9u+i4Q2 2b6Ik9vDTNwJlyJh1RVmU5r5aOVB4eADwfWsBq1gn9w59Gw1gf05t8Q1g0xWQdMCHo+v fCkvCRPHHohtvv4/o6iTnnCoOpW0FK8Tn1Yc4+9Bdh02MS20ORm7ZSpUVm1fPH4LVnvc 2BdJ5yl55aY5ZoOz+k+NYEEmZuvfHqWcH80JFaEmBbsLCOBM4m/gtJuM4WVMEmCDOg6Y 2hfp9oe9SSwbkXx3kDj8V5939WzGvKghwFISdVIS6fV8fhYHD8MfgpMwLrX8ZhbufdZx IO3g== X-Forwarded-Encrypted: i=1; AJvYcCWQv063k6y8Z8ocHDFtxksyKf4sLckAxEu/VrCgrS5wOqfQDL/Ia7UeUr2E3oOL9Sb7Vn1XMBascujGsWs=@vger.kernel.org X-Gm-Message-State: AOJu0YwmLgdqZ0l8wUxZ32Rqnc64tencO/q1sOmxQQ/H9l+FesR3lLgm sqV9B+DYcI0ic4Xo/IroLqG8slv0gnspqRrFgcZ6ow5eY/g9rJYFssKJJ/4BmqQpZNMHfqMTdrN zURTtwGlmHuS6mf5/mYkD7Vj6Hw== X-Google-Smtp-Source: AGHT+IG+ip5lnTk9ayqB7jP8eGKNbUqkVkAL495AT+0kgCNA1tzshuDCmFE76GVP0BcqGg2a70PypynTTnHwd3R29A0= X-Received: from sebkvm.c.googlers.com ([fda3:e722:ac3:cc00:68:fe9:ac10:f1b7]) (user=sebastianene job=sendgmr) by 2002:a05:6000:1281:b0:374:cad2:a75e with SMTP id ffacd0b85a97d-378895c2527mr13872f8f.1.1725886049120; Mon, 09 Sep 2024 05:47:29 -0700 (PDT) Date: Mon, 9 Sep 2024 12:47:18 +0000 In-Reply-To: <20240909124721.1672199-1-sebastianene@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240909124721.1672199-1-sebastianene@google.com> X-Mailer: git-send-email 2.46.0.469.g59c65b2a67-goog Message-ID: <20240909124721.1672199-3-sebastianene@google.com> Subject: [PATCH v10 2/5] arm64: ptdump: Expose the attribute parsing functionality From: Sebastian Ene To: akpm@linux-foundation.org, alexghiti@rivosinc.com, ankita@nvidia.com, ardb@kernel.org, catalin.marinas@arm.com, christophe.leroy@csgroup.eu, james.morse@arm.com, vdonnefort@google.com, mark.rutland@arm.com, maz@kernel.org, oliver.upton@linux.dev, rananta@google.com, ryan.roberts@arm.com, sebastianene@google.com, shahuang@redhat.com, suzuki.poulose@arm.com, will@kernel.org, yuzenghui@huawei.com Cc: kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, kernel-team@android.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Reuse the descriptor parsing functionality to keep the same output format as the original ptdump code. In order for this to happen, move the state tracking objects into a common header. Signed-off-by: Sebastian Ene Acked-by: Will Deacon --- arch/arm64/include/asm/ptdump.h | 41 +++++++++++++++++++++++- arch/arm64/mm/ptdump.c | 55 +++++++-------------------------- 2 files changed, 51 insertions(+), 45 deletions(-) diff --git a/arch/arm64/include/asm/ptdump.h b/arch/arm64/include/asm/ptdum= p.h index 5b1701c76d1c..bd5d3ee3e8dc 100644 --- a/arch/arm64/include/asm/ptdump.h +++ b/arch/arm64/include/asm/ptdump.h @@ -9,6 +9,7 @@ =20 #include #include +#include =20 struct addr_marker { unsigned long start_address; @@ -21,14 +22,52 @@ struct ptdump_info { unsigned long base_addr; }; =20 +struct ptdump_prot_bits { + u64 mask; + u64 val; + const char *set; + const char *clear; +}; + +struct ptdump_pg_level { + const struct ptdump_prot_bits *bits; + char name[4]; + int num; + u64 mask; +}; + +/* + * The page dumper groups page table entries of the same type into a single + * description. It uses pg_state to track the range information while + * iterating over the pte entries. When the continuity is broken it then + * dumps out a description of the range. + */ +struct ptdump_pg_state { + struct ptdump_state ptdump; + struct seq_file *seq; + const struct addr_marker *marker; + const struct mm_struct *mm; + unsigned long start_address; + int level; + u64 current_prot; + bool check_wx; + unsigned long wx_pages; + unsigned long uxn_pages; +}; + void ptdump_walk(struct seq_file *s, struct ptdump_info *info); +void note_page(struct ptdump_state *pt_st, unsigned long addr, int level, + u64 val); #ifdef CONFIG_PTDUMP_DEBUGFS #define EFI_RUNTIME_MAP_END DEFAULT_MAP_WINDOW_64 void __init ptdump_debugfs_register(struct ptdump_info *info, const char *= name); #else static inline void ptdump_debugfs_register(struct ptdump_info *info, const char *name) { } -#endif +#endif /* CONFIG_PTDUMP_DEBUGFS */ +#else +static inline void note_page(void *pt_st, unsigned long addr, + int level, u64 val) { } #endif /* CONFIG_PTDUMP_CORE */ =20 #endif /* __ASM_PTDUMP_H */ diff --git a/arch/arm64/mm/ptdump.c b/arch/arm64/mm/ptdump.c index 6986827e0d64..404751fd30fe 100644 --- a/arch/arm64/mm/ptdump.c +++ b/arch/arm64/mm/ptdump.c @@ -38,33 +38,7 @@ seq_printf(m, fmt); \ }) =20 -/* - * The page dumper groups page table entries of the same type into a single - * description. It uses pg_state to track the range information while - * iterating over the pte entries. When the continuity is broken it then - * dumps out a description of the range. - */ -struct pg_state { - struct ptdump_state ptdump; - struct seq_file *seq; - const struct addr_marker *marker; - const struct mm_struct *mm; - unsigned long start_address; - int level; - u64 current_prot; - bool check_wx; - unsigned long wx_pages; - unsigned long uxn_pages; -}; - -struct prot_bits { - u64 mask; - u64 val; - const char *set; - const char *clear; -}; - -static const struct prot_bits pte_bits[] =3D { +static const struct ptdump_prot_bits pte_bits[] =3D { { .mask =3D PTE_VALID, .val =3D PTE_VALID, @@ -143,14 +117,7 @@ static const struct prot_bits pte_bits[] =3D { } }; =20 -struct pg_level { - const struct prot_bits *bits; - char name[4]; - int num; - u64 mask; -}; - -static struct pg_level pg_level[] __ro_after_init =3D { +static struct ptdump_pg_level pg_level[] __ro_after_init =3D { { /* pgd */ .name =3D "PGD", .bits =3D pte_bits, @@ -174,7 +141,7 @@ static struct pg_level pg_level[] __ro_after_init =3D { }, }; =20 -static void dump_prot(struct pg_state *st, const struct prot_bits *bits, +static void dump_prot(struct ptdump_pg_state *st, const struct ptdump_prot= _bits *bits, size_t num) { unsigned i; @@ -192,7 +159,7 @@ static void dump_prot(struct pg_state *st, const struct= prot_bits *bits, } } =20 -static void note_prot_uxn(struct pg_state *st, unsigned long addr) +static void note_prot_uxn(struct ptdump_pg_state *st, unsigned long addr) { if (!st->check_wx) return; @@ -206,7 +173,7 @@ static void note_prot_uxn(struct pg_state *st, unsigned= long addr) st->uxn_pages +=3D (addr - st->start_address) / PAGE_SIZE; } =20 -static void note_prot_wx(struct pg_state *st, unsigned long addr) +static void note_prot_wx(struct ptdump_pg_state *st, unsigned long addr) { if (!st->check_wx) return; @@ -221,10 +188,10 @@ static void note_prot_wx(struct pg_state *st, unsigne= d long addr) st->wx_pages +=3D (addr - st->start_address) / PAGE_SIZE; } =20 -static void note_page(struct ptdump_state *pt_st, unsigned long addr, int = level, - u64 val) +void note_page(struct ptdump_state *pt_st, unsigned long addr, int level, + u64 val) { - struct pg_state *st =3D container_of(pt_st, struct pg_state, ptdump); + struct ptdump_pg_state *st =3D container_of(pt_st, struct ptdump_pg_state= , ptdump); static const char units[] =3D "KMGTPE"; u64 prot =3D 0; =20 @@ -286,12 +253,12 @@ static void note_page(struct ptdump_state *pt_st, uns= igned long addr, int level, void ptdump_walk(struct seq_file *s, struct ptdump_info *info) { unsigned long end =3D ~0UL; - struct pg_state st; + struct ptdump_pg_state st; =20 if (info->base_addr < TASK_SIZE_64) end =3D TASK_SIZE_64; =20 - st =3D (struct pg_state){ + st =3D (struct ptdump_pg_state){ .seq =3D s, .marker =3D info->markers, .mm =3D info->mm, @@ -324,7 +291,7 @@ static struct ptdump_info kernel_ptdump_info __ro_after= _init =3D { =20 bool ptdump_check_wx(void) { - struct pg_state st =3D { + struct ptdump_pg_state st =3D { .seq =3D NULL, .marker =3D (struct addr_marker[]) { { 0, NULL}, --=20 2.46.0.469.g59c65b2a67-goog From nobody Sat Nov 30 12:49:16 2024 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) (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 AE1AE1B86FF for ; Mon, 9 Sep 2024 12:47:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725886055; cv=none; b=RVjYoFzxmV0suP1Gu6iI/dyz8fjzwYF9PlTOKz3OOjzofvshbM2odbBMB/+9Lj1j9wQHl2QtSKS9TKTwfDae9VpxXSMs5hT9EjggDiqO+OaOPDAmvoMkCc16KrVSyVvbuETB7ifh9GCMiCMLfBs8A4fxMyFdizLXC3NEmLoNQ6M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725886055; c=relaxed/simple; bh=VtR8TuQkNfUkd5xyRn11f5JLt2Zxl4vNilvMS7v/n5o=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=mnGyfmTrSy15pV0Hcxc8GBWlvF++2vWPJWVincEnOyD0qeXCLezVUVE9AV/5EDr/Ba0nAFKeWimRE13DSY2I1ByfX20B4HNGxuIVVSDr1mCkRrxv2bz+jy8D3591aNp42m5eK5HSmQej0Rf+ny6HWBBQkJpI7KOnfXWA4PonbWk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--sebastianene.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=p/375S7T; arc=none smtp.client-ip=209.85.128.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--sebastianene.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="p/375S7T" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-42ac185e26cso34022115e9.3 for ; Mon, 09 Sep 2024 05:47:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1725886052; x=1726490852; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=z5YaerhLrnlA1kDn0gvbQTCDMMWwt29NrOwuLXna95M=; b=p/375S7TjHJMK071UTt/aO7P0VMn2NK7DsEoiMC5pQ621qXfY7zE1Tu/P4H9ywWK26 +5Q0XBddtb4A6YXts781V/fdgdtep86GnO53EWdlcJeIzVjNrvGbWSwNxDFjnFbRMYoI 5HeEX3cSv513uK6NU9AGhubuprOo3O2K3SixGumcnUm+vfqB4idQSEdeVvVZt/xl9HA8 uHYz1lG4gMJ2pLg7WTjwYSpuvCUdmqshvnQmCDQJ3enRbDn9Ir4ThZbeeYGmk+1Kynpw h0XmRpd9sTMnJP++/J5RiFroAcWnuB0FP8mkVbnbU4z6hI5rbJqPeBI8z74tUmdgK+6U JCbA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725886052; x=1726490852; 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=z5YaerhLrnlA1kDn0gvbQTCDMMWwt29NrOwuLXna95M=; b=bO6lBxgPMnHa8a7WZna3g94gziYYtSk089jze9AoTttETSCqWdG3VU6ExbR/e0cnpI KEPBIio/Gvfwao8vE8ww77x1FWHKLO3snvXTNQzsYw2RoPbyE8104iC3vlEGgQHSASZF XPimVy+JcfYH6rjpzjVxr8lG7hldoQa2pAUc6JFW2fMsTtdnOqZeC1cgmuH0D6zkxajm pNoueiiuKVxkCToVxgJWPozTcvW0X6Ei0HNf6YZJ4rZGUWPRubF4io2JApNAq1P676Ko BryAZH2/V9Y6MLJPE6kGWTXjA2mmvEXKfDOW712xOYuhZsGpdSJNzrDNll7yocPHL6N5 pUQw== X-Forwarded-Encrypted: i=1; AJvYcCVtsxBHYPhebBQ7RKRppFN3s4ZD5PdTLR9ty6z5nkNeV7evZtv1B/7EtyTTl1Td1p0xlNi+mrMyQKpJmek=@vger.kernel.org X-Gm-Message-State: AOJu0YwW7lw2oXPTV9SeoGMXeYog6F4BxCGsshPS/7esOqOtPcgk+5NW 9Xag6txojxgPwCTBnNfZKnp0hO5vZTV0ZaZHPJP9kL8WmpdbCyidSdEc6NSIj2IjIgo5zWNHNjt X+YxZcB6Mz6Kj0MlE872bCoX2bg== X-Google-Smtp-Source: AGHT+IHvFQbj0rKg0Z2E+lg4FZtxEigwOSWg+BND77qixGLBIzbCZ5lpRaiz0wEgqDC0ZzG+egBczPT3YnPFvCYHpdQ= X-Received: from sebkvm.c.googlers.com ([fda3:e722:ac3:cc00:68:fe9:ac10:f1b7]) (user=sebastianene job=sendgmr) by 2002:a05:600c:6b6d:b0:42c:8875:95da with SMTP id 5b1f17b1804b1-42c9f9e090emr259485e9.7.1725886051855; Mon, 09 Sep 2024 05:47:31 -0700 (PDT) Date: Mon, 9 Sep 2024 12:47:19 +0000 In-Reply-To: <20240909124721.1672199-1-sebastianene@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240909124721.1672199-1-sebastianene@google.com> X-Mailer: git-send-email 2.46.0.469.g59c65b2a67-goog Message-ID: <20240909124721.1672199-4-sebastianene@google.com> Subject: [PATCH v10 3/5] arm64: ptdump: Use the ptdump description from a local context From: Sebastian Ene To: akpm@linux-foundation.org, alexghiti@rivosinc.com, ankita@nvidia.com, ardb@kernel.org, catalin.marinas@arm.com, christophe.leroy@csgroup.eu, james.morse@arm.com, vdonnefort@google.com, mark.rutland@arm.com, maz@kernel.org, oliver.upton@linux.dev, rananta@google.com, ryan.roberts@arm.com, sebastianene@google.com, shahuang@redhat.com, suzuki.poulose@arm.com, will@kernel.org, yuzenghui@huawei.com Cc: kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, kernel-team@android.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Rename the attributes description array to allow the parsing method to use the description from a local context. To be able to do this, store a pointer to the description array in the state structure. This will allow for the later introduced callers (stage_2 ptdump) to specify their own page table description format to the ptdump parser. Signed-off-by: Sebastian Ene Acked-by: Will Deacon --- arch/arm64/include/asm/ptdump.h | 1 + arch/arm64/mm/ptdump.c | 13 ++++++++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/arch/arm64/include/asm/ptdump.h b/arch/arm64/include/asm/ptdum= p.h index bd5d3ee3e8dc..71a7ed01153a 100644 --- a/arch/arm64/include/asm/ptdump.h +++ b/arch/arm64/include/asm/ptdump.h @@ -44,6 +44,7 @@ struct ptdump_pg_level { */ struct ptdump_pg_state { struct ptdump_state ptdump; + struct ptdump_pg_level *pg_level; struct seq_file *seq; const struct addr_marker *marker; const struct mm_struct *mm; diff --git a/arch/arm64/mm/ptdump.c b/arch/arm64/mm/ptdump.c index 404751fd30fe..ca53ef274a8b 100644 --- a/arch/arm64/mm/ptdump.c +++ b/arch/arm64/mm/ptdump.c @@ -117,7 +117,7 @@ static const struct ptdump_prot_bits pte_bits[] =3D { } }; =20 -static struct ptdump_pg_level pg_level[] __ro_after_init =3D { +static struct ptdump_pg_level kernel_pg_levels[] __ro_after_init =3D { { /* pgd */ .name =3D "PGD", .bits =3D pte_bits, @@ -192,6 +192,7 @@ void note_page(struct ptdump_state *pt_st, unsigned lon= g addr, int level, u64 val) { struct ptdump_pg_state *st =3D container_of(pt_st, struct ptdump_pg_state= , ptdump); + struct ptdump_pg_level *pg_level =3D st->pg_level; static const char units[] =3D "KMGTPE"; u64 prot =3D 0; =20 @@ -262,6 +263,7 @@ void ptdump_walk(struct seq_file *s, struct ptdump_info= *info) .seq =3D s, .marker =3D info->markers, .mm =3D info->mm, + .pg_level =3D &kernel_pg_levels[0], .level =3D -1, .ptdump =3D { .note_page =3D note_page, @@ -279,10 +281,10 @@ static void __init ptdump_initialize(void) { unsigned i, j; =20 - for (i =3D 0; i < ARRAY_SIZE(pg_level); i++) - if (pg_level[i].bits) - for (j =3D 0; j < pg_level[i].num; j++) - pg_level[i].mask |=3D pg_level[i].bits[j].mask; + for (i =3D 0; i < ARRAY_SIZE(kernel_pg_levels); i++) + if (kernel_pg_levels[i].bits) + for (j =3D 0; j < kernel_pg_levels[i].num; j++) + kernel_pg_levels[i].mask |=3D kernel_pg_levels[i].bits[j].mask; } =20 static struct ptdump_info kernel_ptdump_info __ro_after_init =3D { @@ -297,6 +299,7 @@ bool ptdump_check_wx(void) { 0, NULL}, { -1, NULL}, }, + .pg_level =3D &kernel_pg_levels[0], .level =3D -1, .check_wx =3D true, .ptdump =3D { --=20 2.46.0.469.g59c65b2a67-goog From nobody Sat Nov 30 12:49:16 2024 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) (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 E9AEE1B9B2B for ; Mon, 9 Sep 2024 12:47:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725886057; cv=none; b=Vb84aEv8jOIrTtG+aQKvNCKd4EB4nEdnql1iAyVUb6YT3w7s2pgtYruljHmCp1ClcYvJ1i75BGswamTVvLLT3218abIM+XBfIr6CYRF3JEXnQZMwbCODqSnxGfj0G73shXcVT90SuDJbha8a76lKNcTjvzfNToVKaBnhPVGF9tM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725886057; c=relaxed/simple; bh=zuE6bjdcg3jxewdqHSkBJadxfnaCAk197XNJPJ4G1wA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=FfBOoiKSGMWazOClhTC6yUobtemKrles4Cs+IpjTLjC5L6QdPiHog0Cpf7Q8HNDLNpRl+9HkMn0wn9Pb+n+Yz28TUlwskKGF+utSfI/PLYg4B13dCo/wz4ekK1iVsSl38v2RhSVAKb7f68r8NqEPLL7h8gOOeFgs3Dl7Hc1kPGE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--sebastianene.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Vod4ssiG; arc=none smtp.client-ip=209.85.128.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--sebastianene.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Vod4ssiG" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-42cb0ed9072so15264155e9.1 for ; Mon, 09 Sep 2024 05:47:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1725886054; x=1726490854; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=8qvkXCdHWEWBsZylsWm6hwfThTp5oWOCgEDtNeKXpl8=; b=Vod4ssiGW/aREMfcE+5dHsYhPXyWRbDQBF/NGNG2aO6PNcZbXTyV/Kw3yw3P8suDTc xPF3r21HdyJ/08j5yXsdt8F2OpXTGEOwMwMiu0q1HqFo/gkLGGIno3nQ2rh2yjIAWY9G C0l6tyhY4gNJ/LMteYU8QXoK9papIrD8MlsW11arxNLvTREpIxIygiGQglwsZD12L5Vm dgXYkpJjCkR45xD73i+YD0lZuBxf3nd5+NpetZtz5/94CqmuS+ZbKphC/KnJEV3Sp0t+ pIQ9fPEx0Nt50J63R5fFZSp/1I2UJGo7cZoo2stTYjZHdH7wqWV9RXPiOVpQDCLfg+iH QKKw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725886054; x=1726490854; 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=8qvkXCdHWEWBsZylsWm6hwfThTp5oWOCgEDtNeKXpl8=; b=W2HLf25XH2ou7dsTeTwDZsO/EI7nI/ATuafTJ7JPMY6X1PFfyLzzXf3/NvisjfxQd1 F/WpaQlfYyEI3uBT+7sXaLmf4dhtr1ULcnFzPg6GQ8UKivaO2hamvR6XgzmHlrrPHIc7 2YyVkbKGVrpSZuKaKEJLvR2kOfSMKHvL1txkV4/VAK36B32/W7QVmPC4AHP19eqzX5ps 9IN1FMegBEJOve85wSeXA+dg6lquj7+cCfYKWWA79DTOOcHOnHJvMS9muXwcLd7tLjH3 XyMwQWHtvzQIqxZ9QZIHGEmFU1YINj5320bOZAxiLgoLfH4sRdgInmy+bZUYUZ72RYnn yzEg== X-Forwarded-Encrypted: i=1; AJvYcCUva9awfylPuyY6cE8KqaHLsQBHm4JpGD/suMBsq8R/xN6Lyr5aFcM7V+iJaS/h9gBCfEXwpg4GaPgvPFE=@vger.kernel.org X-Gm-Message-State: AOJu0YyC44U45fdpuWPsQSWeHz4h/bTqtCm39/c9YWiCKIjDYbY+eiD1 YNTO1nk1kxjEGmyuDX1pPMU034pyfzEJTvz+bjUp+zFgKH6FXcOqNop0fyn11MnKsOUPYwVu6W2 +CUKEYDPydqHDRgFJ7uDVR+XxWw== X-Google-Smtp-Source: AGHT+IHXNUAE7VdC5OTZOKpQ5J1caGnUk0ojDWhw1KiR9y5AZtlPfQlw2THkS+FmwEps5TC/2oefb8PtdRKpmxLL8Qc= X-Received: from sebkvm.c.googlers.com ([fda3:e722:ac3:cc00:68:fe9:ac10:f1b7]) (user=sebastianene job=sendgmr) by 2002:adf:f4d0:0:b0:374:badb:3cc0 with SMTP id ffacd0b85a97d-3788969b893mr13963f8f.9.1725886054280; Mon, 09 Sep 2024 05:47:34 -0700 (PDT) Date: Mon, 9 Sep 2024 12:47:20 +0000 In-Reply-To: <20240909124721.1672199-1-sebastianene@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240909124721.1672199-1-sebastianene@google.com> X-Mailer: git-send-email 2.46.0.469.g59c65b2a67-goog Message-ID: <20240909124721.1672199-5-sebastianene@google.com> Subject: [PATCH v10 4/5] arm64: ptdump: Don't override the level when operating on the stage-2 tables From: Sebastian Ene To: akpm@linux-foundation.org, alexghiti@rivosinc.com, ankita@nvidia.com, ardb@kernel.org, catalin.marinas@arm.com, christophe.leroy@csgroup.eu, james.morse@arm.com, vdonnefort@google.com, mark.rutland@arm.com, maz@kernel.org, oliver.upton@linux.dev, rananta@google.com, ryan.roberts@arm.com, sebastianene@google.com, shahuang@redhat.com, suzuki.poulose@arm.com, will@kernel.org, yuzenghui@huawei.com Cc: kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, kernel-team@android.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Ptdump uses the init_mm structure directly to dump the kernel pagetables. When ptdump is called on the stage-2 pagetables, this mm argument is not used. Prevent the level from being overwritten by checking the argument against NULL. Signed-off-by: Sebastian Ene Acked-by: Will Deacon --- arch/arm64/mm/ptdump.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arch/arm64/mm/ptdump.c b/arch/arm64/mm/ptdump.c index ca53ef274a8b..264c5f9b97d8 100644 --- a/arch/arm64/mm/ptdump.c +++ b/arch/arm64/mm/ptdump.c @@ -197,8 +197,8 @@ void note_page(struct ptdump_state *pt_st, unsigned lon= g addr, int level, u64 prot =3D 0; =20 /* check if the current level has been folded dynamically */ - if ((level =3D=3D 1 && mm_p4d_folded(st->mm)) || - (level =3D=3D 2 && mm_pud_folded(st->mm))) + if (st->mm && ((level =3D=3D 1 && mm_p4d_folded(st->mm)) || + (level =3D=3D 2 && mm_pud_folded(st->mm)))) level =3D 0; =20 if (level >=3D 0) --=20 2.46.0.469.g59c65b2a67-goog From nobody Sat Nov 30 12:49:16 2024 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) (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 B85021B9B4F for ; Mon, 9 Sep 2024 12:47:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725886060; cv=none; b=P6o/9jnANEpKyNIrT+rJkusZFWAEBd9IlNt24JwypgTjiCaZGW9x2ihi7AK4GOFnkALdu4Y9Y/i7ZZ/XRP54UExu04IQS2O4m+NHJSAQtpD3UuujsTlJgi2tdVD4XkHF07bsKODltCVQeUwrviyWM5DyOejAANzjrgfYWrLkR/o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725886060; c=relaxed/simple; bh=GNNUx7/Gw+vesUQbfarr9gvTXiMt4QErQCEA6J8CJF0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=e9chZIWp5V3oP2tc4lN5Vn8kXjGkcqBG8ja5PRyN51dd0/aHldhWu7w1UFvd2FMdbvtlSyIyhFeS9ek2YiMxM5GBliYRttsJTjVxrTUdoYRBDp7eHyljJzMHbC4vgKe10O3rM3MckUw7B+xXBtHrRff21sdBgyIOCW3maftI5l0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--sebastianene.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=iYsJC58C; arc=none smtp.client-ip=209.85.128.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--sebastianene.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="iYsJC58C" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-42cb99afa97so5351265e9.2 for ; Mon, 09 Sep 2024 05:47:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1725886057; x=1726490857; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=5Qh0Buj26VPt7o3soq7cdfVmyH4HC9Sx5cxY7g+Xi6I=; b=iYsJC58CvDkvZb7xf2J2KTxCkWHCfN4fNFgzmVtwyIFRE4vTtH7c4DifAWo4ja3TTl Ge6xyD4l3lln7jLe2/LecMSjXxjucp3z9eTYwImwRcYIZ48lDhTyfA/mQIzXet9b6OP6 wzTWjZZHhiWbiMbWfZ8H2JFabEDASDCZt9s/dD5a1mWQUvyz8dnl2AHit7Ln/weMndLt IaH+WYb+gcZsOMMaouQsyvo/ZmUTYmVCPF5+mLrPkl89Ws0mZOxvniQdZ3HT1had4Vmm nB+jGs2AH67MCMd+oMtm5H+fXlKgASg9cxK+K2OEB8pi4kvlsp/DRUzQKArtABktAPNF J4WA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725886057; x=1726490857; 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=5Qh0Buj26VPt7o3soq7cdfVmyH4HC9Sx5cxY7g+Xi6I=; b=LeXTFzYmw50eFF0nSaawyQ7fMjb/G0qm926tcyT3nnXnMO+ZHyTimJpXtSHphjippq 5KFYkL1zMxTex/D1WotStbEC2ilDy0TDuXBBnynq32IA3i9mEQDyFLbD0se05NYghwn6 dHWUB6oxvRtb2lO34njSNBrQbf+6bzVcL4eo6l7MhLKZ7/dUhT+GZbIFt/IkHfpJJtg4 KQD0xVt4dCaNAdoDzxwuS8jePiFYFD/4adEeWo4AgMto0cvEe+hRZXLlN366jsb9nsfW 1HyRTOQI2D9CXGWmV8MzgQ9uAqM2rhFJdGfajpd6Pfhx/gU7KupBlPP7PaPMUE4Oqi04 B3Uw== X-Forwarded-Encrypted: i=1; AJvYcCUkluno89T1XUAVT0S5eoAnBDF5qw0ikiUJP2c7L2ygSx9DmEVNqusMPpjH75Z98d0ZnWz9XOvD6Ssj5oU=@vger.kernel.org X-Gm-Message-State: AOJu0YztNsRIB2S1qeDrTelFd6PDEBN0oO5D67Q+HEyNkKcYBzW56MEr l50+f+Cc9XdSzL56EW6ZMRbTe/UnIn8IbFRhZyyzlMdJchRT2ZodvDO8EFMPvzd97lPTwPE4XTC eiWAvT95r+ofmYrp+3S5+/Ik7nw== X-Google-Smtp-Source: AGHT+IFrv0rLDSOUyf6zXDI606xTRZ7fv0Nwzc81o52OBg6AUGVDlns9tuVUU3FDCSkjPXyyiudbP0Q4hvSoN0F+igM= X-Received: from sebkvm.c.googlers.com ([fda3:e722:ac3:cc00:68:fe9:ac10:f1b7]) (user=sebastianene job=sendgmr) by 2002:a05:6000:184f:b0:374:cade:3806 with SMTP id ffacd0b85a97d-378895c5256mr15705f8f.2.1725886056876; Mon, 09 Sep 2024 05:47:36 -0700 (PDT) Date: Mon, 9 Sep 2024 12:47:21 +0000 In-Reply-To: <20240909124721.1672199-1-sebastianene@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240909124721.1672199-1-sebastianene@google.com> X-Mailer: git-send-email 2.46.0.469.g59c65b2a67-goog Message-ID: <20240909124721.1672199-6-sebastianene@google.com> Subject: [PATCH v10 5/5] KVM: arm64: Register ptdump with debugfs on guest creation From: Sebastian Ene To: akpm@linux-foundation.org, alexghiti@rivosinc.com, ankita@nvidia.com, ardb@kernel.org, catalin.marinas@arm.com, christophe.leroy@csgroup.eu, james.morse@arm.com, vdonnefort@google.com, mark.rutland@arm.com, maz@kernel.org, oliver.upton@linux.dev, rananta@google.com, ryan.roberts@arm.com, sebastianene@google.com, shahuang@redhat.com, suzuki.poulose@arm.com, will@kernel.org, yuzenghui@huawei.com Cc: kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, kernel-team@android.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" While arch/*/mem/ptdump handles the kernel pagetable dumping code, introduce KVM/ptdump to show the guest stage-2 pagetables. The separation is necessary because most of the definitions from the stage-2 pagetable reside in the KVM path and we will be invoking functionality specific to KVM. Introduce the PTDUMP_STAGE2_DEBUGFS config. When a guest is created, register a new file entry under the guest debugfs dir which allows userspace to show the contents of the guest stage-2 pagetables when accessed. Signed-off-by: Sebastian Ene Reviewed-by: Vincent Donnefort --- arch/arm64/include/asm/kvm_host.h | 6 + arch/arm64/kvm/Kconfig | 17 ++ arch/arm64/kvm/Makefile | 1 + arch/arm64/kvm/arm.c | 1 + arch/arm64/kvm/ptdump.c | 268 ++++++++++++++++++++++++++++++ 5 files changed, 293 insertions(+) create mode 100644 arch/arm64/kvm/ptdump.c diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm= _host.h index a33f5996ca9f..4acd589f086b 100644 --- a/arch/arm64/include/asm/kvm_host.h +++ b/arch/arm64/include/asm/kvm_host.h @@ -1473,4 +1473,10 @@ void kvm_set_vm_id_reg(struct kvm *kvm, u32 reg, u64= val); (pa + pi + pa3) =3D=3D 1; \ }) =20 +#ifdef CONFIG_PTDUMP_STAGE2_DEBUGFS +void kvm_s2_ptdump_create_debugfs(struct kvm *kvm); +#else +static inline void kvm_s2_ptdump_create_debugfs(struct kvm *kvm) {} +#endif /* CONFIG_PTDUMP_STAGE2_DEBUGFS */ + #endif /* __ARM64_KVM_HOST_H__ */ diff --git a/arch/arm64/kvm/Kconfig b/arch/arm64/kvm/Kconfig index 8304eb342be9..ead632ad01b4 100644 --- a/arch/arm64/kvm/Kconfig +++ b/arch/arm64/kvm/Kconfig @@ -66,4 +66,21 @@ config PROTECTED_NVHE_STACKTRACE =20 If unsure, or not using protected nVHE (pKVM), say N. =20 +config PTDUMP_STAGE2_DEBUGFS + bool "Present the stage-2 pagetables to debugfs" + depends on KVM + depends on DEBUG_KERNEL + depends on DEBUG_FS + depends on GENERIC_PTDUMP + select PTDUMP_CORE + default n + help + Say Y here if you want to show the stage-2 kernel pagetables + layout in a debugfs file. This information is only useful for kernel de= velopers + who are working in architecture specific areas of the kernel. + It is probably not a good idea to enable this feature in a production + kernel. + + If in doubt, say N. + endif # VIRTUALIZATION diff --git a/arch/arm64/kvm/Makefile b/arch/arm64/kvm/Makefile index 86a629aaf0a1..e4233b323a73 100644 --- a/arch/arm64/kvm/Makefile +++ b/arch/arm64/kvm/Makefile @@ -27,6 +27,7 @@ kvm-y +=3D arm.o mmu.o mmio.o psci.o hypercalls.o pvtime.= o \ =20 kvm-$(CONFIG_HW_PERF_EVENTS) +=3D pmu-emul.o pmu.o kvm-$(CONFIG_ARM64_PTR_AUTH) +=3D pauth.o +kvm-$(CONFIG_PTDUMP_STAGE2_DEBUGFS) +=3D ptdump.o =20 always-y :=3D hyp_constants.h hyp-constants.s =20 diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index 9bef7638342e..b9fd928d3477 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -228,6 +228,7 @@ vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, s= truct vm_fault *vmf) void kvm_arch_create_vm_debugfs(struct kvm *kvm) { kvm_sys_regs_create_debugfs(kvm); + kvm_s2_ptdump_create_debugfs(kvm); } =20 static void kvm_destroy_mpidr_data(struct kvm *kvm) diff --git a/arch/arm64/kvm/ptdump.c b/arch/arm64/kvm/ptdump.c new file mode 100644 index 000000000000..e29604a864b7 --- /dev/null +++ b/arch/arm64/kvm/ptdump.c @@ -0,0 +1,268 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Debug helper used to dump the stage-2 pagetables of the system and their + * associated permissions. + * + * Copyright (C) Google, 2024 + * Author: Sebastian Ene + */ +#include +#include +#include + +#include +#include +#include + +#define MARKERS_LEN 2 +#define KVM_PGTABLE_MAX_LEVELS (KVM_PGTABLE_LAST_LEVEL + 1) + +struct kvm_ptdump_guest_state { + struct kvm *kvm; + struct ptdump_pg_state parser_state; + struct addr_marker ipa_marker[MARKERS_LEN]; + struct ptdump_pg_level level[KVM_PGTABLE_MAX_LEVELS]; + struct ptdump_range range[MARKERS_LEN]; +}; + +static const struct ptdump_prot_bits stage2_pte_bits[] =3D { + { + .mask =3D PTE_VALID, + .val =3D PTE_VALID, + .set =3D " ", + .clear =3D "F", + }, { + .mask =3D KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R | PTE_VALID, + .val =3D KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R | PTE_VALID, + .set =3D "R", + .clear =3D " ", + }, { + .mask =3D KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W | PTE_VALID, + .val =3D KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W | PTE_VALID, + .set =3D "W", + .clear =3D " ", + }, { + .mask =3D KVM_PTE_LEAF_ATTR_HI_S2_XN | PTE_VALID, + .val =3D PTE_VALID, + .set =3D " ", + .clear =3D "X", + }, { + .mask =3D KVM_PTE_LEAF_ATTR_LO_S2_AF | PTE_VALID, + .val =3D KVM_PTE_LEAF_ATTR_LO_S2_AF | PTE_VALID, + .set =3D "AF", + .clear =3D " ", + }, { + .mask =3D PTE_TABLE_BIT | PTE_VALID, + .val =3D PTE_VALID, + .set =3D "BLK", + .clear =3D " ", + }, +}; + +static int kvm_ptdump_visitor(const struct kvm_pgtable_visit_ctx *ctx, + enum kvm_pgtable_walk_flags visit) +{ + struct ptdump_pg_state *st =3D ctx->arg; + struct ptdump_state *pt_st =3D &st->ptdump; + + note_page(pt_st, ctx->addr, ctx->level, ctx->old); + + return 0; +} + +static int kvm_ptdump_build_levels(struct ptdump_pg_level *level, u32 star= t_lvl) +{ + u32 i; + u64 mask; + + if (WARN_ON_ONCE(start_lvl >=3D KVM_PGTABLE_LAST_LEVEL)) + return -EINVAL; + + mask =3D 0; + for (i =3D 0; i < ARRAY_SIZE(stage2_pte_bits); i++) + mask |=3D stage2_pte_bits[i].mask; + + for (i =3D start_lvl; i < KVM_PGTABLE_MAX_LEVELS; i++) { + snprintf(level[i].name, sizeof(level[i].name), "%u", i); + + level[i].num =3D ARRAY_SIZE(stage2_pte_bits); + level[i].bits =3D stage2_pte_bits; + level[i].mask =3D mask; + } + + return 0; +} + +static struct kvm_ptdump_guest_state *kvm_ptdump_parser_create(struct kvm = *kvm) +{ + struct kvm_ptdump_guest_state *st; + struct kvm_s2_mmu *mmu =3D &kvm->arch.mmu; + struct kvm_pgtable *pgtable =3D mmu->pgt; + int ret; + + st =3D kzalloc(sizeof(struct kvm_ptdump_guest_state), GFP_KERNEL_ACCOUNT); + if (!st) + return ERR_PTR(-ENOMEM); + + ret =3D kvm_ptdump_build_levels(&st->level[0], pgtable->start_level); + if (ret) { + kfree(st); + return ERR_PTR(ret); + } + + st->ipa_marker[0].name =3D "Guest IPA"; + st->ipa_marker[1].start_address =3D BIT(pgtable->ia_bits); + st->range[0].end =3D BIT(pgtable->ia_bits); + + st->kvm =3D kvm; + st->parser_state =3D (struct ptdump_pg_state) { + .marker =3D &st->ipa_marker[0], + .level =3D -1, + .pg_level =3D &st->level[0], + .ptdump.range =3D &st->range[0], + .start_address =3D 0, + }; + + return st; +} + +static int kvm_ptdump_guest_show(struct seq_file *m, void *unused) +{ + int ret; + struct kvm_ptdump_guest_state *st =3D m->private; + struct kvm *kvm =3D st->kvm; + struct kvm_s2_mmu *mmu =3D &kvm->arch.mmu; + struct ptdump_pg_state *parser_state =3D &st->parser_state; + struct kvm_pgtable_walker walker =3D (struct kvm_pgtable_walker) { + .cb =3D kvm_ptdump_visitor, + .arg =3D parser_state, + .flags =3D KVM_PGTABLE_WALK_LEAF, + }; + + parser_state->seq =3D m; + + write_lock(&kvm->mmu_lock); + ret =3D kvm_pgtable_walk(mmu->pgt, 0, BIT(mmu->pgt->ia_bits), &walker); + write_unlock(&kvm->mmu_lock); + + return ret; +} + +static int kvm_ptdump_guest_open(struct inode *m, struct file *file) +{ + struct kvm *kvm =3D m->i_private; + struct kvm_ptdump_guest_state *st; + int ret; + + if (!kvm_get_kvm_safe(kvm)) + return -ENOENT; + + st =3D kvm_ptdump_parser_create(kvm); + if (IS_ERR(st)) { + ret =3D PTR_ERR(st); + goto err_with_kvm_ref; + } + + ret =3D single_open(file, kvm_ptdump_guest_show, st); + if (!ret) + return 0; + + kfree(st); +err_with_kvm_ref: + kvm_put_kvm(kvm); + return ret; +} + +static int kvm_ptdump_guest_close(struct inode *m, struct file *file) +{ + struct kvm *kvm =3D m->i_private; + void *st =3D ((struct seq_file *)file->private_data)->private; + + kfree(st); + kvm_put_kvm(kvm); + + return single_release(m, file); +} + +static const struct file_operations kvm_ptdump_guest_fops =3D { + .open =3D kvm_ptdump_guest_open, + .read =3D seq_read, + .llseek =3D seq_lseek, + .release =3D kvm_ptdump_guest_close, +}; + +static int kvm_pgtable_range_show(struct seq_file *m, void *unused) +{ + struct kvm_pgtable *pgtable =3D m->private; + + seq_printf(m, "%2u\n", pgtable->ia_bits); + return 0; +} + +static int kvm_pgtable_levels_show(struct seq_file *m, void *unused) +{ + struct kvm_pgtable *pgtable =3D m->private; + + seq_printf(m, "%1d\n", KVM_PGTABLE_MAX_LEVELS - pgtable->start_level); + return 0; +} + +static int kvm_pgtable_debugfs_open(struct inode *m, struct file *file, + int (*show)(struct seq_file *, void *)) +{ + struct kvm *kvm =3D m->i_private; + struct kvm_pgtable *pgtable; + int ret; + + if (!kvm_get_kvm_safe(kvm)) + return -ENOENT; + + pgtable =3D kvm->arch.mmu.pgt; + + ret =3D single_open(file, show, pgtable); + if (ret < 0) + kvm_put_kvm(kvm); + return ret; +} + +static int kvm_pgtable_range_open(struct inode *m, struct file *file) +{ + return kvm_pgtable_debugfs_open(m, file, kvm_pgtable_range_show); +} + +static int kvm_pgtable_levels_open(struct inode *m, struct file *file) +{ + return kvm_pgtable_debugfs_open(m, file, kvm_pgtable_levels_show); +} + +static int kvm_pgtable_debugfs_close(struct inode *m, struct file *file) +{ + struct kvm *kvm =3D m->i_private; + + kvm_put_kvm(kvm); + return single_release(m, file); +} + +static const struct file_operations kvm_pgtable_range_fops =3D { + .open =3D kvm_pgtable_range_open, + .read =3D seq_read, + .llseek =3D seq_lseek, + .release =3D kvm_pgtable_debugfs_close, +}; + +static const struct file_operations kvm_pgtable_levels_fops =3D { + .open =3D kvm_pgtable_levels_open, + .read =3D seq_read, + .llseek =3D seq_lseek, + .release =3D kvm_pgtable_debugfs_close, +}; + +void kvm_s2_ptdump_create_debugfs(struct kvm *kvm) +{ + debugfs_create_file("stage2_page_tables", 0400, kvm->debugfs_dentry, + kvm, &kvm_ptdump_guest_fops); + debugfs_create_file("ipa_range", 0400, kvm->debugfs_dentry, kvm, + &kvm_pgtable_range_fops); + debugfs_create_file("stage2_levels", 0400, kvm->debugfs_dentry, + kvm, &kvm_pgtable_levels_fops); +} --=20 2.46.0.469.g59c65b2a67-goog