Received: by 10.223.164.202 with SMTP id h10csp194900wrb; Wed, 29 Nov 2017 20:04:47 -0800 (PST) X-Google-Smtp-Source: AGs4zMard4PZq+QCadYo+cdPHn0JCXfNZ2rZ2SDxnVQN69m2FAYEpnWMmj/P8XkNYoIYXA1Svq5y X-Received: by 10.84.214.2 with SMTP id h2mr1248888pli.142.1512014687661; Wed, 29 Nov 2017 20:04:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1512014687; cv=none; d=google.com; s=arc-20160816; b=BnUldv1X5gWwOapppy/ZHzJQgOzXEcxzKdP2o6VoGyWgx4I+GwUivIM5demqgLce+h 34Do9B1MESjhgzhE02noRVXPYh37hGKxGPZoChIy02cXGP/fo0kCNPHY7Ydgi0s2PQbP /h2ap4m88p7550c9SM3ONWPK0fMVjTFrtjVwxrNB7OGMWuZm+alC7kYaWKAOA1cMRLkw ohNjbvnNnyG+ug3f3fAb1u7p0cjljpO+qrLIpFrkN+6nDcnApSVk8eFwyNyYWw5lmL6X CnUw3b/ZwKnFSdoLSDuuQbva/Dax6W0y6GmAIa+6mEzmedQgI1xD70xcI6/je6Pm6TKA A94g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature:arc-authentication-results; bh=I4Y/nNaxvdorAktwlJjyFV5qKg4OrBVJiY58aS+88fQ=; b=QVrY1CBk4KQzFrXNF0wKC5QJlN27t/DTPPwVemb/CbsdH6EkvUkGm+d1SuUAkj0LYO 0b6LEJn/m+pUCE10xm58mZTAlL3W62KkD/8EaoaUuayUOD/MuQITY+8mKz4nqnj2cXaL kMAT4GlZgq0LUj4m9hWqvGwvx3giJJMY9kecOWuOsY3qkI8WHoHq8PycsNwWiH4o4tMt jFCQSopB5UockmLKHwZR/13hvl7gellrMHyakI6kpVAb5sVdPa7RIUBPgZw3ezNHBnon MNcFOmq2krnIkEm1CN4SOTNk9Mn58wWqblisi+RO4IOI5ZfW9I3JLicsSvANtBSza6JE YcwQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gibson.dropbear.id.au header.s=201602 header.b=pdzymhRe; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b5si2375969pgr.120.2017.11.29.20.04.34; Wed, 29 Nov 2017 20:04:47 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gibson.dropbear.id.au header.s=201602 header.b=pdzymhRe; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753859AbdK3EDG (ORCPT + 99 others); Wed, 29 Nov 2017 23:03:06 -0500 Received: from ozlabs.org ([103.22.144.67]:47035 "EHLO ozlabs.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753145AbdK3EDD (ORCPT ); Wed, 29 Nov 2017 23:03:03 -0500 Received: by ozlabs.org (Postfix, from userid 1007) id 3ynNzT4TlRz9sBd; Thu, 30 Nov 2017 15:03:01 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gibson.dropbear.id.au; s=201602; t=1512014581; bh=O+Fml7u7G5/Fz1iRlqpADgQ8sDXkc17qWcDEEwcJm9s=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=pdzymhReUeJtNBkJCiJs1C7Rq+9e+pHpp11YjLwNIyzuwHWt/MpL+kWUOjIG06fUf GUMYUCQYK6LA+QLLQdsuM3uKQiDu8WCpXHMIhNECEi0zmtj8sqgBwBs5SN9Zl9r0jm wkHdgRaN6eG+yl2tqMcOIghj8YmW+/UWkTOya3Js= Date: Thu, 30 Nov 2017 14:45:58 +1100 From: David Gibson To: Serhii Popovych Cc: linux-kernel@vger.kernel.org, michael@ellerman.id.au, paulus@samba.org, linuxppc-dev@lists.ozlabs.org, kvm-ppc@vger.kernel.org Subject: Re: [PATCH 2/4] KVM: PPC: Book3S HV: Improve kvmppc_allocate_hpt()/kvmppc_free_hpt() Message-ID: <20171130034558.GP3023@umbus.fritz.box> References: <1511973506-65683-1-git-send-email-spopovyc@redhat.com> <1511973506-65683-3-git-send-email-spopovyc@redhat.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="xNm6VWMD3PcA105u" Content-Disposition: inline In-Reply-To: <1511973506-65683-3-git-send-email-spopovyc@redhat.com> User-Agent: Mutt/1.9.1 (2017-09-22) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org --xNm6VWMD3PcA105u Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Wed, Nov 29, 2017 at 11:38:24AM -0500, Serhii Popovych wrote: > There are several points of improvements: >=20 > 1) Make kvmppc_free_hpt() check if allocation is made before attempt > to release. This follows kfree(p) semantics where p =3D=3D NULL. >=20 > 2) Return initialized @info parameter from kvmppc_allocate_hpt() > even if allocation fails. >=20 > This allows to use kvmppc_free_hpt() in the caller without > checking that preceded kvmppc_allocate_hpt() was successful >=20 > p =3D kmalloc(size, gfp); > kfree(p); >=20 > which is correct for both p !=3D NULL and p =3D=3D NULL. Followup > change will rely on this behaviour. >=20 > 3) Better code reuse: kvmppc_free_hpt() can be reused on error > path in kvmppc_allocate_hpt() to avoid code duplication. >=20 > 4) No need to check for !hpt if allocated from CMA: neither > pfn_to_kaddr() nor page_to_pfn() is 0 in case of page !=3D NULL. >=20 > Signed-off-by: Serhii Popovych Reviewed-by: David Gibson > --- > arch/powerpc/kvm/book3s_64_mmu_hv.c | 54 ++++++++++++++++++-------------= ------ > 1 file changed, 26 insertions(+), 28 deletions(-) >=20 > diff --git a/arch/powerpc/kvm/book3s_64_mmu_hv.c b/arch/powerpc/kvm/book3= s_64_mmu_hv.c > index 0534aab..3e9abd9 100644 > --- a/arch/powerpc/kvm/book3s_64_mmu_hv.c > +++ b/arch/powerpc/kvm/book3s_64_mmu_hv.c > @@ -82,47 +82,44 @@ struct kvm_resize_hpt { > int kvmppc_allocate_hpt(struct kvm_hpt_info *info, u32 order) > { > unsigned long hpt =3D 0; > - int cma =3D 0; > - struct page *page =3D NULL; > - struct revmap_entry *rev; > + int err, cma =3D 0; > + struct page *page; > + struct revmap_entry *rev =3D NULL; > unsigned long npte; > =20 > + err =3D -EINVAL; > if ((order < PPC_MIN_HPT_ORDER) || (order > PPC_MAX_HPT_ORDER)) > - return -EINVAL; > + goto out; > =20 > + err =3D -ENOMEM; > page =3D kvm_alloc_hpt_cma(1ul << (order - PAGE_SHIFT)); > if (page) { > hpt =3D (unsigned long)pfn_to_kaddr(page_to_pfn(page)); > memset((void *)hpt, 0, (1ul << order)); > cma =3D 1; > - } > - > - if (!hpt) > + } else { > hpt =3D __get_free_pages(GFP_KERNEL|__GFP_ZERO|__GFP_RETRY_MAYFAIL > |__GFP_NOWARN, order - PAGE_SHIFT); > - > - if (!hpt) > - return -ENOMEM; > + if (!hpt) > + goto out; > + } > =20 > /* HPTEs are 2**4 bytes long */ > npte =3D 1ul << (order - 4); > =20 > /* Allocate reverse map array */ > - rev =3D vmalloc(sizeof(struct revmap_entry) * npte); > - if (!rev) { > - if (cma) > - kvm_free_hpt_cma(page, 1 << (order - PAGE_SHIFT)); > - else > - free_pages(hpt, order - PAGE_SHIFT); > - return -ENOMEM; > - } > - > + rev =3D vmalloc(sizeof(*rev) * npte); > + if (rev) > + err =3D 0; > +out: > info->order =3D order; > info->virt =3D hpt; > info->cma =3D cma; > info->rev =3D rev; > =20 > - return 0; > + if (err) > + kvmppc_free_hpt(info); > + return err; > } > =20 > void kvmppc_set_hpt(struct kvm *kvm, struct kvm_hpt_info *info) > @@ -190,12 +187,14 @@ void kvmppc_free_hpt(struct kvm_hpt_info *info) > { > vfree(info->rev); > info->rev =3D NULL; > - if (info->cma) > - kvm_free_hpt_cma(virt_to_page(info->virt), > - 1 << (info->order - PAGE_SHIFT)); > - else if (info->virt) > - free_pages(info->virt, info->order - PAGE_SHIFT); > - info->virt =3D 0; > + if (info->virt) { > + if (info->cma) > + kvm_free_hpt_cma(virt_to_page(info->virt), > + 1 << (info->order - PAGE_SHIFT)); > + else > + free_pages(info->virt, info->order - PAGE_SHIFT); > + info->virt =3D 0; > + } > info->order =3D 0; > } > =20 > @@ -1423,8 +1422,7 @@ static void resize_hpt_release(struct kvm *kvm, str= uct kvm_resize_hpt *resize) > if (!resize) > return; > =20 > - if (resize->hpt.virt) > - kvmppc_free_hpt(&resize->hpt); > + kvmppc_free_hpt(&resize->hpt); > =20 > kvm->arch.resize_hpt =3D NULL; > kfree(resize); --=20 David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson --xNm6VWMD3PcA105u Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEdfRlhq5hpmzETofcbDjKyiDZs5IFAloffvUACgkQbDjKyiDZ s5LQfQ/9EdvrqvRBkH5UV/sTaevMDOyQ26ln1S+4XmwBpDYehptamA0ySmOKSZeZ iBKIlLzujg0MbTTLpKGLa1u5kT7wqtjUK2YA7qNxAaGy/e+jaWYGgGcmSL3qWgmD AQ4OPJGUbqEnkanZ6YtTm0EaCbUP0GXPU6qLY4HjPDSnx7D2/ZW5C+ZRCNs6KtG5 PiJ/5SZqA81wxcyGEfU0rCx2tvQWYltDEKl8Mhq28gb7LlyZLACoWeyDt2oGvexf 0wHT6rz+JxRiZS5fyxILUQLbwEQxookpzQ3du43mw/5dyF6BPgl5SAe459qXQA9t Fn+IwvDxDycWlDlbSO3oheUdJsQTPftOzVH6tFnA+NBIl+kwnNMh3OvH8p1wBrbZ GC3/PlTFCMbhtsFbgrbdflApQhTYz+D/lNjAAH7AOrHk59+TsQkSnVlFLOSVEVPl jf4lspDVB9GT+Srrnip/sxiupVUYA0aK6yaSrsxMEDwYKlUx8+46FQv3/apKWR0W BYoIpNb4c8LnuZyQGm0csp062+wRL78foxBQ1zEloxhPB5CRKYKxMkWlD6Xdwufa K6HF7REoWoUAiTJQ/u/mySDnyHlrkbaaOVtiXg/ieQ8u42jwa2BT5uGiGsTJOlTR 1p3T2W7wQLk50YYIkMKO0Roj1hRqouK5i6PzoUF2N7whL/0wDPA= =eg2a -----END PGP SIGNATURE----- --xNm6VWMD3PcA105u-- From 1585419272256215063@xxx Wed Nov 29 16:40:40 +0000 2017 X-GM-THRID: 1585419272256215063 X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread