From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753523AbcKORCf (ORCPT ); Tue, 15 Nov 2016 12:02:35 -0500 Received: from mail-cys01nam02on0069.outbound.protection.outlook.com ([104.47.37.69]:32758 "EHLO NAM02-CY1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752270AbcKORCa (ORCPT ); Tue, 15 Nov 2016 12:02:30 -0500 Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=Thomas.Lendacky@amd.com; Subject: Re: [RFC PATCH v3 13/20] x86: DMA support for memory encryption To: =?UTF-8?B?UmFkaW0gS3LEjW3DocWZ?= References: <20161110003426.3280.2999.stgit@tlendack-t1.amdoffice.net> <20161110003723.3280.62636.stgit@tlendack-t1.amdoffice.net> <20161115143943.GC2185@potion> CC: , , , , , , , , , Rik van Riel , Arnd Bergmann , Jonathan Corbet , Matt Fleming , Joerg Roedel , Konrad Rzeszutek Wilk , Paolo Bonzini , Larry Woodman , Ingo Molnar , Borislav Petkov , Andy Lutomirski , "H. Peter Anvin" , Andrey Ryabinin , Alexander Potapenko , Thomas Gleixner , Dmitry Vyukov From: Tom Lendacky Message-ID: Date: Tue, 15 Nov 2016 11:02:20 -0600 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 MIME-Version: 1.0 In-Reply-To: <20161115143943.GC2185@potion> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8bit X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: CY1PR04CA0027.namprd04.prod.outlook.com (10.166.187.37) To MWHPR12MB1150.namprd12.prod.outlook.com (10.169.204.14) X-Microsoft-Exchange-Diagnostics: 1;MWHPR12MB1150;2:MqZgosN0QVWNVrhZ9ZM9n0FR/rFW1sl1KC/nT6RqUyWVPZM8QZXYOPWlOXSejvp/Sg7EFy9nIGzMRKEND7sII1k+yIbvM1KkROXlwmPbMsC1k+MiaBmsF4UVlIxNsYQybwe+20HNSwGReL5m2hFJPhOW/JX8lJ8nUrUeORFhn1E=;3:y2w1k58PaAMsy3zbyc+tSUQ+j+bGSvzsNpuo6UyMYQSyywx2/Fxni6y47RdmONVN3/bMsehpf7Rv6Agun9zloLPVxAreiU5H1fH0s5NIeKmTVbhX9Ffzyc2iVejOH3vSpNAKmAs3FhA9CvzaBFkJMSVx5omPp9FnoYXmIk/7Zag=;25:SxQgrCZ0y4L5cru3Dw5UPoP9Oe/3iuUkcu18YOUFwjaO5ZxQZLJShYap19HISz+sYDAXvCYdWeYusB1tA9eGPhk78glXPmoFZXpf6/vS299uvOSIA5V+AauolrnOPIwgVJbkhsI+ixBZa1UoHTr09k9TS1WLPzYUdSgsIemCe63+aXlLF0nEj9n1t2hcZQxt31sTVFQGFZXC4+58VPetNYcacfDJgokW8eqFQLnrNuZbWI1R9vE1XwuBtqTcvOWqUI+xc/s3Sp45JRWYnhGQ+hHlkx1m7tE2dQiF4E6tKMwu5WHl+v81BOjxjCyLVyT/bjc/ao7FryoyuNDutOzEipMuWam6DwjdNoRe8Gs/xecMzU/4dPTEdSGq22iPl+9Z//CZ8wASYd+K92zj4km3ajNNLDxBgQSY2RIT1RdfY66bUA9OVCC8+Iw8s3J+60c2DNAWHuYZ4KWhI/Tk/Yfdtg== X-MS-Office365-Filtering-Correlation-Id: fc58c5f5-5ddf-4a14-88e9-08d40d792cf0 X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(22001);SRVR:MWHPR12MB1150; X-Microsoft-Exchange-Diagnostics: 1;MWHPR12MB1150;31:T0EVnw9OnUi4G51sa8eArGLrqwC2pr+BCQITrkACwYrak4+pM9uikPe1VuFdtVRwywvzCikVdsiJft/pCXlneb3vi5xMybgDs9UUldxceIIlwna8lYCMYfqo+WxgFCJdsyuqS4ZtMIEw0k/x4UFtdosXAaFf130fHmBcfu00T/783H4qvFexdXzCsZyu41VLrzSd5mCjQgG8uDXyxMo4XkesRamZFIga9xUP7GkjVm7JDB0yJKG07pwXa8woRgJB651Q8NyuYWTsrWAxe9ll5w==;20:5q+i1Kr2JzuPJEf81ax2YxSzxBPKOal5Ibez6w4xNJ8kK8MSBOsiGMgxg8K8tyWOegd2Mlbf1URA58kZAoaJ0fOiOLzDH8P5JWt7iXbDagFXSngCmBQvY7wYDINPfnkLHUcX8krDSI0pIS0pC/EJOVGlRgOGGKQfh+Wgvxu54sPPbmK/KZ6flJLEnI8CySClZJT8GAWlkgfGR81BtCYR/S8p0QXpZKQAESfe9bQvQ59fVGdN/t98vTs64gB6IfpmxnEzlwdAM6r/kqo9Ob7FKr0OXRBTneW0BbnmXjslyFu2u6oJRXJqov4PBtF5C2J06Wb20ZW8ZbUvqUCG4bLamH9YTvoEzrrLJcTobigemD0l53wkYTsHMyTkW6hXXXcYX5vu7Jdsxrlp/N8KPsa0HpK0F0zNcJANCctt79nmbNRrYuGt8pydHQhyop3p6GTBsCbNXxALALkLP3PhPCbwouZpk+w3uJG8Op90AZfpKZGdGC8fqQ2KRCuEpGgjRMhs X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(767451399110); X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(6060326)(601004)(2401047)(8121501046)(5005006)(3002001)(10201501046)(6055026)(6061324);SRVR:MWHPR12MB1150;BCL:0;PCL:0;RULEID:;SRVR:MWHPR12MB1150; X-Microsoft-Exchange-Diagnostics: 1;MWHPR12MB1150;4:H5/j9TAWgQm64feQqKEDJyP+AmlPFnY2TCIloKqP3ENoBgrX32/jAK/ityzOScFIWk+qpkXqeKISmpA/tyO32cShggQamsV1T2Q46QRYet2C+97Jaf8ZOWHqUKM+SJM108kZRrshb52MyxSXHVPvfGIyjM5e0faaZ+5FUQPivhXYGeu57IDu6/FmK1s7IPEdEPp+eZl2/2N3zuuqUXKv4i8UGig/NHbUHtEML2LS/ROWqbyEQIbtWt1p8ehjRGZKiPm7RQklcxqslRf0MUvTftkP3XWMq1RC5mYGqDT5QkeDvmYOV2BJ+0XKK1ZSFeTFzqsNcUHZLH5ji6Pi6AWRVvHJ+ND8vS1I/HqCcTfsYQUa194C8SMXudWVvQK8n8at0f8QeV2SNoZRH3NDWPwmn1ySp+HS6men4SeHrGG5WS+8oj07qZcxmDynPhK196fII1g5mRU7QInRGbUS903prlSQcsWxb2DHPuPzyqNaV98= X-Forefront-PRVS: 012792EC17 X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10009020)(4630300001)(6009001)(6049001)(7916002)(189002)(24454002)(199003)(377454003)(377424004)(5820100001)(54356999)(33646002)(76176999)(2950100002)(64126003)(42186005)(23676002)(50986999)(7846002)(83506001)(2870700001)(2906002)(68736007)(101416001)(6916009)(6666003)(4001350100001)(31686004)(92566002)(36756003)(86362001)(66066001)(7416002)(189998001)(4326007)(77096005)(65826007)(5660300001)(8676002)(81156014)(47776003)(50466002)(106356001)(110136003)(6116002)(65956001)(229853002)(105586002)(65806001)(3846002)(31696002)(81166006)(97736004)(7736002)(305945005)(217873001);DIR:OUT;SFP:1101;SCL:1;SRVR:MWHPR12MB1150;H:[10.236.64.222];FPR:;SPF:None;PTR:InfoNoRecords;MX:1;A:1;LANG:en; X-Microsoft-Exchange-Diagnostics: =?utf-8?B?MTtNV0hQUjEyTUIxMTUwOzIzOmh2V2h2Kzk1MnBDb0dPZytueDJHb3JuQjRW?= =?utf-8?B?MVlqMUZzb1Ywa3U5WEdhYTlXZ25lck5ENVY4Wll0VGNJcDF4VTJhbllWa3RS?= =?utf-8?B?bkdzZm5Sdmg4OW1pRUNRRmRMQ0NFWXRKQlFHRkF1MjRsbjdVdUt4MGN1cDdC?= =?utf-8?B?eFRhTGI1VFNINE5OdEFFQUl6T09aaloyOTlFZnhXK0djYW5taks4Y240ZTAv?= =?utf-8?B?ZCtGdkJVaVRoV3p0aThFdERsK2hGRW5kWTBQQUtpZ2JVck1YNjR1NE1Md0ly?= =?utf-8?B?MGJ3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wrBHG5Z0PybQWfwfLLehghD6QCGyHunc8h8K2ctVGqbWRbuEhC2xxolZvp7Wy2yCGdEBHROkP X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Nov 2016 17:02:24.5944 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: MWHPR12MB1150 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 11/15/2016 8:39 AM, Radim Krčmář wrote: > 2016-11-09 18:37-0600, Tom Lendacky: >> Since DMA addresses will effectively look like 48-bit addresses when the >> memory encryption mask is set, SWIOTLB is needed if the DMA mask of the >> device performing the DMA does not support 48-bits. SWIOTLB will be >> initialized to create un-encrypted bounce buffers for use by these devices. >> >> Signed-off-by: Tom Lendacky >> --- >> diff --git a/arch/x86/kernel/pci-nommu.c b/arch/x86/kernel/pci-nommu.c >> @@ -30,7 +30,7 @@ static dma_addr_t nommu_map_page(struct device *dev, struct page *page, >> enum dma_data_direction dir, >> unsigned long attrs) >> { >> - dma_addr_t bus = page_to_phys(page) + offset; >> + dma_addr_t bus = phys_to_dma(dev, page_to_phys(page)) + offset; >> WARN_ON(size == 0); >> if (!check_addr("map_single", dev, bus, size)) >> return DMA_ERROR_CODE; >> diff --git a/arch/x86/kernel/pci-swiotlb.c b/arch/x86/kernel/pci-swiotlb.c >> @@ -12,6 +12,8 @@ >> int swiotlb __read_mostly; >> >> void *x86_swiotlb_alloc_coherent(struct device *hwdev, size_t size, >> @@ -64,13 +66,15 @@ static struct dma_map_ops swiotlb_dma_ops = { >> * pci_swiotlb_detect_override - set swiotlb to 1 if necessary >> * >> * This returns non-zero if we are forced to use swiotlb (by the boot >> - * option). >> + * option). If memory encryption is enabled then swiotlb will be set >> + * to 1 so that bounce buffers are allocated and used for devices that >> + * do not support the addressing range required for the encryption mask. >> */ >> int __init pci_swiotlb_detect_override(void) >> { >> int use_swiotlb = swiotlb | swiotlb_force; >> >> - if (swiotlb_force) >> + if (swiotlb_force || sme_me_mask) >> swiotlb = 1; >> >> return use_swiotlb; > > We want to return 1 even if only sme_me_mask is 1, because the return > value is used for detection. The following would be less obscure, IMO: > > if (swiotlb_force || sme_me_mask) > swiotlb = 1; > > return swiotlb; If we do that then all DMA would go through the swiotlb bounce buffers. By setting swiotlb to 1 we indicate that the bounce buffers will be needed for those devices that can't support the addressing range when the encryption bit is set (48 bit DMA). But if the device can support the addressing range we won't use the bounce buffers. > >> diff --git a/init/main.c b/init/main.c >> @@ -598,6 +602,15 @@ asmlinkage __visible void __init start_kernel(void) >> */ >> locking_selftest(); >> >> + /* >> + * This needs to be called before any devices perform DMA >> + * operations that might use the swiotlb bounce buffers. >> + * This call will mark the bounce buffers as un-encrypted so >> + * that their usage will not cause "plain-text" data to be >> + * decrypted when accessed. >> + */ >> + mem_encrypt_init(); > > (Comments below are connected to the reason why we call this.) > >> diff --git a/lib/swiotlb.c b/lib/swiotlb.c >> @@ -159,6 +171,31 @@ void swiotlb_print_info(void) >> +/* >> + * If memory encryption is active, the DMA address for an encrypted page may >> + * be beyond the range of the device. If bounce buffers are required be sure >> + * that they are not on an encrypted page. This should be called before the >> + * iotlb area is used. >> + */ >> +void __init swiotlb_clear_encryption(void) >> +{ >> + void *vaddr; >> + unsigned long bytes; >> + >> + if (no_iotlb_memory || !io_tlb_start || late_alloc) > > io_tlb_start seems redundant -- when can !no_iotlb_memory && > !io_tlb_start happen? Yes, the io_tlb_start check can be removed. > > Is the order of calls > 1) swiotlb init > 2) SME init > 3) swiotlb late init > ? Yes, sort of. The swiotlb late init may not be called. > > We setup encrypted swiotlb and then decrypt it, but sometimes set it up > decrypted (late_alloc) ... why isn't the swiotlb set up decrypted > directly? When swiotlb is allocated in swiotlb_init(), it is too early to make use of the api to the change the page attributes. Because of this, the callback to make those changes is needed. > >> + return; >> + >> + vaddr = phys_to_virt(io_tlb_start); >> + bytes = PAGE_ALIGN(io_tlb_nslabs << IO_TLB_SHIFT); >> + swiotlb_set_mem_unenc(vaddr, bytes); >> + memset(vaddr, 0, bytes); >> + >> + vaddr = phys_to_virt(io_tlb_overflow_buffer); >> + bytes = PAGE_ALIGN(io_tlb_overflow); >> + swiotlb_set_mem_unenc(vaddr, bytes); >> + memset(vaddr, 0, bytes); >> +} >> + >> @@ -541,7 +583,7 @@ static phys_addr_t >> map_single(struct device *hwdev, phys_addr_t phys, size_t size, >> enum dma_data_direction dir) >> { >> - dma_addr_t start_dma_addr = phys_to_dma(hwdev, io_tlb_start); >> + dma_addr_t start_dma_addr = swiotlb_phys_to_dma(hwdev, io_tlb_start); > > We have decrypted io_tlb_start before, so shouldn't its physical address > be saved without the sme bit? (Which changes a lot ...) I'm not sure what you mean here, can you elaborate a bit more? Thanks, Tom > > Thanks. >