From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by spool.mail.gandi.net (Postfix) with ESMTPS id B562A941A5D for ; Wed, 19 Jul 2023 16:32:05 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=gKmoX3EE+v5+gFLEAwhbkTa9sIObJ/RM06ZVT/D6oYA=; c=relaxed/simple; d=groups.io; h=X-Received:X-Received:X-Received:X-MC-Unique:X-Received:X-Received:X-Received:Date:From:To:Cc:Subject:Message-ID:References:MIME-Version:In-Reply-To:X-Scanned-By:X-Mimecast-Spam-Score:X-Mimecast-Originator:Precedence:List-Unsubscribe:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Reply-To:X-Gm-Message-State:Content-Type:Content-Disposition; s=20140610; t=1689784324; v=1; b=uLY2xCvF3CR1dSPs+v+Vh9jmFWQRwfIl6NszXx+2AV8XycoQtRpp4mHt0LstyGcotdHSKSyv EUrazkLzNbuWqjN1/3PlNQSyMsx8a/t8PUEo5ybgLRaUG4EsR9EMhwpOLrRdNnAKbrHd33I4LXv YdpVuroVtxgulC2/gOuk5nlk= X-Received: by 127.0.0.2 with SMTP id qFsFYY7687511xZfe8bsckLH; Wed, 19 Jul 2023 09:32:04 -0700 X-Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mx.groups.io with SMTP id smtpd.web10.1914.1689784323650407841 for ; Wed, 19 Jul 2023 09:32:03 -0700 X-Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-520-oifB7w5aMwO3rDBHTWcjVQ-1; Wed, 19 Jul 2023 12:31:55 -0400 X-MC-Unique: oifB7w5aMwO3rDBHTWcjVQ-1 X-Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id B44FE936D21; Wed, 19 Jul 2023 16:31:54 +0000 (UTC) X-Received: from sirius.home.kraxel.org (unknown [10.39.193.116]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 8691A492B01; Wed, 19 Jul 2023 16:31:54 +0000 (UTC) X-Received: by sirius.home.kraxel.org (Postfix, from userid 1000) id 2E27F1800613; Wed, 19 Jul 2023 18:31:53 +0200 (CEST) Date: Wed, 19 Jul 2023 18:31:53 +0200 From: "Gerd Hoffmann" To: Michael Brown Cc: devel@edk2.groups.io, Ard Biesheuvel , Jiewen Yao , Jordan Justen Subject: Re: [edk2-devel] [PATCH 1/1] OvmfPkg/IoMmuDxe: add locking to IoMmuAllocateBounceBuffer Message-ID: References: <20230719113317.276124-1-kraxel@redhat.com> <010201896ee54d34-2b5c712c-d799-49b0-a2eb-f0838988b313-000000@eu-west-1.amazonses.com> MIME-Version: 1.0 In-Reply-To: <010201896ee54d34-2b5c712c-d799-49b0-a2eb-f0838988b313-000000@eu-west-1.amazonses.com> X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Precedence: Bulk List-Unsubscribe: List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,kraxel@redhat.com X-Gm-Message-State: ECWfslyvluSzbPLzushroLQjx7686176AA= Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20140610 header.b=uLY2xCvF; dmarc=fail reason="SPF not aligned (relaxed), DKIM not aligned (relaxed)" header.from=redhat.com (policy=none); spf=pass (spool.mail.gandi.net: domain of bounce@groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce@groups.io On Wed, Jul 19, 2023 at 04:04:28PM +0000, Michael Brown wrote: > On 19/07/2023 12:33, Gerd Hoffmann wrote: > > Searching for an unused bounce buffer in mReservedMemBitmap and > > reserving the buffer by flipping the bit is a critical section > > which must not be interrupted. Raise the TPL level to ensure > > that. > > > > Without this fix it can happen that IoMmuDxe hands out the same > > bounce buffer twice, causing trouble down the road. Seen happening > > in practice with VirtioNetDxe setting up the network interface (and > > calling into IoMmuDxe from a polling timer callback) in parallel with > > Boot Manager doing some disk I/O. An ASSERT() in VirtioNet caught > > the buffer inconsistency. > > > > Full story with lots of details and discussions is available here: > > https://bugzilla.redhat.com/show_bug.cgi?id=2211060 > > > > Signed-off-by: Gerd Hoffmann > > --- > > OvmfPkg/IoMmuDxe/IoMmuBuffer.c | 3 +++ > > 1 file changed, 3 insertions(+) > > > > diff --git a/OvmfPkg/IoMmuDxe/IoMmuBuffer.c b/OvmfPkg/IoMmuDxe/IoMmuBuffer.c > > index c8f6cf4818e8..7f8a0368ab5d 100644 > > --- a/OvmfPkg/IoMmuDxe/IoMmuBuffer.c > > +++ b/OvmfPkg/IoMmuDxe/IoMmuBuffer.c > > @@ -367,7 +367,9 @@ IoMmuAllocateBounceBuffer ( > > { > > EFI_STATUS Status; > > UINT32 ReservedMemBitmap; > > + EFI_TPL OldTpl; > > + OldTpl = gBS->RaiseTPL (TPL_NOTIFY); > > ReservedMemBitmap = 0; > > Status = InternalAllocateBuffer ( > > Type, > > @@ -378,6 +380,7 @@ IoMmuAllocateBounceBuffer ( > > ); > > MapInfo->ReservedMemBitmap = ReservedMemBitmap; > > mReservedMemBitmap |= ReservedMemBitmap; > > + gBS->RestoreTPL (OldTpl); > > ASSERT (Status == EFI_SUCCESS); > > It looks as though IoMmuFreeBounceBuffer() should also raise to TPL_NOTIFY > while modifying mReservedMemBitmap, since the modification made in > IoMmuFreeBounceBuffer() is not an atomic operation: > > mReservedMemBitmap &= (UINT32)(~MapInfo->ReservedMemBitmap); I'd expect modern compilers optimize that to a single instruction, but yes, it's not guaranteed to happen, the compiler can choose to generate a series of load + and + store instructions instead. Let's play safe, I'll send v2. take care, Gerd -=-=-=-=-=-=-=-=-=-=-=- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#107058): https://edk2.groups.io/g/devel/message/107058 Mute This Topic: https://groups.io/mt/100233359/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=-=-=-=-=-=-=-=-=-=-=-