Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp1617708iog; Thu, 16 Jun 2022 10:00:12 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vkojO6fD/XyFPuw7AvL+PzfDw2I3IcIHeKQ+y8lAUlgJ4SWNBoUW48x4zRn/DgtQjLRqgb X-Received: by 2002:a17:906:77de:b0:712:1130:eb7b with SMTP id m30-20020a17090677de00b007121130eb7bmr5265996ejn.106.1655398812724; Thu, 16 Jun 2022 10:00:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655398812; cv=none; d=google.com; s=arc-20160816; b=UVIzOErLeF/cQldf6uzjpiu+5nSb2WVnoPlglfjlqY+zMFXTLg6BOyx9BjdYDrCyd0 uTU6XiBOkoFnlvY7QAMT4i/9Dt26cwecHMWuDJMf/JZDdcKXJnuHExgPKZjqT+4EZjGC QJYoytY/Um8vdeF3EK5iJrUHZIqvTAW/03yQ/+JNfUGIpHWzwZ/E6R+Aaa+ZxJ4BmLfW WZNjo9cnsePVzFcQuBpjpcp2pLzSxNEeEUNR4XKBduKJtROy8lonpCFLV6RdP2oRFSSB 4jdKb2QDd0sqUGpy1sMkdzjOszPcrDlZIm0UPw/6PF3toRsmjPeBd8hmGvGIAgDO35F6 GKSQ== 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=vTK8hDpQxXCXPLmxeqltDJov0V8ilwrPtQDdhINn/+M=; b=N4JAntnTUL1iAR9EffRVeFYpa0/FJJX35WS98fKKhlxkpGVqSi1DHuplV327aYHFL8 jqEhMn1+CPGOP6HzDYZkdJSwRCskt7kQE0FyaPcNJJxoEiQx0zhXN9kGSZJlLrzLnLiC MR6VNCWleBZd0yPI4vbODbATh+1FD9xBvhiuAyHV3ORBCb7KauGAkrr2yGAH0ZQPELV5 +aSYMJmoffBMFYnSBTnWB6Xt1AYPrIQhXUGPkYmXnyxRnGzRcvVYOrmlLCQVEE+QmuK5 famaQOhd7P2KCRhlCndL8xYXDXwcmIhfAOcQdd2tVxDe+pFZoUm2Vu0faxM+SYkDeqIo M0fg== 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 h21-20020a170906111500b006f39e0cbe8esi1916773eja.227.2022.06.16.09.59.46; Thu, 16 Jun 2022 10:00:12 -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 S235097AbiFPQtA convert rfc822-to-8bit (ORCPT + 99 others); Thu, 16 Jun 2022 12:49:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48532 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1378444AbiFPQs3 (ORCPT ); Thu, 16 Jun 2022 12:48:29 -0400 Received: from eu-smtp-delivery-151.mimecast.com (eu-smtp-delivery-151.mimecast.com [185.58.85.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 49FB337020 for ; Thu, 16 Jun 2022 09:48:08 -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-90-KsoYMlcUM_yKluVn1AzPOw-1; Thu, 16 Jun 2022 17:48:06 +0100 X-MC-Unique: KsoYMlcUM_yKluVn1AzPOw-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.36; Thu, 16 Jun 2022 17:48:04 +0100 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.036; Thu, 16 Jun 2022 17:48:04 +0100 From: David Laight To: 'Andrew Jones' CC: Raghavendra Rao Ananta , Marc Zyngier , James Morse , Alexandru Elisei , Suzuki K Poulose , "Paolo Bonzini" , Catalin Marinas , Will Deacon , Peter Shier , "Ricardo Koller" , Oliver Upton , "Reiji Watanabe" , Jing Zhang , "Colton Lewis" , "linux-arm-kernel@lists.infradead.org" , "kvmarm@lists.cs.columbia.edu" , "linux-kernel@vger.kernel.org" , "kvm@vger.kernel.org" , "linux-kselftest@vger.kernel.org" Subject: RE: [PATCH] selftests: KVM: Handle compiler optimizations in ucall Thread-Topic: [PATCH] selftests: KVM: Handle compiler optimizations in ucall Thread-Index: AQHYgXkNkdHi2edO0UOJoWwOrv/ni61SMGPg///3a4CAABX/cA== Date: Thu, 16 Jun 2022 16:48:04 +0000 Message-ID: <3e73cb07968d4c92b797781b037c2d45@AcuMS.aculab.com> References: <20220615185706.1099208-1-rananta@google.com> <20220616120232.ctkekviusrozqpru@gator> <33ca91aeb5254831a88e187ff8d9a2c2@AcuMS.aculab.com> <20220616162557.55bopzfa6glusuh5@gator> In-Reply-To: <20220616162557.55bopzfa6glusuh5@gator> 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=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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: Andrew Jones > Sent: 16 June 2022 17:26 > > On Thu, Jun 16, 2022 at 03:58:52PM +0000, David Laight wrote: > > From: Andrew Jones > > > Sent: 16 June 2022 13:03 > > > > > > On Wed, Jun 15, 2022 at 06:57:06PM +0000, Raghavendra Rao Ananta wrote: > > > > The selftests, when built with newer versions of clang, is found > > > > to have over optimized guests' ucall() function, and eliminating > > > > the stores for uc.cmd (perhaps due to no immediate readers). This > > > > resulted in the userspace side always reading a value of '0', and > > > > causing multiple test failures. > > > > > > > > As a result, prevent the compiler from optimizing the stores in > > > > ucall() with WRITE_ONCE(). > > > > > > > > Suggested-by: Ricardo Koller > > > > Suggested-by: Reiji Watanabe > > > > Signed-off-by: Raghavendra Rao Ananta > > > > --- > > > > tools/testing/selftests/kvm/lib/aarch64/ucall.c | 9 ++++----- > > > > 1 file changed, 4 insertions(+), 5 deletions(-) > > > > > > > > diff --git a/tools/testing/selftests/kvm/lib/aarch64/ucall.c > > > b/tools/testing/selftests/kvm/lib/aarch64/ucall.c > > > > index e0b0164e9af8..be1d9728c4ce 100644 > > > > --- a/tools/testing/selftests/kvm/lib/aarch64/ucall.c > > > > +++ b/tools/testing/selftests/kvm/lib/aarch64/ucall.c > > > > @@ -73,20 +73,19 @@ void ucall_uninit(struct kvm_vm *vm) > > > > > > > > void ucall(uint64_t cmd, int nargs, ...) > > > > { > > > > - struct ucall uc = { > > > > - .cmd = cmd, > > > > - }; > > > > + struct ucall uc = {}; > > > > va_list va; > > > > int i; > > > > > > > > + WRITE_ONCE(uc.cmd, cmd); > > > > nargs = nargs <= UCALL_MAX_ARGS ? nargs : UCALL_MAX_ARGS; > > > > > > > > va_start(va, nargs); > > > > for (i = 0; i < nargs; ++i) > > > > - uc.args[i] = va_arg(va, uint64_t); > > > > + WRITE_ONCE(uc.args[i], va_arg(va, uint64_t)); > > > > va_end(va); > > > > > > > > - *ucall_exit_mmio_addr = (vm_vaddr_t)&uc; > > > > + WRITE_ONCE(*ucall_exit_mmio_addr, (vm_vaddr_t)&uc); > > > > } > > > > Am I misreading things again? > > That function looks like it writes the address of an on-stack > > item into global data. > > The write to the address that the global points at causes a switch > from guest to host context. The guest's stack remains intact while > executing host code and the host can access the uc stack variable > directly by its address. Take a look at lib/aarch64/ucall.c to see > all the details. No wonder I was confused. It's not surprising the compiler optimises it all away. It doesn't seem right to be 'abusing' WRITE_ONCE() here. Just adding barrier() should be enough and much more descriptive. David - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)