From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by mx.groups.io with SMTP id smtpd.web10.3379.1633657184809975232 for ; Thu, 07 Oct 2021 18:39:46 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@intel.onmicrosoft.com header.s=selector2-intel-onmicrosoft-com header.b=yA1p/Y8o; spf=pass (domain: intel.com, ip: 134.134.136.65, mailfrom: dandan.bi@intel.com) X-IronPort-AV: E=McAfee;i="6200,9189,10130"; a="226364040" X-IronPort-AV: E=Sophos;i="5.85,356,1624345200"; d="scan'208";a="226364040" Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Oct 2021 18:39:44 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.85,356,1624345200"; d="scan'208";a="590382594" Received: from fmsmsx606.amr.corp.intel.com ([10.18.126.86]) by orsmga004.jf.intel.com with ESMTP; 07 Oct 2021 18:39:43 -0700 Received: from fmsmsx608.amr.corp.intel.com (10.18.126.88) by fmsmsx606.amr.corp.intel.com (10.18.126.86) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2242.12; Thu, 7 Oct 2021 18:39:42 -0700 Received: from FMSEDG603.ED.cps.intel.com (10.1.192.133) by fmsmsx608.amr.corp.intel.com (10.18.126.88) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2242.12 via Frontend Transport; Thu, 7 Oct 2021 18:39:42 -0700 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (104.47.56.170) by edgegateway.intel.com (192.55.55.68) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2242.12; Thu, 7 Oct 2021 18:39:41 -0700 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=lEhoJsy6tIjoNkhrfT+eHG8ddY/pmca+EYp/kJxqVqqxlPyct1FJNL5C05jk1zN7smTW/FrbYV7I1FZrZgQPIaQfbRcZU9LgXFwiRkmCnqP/wL2VYDMktfsxDoE2GJV77Aj1d7bU3VO+w5n7TSsA6oX8rT+WMAoznPUXHyF83WPlPTbxpQok0FXyoSotfDyLpy9YSJ46+5Ec1sgyLNXyF9SDOtmnCLwD1aXUoRkdwxqfHXuYnyM/JbWNAfItRwQ2FteNS96a5FY+rIvIb93YHuEjt5QSQ+G8hpdwNyN8gd1YKmSs5mWdfBbVouSByU6UqiojNgRNRM89dU0Ts5R78g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=5LkrmV8UEsEoNhhTulPCckONV68CCQefFMNTOpLmU6E=; b=dRTl4bBveOl1uawf0pxwMOsZ+WueNp2c+dsweRzex+dAluXmCf2NCRtOWY4nA01KHjQjG8jRKbihPV/Q25+6vef7Xuj44H3lMq6y4a+jqywL7qaB20rsYIn5KRgjRiUFd6L6IBhNLlC17mepNU5Rx3HnKNBYPLeHoQBDKYMdZKX9dML/ZwUEz5jaIPa4rmcUdOVHiy6zgHFOgzo+iH4VehdvkhB4hMRyN0606rpWdw/5VkL+hu/clLxh8wsJJ7O3AuK6v+QFmxtN77NsmTp3fJIH14prtOwOinM8OR2Xv+7jQi3z2Mx3yj3E0tjiOTRFVsaRCQiB1TLxFveDBznLmw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=intel.com; dmarc=pass action=none header.from=intel.com; dkim=pass header.d=intel.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.onmicrosoft.com; s=selector2-intel-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=5LkrmV8UEsEoNhhTulPCckONV68CCQefFMNTOpLmU6E=; b=yA1p/Y8o56I043AX+ovDUYDyMLLgccU1UQnMyfwseivzDyY58AWBYEuQFL9CayRE94eskd29YJo1PZhbkp1UK9dG0rkKyVr9r3LARChzs3Ux8iRVJmqAgw+xgFxQ76dPxc72BXHw36tjo2q9W3UfrLpYPy8yyXtZtD+ceebgMnk= Received: from DM4PR11MB5453.namprd11.prod.outlook.com (2603:10b6:5:398::15) by DM6PR11MB3609.namprd11.prod.outlook.com (2603:10b6:5:140::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4587.22; Fri, 8 Oct 2021 01:39:41 +0000 Received: from DM4PR11MB5453.namprd11.prod.outlook.com ([fe80::ed4c:25e0:281e:da52]) by DM4PR11MB5453.namprd11.prod.outlook.com ([fe80::ed4c:25e0:281e:da52%2]) with mapi id 15.20.4587.020; Fri, 8 Oct 2021 01:39:41 +0000 From: "Dandan Bi" To: "Ma, Hua" , "devel@edk2.groups.io" CC: "Wang, Jian J" , Liming Gao Subject: Re: [PATCH] MdeModulePkg/Core/Dxe: Add lock protection in CoreLocateHandleBuffer() Thread-Topic: [PATCH] MdeModulePkg/Core/Dxe: Add lock protection in CoreLocateHandleBuffer() Thread-Index: AQHXtPXX8dlF+bnmh0Sw/Mv+ET5aTKvIYJkg Date: Fri, 8 Oct 2021 01:39:40 +0000 Message-ID: References: <33349c0e687ed186cd13db413fa708a8b761ffea.1632894508.git.hua.ma@intel.com> In-Reply-To: <33349c0e687ed186cd13db413fa708a8b761ffea.1632894508.git.hua.ma@intel.com> Accept-Language: zh-CN, en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: authentication-results: intel.com; dkim=none (message not signed) header.d=none;intel.com; dmarc=none action=none header.from=intel.com; x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: f4fe62b3-d40f-4a95-0820-08d989fc7f4d x-ms-traffictypediagnostic: DM6PR11MB3609: x-ms-exchange-transport-forked: True x-microsoft-antispam-prvs: x-ms-oob-tlc-oobclassifiers: OLM:255; x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: jla7oRwgQeO5o2AshOwbcJg5lo+7D+I6tRs3ZGhRK648gKr4s6qBFX2f77r7uRoUd83nB+6HkzWKmgpWsCi16zCdxGIzorAOMECFWgOfCqfmGMpndkL4XN3salmpJtS6Cd3nEd5KhFda6/QCHbgeq/6OqRlEwNUJj97MPMkqE1SvLWqQ91BjjmnhfFuLeBGUqocvsDt7IB8qRtND9uiZouflt/3RYNBhc1N7+/Ev38QBmHagZvR7t7+xirhMxKgjP0wZFz5iG26OoplTvt7GFM/jhArkPCnGLvqBsuM7uXs7QBXVIQAmSZIiG45oQlxyGNrVDIvai2L6BBOdNkJS+uWWlyTJoXsWYkThgu5XUgBlwsEsOsypft/NnpR7H+n+n95xKqVLnjF59MXwzGhbUNTw7HJ4AeQZQ53d0cWRzDQ4gr6BAF7OE4+ZZBdwCgz51B3x8y9Wk5V4OtEQPzQGylloasVHNFtgCPpbN2WHUHWcTMMyLIz4uXou8FrznkkfsfBvS0DjuTy5efFW3/men/p1E+ephV9ro2aBO2D+OtCXdgPnbTNSGLvOpBBg0vjX9iNnRH3KFaGMzCb7KvIdCMQC+UTSYN0v6PjNmSiVhhngoYnw2qMUvNah1SYP3pE4Kzlws8+ec9H3G1vNgeWxz5J7yplflECUUwFHyPV/DsWbmhtiUDG3RGdItmWihFQD/JNXIk4kste9OgKvrhjlmtGPqtbviyrB1Sp3Rdxo4Ik= x-forefront-antispam-report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM4PR11MB5453.namprd11.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(366004)(7696005)(54906003)(316002)(110136005)(508600001)(2906002)(86362001)(4326008)(966005)(76116006)(52536014)(66476007)(66556008)(64756008)(66446008)(66946007)(55016002)(53546011)(6506007)(71200400001)(186003)(26005)(33656002)(8936002)(9686003)(8676002)(5660300002);DIR:OUT;SFP:1102; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?us-ascii?Q?0+MR7CnccPPT0fka6a9nb6qqqwveQ2Lcy4REnNZwjQLV1fzdp8VwPTYjep/3?= =?us-ascii?Q?TEvhu5pCcxnb6ZIyEhaULREzCvNPkllCQYv18n5tNXDWxSuKZDcWAjytDU5C?= =?us-ascii?Q?FJp0KR3N9rVEwq/eys7G69+eOcHcSk0omijdm2sJOiv6QuIEnjjtkyGvjx9Z?= =?us-ascii?Q?Wlr8zo1wwOMS8G+rFkQPU4RAjeX7+zUYhVaD2+Cv1ohOIQxZ3KA/SOm3GySO?= =?us-ascii?Q?49Pt8MMpsB2L5GHcR087gRm6oYgJYCqTkBjrey2PvrhswgZXNJ3FTg6lz9cT?= =?us-ascii?Q?OPfjUs48N8TOxu+xQYa4tIdo4mMIFfuDwD/q8mFdjXUcU9+AYoM8pwoSpF0t?= =?us-ascii?Q?8YZOmiI0JFDp1zPddAPpEbxWfTvXqQjAINYBGPrh5vsfYy4ghIfv3hGC0rez?= =?us-ascii?Q?7VAq1PAOIPln9U381r305ZQB6ykZBYCaKaM08RHqq5Fd2lSq6qyM2+nBtTKN?= =?us-ascii?Q?uI4ArmwnCxskzMWFcWb5ofotcQhf5mBUIBaYgOq17xTTAA9SX6iEVHHI8oba?= =?us-ascii?Q?vse0frrjY13ZErYes9wMqxnjkuX9/EPFDTDmEQFzfztPIObRygega1gBgzBf?= =?us-ascii?Q?jAakrODm6aCkLPWc5H0RoCFLe70CfHc/bS6USD/FoShhU84HtXt/E/+VeNSZ?= =?us-ascii?Q?4wtYOPV1LsfBQIKYaWrncgpdHlPQkdMAxxffRmP/b9d2nwD/goMrPqZBBcbb?= =?us-ascii?Q?vfWVQKPgNT+wax/ovfDzsbO35o3dMZuQZsCQSPaBqOWLux+xDZM7kj+aBWTw?= =?us-ascii?Q?3CvChuMe7lGIOv/s0nqWueAvdoOF3eq9gfZlCPkVRBSXxncGvxJEqB9bK76z?= =?us-ascii?Q?qq2pigjRvwaK8j8o7cuk3N+G/iFL10N6vFB3VSEZ7qXXJ1FDp1I9HL5ANMBt?= =?us-ascii?Q?BC6mueW1f2MhGnVMr67PG2mT48c4IEKcrhJN/UVcCDYOCWI8EDWshgqyUpZq?= =?us-ascii?Q?kY04GYXLXTysja3mtqpB0lm4ARZIF6Go/5oP677uSq0PQfO1mQUro/2ilLRp?= =?us-ascii?Q?XgY5kdn5qvTjNGsb7H7qTLYH2k7h6judTZFyoyQqjnaCAk4tgIP3B8bmwcED?= =?us-ascii?Q?XSpakCAArGfUlhKtt9yqXKw/So+3HmiQUBYDDnDla6XxOJzKMQw3lBf4Xlqz?= =?us-ascii?Q?TVyUsY9hHbvJry/Miv9QswN2Ug0/F3r7XiDrz+YMcwFsOeNiMrG/jCQQQCu4?= =?us-ascii?Q?lsBKe+GbDraoj2Ibwtk2Oy00W4mBI+83Z9bb4ExEblSkmvKYupv+6ovaeUhW?= =?us-ascii?Q?iCovzvdS9dx87Llm4dPuaPcY0DPdb4lY25fopjgOKknVcMihtGIM7NIh6ycC?= =?us-ascii?Q?Opg=3D?= MIME-Version: 1.0 X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: DM4PR11MB5453.namprd11.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: f4fe62b3-d40f-4a95-0820-08d989fc7f4d X-MS-Exchange-CrossTenant-originalarrivaltime: 08 Oct 2021 01:39:40.8840 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 46c98d88-e344-4ed4-8496-4ed7712e255d X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: 93cja016v0jJR3pHYrD3lg7vvqVPx/8w4JV72d8tY+BxUrgZy+Lf1w2qrU+tpqL1hKcIfb3WhmjuQ7CoxYO15Q== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR11MB3609 Return-Path: dandan.bi@intel.com X-OriginatorOrg: intel.com Content-Language: en-US Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Reviewed-by: Dandan Bi Thanks, Dandan > -----Original Message----- > From: Ma, Hua > Sent: Wednesday, September 29, 2021 1:49 PM > To: devel@edk2.groups.io > Cc: Ma, Hua ; Wang, Jian J ; > Liming Gao ; Bi, Dandan > Subject: [PATCH] MdeModulePkg/Core/Dxe: Add lock protection in > CoreLocateHandleBuffer() >=20 > REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3D3666 >=20 > Currently, CoreLocateHandleBuffer() follows three steps: > 1) get the size of protocol database firstly > 2) allocate the buffer based on the size > 3) get the protocol database into the buffer There is no lock protection = for > the whole three steps. If a new protocol added in step 2) by other task, = e.g. > (event timer handle USB device hotplug). The size of protocol database ma= y > be increased and cannot fit into the previous buffer in step 3). The prot= ocol > database cannot be returned successfully, EFI_BUFFER_TOO_SMALL error > will be returned. >=20 > This patch adds the lock to protect the whole three steps. > It can make sure the correct protocol database be returned. >=20 > Cc: Jian J Wang > Cc: Liming Gao > Cc: Dandan Bi > Signed-off-by: Hua Ma > --- > MdeModulePkg/Core/Dxe/Hand/Locate.c | 64 > +++++++++++++++++++++++------ > 1 file changed, 51 insertions(+), 13 deletions(-) >=20 > diff --git a/MdeModulePkg/Core/Dxe/Hand/Locate.c > b/MdeModulePkg/Core/Dxe/Hand/Locate.c > index be17f4cbc3..4987c046c6 100644 > --- a/MdeModulePkg/Core/Dxe/Hand/Locate.c > +++ b/MdeModulePkg/Core/Dxe/Hand/Locate.c > @@ -86,7 +86,8 @@ CoreGetNextLocateByProtocol ( >=20 >=20 > /** > - Locates the requested handle(s) and returns them in Buffer. > + Internal function for locating the requested handle(s) and returns the= m in > Buffer. > + The caller should already have acquired the ProtocolLock. >=20 > @param SearchType The type of search to perform to locate= the > handles @@ -104,8 +105,7 @@ > CoreGetNextLocateByProtocol ( >=20 > **/ > EFI_STATUS > -EFIAPI > -CoreLocateHandle ( > +InternalCoreLocateHandle ( > IN EFI_LOCATE_SEARCH_TYPE SearchType, > IN EFI_GUID *Protocol OPTIONAL, > IN VOID *SearchKey OPTIONAL, > @@ -143,11 +143,6 @@ CoreLocateHandle ( > ResultBuffer =3D (IHANDLE **) Buffer; > Status =3D EFI_SUCCESS; >=20 > - // > - // Lock the protocol database > - // > - CoreAcquireProtocolLock (); > - > // > // Get the search function based on type > // > @@ -190,7 +185,6 @@ CoreLocateHandle ( > } >=20 > if (EFI_ERROR(Status)) { > - CoreReleaseProtocolLock (); > return Status; > } >=20 > @@ -247,10 +241,47 @@ CoreLocateHandle ( > } > } >=20 > - CoreReleaseProtocolLock (); > return Status; > } >=20 > +/** > + Locates the requested handle(s) and returns them in Buffer. > + > + @param SearchType The type of search to perform to locate= the > + handles > + @param Protocol The protocol to search for > + @param SearchKey Dependant on SearchType > + @param BufferSize On input the size of Buffer. On output= the > + size of data returned. > + @param Buffer The buffer to return the results in > + > + @retval EFI_BUFFER_TOO_SMALL Buffer too small, required buffer size = is > + returned in BufferSize. > + @retval EFI_INVALID_PARAMETER Invalid parameter > + @retval EFI_SUCCESS Successfully found the requested handle= (s) > and > + returns them in Buffer. > + > +**/ > +EFI_STATUS > +EFIAPI > +CoreLocateHandle ( > + IN EFI_LOCATE_SEARCH_TYPE SearchType, > + IN EFI_GUID *Protocol OPTIONAL, > + IN VOID *SearchKey OPTIONAL, > + IN OUT UINTN *BufferSize, > + OUT EFI_HANDLE *Buffer > + ) > +{ > + EFI_STATUS Status; > + > + // > + // Lock the protocol database > + // > + CoreAcquireProtocolLock (); > + Status =3D InternalCoreLocateHandle(SearchType, Protocol, SearchKey, > +BufferSize, Buffer); > + CoreReleaseProtocolLock (); > + return Status; > +} >=20 >=20 > /** > @@ -610,7 +641,6 @@ Done: > return Status; > } >=20 > - > /** > Function returns an array of handles that support the requested protoc= ol > in a buffer allocated from pool. This is a version of CoreLocateHandle= () @@ > -657,7 +687,12 @@ CoreLocateHandleBuffer ( > BufferSize =3D 0; > *NumberHandles =3D 0; > *Buffer =3D NULL; > - Status =3D CoreLocateHandle ( > + > + // > + // Lock the protocol database > + // > + CoreAcquireProtocolLock(); > + Status =3D InternalCoreLocateHandle ( > SearchType, > Protocol, > SearchKey, > @@ -674,15 +709,17 @@ CoreLocateHandleBuffer ( > if (Status !=3D EFI_INVALID_PARAMETER) { > Status =3D EFI_NOT_FOUND; > } > + CoreReleaseProtocolLock (); > return Status; > } >=20 > *Buffer =3D AllocatePool (BufferSize); > if (*Buffer =3D=3D NULL) { > + CoreReleaseProtocolLock (); > return EFI_OUT_OF_RESOURCES; > } >=20 > - Status =3D CoreLocateHandle ( > + Status =3D InternalCoreLocateHandle ( > SearchType, > Protocol, > SearchKey, > @@ -695,6 +732,7 @@ CoreLocateHandleBuffer ( > *NumberHandles =3D 0; > } >=20 > + CoreReleaseProtocolLock (); > return Status; > } >=20 > -- > 2.32.0.windows.2