Re: [kvm-unit-tests] its-migration segmentation fault

2021-06-14 Thread Andrew Jones
On Tue, Jun 15, 2021 at 11:21:05AM +0800, Po-Hsu Lin wrote:
> On Fri, Nov 20, 2020 at 8:35 PM Andrew Jones  wrote:
> >
> > On Fri, Nov 20, 2020 at 12:02:10PM +, Alexandru Elisei wrote:
> > > When running all the tests with taskset -c 0-3 ./run_tests.sh on a 
> > > rockpro64 (on
> > > the Cortex-a53 cores) the its-migration test hangs. In the log file I see:
> > >
> > > run_migration timeout -k 1s --foreground 90s /usr/bin/qemu-system-aarch64
> > > -nodefaults -machine virt,gic-version=host,accel=kvm -cpu host -device
> > > virtio-serial-device -device virtconsole,chardev=ctd -chardev 
> > > testdev,id=ctd
> > > -device pci-testdev -display none -serial stdio -kernel arm/gic.flat -smp 
> > > 6
> > > -machine gic-version=3 -append its-migration # -initrd /tmp/tmp.OrlQiorBpY
> > > ITS: MAPD devid=2 size = 0x8 itt=0x4042 valid=1
> > > ITS: MAPD devid=7 size = 0x8 itt=0x4043 valid=1
> > > MAPC col_id=3 target_addr = 0x3 valid=1
> > > MAPC col_id=2 target_addr = 0x2 valid=1
> > > INVALL col_id=2
> > > INVALL col_id=3
> > > MAPTI dev_id=2 event_id=20 -> phys_id=8195, col_id=3
> > > MAPTI dev_id=7 event_id=255 -> phys_id=8196, col_id=2
> > > Now migrate the VM, then press a key to continue...
> > > scripts/arch-run.bash: line 103: 48549 Doneecho '{ 
> > > "execute":
> > > "qmp_capabilities" }{ "execute":' "$2" '}'
> > >  48550 Segmentation fault  (core dumped) | ncat -U $1
> > > scripts/arch-run.bash: line 103: 48568 Doneecho '{ 
> > > "execute":
> > > "qmp_capabilities" }{ "execute":' "$2" '}'
> > >  48569 Segmentation fault  (core dumped) | ncat -U $1
> > > scripts/arch-run.bash: line 103: 48583 Doneecho '{ 
> > > "execute":
> > > "qmp_capabilities" }{ "execute":' "$2" '}'
> > >  48584 Segmentation fault  (core dumped) | ncat -U $1
> > > [..]
> > > scripts/arch-run.bash: line 103: 49414 Doneecho '{ 
> > > "execute":
> > > "qmp_capabilities" }{ "execute":' "$2" '}'
> > >  49415 Segmentation fault  (core dumped) | ncat -U $1
> > > qemu-system-aarch64: terminating on signal 15 from pid 48496 (timeout)
> > > qemu-system-aarch64: terminating on signal 15 from pid 48504 (timeout)
> > > scripts/arch-run.bash: line 103: 49430 Doneecho '{ 
> > > "execute":
> > > "qmp_capabilities" }{ "execute":' "$2" '}'
> > >  49431 Segmentation fault  (core dumped) | ncat -U $1
> > > scripts/arch-run.bash: line 103: 49445 Doneecho '{ 
> > > "execute":
> > > "qmp_capabilities" }{ "execute":' "$2" '}'
> > > [..]
> >
> > Is your ncat segfaulting? It looks like it from this output. Have you
> > tried running your ncat with a UNIX socket independently of this test?
> >
> > Is this the first time you've tried this test in this environment, or
> > is this a regression for you?
> >
> > >
> > > If I run the test manually:
> > >
> > > $ taskset -c 0-3 ./arm-run arm/gic.flat -smp 4 -machine gic-version=3 
> > > -append
> > > 'its-migration'
> >
> > This won't work because we need run_tests.sh to setup the run_migration()
> > call. The only ways to run migration tests separately are
> >
> >  $ ./run_tests.sh its-migration
> >
> > and
> >
> >  $ tests/its-migration
> >
> > For the second one you need to do 'make standalone' first.
> >
> >
> > >
> > > /usr/bin/qemu-system-aarch64 -nodefaults -machine 
> > > virt,gic-version=host,accel=kvm
> > > -cpu host -device virtio-serial-device -device virtconsole,chardev=ctd 
> > > -chardev
> > > testdev,id=ctd -device pci-testdev -display none -serial stdio -kernel
> > > arm/gic.flat -smp 4 -machine gic-version=3 -append its-migration # -initrd
> > > /tmp/tmp.OtsTj3QD4J
> > > ITS: MAPD devid=2 size = 0x8 itt=0x403a valid=1
> > > ITS: MAPD devid=7 size = 0x8 itt=0x403b valid=1
> > > MAPC col_id=3 target_addr = 0x3 valid=1
> > > MAPC col_id=2 target_addr = 0x2 valid=1
> > > INVALL col_id=2
> > > INVALL col_id=3
> > > MAPTI dev_id=2 event_id=20 -> phys_id=8195, col_id=3
> > > MAPTI dev_id=7 event_id=255 -> phys_id=8196, col_id=2
> > > Now migrate the VM, then press a key to continue...
> > >
> > > And the test hangs here after I press a key.
> >
> > The test doesn't get your input because of the ' > which ./arm-run calls. So it's not hanging it's just waiting forever on
> > the key press.
> Hello Andrew,
> We have found this waiting for key press issue on our side as well
> [1], the test will fail with TIMEOUT, it looks like it's not getting
> my input like you mentioned here.
> I would like to ask what is the expected behaviour of these migration
> related tests (its-pending-migration / its-migration /
> its-migrate-unmapped-collection)? Should they pass right after the
> tester hit a key?

They should, but normally users don't need to press a key, because the
script uses ncat to do it for them.

> Also, if these test would require user interaction, should they be
> moved to some special group like 'nodefault' to preve

[PATCH] KVM: arm64: Trival coding style fixes for all vgic-related files

2021-06-14 Thread Yanan Wang
These fixes introduce no functional change but just adjustment about
coding style issues for ARM64 vgic code. They mainly include identation
fix of function parameters/arguments, identation fix of structure
initialization, identation fix of comment, also the deletion of some
superfluous space lines.

Signed-off-by: Yanan Wang 
---
 arch/arm64/kvm/vgic/vgic-debug.c  |  6 +++---
 arch/arm64/kvm/vgic/vgic-init.c   |  1 -
 arch/arm64/kvm/vgic/vgic-irqfd.c  |  4 ++--
 arch/arm64/kvm/vgic/vgic-its.c| 20 +---
 arch/arm64/kvm/vgic/vgic-kvm-device.c |  2 +-
 arch/arm64/kvm/vgic/vgic-mmio-v3.c|  3 +--
 arch/arm64/kvm/vgic/vgic-mmio.c   | 10 +-
 arch/arm64/kvm/vgic/vgic-mmio.h   |  2 +-
 arch/arm64/kvm/vgic/vgic.c|  4 ++--
 arch/arm64/kvm/vgic/vgic.h|  6 +++---
 10 files changed, 27 insertions(+), 31 deletions(-)

diff --git a/arch/arm64/kvm/vgic/vgic-debug.c b/arch/arm64/kvm/vgic/vgic-debug.c
index f38c40a76251..342be3563a44 100644
--- a/arch/arm64/kvm/vgic/vgic-debug.c
+++ b/arch/arm64/kvm/vgic/vgic-debug.c
@@ -75,9 +75,9 @@ static void iter_init(struct kvm *kvm, struct vgic_state_iter 
*iter,
 static bool end_of_vgic(struct vgic_state_iter *iter)
 {
return iter->dist_id > 0 &&
-   iter->vcpu_id == iter->nr_cpus &&
-   iter->intid >= (iter->nr_spis + VGIC_NR_PRIVATE_IRQS) &&
-   iter->lpi_idx > iter->nr_lpis;
+  iter->vcpu_id == iter->nr_cpus &&
+  iter->intid >= (iter->nr_spis + VGIC_NR_PRIVATE_IRQS) &&
+  iter->lpi_idx > iter->nr_lpis;
 }
 
 static void *vgic_debug_start(struct seq_file *s, loff_t *pos)
diff --git a/arch/arm64/kvm/vgic/vgic-init.c b/arch/arm64/kvm/vgic/vgic-init.c
index 58cbda00e56d..b0bae4fee58e 100644
--- a/arch/arm64/kvm/vgic/vgic-init.c
+++ b/arch/arm64/kvm/vgic/vgic-init.c
@@ -464,7 +464,6 @@ static int vgic_init_cpu_starting(unsigned int cpu)
return 0;
 }
 
-
 static int vgic_init_cpu_dying(unsigned int cpu)
 {
disable_percpu_irq(kvm_vgic_global_state.maint_irq);
diff --git a/arch/arm64/kvm/vgic/vgic-irqfd.c b/arch/arm64/kvm/vgic/vgic-irqfd.c
index 79f8899b234c..722a6f398b0e 100644
--- a/arch/arm64/kvm/vgic/vgic-irqfd.c
+++ b/arch/arm64/kvm/vgic/vgic-irqfd.c
@@ -16,8 +16,8 @@
  * This is the entry point for irqfd IRQ injection
  */
 static int vgic_irqfd_set_irq(struct kvm_kernel_irq_routing_entry *e,
-   struct kvm *kvm, int irq_source_id,
-   int level, bool line_status)
+ struct kvm *kvm, int irq_source_id,
+ int level, bool line_status)
 {
unsigned int spi_id = e->irqchip.pin + VGIC_NR_PRIVATE_IRQS;
 
diff --git a/arch/arm64/kvm/vgic/vgic-its.c b/arch/arm64/kvm/vgic/vgic-its.c
index 61728c543eb9..84c1dcd69ea5 100644
--- a/arch/arm64/kvm/vgic/vgic-its.c
+++ b/arch/arm64/kvm/vgic/vgic-its.c
@@ -178,12 +178,12 @@ struct vgic_its_abi {
 
 static const struct vgic_its_abi its_table_abi_versions[] = {
[0] = {
-.cte_esz = ABI_0_ESZ,
-.dte_esz = ABI_0_ESZ,
-.ite_esz = ABI_0_ESZ,
-.save_tables = vgic_its_save_tables_v0,
-.restore_tables = vgic_its_restore_tables_v0,
-.commit = vgic_its_commit_v0,
+   .cte_esz = ABI_0_ESZ,
+   .dte_esz = ABI_0_ESZ,
+   .ite_esz = ABI_0_ESZ,
+   .save_tables = vgic_its_save_tables_v0,
+   .restore_tables = vgic_its_restore_tables_v0,
+   .commit = vgic_its_commit_v0,
},
 };
 
@@ -224,7 +224,7 @@ static struct its_device *find_its_device(struct vgic_its 
*its, u32 device_id)
  * Must be called with the its_lock mutex held.
  */
 static struct its_ite *find_ite(struct vgic_its *its, u32 device_id,
- u32 event_id)
+   u32 event_id)
 {
struct its_device *device;
struct its_ite *ite;
@@ -1258,7 +1258,6 @@ static int vgic_its_cmd_handle_clear(struct kvm *kvm, 
struct vgic_its *its,
u32 event_id = its_cmd_get_id(its_cmd);
struct its_ite *ite;
 
-
ite = find_ite(its, device_id, event_id);
if (!ite)
return E_ITS_CLEAR_UNMAPPED_INTERRUPT;
@@ -1283,7 +1282,6 @@ static int vgic_its_cmd_handle_inv(struct kvm *kvm, 
struct vgic_its *its,
u32 event_id = its_cmd_get_id(its_cmd);
struct its_ite *ite;
 
-
ite = find_ite(its, device_id, event_id);
if (!ite)
return E_ITS_INV_UNMAPPED_INTERRUPT;
@@ -2120,7 +2118,7 @@ static int scan_its_table(struct vgic_its *its, gpa_t 
base, int size, u32 esz,
  * vgic_its_save_ite - Save an interrupt translation entry at @gpa
  */
 static int vgic_its_save_ite(struct vgic_its *its, struct its_device *dev,
- struct its_ite *ite, gpa_t gpa, int ite_esz)
+struct its_ite *ite, gpa_t gpa, int ite_esz)
 {
struct kv

Re: [PATCH 0/4] Remove duplicated stats definitions for debugfs

2021-06-14 Thread Jing Zhang
On Mon, Jun 14, 2021 at 4:53 AM Paolo Bonzini  wrote:
>
> On 14/06/21 04:53, Jing Zhang wrote:
> > This is a follow-up patchset to binary stats interface patchset as below:
> > https://lore.kernel.org/kvm/20210611124624.1404010-1-jingzhan...@google.com
> >
> > This patchset contains a commit to fix some missing stats and add static
> > check to make sure we have the right number of stats descriptors and add an
> > 'offset' field in stats descriptor to make sure the or order of stats
> > descriptors is not relevant to the order of stats in vm/vcpu stats
> > structure. This will totally avoid the possibility of missing stats and
> > mismatched stats definitions.
> >
> > The binary stats interface defines stats in another array of descriptors,
> > while the original stats debugfs interface uses array of kvm_stats_debugfs
> > item. To remove the duplicated stats definition, this patchset would
> > utilize only the stats descriptors to provide stats information to debugfs
> > interface. This patchset adds a 'mode' flag to support the read/write mode
> > of stats, which can be used to indicate the file permission of debugfs
> > stats files. It removes the usage of kvm_stats_debugfs_item and all the
> > debugfs_entries defined in all archs.
> >
> > The patch also fixes an issue that read only stats could be cleared in
> > global level, though not permitted in VM level in the original debugfs
> > code.
>
> Thanks for putting this together quickly.  I think we can drop the mode,
> see the suggestion in patch 2.
>
> It's probably best to fold the v8 for the stats functionality, these
> patches (squashed appropriately) and Fuad's remars in a single posting.
>   But I'm quite confident that it can make it in 5.14.
>
> Paolo
>
Thanks. Will drop the mode, squash stats functionality with v8 and fold them
in another single posting (v9).

Jing
___
kvmarm mailing list
kvmarm@lists.cs.columbia.edu
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm


Re: [PATCH 2/4] KVM: stats: Use binary stats descriptors for debugfs interface

2021-06-14 Thread Jing Zhang
Hi Paolo,

On Mon, Jun 14, 2021 at 4:51 AM Paolo Bonzini  wrote:
>
> On 14/06/21 04:53, Jing Zhang wrote:
> > + STATS_DESC_ICOUNTER_RONLY(VM, lpages),
> > + STATS_DESC_ICOUNTER_RONLY(VM, nx_lpage_splits),
> >   STATS_DESC_ICOUNTER(VM, max_mmu_page_hash_collisions)
>
> I think we're now adding too many macros.
>
> max_mmu_page_hash_collisions is the only remaining instant counter that
> is not read-only.  In fact, in the statsfs prototype we made all instant
> counters read-only.
>
> You could add a third type in addition to instant and cumulative, let's
> call it "peak".  This essentially corresponds to the "max" aggregation
> function in the statsfs prototype.  Then instant counters would be read
> only, while cumulative and peak would be read-write.
>
> Paolo
>
Thanks for the suggestion. Will do that.
Jing
___
kvmarm mailing list
kvmarm@lists.cs.columbia.edu
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm


Re: [PATCH 1/4] KVM: stats: Make sure no missing or mismatched binary stats definition

2021-06-14 Thread Jing Zhang
Hi Fuad,

On Mon, Jun 14, 2021 at 4:50 AM Fuad Tabba  wrote:
>
> Hi Jing,
>
> On Mon, Jun 14, 2021 at 3:53 AM Jing Zhang  wrote:
> >
> > Add static check to make sure the number of stats descriptors equals
> > the number of stats defined in vm/vcpu stats structures.
> > Add offset field in stats descriptor to let us define stats
> > descriptors freely, don't have to be in the same order as
> > stats in vm/vcpu stats structures.
> > Also fix some missing/mismatched stats from previous patch.
> >
> > Signed-off-by: Jing Zhang 
>
> I tested this for arm64, and it does assert if there's a mismatch. I
> couldn't find any missing statistics under any of the architectures
> either.
>
> > diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
> > index 5e77f32abef5..692af9177c9f 100644
> > --- a/include/linux/kvm_host.h
> > +++ b/include/linux/kvm_host.h
> > @@ -1296,119 +1296,152 @@ struct _kvm_stats_desc {
> > { n, offsetof(struct kvm_vcpu, stat.generic.x), 
> >\
> >   KVM_STAT_VCPU, ## __VA_ARGS__ }
> >
> > -#define STATS_DESC(stat, type, unit, base, exp)
> >\
> > +#define STATS_DESC_COMMON(type, unit, base, exp)   
> >\
> > +   .flags = type | unit | base |   
> >\
> > +   BUILD_BUG_ON_ZERO(type & ~KVM_STATS_TYPE_MASK) |
> >\
> > +   BUILD_BUG_ON_ZERO(unit & ~KVM_STATS_UNIT_MASK) |
> >\
> > +   BUILD_BUG_ON_ZERO(base & ~KVM_STATS_BASE_MASK), 
> >\
> > +   .exponent = exp,
> >\
> > +   .size = 1
> > +
>
> nit: you seem to be mixing tabs and spaces here
>
> > +#define VM_GENERIC_STATS_DESC(stat, type, unit, base, exp) 
> >\
> > {   
> >\
> > {   
> >\
> > -   .flags = type | unit | base |   
> >\
> > -   BUILD_BUG_ON_ZERO(type & ~KVM_STATS_TYPE_MASK) 
> > |   \
> > -   BUILD_BUG_ON_ZERO(unit & ~KVM_STATS_UNIT_MASK) 
> > |   \
> > -   BUILD_BUG_ON_ZERO(base & ~KVM_STATS_BASE_MASK), 
> >\
> > -   .exponent = exp,
> >\
> > -   .size = 1   
> >\
> > +   STATS_DESC_COMMON(type, unit, base, exp),   
> >\
> > +   .offset = offsetof(struct kvm_vm_stat, 
> > generic.stat)   \
> > },  
> >\
> > -   .name = stat,   
> >\
> > +   .name = #stat,  
> >\
> > }
>
> nit: also here, mixing of tabs and spaces
>
> Tested-by: Fuad Tabba  #arm64
> Reviewed-by: Fuad Tabba 
>
> Thanks,
> /fuad
Thanks for the review and testing!

Jing
___
kvmarm mailing list
kvmarm@lists.cs.columbia.edu
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm


Re: [PATCH v7 3/4] KVM: stats: Add documentation for statistics data binary interface

2021-06-14 Thread Jing Zhang
Hi Fuad,

On Mon, Jun 14, 2021 at 2:57 AM Fuad Tabba  wrote:
>
> Hi Jing,
>
>
> On Thu, Jun 3, 2021 at 10:14 PM Jing Zhang  wrote:
> >
> > Update KVM API documentation for binary statistics.
> >
> > Reviewed-by: David Matlack 
> > Reviewed-by: Ricardo Koller 
> > Signed-off-by: Jing Zhang 
> > ---
> >  Documentation/virt/kvm/api.rst | 180 +
> >  1 file changed, 180 insertions(+)
> >
> > diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst
> > index 7fcb2fd38f42..550bfbdf611b 100644
> > --- a/Documentation/virt/kvm/api.rst
> > +++ b/Documentation/virt/kvm/api.rst
> > @@ -5034,6 +5034,178 @@ see KVM_XEN_VCPU_SET_ATTR above.
> >  The KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_ADJUST type may not be used
> >  with the KVM_XEN_VCPU_GET_ATTR ioctl.
> >
> > +4.130 KVM_GET_STATS_FD
> > +-
>
> nit: missing one - (to match the subtitle length)
>
> > +
> > +:Capability: KVM_CAP_STATS_BINARY_FD
> > +:Architectures: all
> > +:Type: vm ioctl, vcpu ioctl
> > +:Parameters: none
> > +:Returns: statistics file descriptor on success, < 0 on error
> > +
> > +Errors:
> > +
> > +  == ==
> > +  ENOMEM if the fd could not be created due to lack of memory
> > +  EMFILE if the number of opened files exceeds the limit
> > +  == ==
> > +
> > +The file descriptor can be used to read VM/vCPU statistics data in binary
> > +format. The file data is organized into three blocks as below:
> > ++-+
> > +|   Header|
> > ++-+
> > +| Descriptors |
> > ++-+
> > +| Stats Data  |
> > ++-+
> > +
> > +The Header block is always at the start of the file. It is only needed to 
> > be
> > +read one time for the lifetime of the file descriptor.
> > +It is in the form of ``struct kvm_stats_header`` as below::
> > +
> > +   #define KVM_STATS_ID_MAXLEN 64
> > +
> > +   struct kvm_stats_header {
> > +   char id[KVM_STATS_ID_MAXLEN];
> > +   __u32 name_size;
> > +   __u32 count;
> > +   __u32 desc_offset;
> > +   __u32 data_offset;
> > +   };
> > +
> > +The ``id`` field is identification for the corresponding KVM statistics. 
> > For
> > +VM statistics, it is in the form of "kvm-{kvm pid}", like "kvm-12345". For
> > +VCPU statistics, it is in the form of "kvm-{kvm pid}/vcpu-{vcpu id}", like
> > +"kvm-12345/vcpu-12".
> > +
> > +The ``name_size`` field is the size (byte) of the statistics name string
> > +(including trailing '\0') appended to the end of every statistics 
> > descriptor.
> > +
> > +The ``count`` field is the number of statistics.
> > +
> > +The ``desc_offset`` field is the offset of the Descriptors block from the 
> > start
> > +of the file indicated by the file descriptor.
> > +
> > +The ``data_offset`` field is the offset of the Stats Data block from the 
> > start
> > +of the file indicated by the file descriptor.
> > +
> > +The Descriptors block is only needed to be read once for the lifetime of 
> > the
> > +file descriptor. It is an array of ``struct kvm_stats_desc`` as shown in
> > +below code block::
> > +
> > +   #define KVM_STATS_TYPE_SHIFT0
> > +   #define KVM_STATS_TYPE_MASK (0xF << 
> > KVM_STATS_TYPE_SHIFT)
> > +   #define KVM_STATS_TYPE_CUMULATIVE   (0x0 << 
> > KVM_STATS_TYPE_SHIFT)
> > +   #define KVM_STATS_TYPE_INSTANT  (0x1 << 
> > KVM_STATS_TYPE_SHIFT)
> > +   #define KVM_STATS_TYPE_MAX  KVM_STATS_TYPE_INSTANT
> > +
> > +   #define KVM_STATS_UNIT_SHIFT4
> > +   #define KVM_STATS_UNIT_MASK (0xF << 
> > KVM_STATS_UNIT_SHIFT)
> > +   #define KVM_STATS_UNIT_NONE (0x0 << 
> > KVM_STATS_UNIT_SHIFT)
> > +   #define KVM_STATS_UNIT_BYTES(0x1 << 
> > KVM_STATS_UNIT_SHIFT)
> > +   #define KVM_STATS_UNIT_SECONDS  (0x2 << 
> > KVM_STATS_UNIT_SHIFT)
> > +   #define KVM_STATS_UNIT_CYCLES   (0x3 << 
> > KVM_STATS_UNIT_SHIFT)
> > +   #define KVM_STATS_UNIT_MAX  KVM_STATS_UNIT_CYCLES
> > +
> > +   #define KVM_STATS_BASE_SHIFT8
> > +   #define KVM_STATS_BASE_MASK (0xF << 
> > KVM_STATS_BASE_SHIFT)
> > +   #define KVM_STATS_BASE_POW10(0x0 << 
> > KVM_STATS_BASE_SHIFT)
> > +   #define KVM_STATS_BASE_POW2 (0x1 << 
> > KVM_STATS_BASE_SHIFT)
> > +   #define KVM_STATS_BASE_MAX  KVM_STATS_BASE_POW2
> > +
> > +   struct kvm_stats_desc {
> > +   __u32 flags;
> > +   __s16 exponent;
> > +   __u16 size;
> > +   __u32 unused1;
> > +   __u32 unused2;
> > +   char name[0];
> > +   };
> > +
> > +The ``flags`` field contains the type and unit of the statistics data 
> > described
> > +by this descriptor. The fo

