From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from hqemgate16.nvidia.com (hqemgate16.nvidia.com [216.228.121.65]) by mx.groups.io with SMTP id smtpd.web10.4086.1570820143879622600 for ; Fri, 11 Oct 2019 11:55:43 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@nvidia.com header.s=n1 header.b=FdfeZo7K; spf=pass (domain: nvidia.com, ip: 216.228.121.65, mailfrom: ashishsingha@nvidia.com) Received: from hqpgpgate101.nvidia.com (Not Verified[216.228.121.13]) by hqemgate16.nvidia.com (using TLS: TLSv1.2, DES-CBC3-SHA) id ; Fri, 11 Oct 2019 11:55:45 -0700 Received: from hqmail.nvidia.com ([172.20.161.6]) by hqpgpgate101.nvidia.com (PGP Universal service); Fri, 11 Oct 2019 11:55:43 -0700 X-PGP-Universal: processed; by hqpgpgate101.nvidia.com on Fri, 11 Oct 2019 11:55:43 -0700 Received: from HQMAIL109.nvidia.com (172.20.187.15) by HQMAIL111.nvidia.com (172.20.187.18) with Microsoft SMTP Server (TLS) id 15.0.1473.3; Fri, 11 Oct 2019 18:55:43 +0000 Received: from hqnvemgw01.nvidia.com (172.20.150.20) by HQMAIL109.nvidia.com (172.20.187.15) with Microsoft SMTP Server (TLS) id 15.0.1473.3 via Frontend Transport; Fri, 11 Oct 2019 18:55:43 +0000 Received: from ashishsingha-lnx.nvidia.com (Not Verified[10.28.48.147]) by hqnvemgw01.nvidia.com with Trustwave SEG (v7,5,8,10121) id ; Fri, 11 Oct 2019 11:55:43 -0700 From: "Ashish Singhal" To: , , CC: Ashish Singhal Subject: [PATCH v3] MdeModulePkg/XhciDxe: Fix Aligned Page Allocation Date: Fri, 11 Oct 2019 12:55:38 -0600 Message-ID: X-Mailer: git-send-email 2.7.4 In-Reply-To: References: X-NVConfidentiality: public Return-Path: ashishsingha@nvidia.com MIME-Version: 1.0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nvidia.com; s=n1; t=1570820145; bh=AKoNusuz5YzMDnBc4EKba7BvBqFWvcRLaytJFPX5C4M=; h=X-PGP-Universal:From:To:CC:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:X-NVConfidentiality:MIME-Version: Content-Type; b=FdfeZo7K8X0saZgTjiCn1TgH/UBk1E+nxvubXulNeP84tNhIUumh+QqfK90teHA+U Yz+/ayOOmO9MTarjD9sRMMoa0j5zMjA6MXCgv4X/o5kfvoYd0ltH1rqLJtA6hf8Xmk FQVdUUqIF/WAkKzle3sL4GywCjS9sVk1Df51JTxVJPEj86nW6+89FAGQbf+NTnoZOh HqPezAgwIUWRrcBXzOlY0Jz9QGbutqgPNLg+SigEHjMCWG0QXWPjh0Yy/Kqqw+pQxC 0A44XH4ZYceIE9xVtx0qXED+OcGlW2ZmV+65yu/3R8isylFkN9Hk+k2I9eAFsXwVU3 u9y4U+OCjEgNw== Content-Type: text/plain While allocating pages aligned at an alignment higher than 4K, allocate memory taking into consideration the padding required for that alignment. The calls to free pages takes care of this already. Signed-off-by: Ashish Singhal --- MdeModulePkg/Bus/Pci/XhciDxe/UsbHcMem.c | 2 +- MdeModulePkg/Bus/Pci/XhciPei/UsbHcMem.c | 20 +++++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/MdeModulePkg/Bus/Pci/XhciDxe/UsbHcMem.c b/MdeModulePkg/Bus/Pci/XhciDxe/UsbHcMem.c index fd79988..aa69c47 100644 --- a/MdeModulePkg/Bus/Pci/XhciDxe/UsbHcMem.c +++ b/MdeModulePkg/Bus/Pci/XhciDxe/UsbHcMem.c @@ -656,7 +656,7 @@ UsbHcAllocateAlignedPages ( PciIo, AllocateAnyPages, EfiBootServicesData, - Pages, + RealPages, &Memory, 0 ); diff --git a/MdeModulePkg/Bus/Pci/XhciPei/UsbHcMem.c b/MdeModulePkg/Bus/Pci/XhciPei/UsbHcMem.c index 56c0b90..f95b5e5 100644 --- a/MdeModulePkg/Bus/Pci/XhciPei/UsbHcMem.c +++ b/MdeModulePkg/Bus/Pci/XhciPei/UsbHcMem.c @@ -567,6 +567,7 @@ UsbHcAllocateAlignedPages ( EFI_PHYSICAL_ADDRESS DeviceMemory; UINTN AlignedDeviceMemory; UINTN RealPages; + UINTN UnalignedPages; // // Alignment must be a power of two or zero. @@ -593,7 +594,7 @@ UsbHcAllocateAlignedPages ( ASSERT (RealPages > Pages); Status = IoMmuAllocateBuffer ( - Pages, + RealPages, &Memory, &DeviceMemory, Mapping @@ -603,6 +604,23 @@ UsbHcAllocateAlignedPages ( } AlignedMemory = ((UINTN) Memory + AlignmentMask) & ~AlignmentMask; AlignedDeviceMemory = ((UINTN) DeviceMemory + AlignmentMask) & ~AlignmentMask; + UnalignedPages = EFI_SIZE_TO_PAGES (AlignedMemory - (UINTN) Memory); + if (UnalignedPages > 0) { + // + // Free first unaligned page(s). + // + Status = IoMmuFreeBuffer (UnalignedPages, Memory, Mapping); + ASSERT_EFI_ERROR (Status); + } + Memory = (VOID *)(UINTN)(AlignedMemory + EFI_PAGES_TO_SIZE (Pages)); + UnalignedPages = RealPages - Pages - UnalignedPages; + if (UnalignedPages > 0) { + // + // Free last unaligned page(s). + // + Status = IoMmuFreeBuffer (UnalignedPages, Memory, Mapping); + ASSERT_EFI_ERROR (Status); + } } else { // // Do not over-allocate pages in this case. -- 2.7.4