Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp3617162pxb; Sun, 27 Mar 2022 01:08:52 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyrMMyc9M70yiKhLsnyyW3K4odj4xkXDT4e0ma8biFPXPghpXh48fl5ksc4ZEDjBXViCtEu X-Received: by 2002:a05:6402:506:b0:419:46b2:2433 with SMTP id m6-20020a056402050600b0041946b22433mr9275412edv.21.1648368531827; Sun, 27 Mar 2022 01:08:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1648368531; cv=none; d=google.com; s=arc-20160816; b=pt0yg39NhnoaVGy5QVbLTyxlFlHxMeCDjKoHerhAjVUjRw9BCVIHd5j+lDGQ7hjIY9 NLilbPgSVlezz+TOxoZXYrlOy6cAspT/jmW2hN9v4yUFPBzZKkFsVu8EiH434PZVJw1L eBHkSvVOCNTDHUCfQNzUTvMSJCkRnCe51/aodAM4AQ+I8tymEWz1bGhfdZHWd//x0s6Q 5BPfIQ3rVqTk27+5nm/O24AG1WZ1wFwT2TLR7VzZ+RgJ9fjuEYC+Ajcvz2wPOfI98kbt ij3OclezCW/h2QwLdf/t6qzc7wem4ZV+pLXcbNh/lpnhnGZkODdA6sYjJRQdO98dr3J1 RErw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :mime-version:accept-language:in-reply-to:references:message-id:date :thread-index:thread-topic:subject:cc:to:from; bh=wQ/68Vd8OdiocK1Z/1Au7mWNiwdz1n9kMfM2Xpha5BE=; b=i3Y/1zx2wwBbb4a2jXrYha19Gvw/ONniwloPmDOTqZvs14j4oXMGI1TBWqMzjo4tmC bHvv0InfPtB+x6waIR8YScuKQZ50TibUHQqRgsC7cQyVUUxgLAgMIgEz80+BmLxT8QZC jV8WoPCjBeWRTqav6HFGNc8QjA9jTuca6w/cyL0HUvQ4ONoKGACwYXxAmacwPSl0Cw2c vKKecGCjwLn6cYxXQvuScD61HsCy1Vh8O2jQSw0h37tKSTrlL/xL2vBeJIaoSNzRbCrf HUoQOBExCOvXnIqw3f/lKJseoddC4D825b6P0X+oV2mK42Yuw/r7r0XdAZ9tkzvnDPjT MBfw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=aculab.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a19-20020a170906671300b006df76385e43si8378959ejp.739.2022.03.27.01.08.21; Sun, 27 Mar 2022 01:08:51 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=aculab.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233551AbiCZWSB convert rfc822-to-8bit (ORCPT + 99 others); Sat, 26 Mar 2022 18:18:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42942 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229752AbiCZWRr (ORCPT ); Sat, 26 Mar 2022 18:17:47 -0400 Received: from eu-smtp-delivery-151.mimecast.com (eu-smtp-delivery-151.mimecast.com [185.58.86.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 387C42F003 for ; Sat, 26 Mar 2022 15:16:09 -0700 (PDT) Received: from AcuMS.aculab.com (156.67.243.121 [156.67.243.121]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id uk-mta-221-RimintWePiOs3iKZRZSi5g-1; Sat, 26 Mar 2022 22:16:06 +0000 X-MC-Unique: RimintWePiOs3iKZRZSi5g-1 Received: from AcuMS.Aculab.com (fd9f:af1c:a25b:0:994c:f5c2:35d6:9b65) by AcuMS.aculab.com (fd9f:af1c:a25b:0:994c:f5c2:35d6:9b65) with Microsoft SMTP Server (TLS) id 15.0.1497.32; Sat, 26 Mar 2022 22:16:03 +0000 Received: from AcuMS.Aculab.com ([fe80::994c:f5c2:35d6:9b65]) by AcuMS.aculab.com ([fe80::994c:f5c2:35d6:9b65%12]) with mapi id 15.00.1497.033; Sat, 26 Mar 2022 22:16:03 +0000 From: David Laight To: 'Joerg Roedel' , "x86@kernel.org" CC: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "hpa@zytor.com" , Tom Lendacky , Brijesh Singh , Joerg Roedel , "linux-kernel@vger.kernel.org" , "stable@vger.kernel.org" Subject: RE: [PATCH v3] x86/sev: Unroll string mmio with CC_ATTR_GUEST_UNROLL_STRING_IO Thread-Topic: [PATCH v3] x86/sev: Unroll string mmio with CC_ATTR_GUEST_UNROLL_STRING_IO Thread-Index: AQHYQR+Ze4D1k2sivEGZy3Tw8cJv76zSOd1g Date: Sat, 26 Mar 2022 22:16:03 +0000 Message-ID: References: <20220326144127.15967-1-joro@8bytes.org> In-Reply-To: <20220326144127.15967-1-joro@8bytes.org> Accept-Language: en-GB, en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-transport-fromentityheader: Hosted x-originating-ip: [10.202.205.107] MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=C51A453 smtp.mailfrom=david.laight@aculab.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: aculab.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8BIT X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Joerg Roedel > Sent: 26 March 2022 14:41 > > The io specific memcpy/memset functions use string mmio accesses to do > their work. Under SEV the hypervisor can't emulate these instructions, > because they read/write directly from/to encrypted memory. > > KVM will inject a page fault exception into the guest when it is asked > to emulate string mmio instructions for an SEV guest: > > BUG: unable to handle page fault for address: ffffc90000065068 > #PF: supervisor read access in kernel mode > #PF: error_code(0x0000) - not-present page > PGD 8000100000067 P4D 8000100000067 PUD 80001000fb067 PMD 80001000fc067 PTE 80000000fed40173 > Oops: 0000 [#1] PREEMPT SMP NOPTI > CPU: 0 PID: 1 Comm: swapper/0 Not tainted 5.17.0-rc7 #3 > > As string mmio for an SEV guest can not be supported by the > hypervisor, unroll the instructions for CC_ATTR_GUEST_UNROLL_STRING_IO > enabled kernels. > > This issue appears when kernels are launched in recent libvirt-managed > SEV virtual machines, because libvirt started to add a tpm-crb device > to the guest by default. > > The kernel driver for tpm-crb uses memcpy_to/from_io() functions to > access MMIO memory, resulting in a page-fault injected by KVM and > crashing the kernel at boot. > > Cc: stable@vger.kernel.org #4.15+ > Fixes: d8aa7eea78a1 ('x86/mm: Add Secure Encrypted Virtualization (SEV) support') > Reviewed-by: Tom Lendacky > Signed-off-by: Joerg Roedel > --- > Changes v2->v3: > - Fix sparse warnings introduced by v2 > > arch/x86/lib/iomem.c | 65 ++++++++++++++++++++++++++++++++++++++------ > 1 file changed, 57 insertions(+), 8 deletions(-) > > diff --git a/arch/x86/lib/iomem.c b/arch/x86/lib/iomem.c > index df50451d94ef..3e2f33fc33de 100644 > --- a/arch/x86/lib/iomem.c > +++ b/arch/x86/lib/iomem.c > @@ -22,7 +22,7 @@ static __always_inline void rep_movs(void *to, const void *from, size_t n) > : "memory"); > } > > -void memcpy_fromio(void *to, const volatile void __iomem *from, size_t n) > +static void string_memcpy_fromio(void *to, const volatile void __iomem *from, size_t n) > { > if (unlikely(!n)) > return; > @@ -38,9 +38,8 @@ void memcpy_fromio(void *to, const volatile void __iomem *from, size_t n) > } > rep_movs(to, (const void *)from, n); > } > -EXPORT_SYMBOL(memcpy_fromio); > > -void memcpy_toio(volatile void __iomem *to, const void *from, size_t n) > +static void string_memcpy_toio(volatile void __iomem *to, const void *from, size_t n) > { > if (unlikely(!n)) > return; > @@ -56,14 +55,64 @@ void memcpy_toio(volatile void __iomem *to, const void *from, size_t n) > } > rep_movs((void *)to, (const void *) from, n); > } > + > +static void unrolled_memcpy_fromio(void *to, const volatile void __iomem *from, size_t n) > +{ > + const volatile char __iomem *in = from; > + char *out = to; > + int i; > + > + for (i = 0; i < n; ++i) > + out[i] = readb(&in[i]); > +} Wait a minute.... Aren't these functions supposed to be doing 'memory' copies? In which case they need to be using 64bit IO accesses where appropriate - otherwise the performance is horrid. I thought the x86 memcpy_to/from_io() had been changed to always use a software loop rather than using whatever memcpy() ended up using. In particular the 'rep movsb' ERMS (EMRS?) copy that is fast (on some cpu) for memory-memory copies is always a byte copy on uncached locations typical for io addresses. PIO reads from PCIe can be spectacularly slow. You really do want to use the largest register available. David - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)