Re: [PATCH 1/4] KVM: stats: Make sure no missing or mismatched binary stats definition

2021-06-14 Thread Fuad Tabba
Hi Jing,

On Mon, Jun 14, 2021 at 3:53 AM Jing Zhang  wrote:
>
> Add static check to make sure the number of stats descriptors equals
> the number of stats defined in vm/vcpu stats structures.
> Add offset field in stats descriptor to let us define stats
> descriptors freely, don't have to be in the same order as
> stats in vm/vcpu stats structures.
> Also fix some missing/mismatched stats from previous patch.
>
> Signed-off-by: Jing Zhang 

I tested this for arm64, and it does assert if there's a mismatch. I
couldn't find any missing statistics under any of the architectures
either.

> diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
> index 5e77f32abef5..692af9177c9f 100644
> --- a/include/linux/kvm_host.h
> +++ b/include/linux/kvm_host.h
> @@ -1296,119 +1296,152 @@ struct _kvm_stats_desc {
> { n, offsetof(struct kvm_vcpu, stat.generic.x),   
>  \
>   KVM_STAT_VCPU, ## __VA_ARGS__ }
>
> -#define STATS_DESC(stat, type, unit, base, exp)  
>  \
> +#define STATS_DESC_COMMON(type, unit, base, exp) 
>  \
> +   .flags = type | unit | base | 
>  \
> +   BUILD_BUG_ON_ZERO(type & ~KVM_STATS_TYPE_MASK) |  
>  \
> +   BUILD_BUG_ON_ZERO(unit & ~KVM_STATS_UNIT_MASK) |  
>  \
> +   BUILD_BUG_ON_ZERO(base & ~KVM_STATS_BASE_MASK),   
>  \
> +   .exponent = exp,  
>  \
> +   .size = 1
> +

nit: you seem to be mixing tabs and spaces here

> +#define VM_GENERIC_STATS_DESC(stat, type, unit, base, exp)   
>  \
> { 
>  \
> { 
>  \
> -   .flags = type | unit | base | 
>  \
> -   BUILD_BUG_ON_ZERO(type & ~KVM_STATS_TYPE_MASK) |  
>  \
> -   BUILD_BUG_ON_ZERO(unit & ~KVM_STATS_UNIT_MASK) |  
>  \
> -   BUILD_BUG_ON_ZERO(base & ~KVM_STATS_BASE_MASK),   
>  \
> -   .exponent = exp,  
>  \
> -   .size = 1 
>  \
> +   STATS_DESC_COMMON(type, unit, base, exp), 
>  \
> +   .offset = offsetof(struct kvm_vm_stat, generic.stat)  
>  \
> },
>  \
> -   .name = stat, 
>  \
> +   .name = #stat,
>  \
> }

nit: also here, mixing of tabs and spaces

Tested-by: Fuad Tabba  #arm64
Reviewed-by: Fuad Tabba 

Thanks,
/fuad
___
kvmarm mailing list
kvmarm@lists.cs.columbia.edu
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm


Re: [PATCH v7 3/4] KVM: stats: Add documentation for statistics data binary interface

2021-06-14 Thread Fuad Tabba
Hi Jing,


On Thu, Jun 3, 2021 at 10:14 PM Jing Zhang  wrote:
>
> Update KVM API documentation for binary statistics.
>
> Reviewed-by: David Matlack 
> Reviewed-by: Ricardo Koller 
> Signed-off-by: Jing Zhang 
> ---
>  Documentation/virt/kvm/api.rst | 180 +
>  1 file changed, 180 insertions(+)
>
> diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst
> index 7fcb2fd38f42..550bfbdf611b 100644
> --- a/Documentation/virt/kvm/api.rst
> +++ b/Documentation/virt/kvm/api.rst
> @@ -5034,6 +5034,178 @@ see KVM_XEN_VCPU_SET_ATTR above.
>  The KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_ADJUST type may not be used
>  with the KVM_XEN_VCPU_GET_ATTR ioctl.
>
> +4.130 KVM_GET_STATS_FD
> +-

nit: missing one - (to match the subtitle length)

> +
> +:Capability: KVM_CAP_STATS_BINARY_FD
> +:Architectures: all
> +:Type: vm ioctl, vcpu ioctl
> +:Parameters: none
> +:Returns: statistics file descriptor on success, < 0 on error
> +
> +Errors:
> +
> +  == ==
> +  ENOMEM if the fd could not be created due to lack of memory
> +  EMFILE if the number of opened files exceeds the limit
> +  == ==
> +
> +The file descriptor can be used to read VM/vCPU statistics data in binary
> +format. The file data is organized into three blocks as below:
> ++-+
> +|   Header|
> ++-+
> +| Descriptors |
> ++-+
> +| Stats Data  |
> ++-+
> +
> +The Header block is always at the start of the file. It is only needed to be
> +read one time for the lifetime of the file descriptor.
> +It is in the form of ``struct kvm_stats_header`` as below::
> +
> +   #define KVM_STATS_ID_MAXLEN 64
> +
> +   struct kvm_stats_header {
> +   char id[KVM_STATS_ID_MAXLEN];
> +   __u32 name_size;
> +   __u32 count;
> +   __u32 desc_offset;
> +   __u32 data_offset;
> +   };
> +
> +The ``id`` field is identification for the corresponding KVM statistics. For
> +VM statistics, it is in the form of "kvm-{kvm pid}", like "kvm-12345". For
> +VCPU statistics, it is in the form of "kvm-{kvm pid}/vcpu-{vcpu id}", like
> +"kvm-12345/vcpu-12".
> +
> +The ``name_size`` field is the size (byte) of the statistics name string
> +(including trailing '\0') appended to the end of every statistics descriptor.
> +
> +The ``count`` field is the number of statistics.
> +
> +The ``desc_offset`` field is the offset of the Descriptors block from the 
> start
> +of the file indicated by the file descriptor.
> +
> +The ``data_offset`` field is the offset of the Stats Data block from the 
> start
> +of the file indicated by the file descriptor.
> +
> +The Descriptors block is only needed to be read once for the lifetime of the
> +file descriptor. It is an array of ``struct kvm_stats_desc`` as shown in
> +below code block::
> +
> +   #define KVM_STATS_TYPE_SHIFT0
> +   #define KVM_STATS_TYPE_MASK (0xF << KVM_STATS_TYPE_SHIFT)
> +   #define KVM_STATS_TYPE_CUMULATIVE   (0x0 << KVM_STATS_TYPE_SHIFT)
> +   #define KVM_STATS_TYPE_INSTANT  (0x1 << KVM_STATS_TYPE_SHIFT)
> +   #define KVM_STATS_TYPE_MAX  KVM_STATS_TYPE_INSTANT
> +
> +   #define KVM_STATS_UNIT_SHIFT4
> +   #define KVM_STATS_UNIT_MASK (0xF << KVM_STATS_UNIT_SHIFT)
> +   #define KVM_STATS_UNIT_NONE (0x0 << KVM_STATS_UNIT_SHIFT)
> +   #define KVM_STATS_UNIT_BYTES(0x1 << KVM_STATS_UNIT_SHIFT)
> +   #define KVM_STATS_UNIT_SECONDS  (0x2 << KVM_STATS_UNIT_SHIFT)
> +   #define KVM_STATS_UNIT_CYCLES   (0x3 << KVM_STATS_UNIT_SHIFT)
> +   #define KVM_STATS_UNIT_MAX  KVM_STATS_UNIT_CYCLES
> +
> +   #define KVM_STATS_BASE_SHIFT8
> +   #define KVM_STATS_BASE_MASK (0xF << KVM_STATS_BASE_SHIFT)
> +   #define KVM_STATS_BASE_POW10(0x0 << KVM_STATS_BASE_SHIFT)
> +   #define KVM_STATS_BASE_POW2 (0x1 << KVM_STATS_BASE_SHIFT)
> +   #define KVM_STATS_BASE_MAX  KVM_STATS_BASE_POW2
> +
> +   struct kvm_stats_desc {
> +   __u32 flags;
> +   __s16 exponent;
> +   __u16 size;
> +   __u32 unused1;
> +   __u32 unused2;
> +   char name[0];
> +   };
> +
> +The ``flags`` field contains the type and unit of the statistics data 
> described
> +by this descriptor. The following flags are supported:
> +
> +Bits 0-3 of ``flags`` encode the type:
> +  * ``KVM_STATS_TYPE_CUMULATIVE``
> +The statistics data is cumulative. The value of data can only be 
> increased.
> +Most of the counters used in KVM are of this type.
> +The corresponding ``count`` filed for this type is always 1.

filed -> field

> +  * ``KVM_STATS_TYPE_INSTANT

Re: [PATCH 0/4] Remove duplicated stats definitions for debugfs

2021-06-14 Thread Paolo Bonzini

On 14/06/21 04:53, Jing Zhang wrote:

This is a follow-up patchset to binary stats interface patchset as below:
https://lore.kernel.org/kvm/20210611124624.1404010-1-jingzhan...@google.com

This patchset contains a commit to fix some missing stats and add static
check to make sure we have the right number of stats descriptors and add an
'offset' field in stats descriptor to make sure the or order of stats
descriptors is not relevant to the order of stats in vm/vcpu stats
structure. This will totally avoid the possibility of missing stats and
mismatched stats definitions.

The binary stats interface defines stats in another array of descriptors,
while the original stats debugfs interface uses array of kvm_stats_debugfs
item. To remove the duplicated stats definition, this patchset would
utilize only the stats descriptors to provide stats information to debugfs
interface. This patchset adds a 'mode' flag to support the read/write mode
of stats, which can be used to indicate the file permission of debugfs
stats files. It removes the usage of kvm_stats_debugfs_item and all the
debugfs_entries defined in all archs.

The patch also fixes an issue that read only stats could be cleared in
global level, though not permitted in VM level in the original debugfs
code.


Thanks for putting this together quickly.  I think we can drop the mode, 
see the suggestion in patch 2.


It's probably best to fold the v8 for the stats functionality, these 
patches (squashed appropriately) and Fuad's remars in a single posting. 
 But I'm quite confident that it can make it in 5.14.


Paolo

___
kvmarm mailing list
kvmarm@lists.cs.columbia.edu
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm


Re: [PATCH 2/4] KVM: stats: Use binary stats descriptors for debugfs interface

2021-06-14 Thread Paolo Bonzini

On 14/06/21 04:53, Jing Zhang wrote:

+   STATS_DESC_ICOUNTER_RONLY(VM, lpages),
+   STATS_DESC_ICOUNTER_RONLY(VM, nx_lpage_splits),
STATS_DESC_ICOUNTER(VM, max_mmu_page_hash_collisions)


I think we're now adding too many macros.

max_mmu_page_hash_collisions is the only remaining instant counter that 
is not read-only.  In fact, in the statsfs prototype we made all instant 
counters read-only.


You could add a third type in addition to instant and cumulative, let's 
call it "peak".  This essentially corresponds to the "max" aggregation 
function in the statsfs prototype.  Then instant counters would be read 
only, while cumulative and peak would be read-write.


Paolo

___
kvmarm mailing list
kvmarm@lists.cs.columbia.edu
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm


[PATCH v15 7/7] KVM: arm64: Document MTE capability and ioctl

2021-06-14 Thread Steven Price
A new capability (KVM_CAP_ARM_MTE) identifies that the kernel supports
granting a guest access to the tags, and provides a mechanism for the
VMM to enable it.

A new ioctl (KVM_ARM_MTE_COPY_TAGS) provides a simple way for a VMM to
access the tags of a guest without having to maintain a PROT_MTE mapping
in userspace. The above capability gates access to the ioctl.

Reviewed-by: Catalin Marinas 
Signed-off-by: Steven Price 
---
 Documentation/virt/kvm/api.rst | 57 ++
 1 file changed, 57 insertions(+)

diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst
index 22d077562149..d412928e50cd 100644
--- a/Documentation/virt/kvm/api.rst
+++ b/Documentation/virt/kvm/api.rst
@@ -5034,6 +5034,43 @@ see KVM_XEN_VCPU_SET_ATTR above.
 The KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_ADJUST type may not be used
 with the KVM_XEN_VCPU_GET_ATTR ioctl.
 
+4.130 KVM_ARM_MTE_COPY_TAGS
+---
+
+:Capability: KVM_CAP_ARM_MTE
+:Architectures: arm64
+:Type: vm ioctl
+:Parameters: struct kvm_arm_copy_mte_tags
+:Returns: number of bytes copied, < 0 on error (-EINVAL for incorrect
+  arguments, -EFAULT if memory cannot be accessed).
+
+::
+
+  struct kvm_arm_copy_mte_tags {
+   __u64 guest_ipa;
+   __u64 length;
+   void __user *addr;
+   __u64 flags;
+   __u64 reserved[2];
+  };
+
+Copies Memory Tagging Extension (MTE) tags to/from guest tag memory. The
+``guest_ipa`` and ``length`` fields must be ``PAGE_SIZE`` aligned. The ``addr``
+field must point to a buffer which the tags will be copied to or from.
+
+``flags`` specifies the direction of copy, either ``KVM_ARM_TAGS_TO_GUEST`` or
+``KVM_ARM_TAGS_FROM_GUEST``.
+
+The size of the buffer to store the tags is ``(length / 16)`` bytes
+(granules in MTE are 16 bytes long). Each byte contains a single tag
+value. This matches the format of ``PTRACE_PEEKMTETAGS`` and
+``PTRACE_POKEMTETAGS``.
+
+If an error occurs before any data is copied then a negative error code is
+returned. If some tags have been copied before an error occurs then the number
+of bytes successfully copied is returned. If the call completes successfully
+then ``length`` is returned.
+
 5. The kvm_run structure
 
 
@@ -6362,6 +6399,26 @@ default.
 
 See Documentation/x86/sgx/2.Kernel-internals.rst for more details.
 
+7.26 KVM_CAP_ARM_MTE
+
+
+:Architectures: arm64
+:Parameters: none
+
+This capability indicates that KVM (and the hardware) supports exposing the
+Memory Tagging Extensions (MTE) to the guest. It must also be enabled by the
+VMM before creating any VCPUs to allow the guest access. Note that MTE is only
+available to a guest running in AArch64 mode and enabling this capability will
+cause attempts to create AArch32 VCPUs to fail.
+
+When enabled the guest is able to access tags associated with any memory given
+to the guest. KVM will ensure that the tags are maintained during swap or
+hibernation of the host; however the VMM needs to manually save/restore the
+tags as appropriate if the VM is migrated.
+
+When enabled the VMM may make use of the ``KVM_ARM_MTE_COPY_TAGS`` ioctl to
+perform a bulk copy of tags to/from the guest.
+
 8. Other capabilities.
 ==
 
-- 
2.20.1

___
kvmarm mailing list
kvmarm@lists.cs.columbia.edu
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm


[PATCH v15 6/7] KVM: arm64: ioctl to fetch/store tags in a guest

2021-06-14 Thread Steven Price
The VMM may not wish to have it's own mapping of guest memory mapped
with PROT_MTE because this causes problems if the VMM has tag checking
enabled (the guest controls the tags in physical RAM and it's unlikely
the tags are correct for the VMM).

Instead add a new ioctl which allows the VMM to easily read/write the
tags from guest memory, allowing the VMM's mapping to be non-PROT_MTE
while the VMM can still read/write the tags for the purpose of
migration.

Reviewed-by: Catalin Marinas 
Signed-off-by: Steven Price 
---
 arch/arm64/include/asm/kvm_host.h |  3 ++
 arch/arm64/include/asm/mte-def.h  |  1 +
 arch/arm64/include/uapi/asm/kvm.h | 11 +
 arch/arm64/kvm/arm.c  |  7 +++
 arch/arm64/kvm/guest.c| 82 +++
 include/uapi/linux/kvm.h  |  1 +
 6 files changed, 105 insertions(+)

diff --git a/arch/arm64/include/asm/kvm_host.h 
b/arch/arm64/include/asm/kvm_host.h
index 309e36cc1b42..6a2ac4636d42 100644
--- a/arch/arm64/include/asm/kvm_host.h
+++ b/arch/arm64/include/asm/kvm_host.h
@@ -729,6 +729,9 @@ int kvm_arm_vcpu_arch_get_attr(struct kvm_vcpu *vcpu,
 int kvm_arm_vcpu_arch_has_attr(struct kvm_vcpu *vcpu,
   struct kvm_device_attr *attr);
 
+long kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm,
+   struct kvm_arm_copy_mte_tags *copy_tags);
+
 /* Guest/host FPSIMD coordination helpers */
 int kvm_arch_vcpu_run_map_fp(struct kvm_vcpu *vcpu);
 void kvm_arch_vcpu_load_fp(struct kvm_vcpu *vcpu);
diff --git a/arch/arm64/include/asm/mte-def.h b/arch/arm64/include/asm/mte-def.h
index cf241b0f0a42..626d359b396e 100644
--- a/arch/arm64/include/asm/mte-def.h
+++ b/arch/arm64/include/asm/mte-def.h
@@ -7,6 +7,7 @@
 
 #define MTE_GRANULE_SIZE   UL(16)
 #define MTE_GRANULE_MASK   (~(MTE_GRANULE_SIZE - 1))
+#define MTE_GRANULES_PER_PAGE  (PAGE_SIZE / MTE_GRANULE_SIZE)
 #define MTE_TAG_SHIFT  56
 #define MTE_TAG_SIZE   4
 #define MTE_TAG_MASK   GENMASK((MTE_TAG_SHIFT + (MTE_TAG_SIZE - 1)), 
MTE_TAG_SHIFT)
diff --git a/arch/arm64/include/uapi/asm/kvm.h 
b/arch/arm64/include/uapi/asm/kvm.h
index 24223adae150..b3edde68bc3e 100644
--- a/arch/arm64/include/uapi/asm/kvm.h
+++ b/arch/arm64/include/uapi/asm/kvm.h
@@ -184,6 +184,17 @@ struct kvm_vcpu_events {
__u32 reserved[12];
 };
 
+struct kvm_arm_copy_mte_tags {
+   __u64 guest_ipa;
+   __u64 length;
+   void __user *addr;
+   __u64 flags;
+   __u64 reserved[2];
+};
+
+#define KVM_ARM_TAGS_TO_GUEST  0
+#define KVM_ARM_TAGS_FROM_GUEST1
+
 /* If you need to interpret the index values, here is the key: */
 #define KVM_REG_ARM_COPROC_MASK0x0FFF
 #define KVM_REG_ARM_COPROC_SHIFT   16
diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c
index e89a5e275e25..baa33359e477 100644
--- a/arch/arm64/kvm/arm.c
+++ b/arch/arm64/kvm/arm.c
@@ -1345,6 +1345,13 @@ long kvm_arch_vm_ioctl(struct file *filp,
 
return 0;
}
+   case KVM_ARM_MTE_COPY_TAGS: {
+   struct kvm_arm_copy_mte_tags copy_tags;
+
+   if (copy_from_user(©_tags, argp, sizeof(copy_tags)))
+   return -EFAULT;
+   return kvm_vm_ioctl_mte_copy_tags(kvm, ©_tags);
+   }
default:
return -EINVAL;
}
diff --git a/arch/arm64/kvm/guest.c b/arch/arm64/kvm/guest.c
index 5cb4a1cd5603..4ddb20017b2f 100644
--- a/arch/arm64/kvm/guest.c
+++ b/arch/arm64/kvm/guest.c
@@ -995,3 +995,85 @@ int kvm_arm_vcpu_arch_has_attr(struct kvm_vcpu *vcpu,
 
return ret;
 }
+
+long kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm,
+   struct kvm_arm_copy_mte_tags *copy_tags)
+{
+   gpa_t guest_ipa = copy_tags->guest_ipa;
+   size_t length = copy_tags->length;
+   void __user *tags = copy_tags->addr;
+   gpa_t gfn;
+   bool write = !(copy_tags->flags & KVM_ARM_TAGS_FROM_GUEST);
+   int ret = 0;
+
+   if (!kvm_has_mte(kvm))
+   return -EINVAL;
+
+   if (copy_tags->reserved[0] || copy_tags->reserved[1])
+   return -EINVAL;
+
+   if (copy_tags->flags & ~KVM_ARM_TAGS_FROM_GUEST)
+   return -EINVAL;
+
+   if (length & ~PAGE_MASK || guest_ipa & ~PAGE_MASK)
+   return -EINVAL;
+
+   gfn = gpa_to_gfn(guest_ipa);
+
+   mutex_lock(&kvm->slots_lock);
+
+   while (length > 0) {
+   kvm_pfn_t pfn = gfn_to_pfn_prot(kvm, gfn, write, NULL);
+   void *maddr;
+   unsigned long num_tags;
+   struct page *page;
+
+   if (is_error_noslot_pfn(pfn)) {
+   ret = -EFAULT;
+   goto out;
+   }
+
+   page = pfn_to_online_page(pfn);
+   if (!page) {
+   /* Reject ZONE_DEVICE memory */
+   ret = -EFAULT;
+   goto out;
+  

[PATCH v15 5/7] KVM: arm64: Expose KVM_ARM_CAP_MTE

2021-06-14 Thread Steven Price
It's now safe for the VMM to enable MTE in a guest, so expose the
capability to user space.

Reviewed-by: Catalin Marinas 
Signed-off-by: Steven Price 
---
 arch/arm64/kvm/arm.c  | 9 +
 arch/arm64/kvm/reset.c| 3 ++-
 arch/arm64/kvm/sys_regs.c | 3 +++
 3 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c
index 1cb39c0803a4..e89a5e275e25 100644
--- a/arch/arm64/kvm/arm.c
+++ b/arch/arm64/kvm/arm.c
@@ -93,6 +93,12 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
r = 0;
kvm->arch.return_nisv_io_abort_to_user = true;
break;
+   case KVM_CAP_ARM_MTE:
+   if (!system_supports_mte() || kvm->created_vcpus)
+   return -EINVAL;
+   r = 0;
+   kvm->arch.mte_enabled = true;
+   break;
default:
r = -EINVAL;
break;
@@ -237,6 +243,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 */
r = 1;
break;
+   case KVM_CAP_ARM_MTE:
+   r = system_supports_mte();
+   break;
case KVM_CAP_STEAL_TIME:
r = kvm_arm_pvtime_supported();
break;
diff --git a/arch/arm64/kvm/reset.c b/arch/arm64/kvm/reset.c
index 956cdc240148..50635eacfa43 100644
--- a/arch/arm64/kvm/reset.c
+++ b/arch/arm64/kvm/reset.c
@@ -220,7 +220,8 @@ int kvm_reset_vcpu(struct kvm_vcpu *vcpu)
switch (vcpu->arch.target) {
default:
if (test_bit(KVM_ARM_VCPU_EL1_32BIT, vcpu->arch.features)) {
-   if (!cpus_have_const_cap(ARM64_HAS_32BIT_EL1)) {
+   if (!cpus_have_const_cap(ARM64_HAS_32BIT_EL1) ||
+   vcpu->kvm->arch.mte_enabled) {
ret = -EINVAL;
goto out;
}
diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c
index 440315a556c2..d4e1c1b1a08d 100644
--- a/arch/arm64/kvm/sys_regs.c
+++ b/arch/arm64/kvm/sys_regs.c
@@ -1312,6 +1312,9 @@ static bool access_ccsidr(struct kvm_vcpu *vcpu, struct 
sys_reg_params *p,
 static unsigned int mte_visibility(const struct kvm_vcpu *vcpu,
   const struct sys_reg_desc *rd)
 {
+   if (kvm_has_mte(vcpu->kvm))
+   return 0;
+
return REG_HIDDEN;
 }
 
-- 
2.20.1

___
kvmarm mailing list
kvmarm@lists.cs.columbia.edu
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm


[PATCH v15 4/7] KVM: arm64: Save/restore MTE registers

2021-06-14 Thread Steven Price
Define the new system registers that MTE introduces and context switch
them. The MTE feature is still hidden from the ID register as it isn't
supported in a VM yet.

Reviewed-by: Catalin Marinas 
Signed-off-by: Steven Price 
---
 arch/arm64/include/asm/kvm_arm.h   |  3 +-
 arch/arm64/include/asm/kvm_host.h  |  6 ++
 arch/arm64/include/asm/kvm_mte.h   | 66 ++
 arch/arm64/include/asm/sysreg.h|  3 +-
 arch/arm64/kernel/asm-offsets.c|  2 +
 arch/arm64/kvm/hyp/entry.S |  7 +++
 arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h | 21 +++
 arch/arm64/kvm/sys_regs.c  | 22 ++--
 8 files changed, 124 insertions(+), 6 deletions(-)
 create mode 100644 arch/arm64/include/asm/kvm_mte.h

diff --git a/arch/arm64/include/asm/kvm_arm.h b/arch/arm64/include/asm/kvm_arm.h
index 692c9049befa..d436831dd706 100644
--- a/arch/arm64/include/asm/kvm_arm.h
+++ b/arch/arm64/include/asm/kvm_arm.h
@@ -12,7 +12,8 @@
 #include 
 
 /* Hyp Configuration Register (HCR) bits */
-#define HCR_ATA(UL(1) << 56)
+#define HCR_ATA_SHIFT  56
+#define HCR_ATA(UL(1) << HCR_ATA_SHIFT)
 #define HCR_FWB(UL(1) << 46)
 #define HCR_API(UL(1) << 41)
 #define HCR_APK(UL(1) << 40)
diff --git a/arch/arm64/include/asm/kvm_host.h 
b/arch/arm64/include/asm/kvm_host.h
index afaa5333f0e4..309e36cc1b42 100644
--- a/arch/arm64/include/asm/kvm_host.h
+++ b/arch/arm64/include/asm/kvm_host.h
@@ -208,6 +208,12 @@ enum vcpu_sysreg {
CNTP_CVAL_EL0,
CNTP_CTL_EL0,
 
+   /* Memory Tagging Extension registers */
+   RGSR_EL1,   /* Random Allocation Tag Seed Register */
+   GCR_EL1,/* Tag Control Register */
+   TFSR_EL1,   /* Tag Fault Status Register (EL1) */
+   TFSRE0_EL1, /* Tag Fault Status Register (EL0) */
+
/* 32bit specific registers. Keep them at the end of the range */
DACR32_EL2, /* Domain Access Control Register */
IFSR32_EL2, /* Instruction Fault Status Register */
diff --git a/arch/arm64/include/asm/kvm_mte.h b/arch/arm64/include/asm/kvm_mte.h
new file mode 100644
index ..88dd1199670b
--- /dev/null
+++ b/arch/arm64/include/asm/kvm_mte.h
@@ -0,0 +1,66 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2020-2021 ARM Ltd.
+ */
+#ifndef __ASM_KVM_MTE_H
+#define __ASM_KVM_MTE_H
+
+#ifdef __ASSEMBLY__
+
+#include 
+
+#ifdef CONFIG_ARM64_MTE
+
+.macro mte_switch_to_guest g_ctxt, h_ctxt, reg1
+alternative_if_not ARM64_MTE
+   b   .L__skip_switch\@
+alternative_else_nop_endif
+   mrs \reg1, hcr_el2
+   tbz \reg1, #(HCR_ATA_SHIFT), .L__skip_switch\@
+
+   mrs_s   \reg1, SYS_RGSR_EL1
+   str \reg1, [\h_ctxt, #CPU_RGSR_EL1]
+   mrs_s   \reg1, SYS_GCR_EL1
+   str \reg1, [\h_ctxt, #CPU_GCR_EL1]
+
+   ldr \reg1, [\g_ctxt, #CPU_RGSR_EL1]
+   msr_s   SYS_RGSR_EL1, \reg1
+   ldr \reg1, [\g_ctxt, #CPU_GCR_EL1]
+   msr_s   SYS_GCR_EL1, \reg1
+
+.L__skip_switch\@:
+.endm
+
+.macro mte_switch_to_hyp g_ctxt, h_ctxt, reg1
+alternative_if_not ARM64_MTE
+   b   .L__skip_switch\@
+alternative_else_nop_endif
+   mrs \reg1, hcr_el2
+   tbz \reg1, #(HCR_ATA_SHIFT), .L__skip_switch\@
+
+   mrs_s   \reg1, SYS_RGSR_EL1
+   str \reg1, [\g_ctxt, #CPU_RGSR_EL1]
+   mrs_s   \reg1, SYS_GCR_EL1
+   str \reg1, [\g_ctxt, #CPU_GCR_EL1]
+
+   ldr \reg1, [\h_ctxt, #CPU_RGSR_EL1]
+   msr_s   SYS_RGSR_EL1, \reg1
+   ldr \reg1, [\h_ctxt, #CPU_GCR_EL1]
+   msr_s   SYS_GCR_EL1, \reg1
+
+   isb
+
+.L__skip_switch\@:
+.endm
+
+#else /* CONFIG_ARM64_MTE */
+
+.macro mte_switch_to_guest g_ctxt, h_ctxt, reg1
+.endm
+
+.macro mte_switch_to_hyp g_ctxt, h_ctxt, reg1
+.endm
+
+#endif /* CONFIG_ARM64_MTE */
+#endif /* __ASSEMBLY__ */
+#endif /* __ASM_KVM_MTE_H */
diff --git a/arch/arm64/include/asm/sysreg.h b/arch/arm64/include/asm/sysreg.h
index 65d15700a168..347ccac2341e 100644
--- a/arch/arm64/include/asm/sysreg.h
+++ b/arch/arm64/include/asm/sysreg.h
@@ -651,7 +651,8 @@
 
 #define INIT_SCTLR_EL2_MMU_ON  \
(SCTLR_ELx_M  | SCTLR_ELx_C | SCTLR_ELx_SA | SCTLR_ELx_I |  \
-SCTLR_ELx_IESB | SCTLR_ELx_WXN | ENDIAN_SET_EL2 | SCTLR_EL2_RES1)
+SCTLR_ELx_IESB | SCTLR_ELx_WXN | ENDIAN_SET_EL2 |  \
+SCTLR_ELx_ITFSB | SCTLR_EL2_RES1)
 
 #define INIT_SCTLR_EL2_MMU_OFF \
(SCTLR_EL2_RES1 | ENDIAN_SET_EL2)
diff --git a/arch/arm64/kernel/asm-offsets.c b/arch/arm64/kernel/asm-offsets.c
index 0cb34ccb6e73..6f0044cb233e 100644
--- a/arch/arm64/kernel/asm-offsets.c
+++ b/arch/arm64/kernel/asm-offsets.c
@@ -111,6 +111,8 @@ int main(void)
   DEFINE(VCPU_WORKAROUND_FLAGS,offsetof(struct kvm_vcpu, 
arch.workaround_flags));
   DEFINE(VCPU_HCR_EL2, offsetof(struct kvm_vcpu, arch.hcr_el2));
   DEFINE(CPU

[PATCH v15 3/7] KVM: arm64: Introduce MTE VM feature

2021-06-14 Thread Steven Price
Add a new VM feature 'KVM_ARM_CAP_MTE' which enables memory tagging
for a VM. This will expose the feature to the guest and automatically
tag memory pages touched by the VM as PG_mte_tagged (and clear the tag
storage) to ensure that the guest cannot see stale tags, and so that
the tags are correctly saved/restored across swap.

Actually exposing the new capability to user space happens in a later
patch.

Reviewed-by: Catalin Marinas 
Signed-off-by: Steven Price 
---
 arch/arm64/include/asm/kvm_emulate.h |  3 ++
 arch/arm64/include/asm/kvm_host.h|  3 ++
 arch/arm64/include/asm/mte.h |  4 +++
 arch/arm64/kernel/mte.c  | 17 +++
 arch/arm64/kvm/hyp/exception.c   |  3 +-
 arch/arm64/kvm/mmu.c | 42 +++-
 arch/arm64/kvm/sys_regs.c|  7 +
 include/uapi/linux/kvm.h |  1 +
 8 files changed, 78 insertions(+), 2 deletions(-)

diff --git a/arch/arm64/include/asm/kvm_emulate.h 
b/arch/arm64/include/asm/kvm_emulate.h
index f612c090f2e4..6bf776c2399c 100644
--- a/arch/arm64/include/asm/kvm_emulate.h
+++ b/arch/arm64/include/asm/kvm_emulate.h
@@ -84,6 +84,9 @@ static inline void vcpu_reset_hcr(struct kvm_vcpu *vcpu)
if (cpus_have_const_cap(ARM64_MISMATCHED_CACHE_TYPE) ||
vcpu_el1_is_32bit(vcpu))
vcpu->arch.hcr_el2 |= HCR_TID2;
+
+   if (kvm_has_mte(vcpu->kvm))
+   vcpu->arch.hcr_el2 |= HCR_ATA;
 }
 
 static inline unsigned long *vcpu_hcr(struct kvm_vcpu *vcpu)
diff --git a/arch/arm64/include/asm/kvm_host.h 
b/arch/arm64/include/asm/kvm_host.h
index 7cd7d5c8c4bc..afaa5333f0e4 100644
--- a/arch/arm64/include/asm/kvm_host.h
+++ b/arch/arm64/include/asm/kvm_host.h
@@ -132,6 +132,8 @@ struct kvm_arch {
 
u8 pfr0_csv2;
u8 pfr0_csv3;
+   /* Memory Tagging Extension enabled for the guest */
+   bool mte_enabled;
 };
 
 struct kvm_vcpu_fault_info {
@@ -769,6 +771,7 @@ bool kvm_arm_vcpu_is_finalized(struct kvm_vcpu *vcpu);
 #define kvm_arm_vcpu_sve_finalized(vcpu) \
((vcpu)->arch.flags & KVM_ARM64_VCPU_SVE_FINALIZED)
 
+#define kvm_has_mte(kvm) (system_supports_mte() && (kvm)->arch.mte_enabled)
 #define kvm_vcpu_has_pmu(vcpu) \
(test_bit(KVM_ARM_VCPU_PMU_V3, (vcpu)->arch.features))
 
diff --git a/arch/arm64/include/asm/mte.h b/arch/arm64/include/asm/mte.h
index 347ef38a35f7..be1de541a11c 100644
--- a/arch/arm64/include/asm/mte.h
+++ b/arch/arm64/include/asm/mte.h
@@ -37,6 +37,7 @@ void mte_free_tag_storage(char *storage);
 /* track which pages have valid allocation tags */
 #define PG_mte_tagged  PG_arch_2
 
+void mte_prepare_page_tags(struct page *page);
 void mte_sync_tags(pte_t old_pte, pte_t pte);
 void mte_copy_page_tags(void *kto, const void *kfrom);
 void mte_thread_init_user(void);
@@ -53,6 +54,9 @@ int mte_ptrace_copy_tags(struct task_struct *child, long 
request,
 /* unused if !CONFIG_ARM64_MTE, silence the compiler */
 #define PG_mte_tagged  0
 
+static inline void mte_prepare_page_tags(struct page *page)
+{
+}
 static inline void mte_sync_tags(pte_t old_pte, pte_t pte)
 {
 }
diff --git a/arch/arm64/kernel/mte.c b/arch/arm64/kernel/mte.c
index ae0a3c68fece..b120f82a2258 100644
--- a/arch/arm64/kernel/mte.c
+++ b/arch/arm64/kernel/mte.c
@@ -72,6 +72,23 @@ static void mte_sync_page_tags(struct page *page, pte_t 
old_pte,
spin_unlock_irqrestore(&tag_sync_lock, flags);
 }
 
+void mte_prepare_page_tags(struct page *page)
+{
+   unsigned long flags;
+
+   spin_lock_irqsave(&tag_sync_lock, flags);
+
+   /* Recheck with the lock held */
+   if (test_bit(PG_mte_tagged, &page->flags))
+   goto out;
+
+   mte_clear_page_tags(page_address(page));
+   set_bit(PG_mte_tagged, &page->flags);
+
+out:
+   spin_unlock_irqrestore(&tag_sync_lock, flags);
+}
+
 void mte_sync_tags(pte_t old_pte, pte_t pte)
 {
struct page *page = pte_page(pte);
diff --git a/arch/arm64/kvm/hyp/exception.c b/arch/arm64/kvm/hyp/exception.c
index 73629094f903..56426565600c 100644
--- a/arch/arm64/kvm/hyp/exception.c
+++ b/arch/arm64/kvm/hyp/exception.c
@@ -112,7 +112,8 @@ static void enter_exception64(struct kvm_vcpu *vcpu, 
unsigned long target_mode,
new |= (old & PSR_C_BIT);
new |= (old & PSR_V_BIT);
 
-   // TODO: TCO (if/when ARMv8.5-MemTag is exposed to guests)
+   if (kvm_has_mte(vcpu->kvm))
+   new |= PSR_TCO_BIT;
 
new |= (old & PSR_DIT_BIT);
 
diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c
index c5d1f3c87dbd..ed7c624e7362 100644
--- a/arch/arm64/kvm/mmu.c
+++ b/arch/arm64/kvm/mmu.c
@@ -822,6 +822,36 @@ transparent_hugepage_adjust(struct kvm_memory_slot 
*memslot,
return PAGE_SIZE;
 }
 
+static int sanitise_mte_tags(struct kvm *kvm, kvm_pfn_t pfn,
+unsigned long size)
+{
+   unsigned long i, nr_pages = size >> PAGE_SHIFT;
+   struct page *page;
+
+   if (!kvm_has_mte(kvm))
+  

[PATCH v15 2/7] arm64: mte: Sync tags for pages where PTE is untagged

2021-06-14 Thread Steven Price
A KVM guest could store tags in a page even if the VMM hasn't mapped
the page with PROT_MTE. So when restoring pages from swap we will
need to check to see if there are any saved tags even if !pte_tagged().

However don't check pages for which pte_access_permitted() returns false
as these will not have been swapped out.

Reviewed-by: Catalin Marinas 
Signed-off-by: Steven Price 
---
 arch/arm64/include/asm/mte.h |  4 ++--
 arch/arm64/include/asm/pgtable.h | 22 +++---
 arch/arm64/kernel/mte.c  | 17 +
 3 files changed, 34 insertions(+), 9 deletions(-)

diff --git a/arch/arm64/include/asm/mte.h b/arch/arm64/include/asm/mte.h
index bc88a1ced0d7..347ef38a35f7 100644
--- a/arch/arm64/include/asm/mte.h
+++ b/arch/arm64/include/asm/mte.h
@@ -37,7 +37,7 @@ void mte_free_tag_storage(char *storage);
 /* track which pages have valid allocation tags */
 #define PG_mte_tagged  PG_arch_2
 
-void mte_sync_tags(pte_t *ptep, pte_t pte);
+void mte_sync_tags(pte_t old_pte, pte_t pte);
 void mte_copy_page_tags(void *kto, const void *kfrom);
 void mte_thread_init_user(void);
 void mte_thread_switch(struct task_struct *next);
@@ -53,7 +53,7 @@ int mte_ptrace_copy_tags(struct task_struct *child, long 
request,
 /* unused if !CONFIG_ARM64_MTE, silence the compiler */
 #define PG_mte_tagged  0
 
-static inline void mte_sync_tags(pte_t *ptep, pte_t pte)
+static inline void mte_sync_tags(pte_t old_pte, pte_t pte)
 {
 }
 static inline void mte_copy_page_tags(void *kto, const void *kfrom)
diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h
index 0b10204e72fc..db5402168841 100644
--- a/arch/arm64/include/asm/pgtable.h
+++ b/arch/arm64/include/asm/pgtable.h
@@ -314,9 +314,25 @@ static inline void set_pte_at(struct mm_struct *mm, 
unsigned long addr,
if (pte_present(pte) && pte_user_exec(pte) && !pte_special(pte))
__sync_icache_dcache(pte);
 
-   if (system_supports_mte() &&
-   pte_present(pte) && pte_tagged(pte) && !pte_special(pte))
-   mte_sync_tags(ptep, pte);
+   /*
+* If the PTE would provide user space access to the tags associated
+* with it then ensure that the MTE tags are synchronised.  Although
+* pte_access_permitted() returns false for exec only mappings, they
+* don't expose tags (instruction fetches don't check tags).
+*/
+   if (system_supports_mte() && pte_access_permitted(pte, false) &&
+   !pte_special(pte)) {
+   pte_t old_pte = READ_ONCE(*ptep);
+   /*
+* We only need to synchronise if the new PTE has tags enabled
+* or if swapping in (in which case another mapping may have
+* set tags in the past even if this PTE isn't tagged).
+* (!pte_none() && !pte_present()) is an open coded version of
+* is_swap_pte()
+*/
+   if (pte_tagged(pte) || (!pte_none(old_pte) && 
!pte_present(old_pte)))
+   mte_sync_tags(old_pte, pte);
+   }
 
__check_racy_pte_update(mm, ptep, pte);
 
diff --git a/arch/arm64/kernel/mte.c b/arch/arm64/kernel/mte.c
index a3583a7fd400..ae0a3c68fece 100644
--- a/arch/arm64/kernel/mte.c
+++ b/arch/arm64/kernel/mte.c
@@ -33,10 +33,10 @@ DEFINE_STATIC_KEY_FALSE(mte_async_mode);
 EXPORT_SYMBOL_GPL(mte_async_mode);
 #endif
 
-static void mte_sync_page_tags(struct page *page, pte_t *ptep, bool check_swap)
+static void mte_sync_page_tags(struct page *page, pte_t old_pte,
+  bool check_swap, bool pte_is_tagged)
 {
unsigned long flags;
-   pte_t old_pte = READ_ONCE(*ptep);
 
spin_lock_irqsave(&tag_sync_lock, flags);
 
@@ -53,6 +53,9 @@ static void mte_sync_page_tags(struct page *page, pte_t 
*ptep, bool check_swap)
}
}
 
+   if (!pte_is_tagged)
+   goto out;
+
page_kasan_tag_reset(page);
/*
 * We need smp_wmb() in between setting the flags and clearing the
@@ -69,16 +72,22 @@ static void mte_sync_page_tags(struct page *page, pte_t 
*ptep, bool check_swap)
spin_unlock_irqrestore(&tag_sync_lock, flags);
 }
 
-void mte_sync_tags(pte_t *ptep, pte_t pte)
+void mte_sync_tags(pte_t old_pte, pte_t pte)
 {
struct page *page = pte_page(pte);
long i, nr_pages = compound_nr(page);
bool check_swap = nr_pages == 1;
+   bool pte_is_tagged = pte_tagged(pte);
+
+   /* Early out if there's nothing to do */
+   if (!check_swap && !pte_is_tagged)
+   return;
 
/* if PG_mte_tagged is set, tags have already been initialised */
for (i = 0; i < nr_pages; i++, page++) {
if (!test_bit(PG_mte_tagged, &page->flags))
-   mte_sync_page_tags(page, ptep, check_swap);
+   mte_sync_page_tags(page, old_pte, check_swap,
+  pte_is_tagged);
 

[PATCH v15 1/7] arm64: mte: Handle race when synchronising tags

2021-06-14 Thread Steven Price
mte_sync_tags() used test_and_set_bit() to set the PG_mte_tagged flag
before restoring/zeroing the MTE tags. However if another thread were to
race and attempt to sync the tags on the same page before the first
thread had completed restoring/zeroing then it would see the flag is
already set and continue without waiting. This would potentially expose
the previous contents of the tags to user space, and cause any updates
that user space makes before the restoring/zeroing has completed to
potentially be lost.

Since this code is run from atomic contexts we can't just lock the page
during the process. Instead implement a new (global) spinlock to protect
the mte_sync_page_tags() function.

Fixes: 34bfeea4a9e9 ("arm64: mte: Clear the tags when a page is mapped in 
user-space with PROT_MTE")
Reviewed-by: Catalin Marinas 
Signed-off-by: Steven Price 
---
 arch/arm64/kernel/mte.c | 20 +---
 1 file changed, 17 insertions(+), 3 deletions(-)

diff --git a/arch/arm64/kernel/mte.c b/arch/arm64/kernel/mte.c
index 125a10e413e9..a3583a7fd400 100644
--- a/arch/arm64/kernel/mte.c
+++ b/arch/arm64/kernel/mte.c
@@ -25,6 +25,7 @@
 u64 gcr_kernel_excl __ro_after_init;
 
 static bool report_fault_once = true;
+static DEFINE_SPINLOCK(tag_sync_lock);
 
 #ifdef CONFIG_KASAN_HW_TAGS
 /* Whether the MTE asynchronous mode is enabled. */
@@ -34,13 +35,22 @@ EXPORT_SYMBOL_GPL(mte_async_mode);
 
 static void mte_sync_page_tags(struct page *page, pte_t *ptep, bool check_swap)
 {
+   unsigned long flags;
pte_t old_pte = READ_ONCE(*ptep);
 
+   spin_lock_irqsave(&tag_sync_lock, flags);
+
+   /* Recheck with the lock held */
+   if (test_bit(PG_mte_tagged, &page->flags))
+   goto out;
+
if (check_swap && is_swap_pte(old_pte)) {
swp_entry_t entry = pte_to_swp_entry(old_pte);
 
-   if (!non_swap_entry(entry) && mte_restore_tags(entry, page))
-   return;
+   if (!non_swap_entry(entry) && mte_restore_tags(entry, page)) {
+   set_bit(PG_mte_tagged, &page->flags);
+   goto out;
+   }
}
 
page_kasan_tag_reset(page);
@@ -53,6 +63,10 @@ static void mte_sync_page_tags(struct page *page, pte_t 
*ptep, bool check_swap)
 */
smp_wmb();
mte_clear_page_tags(page_address(page));
+   set_bit(PG_mte_tagged, &page->flags);
+
+out:
+   spin_unlock_irqrestore(&tag_sync_lock, flags);
 }
 
 void mte_sync_tags(pte_t *ptep, pte_t pte)
@@ -63,7 +77,7 @@ void mte_sync_tags(pte_t *ptep, pte_t pte)
 
/* if PG_mte_tagged is set, tags have already been initialised */
for (i = 0; i < nr_pages; i++, page++) {
-   if (!test_and_set_bit(PG_mte_tagged, &page->flags))
+   if (!test_bit(PG_mte_tagged, &page->flags))
mte_sync_page_tags(page, ptep, check_swap);
}
 }
-- 
2.20.1

___
kvmarm mailing list
kvmarm@lists.cs.columbia.edu
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm


[PATCH v15 0/7] MTE support for KVM guest

2021-06-14 Thread Steven Price
This series adds support for using the Arm Memory Tagging Extensions
(MTE) in a KVM guest.

I realise there are still open questions[1] around the performance of
this series (the 'big lock', tag_sync_lock, introduced in the first
patch). But there should be no impact on non-MTE workloads and until we
get real MTE-enabled hardware it's hard to know whether there is a need
for something more sophisticated or not. Peter Collingbourne's patch[3]
to clear the tags at page allocation time should hide more of the impact
for non-VM cases. So the remaining concern is around VM startup which
could be effectively serialised through the lock.

Changes since v14[2]:

 * Dropped "Handle MTE tags zeroing" patch in favour of Peter's similar
   patch[3] (now in arm64 tree).

 * Improved documentation following Catalin's review.

[1]: https://lore.kernel.org/r/874ke7z3ng.wl-maz%40kernel.org
[2]: https://lore.kernel.org/r/20210607110816.25762-1-steven.pr...@arm.com/
[3]: https://lore.kernel.org/r/20210602235230.3928842-4-...@google.com/

Steven Price (7):
  arm64: mte: Handle race when synchronising tags
  arm64: mte: Sync tags for pages where PTE is untagged
  KVM: arm64: Introduce MTE VM feature
  KVM: arm64: Save/restore MTE registers
  KVM: arm64: Expose KVM_ARM_CAP_MTE
  KVM: arm64: ioctl to fetch/store tags in a guest
  KVM: arm64: Document MTE capability and ioctl

 Documentation/virt/kvm/api.rst | 57 +++
 arch/arm64/include/asm/kvm_arm.h   |  3 +-
 arch/arm64/include/asm/kvm_emulate.h   |  3 +
 arch/arm64/include/asm/kvm_host.h  | 12 
 arch/arm64/include/asm/kvm_mte.h   | 66 +
 arch/arm64/include/asm/mte-def.h   |  1 +
 arch/arm64/include/asm/mte.h   |  8 ++-
 arch/arm64/include/asm/pgtable.h   | 22 +-
 arch/arm64/include/asm/sysreg.h|  3 +-
 arch/arm64/include/uapi/asm/kvm.h  | 11 +++
 arch/arm64/kernel/asm-offsets.c|  2 +
 arch/arm64/kernel/mte.c| 54 --
 arch/arm64/kvm/arm.c   | 16 +
 arch/arm64/kvm/guest.c | 82 ++
 arch/arm64/kvm/hyp/entry.S |  7 ++
 arch/arm64/kvm/hyp/exception.c |  3 +-
 arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h | 21 ++
 arch/arm64/kvm/mmu.c   | 42 ++-
 arch/arm64/kvm/reset.c |  3 +-
 arch/arm64/kvm/sys_regs.c  | 32 +++--
 include/uapi/linux/kvm.h   |  2 +
 21 files changed, 429 insertions(+), 21 deletions(-)
 create mode 100644 arch/arm64/include/asm/kvm_mte.h

-- 
2.20.1

___
kvmarm mailing list
kvmarm@lists.cs.columbia.edu
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm


Re: [kvm-unit-tests PATCH v2 1/7] README.md: add guideline for header guards format

2021-06-14 Thread Andrew Jones
On Wed, Jun 09, 2021 at 04:37:06PM +0200, Cornelia Huck wrote:
> Signed-off-by: Cornelia Huck 
> ---
>  README.md | 9 +
>  1 file changed, 9 insertions(+)
> 
> diff --git a/README.md b/README.md
> index 24d4bdaaee0d..687ff50d0af1 100644
> --- a/README.md
> +++ b/README.md
> @@ -156,6 +156,15 @@ Exceptions:
>  
>- While the kernel standard requires 80 columns, we allow up to 120.
>  
> +Header guards:
> +
> +Please try to adhere to adhere to the following patterns when adding

Double 'to adhere'

Thanks,
drew

> +"#ifndef <...> #define <...>" header guards:
> +./lib: _HEADER_H_
> +./lib/:  _ARCH_HEADER_H_
> +./lib//asm:  _ASMARCH_HEADER_H_
> +./:  ARCH_HEADER_H
> +
>  ## Patches
>  
>  Patches are welcome at the KVM mailing list .
> -- 
> 2.31.1
> 

___
kvmarm mailing list
kvmarm@lists.cs.columbia.edu
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm


Re: [kvm-unit-tests PATCH] configure: arm: Update kvmtool UART address

2021-06-14 Thread Andrew Jones
On Fri, Jun 11, 2021 at 04:26:21PM +0100, Alexandru Elisei wrote:
> kvmtool commit 45b4968e0de1 ("hw/serial: ARM/arm64: Use MMIO at higher
> addresses") changed the UART address from 0x3f8 to 0x100. Update the
> UART early address accordingly when kvm-unit-tests is configured with
> --target=kvmtool.
> 
> Users of older kvmtool versions can still enjoy having a working early UART
> by configuring kvm-unit-tests with --earlycon=uart,mmio,0x3f8. Note that in
> this case --target=kvmtool is still recommended because it enables all
> erratas.
> 
> Signed-off-by: Alexandru Elisei 
> ---
>  configure | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/configure b/configure
> index 4ad5a4bcd782..bd0c14edb777 100755
> --- a/configure
> +++ b/configure
> @@ -189,7 +189,7 @@ elif [ "$arch" = "arm" ] || [ "$arch" = "arm64" ]; then
>  if [ "$target" = "qemu" ]; then
>  arm_uart_early_addr=0x0900
>  elif [ "$target" = "kvmtool" ]; then
> -arm_uart_early_addr=0x3f8
> +arm_uart_early_addr=0x100
>  errata_force=1
>  else
>  echo "--target must be one of 'qemu' or 'kvmtool'!"
> -- 
> 2.32.0
>

Applied to arm/queue

https://gitlab.com/rhdrjones/kvm-unit-tests/-/commits/arm/queue

Thanks,
drew 

___
kvmarm mailing list
kvmarm@lists.cs.columbia.edu
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm


Re: [PATCH v4 0/6] KVM: selftests: arm64 exception handling and debug test

2021-06-14 Thread Marc Zyngier
On Thu, 10 Jun 2021 18:10:14 -0700, Ricardo Koller wrote:
> These patches add a debug exception test in aarch64 KVM selftests while
> also adding basic exception handling support.
> 
> The structure of the exception handling is based on its x86 counterpart.
> Tests use the same calls to initialize exception handling and both
> architectures allow tests to override the handler for a particular
> vector, or (vector, ec) for synchronous exceptions in the arm64 case.
> 
> [...]

Applied to next, thanks!

[1/6] KVM: selftests: Rename vm_handle_exception
  commit: b78f4a596692f6805e796a4c13f2d921b8a95166
[2/6] KVM: selftests: Complete x86_64/sync_regs_test ucall
  commit: b7326c01122683b88e273a0cc826cd4c01234470
[3/6] KVM: selftests: Introduce UCALL_UNHANDLED for unhandled vector reporting
  commit: 75275d7fbef47805b77e8af81a4d51e2d92db70f
[4/6] KVM: selftests: Move GUEST_ASSERT_EQ to utils header
  commit: 67f709f52bf0b5c19f24d1234163123cbb6af545
[5/6] KVM: selftests: Add exception handling support for aarch64
  commit: e3db7579ef355a0b2bfef4448b84d9ac882c8f2c
[6/6] KVM: selftests: Add aarch64/debug-exceptions test
  commit: 4f05223acaeaabe0a1a188e25fab334735d85c5e

Cheers,

M.
-- 
Without deviation from the norm, progress is not possible.


___
kvmarm mailing list
kvmarm@lists.cs.columbia.edu
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm


Re: [PATCH v4 0/6] KVM: selftests: arm64 exception handling and debug test

2021-06-14 Thread Andrew Jones
On Thu, Jun 10, 2021 at 06:10:14PM -0700, Ricardo Koller wrote:
> Hi,
> 
> These patches add a debug exception test in aarch64 KVM selftests while
> also adding basic exception handling support.
> 
> The structure of the exception handling is based on its x86 counterpart.
> Tests use the same calls to initialize exception handling and both
> architectures allow tests to override the handler for a particular
> vector, or (vector, ec) for synchronous exceptions in the arm64 case.
> 
> The debug test is similar to x86_64/debug_regs, except that the x86 one
> controls the debugging from outside the VM. This proposed arm64 test
> controls and handles debug exceptions from the inside.
> 
> Thanks,
> Ricardo
> 
> v3 -> v4:
> 
> V3 was dropped because it was breaking x86 selftests builds (reported by
> the kernel test robot).
> - rename vm_handle_exception to vm_install_sync_handler instead of
>   vm_install_vector_handlers. [Sean]
> - use a single level of routing for exception handling. [Sean]
> - fix issue in x86_64/sync_regs_test when switching to ucalls for unhandled
>   exceptions reporting.
> 
> v2 -> v3:
> 
> Addressed comments from Andrew and Marc (thanks again). Also, many thanks for
> the reviews and tests from Eric and Zenghui.
> - add missing ISBs after writing into debug registers.
> - not store/restore of sp_el0 on exceptions.
> - add default handlers for Error and FIQ.
> - change multiple TEST_ASSERT(false, ...) to TEST_FAIL.
> - use Andrew's suggestion regarding __GUEST_ASSERT modifications
>   in order to easier implement GUEST_ASSERT_EQ (Thanks Andrew).
> 
> v1 -> v2:
> 
> Addressed comments from Andrew and Marc (thank you very much):
> - rename vm_handle_exception in all tests.
> - introduce UCALL_UNHANDLED in x86 first.
> - move GUEST_ASSERT_EQ to common utils header.
> - handle sync and other exceptions separately: use two tables (like
>   kvm-unit-tests).
> - add two separate functions for installing sync versus other exceptions
> - changes in handlers.S: use the same layout as user_pt_regs, treat the
>   EL1t vectors as invalid, refactor the vector table creation to not use
>   manual numbering, add comments, remove LR from the stored registers.
> - changes in debug-exceptions.c: remove unused headers, use the common
>   GUEST_ASSERT_EQ, use vcpu_run instead of _vcpu_run.
> - changes in processor.h: write_sysreg with support for xzr, replace EL1
>   with current in macro names, define ESR_EC_MASK as ESR_EC_NUM-1.
> 
> Ricardo Koller (6):
>   KVM: selftests: Rename vm_handle_exception
>   KVM: selftests: Complete x86_64/sync_regs_test ucall
>   KVM: selftests: Introduce UCALL_UNHANDLED for unhandled vector
> reporting
>   KVM: selftests: Move GUEST_ASSERT_EQ to utils header
>   KVM: selftests: Add exception handling support for aarch64
>   KVM: selftests: Add aarch64/debug-exceptions test
> 
>  tools/testing/selftests/kvm/.gitignore|   1 +
>  tools/testing/selftests/kvm/Makefile  |   3 +-
>  .../selftests/kvm/aarch64/debug-exceptions.c  | 250 ++
>  .../selftests/kvm/include/aarch64/processor.h |  83 +-
>  .../testing/selftests/kvm/include/kvm_util.h  |  23 +-
>  .../selftests/kvm/include/x86_64/processor.h  |   4 +-
>  .../selftests/kvm/lib/aarch64/handlers.S  | 126 +
>  .../selftests/kvm/lib/aarch64/processor.c |  97 +++
>  .../selftests/kvm/lib/x86_64/processor.c  |  23 +-
>  .../testing/selftests/kvm/x86_64/evmcs_test.c |   4 +-
>  .../selftests/kvm/x86_64/kvm_pv_test.c|   2 +-
>  .../selftests/kvm/x86_64/sync_regs_test.c |   7 +-
>  .../selftests/kvm/x86_64/tsc_msrs_test.c  |   9 -
>  .../kvm/x86_64/userspace_msr_exit_test.c  |   8 +-
>  .../selftests/kvm/x86_64/xapic_ipi_test.c |   2 +-
>  15 files changed, 592 insertions(+), 50 deletions(-)
>  create mode 100644 tools/testing/selftests/kvm/aarch64/debug-exceptions.c
>  create mode 100644 tools/testing/selftests/kvm/lib/aarch64/handlers.S
> 
> -- 
> 2.32.0.272.g935e593368-goog
>

For the series

Reviewed-by: Andrew Jones 

Thanks,
drew

___
kvmarm mailing list
kvmarm@lists.cs.columbia.edu
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm