Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753292AbcKOUdY (ORCPT ); Tue, 15 Nov 2016 15:33:24 -0500 Received: from mail-bl2nam02on0089.outbound.protection.outlook.com ([104.47.38.89]:50519 "EHLO NAM02-BL2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752923AbcKOUdR (ORCPT ); Tue, 15 Nov 2016 15:33:17 -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> <20161115181736.GA14060@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: <55bba6f6-b134-6c7b-fc20-10d2dbf781f1@amd.com> Date: Tue, 15 Nov 2016 14:33:06 -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: <20161115181736.GA14060@potion> Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: BLUPR01CA040.prod.exchangelabs.com (10.160.23.30) To CY4PR12MB1143.namprd12.prod.outlook.com (10.168.164.135) X-Microsoft-Exchange-Diagnostics: 1;CY4PR12MB1143;2:giIPE/tcaPD9KgBw1Ml5SxZkw4eu6Yg4E/8a97r0+tpNU1EENUqKb+goDjIPZTyb7VKtbVHAt9DcxE8vc03N8W7PYZBhC5pR80azT1JpqqJw89J7erTMAQ/DZbhRxc+Dn9s/Jd3fn8Ck9oRVgE5n9/sZlEmglcae24piGM0rVGI=;3:Tv2rMgfu/P8mnI1pv3BelYgONV/3zPlF5sW5ArnOybED7NdNvm6/6Rf5FmEgXr4hwr0Bi5fPXLItvc15Lmp9QyYdHMaZFnyFH+SC2SE6htftKmJjZMl7Blok2JYmcujqN/0B4OI0M+Y4TH4iasWnMIgCx2CA8wHouVFf1Sh5xNk=;25:3u2VdIwuOmbWmYFnfDN2JDw9MCWTxB1JnxNo1bZ06rpykN1AJtA8l7p2s9VUzWU4eiv+UQqxMcj0bTGmGiT9hBIGyZRjeF2IOMxoGXNNk872QP6Gvdhy8oY5Y44f06cK4RUxWth9CD1mn9vy/sJVjj1efNhepOUc73Z5HTlyoX9Ht8wmJb1ublZaSnjeBJh9yHvKVOioDqnHkBMOuf1wWK+y7o2ksUdjezKEhqv8XmhXP5Y/jFF/T3zu/xynvuSi2mGv8qKE+eohIo48RTVvRzr2KUKS7CzktF5Q87g14VLeKQ3A4CmTIU+GjYFkyZKqoLPm1SxL1AYa1uzmE7Djvwf2zYBVLpAk+0lg47xPnDifxtIG4qr6gzEUdsNeupeqJ1wH9TfXiBXbBxQXJVifd6DM4J3g6YUpBDw/pgOh1qHXedHyDTbcqhRTS4gc//XjS1FLMjsGd6hkPdS3++g4Pw== X-MS-Office365-Filtering-Correlation-Id: 74fe1995-2399-4879-7f70-08d40d969f37 X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(22001);SRVR:CY4PR12MB1143; X-Microsoft-Exchange-Diagnostics: 1;CY4PR12MB1143;31:K47ZfEfPtTgJHuF5D6rvYjfbJ8vqdcx8TDLRWi1HvRB0cMb6lp7QOLGIa2f8FL+VGlSfAJakmqgAj6K8QW9VaEl+y0JwVKp1mVi095K0mfAD3oTniYIDMqM5SF/v0IbrYLBmaHHklzW6sUvAp6yfPC/uzfLthGRBZdjVdqtcIpH2X66xbSTeaLIGiA8fq3Beb9o0PEqSTcN9nrxwXc5tbypVun2BnER5v+W/giOB5QbEPVbFMumn25SdFysXgS39F7iNQSh5JDPBXaCHqSLsgA==;20:0fcuWSNiilp19Uq19FwnnS01djnPCm1CLAaRKLP8rRkhhll+2vVMwrWvzZmjQ/9R/atLz6/nOxj+U/KPlBpnphzIkQmWu8iD1OaF4xg6KYXL93BtUQi+wXqXVK9xx70LuA9tjSUZWZ5fM+2I4zHh8EzaTsN0p9XSCs5eNzd/VCwT8hYQtHDHgueiSn9LOIoS5a41gVQ6XSrYQgoIPBTc9hlnqBYd+ApQfG1dVnvvl6Z2dAm9usimCgH/ALVKfcL7+sXFaVTcRrRDz5Dq2PuBBX7yeM0/NFgnJulihx4ALv4zu8uLwK+67L/tBZyqpqo6n0GW7joleTVZEMjoddJQ3XHW7UENHN20leKMU7sojSgVAxgvJPx2ekBzpO7I4poR88Lzjt7LgGPmYp1W3ZN6fJ48akloVOHvx9sjyPANmrBvKfE6taW4wKMRQQm9r/BJMphJiMc+lvw85uK207Nl6mjHVdHmCSLZVutFNkwD7Ot6U8RmJxEmHkOUK2t94czf 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)(10201501046)(3002001)(6055026)(6061324);SRVR:CY4PR12MB1143;BCL:0;PCL:0;RULEID:;SRVR:CY4PR12MB1143; X-Microsoft-Exchange-Diagnostics: 1;CY4PR12MB1143;4:ndT1rTgdsYKZLGar3kmeTFrdpOHBpyWGgEgm6M1RQYhm2y2D2PepeO2L8tTXpmWiQyu2EqoO08qJLtjxJP0YxkNnAVmHSfXXixWxGYU11XPxVYZN0IKZfpMdwUPCo8GKDXQdHjdFLGp7e0Hfl+68PmrEUUTik9m/XHt19GW3c9fOHGoWpmQHQ9eJntXKZhGz2EgX1HkZAFHxfgn4oYtadDKAQ4IJADQCxmLLoqB1PshSKcEVGFDeFS6ZDjyEt0Pq3Gzogr/StU2Zv/nRqsblRR1jylHvPiNOpBFN2BIJWdzQfnxfQjbFaIcKcN4gQ8PaKm4Bpo7BXaBSld/IFwXDQ5ktFn7Aec4t+ho5shPGpAvqeOB+dIh5OxvgOaDwjL8KiWrDlRFKsn0e6WQ71OH0DZRFTRqutRY/BJdSLdqEJH/QXgG+AlxjL+yZt013DgVJpqQN8e5yjQDUqtBBM30SN7fKgyIz5Y4b1xQw91nRVZc= X-Forefront-PRVS: 012792EC17 X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10009020)(4630300001)(6049001)(6009001)(7916002)(377454003)(189002)(377424004)(199003)(24454002)(68736007)(92566002)(77096005)(2870700001)(4326007)(2906002)(65806001)(86362001)(8676002)(6116002)(50466002)(23676002)(105586002)(81166006)(3846002)(81156014)(65956001)(66066001)(42186005)(106356001)(31686004)(6916009)(5660300001)(83506001)(31696002)(47776003)(36756003)(97736004)(2950100002)(7736002)(229853002)(4001350100001)(50986999)(33646002)(189998001)(76176999)(54356999)(4001150100001)(7416002)(7846002)(101416001)(93886004)(64126003)(65826007)(110136003)(6666003)(305945005)(217873001);DIR:OUT;SFP:1101;SCL:1;SRVR:CY4PR12MB1143;H:[10.236.64.222];FPR:;SPF:None;PTR:InfoNoRecords;MX:1;A:1;LANG:en; X-Microsoft-Exchange-Diagnostics: =?utf-8?B?MTtDWTRQUjEyTUIxMTQzOzIzOmMrZGdwb2ZncmxCVkc4akpaWTB4NFp4M1Zr?= =?utf-8?B?V3YyTnA1MngxbHRXMlFuUDBTYmtBMmhBN3JsdzQ0RGFKLzlsNFEzendCNEs5?= =?utf-8?B?ZXF5K2xYaVA5d01UNWVsZTl4R2svK0ZnN0x2YTlRQ2lrMlF6S1ErNGNHUTNo?= =?utf-8?B?eStWdUZnWklPbkdwdlcxYUtHNS9nSS96cjdyb2RaSy9mVEtJMm5zd1hycm1l?= =?utf-8?B?dDViNzZvSVBOazJ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RwbCMGPTGOqcg4VxDJ2RB51WeX/8BV48VF3epJF+C9aFszqPAPSwhz4kgB8muyGOmdQvqWGTQ/IyiS7i3pQhEph/pNt+6FWFZ0HgQECgFxyNdaFZd+lsd38JsBaYpXVZgsI2la7Q0IERorMsXx X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Nov 2016 20:33:10.7100 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY4PR12MB1143 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 4570 Lines: 111 On 11/15/2016 12:17 PM, Radim Krčmář wrote: > 2016-11-15 11:02-0600, Tom Lendacky: >> 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-swiotlb.c b/arch/x86/kernel/pci-swiotlb.c >>>> @@ -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. > > No, that is decided for example in swiotlb_map_page() and we need to > call pci_swiotlb_init() to register that function. > >> 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. > > If we return 0 here, then pci_swiotlb_init() will not be called => > dma_ops won't be set to swiotlb_dma_ops => we won't use bounce buffers. > Ok, I see why this was working for me... By setting swiotlb = 1 and returning 0 it was continuing to the pci_swiotlb_detect_4gb table which would return the current value of swiotlb, which is 1, and so the swiotlb ops were setup. So the change that you mentioned will work, thanks for pointing that out and getting me to dig deeper on it. I'll update the patch. >>> 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. > > Thanks. (I don't know page table setup enough to see a lesser evil. :]) > >>>> @@ -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? > > The C-bit (sme bit) is a part of the physical address. The C-bit (sme_me_mask) isn't part of the physical address for io_tlb_start, but since the original call was to phys_to_dma(), which now will automatically "or" in the C-bit, I needed to adjust that by using swiotlb_phys_to_dma() to remove the C-bit. > If we know that a certain physical page should be accessed as > unencrypted (the bounce buffer) then the C-bit is 0. > I'm wondering why we save the physical address with the C-bit set when > we know that it can't be accessed that way (because we remove it every > time). It's not saved with the C-bit, but the phys_to_dma call will "or" in the C-bit automatically. And since this is common code I need to leave that call to phys_to_dma in. > > The naming is a bit confusing, because physical addresses are actually > virtualized by SME -- maybe we should be calling them SME addresses? Interesting idea... I'll have to look at how that plays out in the patches and documentation. Thanks, Tom >