Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp519281rwe; Fri, 26 Aug 2022 09:07:44 -0700 (PDT) X-Google-Smtp-Source: AA6agR47luIPvwWwVTnGIGbqjVauoJJ7Y2PyDGudR7mrQPaC20JMQ8Ev78bIZ8TLojFl1ypqnJro X-Received: by 2002:a05:6a00:1ac7:b0:52f:4e43:5ace with SMTP id f7-20020a056a001ac700b0052f4e435acemr4547697pfv.59.1661530064201; Fri, 26 Aug 2022 09:07:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661530064; cv=none; d=google.com; s=arc-20160816; b=0NsDIabI/gcGgfpwT5HQt4hFUZoydZYYX95dCPKZcVPar25Un7oAnj+nWC/1HmYcvO 5S2hJ5sxVkGj5M7/JhpSNaOQ/ToyQeJPWvm0vjoTBXBE1+d5hBVwk42Q4rGN95/+KzMr oB2pBgdYsFzWzjmQhIZnnBr7OkiKo/ns29GyWDggfgc4CPDE8l6OO7EAky76oID7QFNq xM3SpInKhOvY0RdH0hvHt9Sqaq0ch3Yvez4hazvUQVoubhfoRJHHDHWt7iJKCUVVZGwu 9nAi8bNiqgbZiIA7B6ihp4huD2vT+hLMoidEBWNTkXlej1QcF+nkZclTwNaFABrODC+S da1g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:user-agent:references:in-reply-to :subject:cc:to:from:message-id:date:dkim-signature; bh=Eogfb/e2tcuDLhkGLY+ZZRf0mW2sCZOXf9T4cN/zcEc=; b=0xpoqiT3HyEnmOtskIQDcDUeMpuilZUFty9QKGACbOMHJLBD10rPk8PeYQS9XJUNdR UrmgrTtyDDA033AiakkZWWmLjwMumBx/Q/CDnD4YCoCozZqrrcSgUsDZznxzc9z+15UN 6MJAH0rfJkH8A6Gdj6KEc5bzodI+EMGQWWG2uZ5pOukDNFR+1A8MT2s773d4VXVCGKdt biMJrMosPDfyZhPE0hbSwrQcxzi2xGfwknG8MGlCnRd2PowVN7WZ4WEBPkZlHRPU4/2c 6a5O/KhKomUmQpVAzjYkYslWYEqUwSN+c10LTG6sEWScl0HTzxAiZccTOX/wfsFBAUSM BO3A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=XyGtAHQC; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x62-20020a638641000000b0041c9a3da63esi1811480pgd.686.2022.08.26.09.07.31; Fri, 26 Aug 2022 09:07:44 -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; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=XyGtAHQC; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344710AbiHZPuU (ORCPT + 99 others); Fri, 26 Aug 2022 11:50:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57030 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344732AbiHZPtw (ORCPT ); Fri, 26 Aug 2022 11:49:52 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 19F7260E3; Fri, 26 Aug 2022 08:49:47 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id B19FEB831BF; Fri, 26 Aug 2022 15:49:45 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5FA0AC433D6; Fri, 26 Aug 2022 15:49:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1661528984; bh=K54SpPn1dkF1YSkCv3pZX5DMshbcliLagDP1w7JkHK8=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=XyGtAHQC8bDQuh5z3roeuf3F7oOT/o24TT23/VB0gxzu/pRzuOmh7+qQIrWBSiYxX +SIitVbdoYfIY2BQgQwcAGyXLjkqcYnDRsh24xpK5O2FuH5dbgYZ0Jyr2YPTWu3Sj9 B/PjOu5a0atbxONek8h/twl3bXND9jqNDTZmChRmPscKr3KwBiDyjvgf0dIAwxZBkU xA0G/XSK7CrIGEMFg0RobNRKgCJNxfikx8UWoZevnrrdT8+2IFDGIsrhtq/yuhRWuH +aJqN0S38A6pFafmjSqOO0plKjcIyGKVTyP51CRHkix/OOEWXrvSNjaExHGVOoMIMn Tx3NWbRclwQuQ== Received: from sofa.misterjones.org ([185.219.108.64] helo=why.misterjones.org) by disco-boy.misterjones.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1oRbaY-005zap-Bd; Fri, 26 Aug 2022 16:49:42 +0100 Date: Fri, 26 Aug 2022 16:49:41 +0100 Message-ID: <8735djvwbu.wl-maz@kernel.org> From: Marc Zyngier To: Paolo Bonzini Cc: Peter Xu , Gavin Shan , kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, corbet@lwn.net, james.morse@arm.com, alexandru.elisei@arm.com, suzuki.poulose@arm.com, oliver.upton@linux.dev, catalin.marinas@arm.com, will@kernel.org, shuah@kernel.org, seanjc@google.com, dmatlack@google.com, bgardon@google.com, ricarkol@google.com, zhenyzha@redhat.com, shan.gavin@gmail.com Subject: Re: [PATCH v1 1/5] KVM: arm64: Enable ring-based dirty memory tracking In-Reply-To: <9e7cb09c-82c5-9492-bccd-5511f5bede26@redhat.com> References: <20220819005601.198436-1-gshan@redhat.com> <20220819005601.198436-2-gshan@redhat.com> <87lerkwtm5.wl-maz@kernel.org> <41fb5a1f-29a9-e6bb-9fab-4c83a2a8fce5@redhat.com> <87fshovtu0.wl-maz@kernel.org> <171d0159-4698-354b-8b2f-49d920d03b1b@redhat.com> <87bksawz0w.wl-maz@kernel.org> <878rnewpaw.wl-maz@kernel.org> <9e7cb09c-82c5-9492-bccd-5511f5bede26@redhat.com> User-Agent: Wanderlust/2.15.9 (Almost Unreal) SEMI-EPG/1.14.7 (Harue) FLIM-LB/1.14.9 (=?UTF-8?B?R29qxY0=?=) APEL-LB/10.8 EasyPG/1.0.0 Emacs/27.1 (x86_64-pc-linux-gnu) MULE/6.0 (HANACHIRUSATO) MIME-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") Content-Type: text/plain; charset=US-ASCII X-SA-Exim-Connect-IP: 185.219.108.64 X-SA-Exim-Rcpt-To: pbonzini@redhat.com, peterx@redhat.com, gshan@redhat.com, kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, corbet@lwn.net, james.morse@arm.com, alexandru.elisei@arm.com, suzuki.poulose@arm.com, oliver.upton@linux.dev, catalin.marinas@arm.com, will@kernel.org, shuah@kernel.org, seanjc@google.com, dmatlack@google.com, bgardon@google.com, ricarkol@google.com, zhenyzha@redhat.com, shan.gavin@gmail.com X-SA-Exim-Mail-From: maz@kernel.org X-SA-Exim-Scanned: No (on disco-boy.misterjones.org); SAEximRunCond expanded to false X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, 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 On Fri, 26 Aug 2022 11:50:24 +0100, Paolo Bonzini wrote: > > On 8/24/22 00:47, Marc Zyngier wrote: > >> I definitely don't think I 100% understand all the ordering things since > >> they're complicated.. but my understanding is that the reset procedure > >> didn't need memory barrier (unlike pushing, where we have explicit wmb), > >> because we assumed the userapp is not hostile so logically it should only > >> modify the flags which is a 32bit field, assuming atomicity guaranteed. > > Atomicity doesn't guarantee ordering, unfortunately. Take the > > following example: CPU0 is changing a bunch of flags for GFNs A, B, C, > > D that exist in the ring in that order, and CPU1 performs an ioctl to > > reset the page state. > > > > CPU0: > > write_flag(A, KVM_DIRTY_GFN_F_RESET) > > write_flag(B, KVM_DIRTY_GFN_F_RESET) > > write_flag(C, KVM_DIRTY_GFN_F_RESET) > > write_flag(D, KVM_DIRTY_GFN_F_RESET) > > [...] > > > > CPU1: > > ioctl(KVM_RESET_DIRTY_RINGS) > > > > Since CPU0 writes do not have any ordering, CPU1 can observe the > > writes in a sequence that have nothing to do with program order, and > > could for example observe that GFN A and D have been reset, but not B > > and C. This in turn breaks the logic in the reset code (B, C, and D > > don't get reset), despite userspace having followed the spec to the > > letter. If each was a store-release (which is the case on x86), it > > wouldn't be a problem, but nothing calls it in the documentation. > > > > Maybe that's not a big deal if it is expected that each CPU will issue > > a KVM_RESET_DIRTY_RINGS itself, ensuring that it observe its own > > writes. But expecting this to work across CPUs without any barrier is > > wishful thinking. > > Agreed, but that's a problem for userspace to solve. If userspace > wants to reset the fields in different CPUs, it has to synchronize > with its own invoking of the ioctl. userspace has no choice. It cannot order on its own the reads that the kernel will do to *other* rings. > That is, CPU0 must ensure that a ioctl(KVM_RESET_DIRTY_RINGS) is done > after (in the memory-ordering sense) its last write_flag(D, > KVM_DIRTY_GFN_F_RESET). If there's no such ordering, there's no > guarantee that the write_flag will have any effect. The problem isn't on CPU0 The problem is that CPU1 does observe inconsistent data on arm64, and I don't think this difference in behaviour is acceptable. Nothing documents this, and there is a baked in assumption that there is a strong ordering between writes as well as between writes and read. > The main reason why I preferred a global KVM_RESET_DIRTY_RINGS ioctl > was because it takes kvm->slots_lock so the execution would be > serialized anyway. Turning slots_lock into an rwsem would be even > worse because it also takes kvm->mmu_lock (since slots_lock is a > mutex, at least two concurrent invocations won't clash with each other > on the mmu_lock). Whatever the reason, the behaviour should be identical on all architectures. As is is, it only really works on x86, and I contend this is a bug that needs fixing. Thankfully, this can be done at zero cost for x86, and at that of a set of load-acquires on other architectures. M. -- Without deviation from the norm, progress is not possible.