From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-1.mimecast.com (us-smtp-delivery-1.mimecast.com [205.139.110.61]) by mx.groups.io with SMTP id smtpd.web10.267.1585693206014951737 for ; Tue, 31 Mar 2020 15:20:06 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=WDS7Zmsd; spf=pass (domain: redhat.com, ip: 205.139.110.61, mailfrom: lersek@redhat.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1585693205; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=lwc3EN3GDg4sBZZ8vo3zWsDcHYCnyCOwpo7Q3MSpBzM=; b=WDS7Zmsdzz3V47obnyMngB1MpTQ9V2ayib9WwPe2DIR1nXxVp4cetNwvHXmuT9kCMUJOOb nTff2wDEkQfIqKj7ATW0zQDDk4ok5df5C6/5AnRJ+W3I3jyg/ANYpZV2CJgF8Uw+uUYjRR sbePZGI3NjjfFNyEcEZhrbfOnr3F8UM= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-24-rozGzQtKNwSvU1IFyhZC7w-1; Tue, 31 Mar 2020 18:20:02 -0400 X-MC-Unique: rozGzQtKNwSvU1IFyhZC7w-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id A343E13FD; Tue, 31 Mar 2020 22:20:01 +0000 (UTC) Received: from lacos-laptop-7.usersys.redhat.com (ovpn-115-131.ams2.redhat.com [10.36.115.131]) by smtp.corp.redhat.com (Postfix) with ESMTP id D48561001B2D; Tue, 31 Mar 2020 22:19:59 +0000 (UTC) Subject: Re: [PATCH] OvmfPkg/PvScsiDxe: Refactor setup of rings to separate function To: Liran Alon , devel@edk2.groups.io Cc: nikita.leshchenko@oracle.com, aaron.young@oracle.com, jordan.l.justen@intel.com, ard.biesheuvel@linaro.org References: <20200331114730.62947-1-liran.alon@oracle.com> From: "Laszlo Ersek" Message-ID: Date: Wed, 1 Apr 2020 00:19:58 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 MIME-Version: 1.0 In-Reply-To: <20200331114730.62947-1-liran.alon@oracle.com> X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Hi Liran, On 03/31/20 13:47, Liran Alon wrote: > Previous to this change, PvScsiFreeRings() was not undoing all > operations that was done by PvScsiInitRings(). > This is because PvScsiInitRings() was both preparing rings (Allocate > memory and map it for device DMA) and setup the rings against device by > issueing a device command. While PvScsiFreeRings() only unmaps the rings > and free their memory. > > Driver do not have a functional error as it makes sure to reset device > before every call site to PvScsiFreeRings(). However, this is not > intuitive. > > Therefore, prefer to refactor the setup of the ring against device to a > separate function than PvScsiInitRings(). > > Signed-off-by: Liran Alon > --- > OvmfPkg/PvScsiDxe/PvScsi.c | 150 +++++++++++++++++++------------------ > 1 file changed, 76 insertions(+), 74 deletions(-) Thanks for the patch. I didn't expect this approach -- hoisting the rings exposure from SetupRings to Init --, but it turns out that I like it more than my own previous suggestion -- pushing ResetAdapter into FreeRings :) May I just trouble you with one further request? > diff --git a/OvmfPkg/PvScsiDxe/PvScsi.c b/OvmfPkg/PvScsiDxe/PvScsi.c > index 1ca50390c0e5..5b7fdcbda10b 100644 > --- a/OvmfPkg/PvScsiDxe/PvScsi.c > +++ b/OvmfPkg/PvScsiDxe/PvScsi.c > @@ -991,13 +991,6 @@ PvScsiInitRings ( > ) > { > EFI_STATUS Status; > - union { > - PVSCSI_CMD_DESC_SETUP_RINGS Cmd; > - UINT32 Uint32; > - } AlignedCmd; > - PVSCSI_CMD_DESC_SETUP_RINGS *Cmd; > - > - Cmd = &AlignedCmd.Cmd; > > Status = PvScsiAllocateSharedPages ( > Dev, > @@ -1032,6 +1025,69 @@ PvScsiInitRings ( > } > ZeroMem (Dev->RingDesc.RingCmps, EFI_PAGE_SIZE); > > + return EFI_SUCCESS; > + > +FreeRingReqs: > + PvScsiFreeSharedPages ( > + Dev, > + 1, > + Dev->RingDesc.RingReqs, > + &Dev->RingDesc.RingReqsDmaDesc > + ); > + > +FreeRingState: > + PvScsiFreeSharedPages ( > + Dev, > + 1, > + Dev->RingDesc.RingState, > + &Dev->RingDesc.RingStateDmaDesc > + ); > + > + return Status; > +} > + > +STATIC > +VOID > +PvScsiFreeRings ( > + IN OUT PVSCSI_DEV *Dev > + ) > +{ > + PvScsiFreeSharedPages ( > + Dev, > + 1, > + Dev->RingDesc.RingCmps, > + &Dev->RingDesc.RingCmpsDmaDesc > + ); > + > + PvScsiFreeSharedPages ( > + Dev, > + 1, > + Dev->RingDesc.RingReqs, > + &Dev->RingDesc.RingReqsDmaDesc > + ); > + > + PvScsiFreeSharedPages ( > + Dev, > + 1, > + Dev->RingDesc.RingState, > + &Dev->RingDesc.RingStateDmaDesc > + ); > +} > + > +STATIC > +EFI_STATUS > +PvScsiSetupRings ( > + IN OUT PVSCSI_DEV *Dev > + ) > +{ > + union { > + PVSCSI_CMD_DESC_SETUP_RINGS Cmd; > + UINT32 Uint32; > + } AlignedCmd; > + PVSCSI_CMD_DESC_SETUP_RINGS *Cmd; > + > + Cmd = &AlignedCmd.Cmd; > + > ZeroMem (Cmd, sizeof (*Cmd)); > Cmd->ReqRingNumPages = 1; > Cmd->CmpRingNumPages = 1; > @@ -1052,71 +1108,12 @@ PvScsiInitRings ( > sizeof (*Cmd) % sizeof (UINT32) == 0, > "Cmd must be multiple of 32-bit words" > ); > - Status = PvScsiWriteCmdDesc ( > - Dev, > - PvScsiCmdSetupRings, > - (UINT32 *)Cmd, > - sizeof (*Cmd) / sizeof (UINT32) > - ); > - if (EFI_ERROR (Status)) { > - goto FreeRingCmps; > - } > - > - return EFI_SUCCESS; > - > -FreeRingCmps: > - PvScsiFreeSharedPages ( > - Dev, > - 1, > - Dev->RingDesc.RingCmps, > - &Dev->RingDesc.RingCmpsDmaDesc > - ); > - > -FreeRingReqs: > - PvScsiFreeSharedPages ( > - Dev, > - 1, > - Dev->RingDesc.RingReqs, > - &Dev->RingDesc.RingReqsDmaDesc > - ); > - > -FreeRingState: > - PvScsiFreeSharedPages ( > - Dev, > - 1, > - Dev->RingDesc.RingState, > - &Dev->RingDesc.RingStateDmaDesc > - ); > - > - return Status; > -} > - > -STATIC > -VOID > -PvScsiFreeRings ( > - IN OUT PVSCSI_DEV *Dev > - ) > -{ > - PvScsiFreeSharedPages ( > - Dev, > - 1, > - Dev->RingDesc.RingCmps, > - &Dev->RingDesc.RingCmpsDmaDesc > - ); > - > - PvScsiFreeSharedPages ( > - Dev, > - 1, > - Dev->RingDesc.RingReqs, > - &Dev->RingDesc.RingReqsDmaDesc > - ); > - > - PvScsiFreeSharedPages ( > - Dev, > - 1, > - Dev->RingDesc.RingState, > - &Dev->RingDesc.RingStateDmaDesc > - ); > + return PvScsiWriteCmdDesc ( > + Dev, > + PvScsiCmdSetupRings, > + (UINT32 *)Cmd, > + sizeof (*Cmd) / sizeof (UINT32) > + ); > } > > STATIC > @@ -1157,6 +1154,10 @@ PvScsiInit ( > if (EFI_ERROR (Status)) { > goto RestorePciAttributes; > } > + Status = PvScsiSetupRings (Dev); > + if (EFI_ERROR (Status)) { > + goto FreeRings; > + } If you could move the PvScsiSetupRings() call *below* the DMA buffer allocation (and rework the error path accordingly -- basically replacing the "UnsetupRings" action with a "FreeDmaBuffer" action), then this patch would be *perfect*. Because then, the PvScsiUninit() order of actions would be a 100% mirror for PvScsiInit()'s; namely with PvScsiResetAdapter() at the top of PvScsiUninit() perfectly matching PvScsiSetupRings() at the end of PvScsiInit()! I don't want to annoy you too much, so if you really want to stick with this version, I'll take it as well. Thanks! Laszlo > > // > // Allocate DMA communication buffer > @@ -1168,7 +1169,7 @@ PvScsiInit ( > &Dev->DmaBufDmaDesc > ); > if (EFI_ERROR (Status)) { > - goto FreeRings; > + goto UnsetupRings; > } > > // > @@ -1202,13 +1203,14 @@ PvScsiInit ( > > return EFI_SUCCESS; > > -FreeRings: > +UnsetupRings: > // > // Reset device to stop device usage of the rings. > // This is required to safely free the rings. > // > PvScsiResetAdapter (Dev); > > +FreeRings: > PvScsiFreeRings (Dev); > > RestorePciAttributes: >