[{"id":1767582,"web_url":"http://patchwork.ozlabs.org/comment/1767582/","msgid":"<20170913044024.GD7550@umbus.fritz.box>","list_archive_url":null,"date":"2017-09-13T04:40:24","subject":"Re: [Qemu-devel] [PATCH v2 1/2] spapr: introduce common helper to\n\twrite HPT address to KVM PR","submitter":{"id":47,"url":"http://patchwork.ozlabs.org/api/people/47/","name":"David Gibson","email":"david@gibson.dropbear.id.au"},"content":"On Wed, Sep 13, 2017 at 12:24:53AM +0200, Greg Kurz wrote:\n> When running with KVM PR, if a new HPT is allocated we need to inform\n> KVM about the HPT address and size. This is currently done with a hack\n> which is open-coded in several places.\n> \n> This patch consolidate the code in a dedicated helper that records\n> the HPT address and size in the sPAPR context, and then does the\n> magic for KVM PR.\n> \n> Note that ppc_spapr_reset() now resets all devices and CPUs before\n> allocating the HPT. This allows to drop the hack from spapr_cpu_reset().\n> \n> Signed-off-by: Greg Kurz <groug@kaod.org>\n\nI like this more than the previous spin, but while discussing stuff\nwith SamB, I thought up a different approach, which I think will be\nboth cleaner and simpler.\n\nIt basically doesn't make sense to put the userspace HPT pointer into\nenv->spr[SDR1], we only do it to make kvmppc_put_books_sregs() do the\nright thing.\n\nInstead, we can have kvmppc_put_books_sregs() populate the \"SDR1\"\nfield in kvm_sregs from a vhyp hook.  We already have the reverse side\nin that kvmppc_get_books_sregs() doesn't update the internal SDR1\nvalue if vhyp is set.\n\nIn any case the spapr hook would compute the correct value direct from\nspapr->htab.\n\nAfter incoming migration I'm not sure we need to do anything - I think\nwe already do a pretty thorough register resync with KVM.\n\n> ---\n>  hw/ppc/spapr.c          |   31 ++++++++++++++++++++++++++-----\n>  hw/ppc/spapr_cpu_core.c |   15 ---------------\n>  hw/ppc/spapr_hcall.c    |   16 +---------------\n>  include/hw/ppc/spapr.h  |    1 +\n>  4 files changed, 28 insertions(+), 35 deletions(-)\n> \n> diff --git a/hw/ppc/spapr.c b/hw/ppc/spapr.c\n> index f680f28a15ea..97f8afdbd7fe 100644\n> --- a/hw/ppc/spapr.c\n> +++ b/hw/ppc/spapr.c\n> @@ -1309,6 +1309,25 @@ void spapr_free_hpt(sPAPRMachineState *spapr)\n>      close_htab_fd(spapr);\n>  }\n>  \n> +void spapr_install_hpt(sPAPRMachineState *spapr, void *htab, uint32_t shift)\n> +{\n> +    assert(htab);\n> +\n> +    spapr->htab = htab;\n> +    spapr->htab_shift = shift;\n> +\n> +    /*\n> +     * This is a hack for the benefit of KVM PR - it abuses the SDR1\n> +     * slot in kvm_sregs to communicate the userspace address of the\n> +     * HPT\n> +     */\n> +    if (kvm_enabled()) {\n> +        target_ulong sdr1 = (target_ulong)(uintptr_t)spapr->htab\n> +            | (spapr->htab_shift - 18);\n> +        kvmppc_update_sdr1(sdr1);\n> +    }\n> +}\n> +\n>  void spapr_reallocate_hpt(sPAPRMachineState *spapr, int shift,\n>                            Error **errp)\n>  {\n> @@ -1339,16 +1358,17 @@ void spapr_reallocate_hpt(sPAPRMachineState *spapr, int shift,\n>          /* kernel-side HPT not needed, allocate in userspace instead */\n>          size_t size = 1ULL << shift;\n>          int i;\n> +        void *htab;\n>  \n> -        spapr->htab = qemu_memalign(size, size);\n> -        if (!spapr->htab) {\n> +        htab = qemu_memalign(size, size);\n> +        if (!htab) {\n>              error_setg_errno(errp, errno,\n>                               \"Could not allocate HPT of order %d\", shift);\n>              return;\n>          }\n>  \n> -        memset(spapr->htab, 0, size);\n> -        spapr->htab_shift = shift;\n> +        memset(htab, 0, size);\n> +        spapr_install_hpt(spapr, htab, shift);\n>  \n>          for (i = 0; i < size / HASH_PTE_SIZE_64; i++) {\n>              DIRTY_HPTE(HPTE(spapr->htab, i));\n> @@ -1405,6 +1425,8 @@ static void ppc_spapr_reset(void)\n>      /* Check for unknown sysbus devices */\n>      foreach_dynamic_sysbus_device(find_unknown_sysbus_device, NULL);\n>  \n> +    qemu_devices_reset();\n> +\n>      if (kvm_enabled() && kvmppc_has_cap_mmu_radix()) {\n>          /* If using KVM with radix mode available, VCPUs can be started\n>           * without a HPT because KVM will start them in radix mode.\n> @@ -1414,7 +1436,6 @@ static void ppc_spapr_reset(void)\n>          spapr_setup_hpt_and_vrma(spapr);\n>      }\n>  \n> -    qemu_devices_reset();\n>      spapr_clear_pending_events(spapr);\n>  \n>      /*\n> diff --git a/hw/ppc/spapr_cpu_core.c b/hw/ppc/spapr_cpu_core.c\n> index c08ee7571a50..c20b5c64b045 100644\n> --- a/hw/ppc/spapr_cpu_core.c\n> +++ b/hw/ppc/spapr_cpu_core.c\n> @@ -73,7 +73,6 @@ void spapr_cpu_parse_features(sPAPRMachineState *spapr)\n>  \n>  static void spapr_cpu_reset(void *opaque)\n>  {\n> -    sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());\n>      PowerPCCPU *cpu = opaque;\n>      CPUState *cs = CPU(cpu);\n>      CPUPPCState *env = &cpu->env;\n> @@ -86,20 +85,6 @@ static void spapr_cpu_reset(void *opaque)\n>      cs->halted = 1;\n>  \n>      env->spr[SPR_HIOR] = 0;\n> -\n> -    /*\n> -     * This is a hack for the benefit of KVM PR - it abuses the SDR1\n> -     * slot in kvm_sregs to communicate the userspace address of the\n> -     * HPT\n> -     */\n> -    if (kvm_enabled()) {\n> -        env->spr[SPR_SDR1] = (target_ulong)(uintptr_t)spapr->htab\n> -            | (spapr->htab_shift - 18);\n> -        if (kvmppc_put_books_sregs(cpu) < 0) {\n> -            error_report(\"Unable to update SDR1 in KVM\");\n> -            exit(1);\n> -        }\n> -    }\n>  }\n>  \n>  static void spapr_cpu_destroy(PowerPCCPU *cpu)\n> diff --git a/hw/ppc/spapr_hcall.c b/hw/ppc/spapr_hcall.c\n> index 57bb411394ed..7892cd3e7ffa 100644\n> --- a/hw/ppc/spapr_hcall.c\n> +++ b/hw/ppc/spapr_hcall.c\n> @@ -730,15 +730,7 @@ static target_ulong h_resize_hpt_commit(PowerPCCPU *cpu,\n>                      pending->hpt, newsize);\n>      if (rc == H_SUCCESS) {\n>          qemu_vfree(spapr->htab);\n> -        spapr->htab = pending->hpt;\n> -        spapr->htab_shift = pending->shift;\n> -\n> -        if (kvm_enabled()) {\n> -            /* For KVM PR, update the HPT pointer */\n> -            target_ulong sdr1 = (target_ulong)(uintptr_t)spapr->htab\n> -                | (spapr->htab_shift - 18);\n> -            kvmppc_update_sdr1(sdr1);\n> -        }\n> +        spapr_install_hpt(spapr, pending->hpt, pending->shift);\n>  \n>          pending->hpt = NULL; /* so it's not free()d */\n>      }\n> @@ -1564,12 +1556,6 @@ static target_ulong h_client_architecture_support(PowerPCCPU *cpu,\n>               * the point this is called, nothing should have been\n>               * entered into the existing HPT */\n>              spapr_reallocate_hpt(spapr, maxshift, &error_fatal);\n> -            if (kvm_enabled()) {\n> -                /* For KVM PR, update the HPT pointer */\n> -                target_ulong sdr1 = (target_ulong)(uintptr_t)spapr->htab\n> -                    | (spapr->htab_shift - 18);\n> -                kvmppc_update_sdr1(sdr1);\n> -            }\n>          }\n>      }\n>  \n> diff --git a/include/hw/ppc/spapr.h b/include/hw/ppc/spapr.h\n> index c1b365f56431..30e5805acca4 100644\n> --- a/include/hw/ppc/spapr.h\n> +++ b/include/hw/ppc/spapr.h\n> @@ -709,4 +709,5 @@ void spapr_do_system_reset_on_cpu(CPUState *cs, run_on_cpu_data arg);\n>  int spapr_vcpu_id(PowerPCCPU *cpu);\n>  PowerPCCPU *spapr_find_cpu(int vcpu_id);\n>  \n> +void spapr_install_hpt(sPAPRMachineState *spapr, void *htab, uint32_t shift);\n>  #endif /* HW_SPAPR_H */\n>","headers":{"Return-Path":"<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":"patchwork-incoming@bilbo.ozlabs.org","Authentication-Results":["ozlabs.org;\n\tspf=pass (mailfrom) smtp.mailfrom=nongnu.org\n\t(client-ip=2001:4830:134:3::11; helo=lists.gnu.org;\n\tenvelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org;\n\treceiver=<UNKNOWN>)","ozlabs.org;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=gibson.dropbear.id.au\n\theader.i=@gibson.dropbear.id.au header.b=\"GE8CEN1p\"; \n\tdkim-atps=neutral"],"Received":["from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11])\n\t(using TLSv1 with cipher AES256-SHA (256/256 bits))\n\t(No client certificate requested)\n\tby ozlabs.org (Postfix) with ESMTPS id 3xsVlh42kQz9ryT\n\tfor <incoming@patchwork.ozlabs.org>;\n\tWed, 13 Sep 2017 15:36:48 +1000 (AEST)","from localhost ([::1]:40219 helo=lists.gnu.org)\n\tby lists.gnu.org with esmtp (Exim 4.71) (envelope-from\n\t<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>)\n\tid 1ds0Lu-0005uJ-L5\n\tfor incoming@patchwork.ozlabs.org; Wed, 13 Sep 2017 01:36:46 -0400","from eggs.gnu.org ([2001:4830:134:3::10]:57386)\n\tby lists.gnu.org with esmtp (Exim 4.71)\n\t(envelope-from <dgibson@ozlabs.org>) id 1ds0L7-0005k2-7W\n\tfor qemu-devel@nongnu.org; Wed, 13 Sep 2017 01:35:58 -0400","from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)\n\t(envelope-from <dgibson@ozlabs.org>) id 1ds0L3-0004IY-6z\n\tfor qemu-devel@nongnu.org; Wed, 13 Sep 2017 01:35:57 -0400","from ozlabs.org ([103.22.144.67]:37053)\n\tby eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32)\n\t(Exim 4.71) (envelope-from <dgibson@ozlabs.org>)\n\tid 1ds0L2-0004EW-42; Wed, 13 Sep 2017 01:35:53 -0400","by ozlabs.org (Postfix, from userid 1007)\n\tid 3xsVkR0jrBz9sRV; Wed, 13 Sep 2017 15:35:43 +1000 (AEST)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple;\n\td=gibson.dropbear.id.au; s=201602; t=1505280943;\n\tbh=ud0QgvL6xc1kfjl7DmL7ay0YNrzL4c2AMRRqU4gP+mw=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=GE8CEN1pgQpYzXsyc+uofQMdvOYlzIYZ2bOQqA3uhveJLfferz8tL2xs2se6WXlZ4\n\tZEVtAMbChGpJxl28Tz0awKq8QI4YaKOORjklCwq7RCnjFs3sONF6il5zqktcGuElv+\n\td/Q0GopDILCjqgUbEhhVBlI+85BEM2z2pG4AzV9U=","Date":"Wed, 13 Sep 2017 14:40:24 +1000","From":"David Gibson <david@gibson.dropbear.id.au>","To":"Greg Kurz <groug@kaod.org>","Message-ID":"<20170913044024.GD7550@umbus.fritz.box>","References":"<150525508489.11068.5231444460720976552.stgit@bahia.lan>\n\t<150525509384.11068.12660058285712851211.stgit@bahia.lan>","MIME-Version":"1.0","Content-Type":"multipart/signed; micalg=pgp-sha256;\n\tprotocol=\"application/pgp-signature\"; boundary=\"6WlEvdN9Dv0WHSBl\"","Content-Disposition":"inline","In-Reply-To":"<150525509384.11068.12660058285712851211.stgit@bahia.lan>","User-Agent":"Mutt/1.8.3 (2017-05-23)","X-detected-operating-system":"by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic]\n\t[fuzzy]","X-Received-From":"103.22.144.67","Subject":"Re: [Qemu-devel] [PATCH v2 1/2] spapr: introduce common helper to\n\twrite HPT address to KVM PR","X-BeenThere":"qemu-devel@nongnu.org","X-Mailman-Version":"2.1.21","Precedence":"list","List-Id":"<qemu-devel.nongnu.org>","List-Unsubscribe":"<https://lists.nongnu.org/mailman/options/qemu-devel>,\n\t<mailto:qemu-devel-request@nongnu.org?subject=unsubscribe>","List-Archive":"<http://lists.nongnu.org/archive/html/qemu-devel/>","List-Post":"<mailto:qemu-devel@nongnu.org>","List-Help":"<mailto:qemu-devel-request@nongnu.org?subject=help>","List-Subscribe":"<https://lists.nongnu.org/mailman/listinfo/qemu-devel>,\n\t<mailto:qemu-devel-request@nongnu.org?subject=subscribe>","Cc":"qemu-ppc@nongnu.org, qemu-devel@nongnu.org","Errors-To":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org","Sender":"\"Qemu-devel\"\n\t<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>"}}]