Received: by 2002:a05:6358:5282:b0:b5:90e7:25cb with SMTP id g2csp4100072rwa; Tue, 23 Aug 2022 16:25:45 -0700 (PDT) X-Google-Smtp-Source: AA6agR4e1iZWGXK6pERG/mcBiO3KhFtpPvNfPkxR2fRQlNtknaSKs8YuCdag+qGXQfvCxOC5SeWf X-Received: by 2002:a05:6a00:1d23:b0:536:d3cb:2547 with SMTP id a35-20020a056a001d2300b00536d3cb2547mr8064340pfx.30.1661297145310; Tue, 23 Aug 2022 16:25:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661297145; cv=none; d=google.com; s=arc-20160816; b=0+6S2mCYX333q7EqsxIylvSWIL5ck6TZCf7+NxigZTPUSFay0T5JfC/QXDM//qmEW5 o7ZO42cV6oPuCdwaoEX3h6+6sB+BymNmzdMMSwzHlYztVNOgAUVGMJCqSczy4AlkTNoj fMUSoKd1JwryMdQmyzOrWFhMseJIgQDrB8Q2goba0WB0dr+YO9llOjTIweoCzV84gRps XfyEgiSFBJa3mGc73kTlVfQ7zU1pWkmKdJes+4yU/Tqsy6309ZeFISv8vLGfc/pPnK0i oXy5rSYZEYP7osjQTryBUK6/xSl3zUVenQJpy6l2sKSTKDusFhNOJkzliTAv91MCq9PT h8ug== 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=XaBtJzlTQqybE1Ns4xBoCI2pGWTDMyoObAAaA1i9vVs=; b=bLeBN95+nwwZkej3+VfmWmBdg6DdsGk55CXiHCnhtpfEvtaNDSfrF8CQa/8vV57DBe pOGLN2KvvpLsMwIpjTIeWsb1iypOMixfYHQqnD3NX5+xBueul0+y5EgJwmBuNIPlVspW 8qyatHRzqj0l2igyYrk8dZ/jk4Ag4XOvclBJZ243IR+mu9EqFXGjtcR0srjz9YZdxvU8 S8sVpUXitcgvpS90HqePNr6CwzMnbyb9A1Zj1GkkZ+dWNT1ZlTC4b3O/L/epkVqGvKoy jKL133J4Rr4CZZrnf1fGF10nfspetiVxrpEEq4WzQAL4IGq8XrCqF1khhwQp6ik5gd8Z FUcg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=PvKWuYH7; 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 b2-20020a056a000a8200b0052f7fb6f2desi18727692pfl.154.2022.08.23.16.25.34; Tue, 23 Aug 2022 16:25:45 -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=PvKWuYH7; 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 S231638AbiHWWrK (ORCPT + 99 others); Tue, 23 Aug 2022 18:47:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44386 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229825AbiHWWrH (ORCPT ); Tue, 23 Aug 2022 18:47:07 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C5E8C883F4; Tue, 23 Aug 2022 15:47:06 -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 dfw.source.kernel.org (Postfix) with ESMTPS id 5F02E61701; Tue, 23 Aug 2022 22:47:06 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id AFC92C433D6; Tue, 23 Aug 2022 22:47:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1661294825; bh=mIdkXaJU12XPc/v1eI1SmEBn2eXK40Tc2GKZ2bh56IQ=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=PvKWuYH7LzURRWG2uBfTZoqdR2VuQQqVwYk91ZT4xs+pNAX8p0ZPGebqwKGcGvWsw GoUsisFbfXbphw/ArYdEGhpuApqBSfKBE1hCpdK0MtV5idNvsmpHu4izogGKT4agwJ didDFhjjvxPLmLCTKMkhCG7QeQTsf7RLoFgcC6e5tFw4gcSQJDZ6CVaAGQOa6SApau zFP5jzetWE1fQW5n6Ts6y7cE8gcK3rMH6LJsw1t4kBC1LzIOyRQ1weWVya8eBlE6j8 sGSESGojwA+XkGsawcc45JFYMZnl9aP7YRxwddrD68qtzwFb8Eq2/hEwVZR9SjbKfY nxiP9lu/ioTug== 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 1oQcfn-005JQ9-Di; Tue, 23 Aug 2022 23:47:03 +0100 Date: Tue, 23 Aug 2022 23:47:03 +0100 Message-ID: <878rnewpaw.wl-maz@kernel.org> From: Marc Zyngier To: Peter Xu Cc: 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, pbonzini@redhat.com, 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: 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> 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: 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, pbonzini@redhat.com, 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 Tue, 23 Aug 2022 22:20:32 +0100, Peter Xu wrote: > > On Tue, Aug 23, 2022 at 08:17:03PM +0100, Marc Zyngier wrote: > > I don't think we really need this check on the hot path. All we need > > is to make the request sticky until userspace gets their act together > > and consumes elements in the ring. Something like: > > > > diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c > > index 986cee6fbc7f..e8ed5e1af159 100644 > > --- a/arch/arm64/kvm/arm.c > > +++ b/arch/arm64/kvm/arm.c > > @@ -747,6 +747,14 @@ static int check_vcpu_requests(struct kvm_vcpu *vcpu) > > > > if (kvm_check_request(KVM_REQ_SUSPEND, vcpu)) > > return kvm_vcpu_suspend(vcpu); > > + > > + if (kvm_check_request(KVM_REQ_RING_SOFT_FULL, vcpu) && > > + kvm_dirty_ring_soft_full(vcpu)) { > > + kvm_make_request(KVM_REQ_RING_SOFT_FULL, vcpu); > > + vcpu->run->exit_reason = KVM_EXIT_DIRTY_RING_FULL; > > + trace_kvm_dirty_ring_exit(vcpu); > > + return 0; > > + } > > } > > > > return 1; > > Right, this seems working. We can also use kvm_test_request() here. > > > > > > > However, I'm a bit concerned by the reset side of things. It iterates > > over the vcpus and expects the view of each ring to be consistent, > > even if userspace is hacking at it from another CPU. For example, I > > can't see what guarantees that the kernel observes the writes from > > userspace in the order they are being performed (the documentation > > provides no requirements other than "it must collect the dirty GFNs in > > sequence", which doesn't mean much from an ordering perspective). > > > > I can see that working on a strongly ordered architecture, but on > > something as relaxed as ARM, the CPUs may^Wwill aggressively reorder > > stuff that isn't explicitly ordered. I have the feeling that a CAS > > operation on both sides would be enough, but someone who actually > > understands how this works should have a look... > > 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. > IIRC we used to discuss similar questions on "what if the user is hostile > and wants to hack the process by messing up with the ring", and our > conclusion was as long as the process wouldn't mess up anything outside > itself it should be okay. E.g. It should not be able to either cause the > host to misfunction, or trigger kernel warnings in dmesg, etc.. I'm not even discussing safety here. I'm purely discussing the interactions between userspace and kernel based on the documentation and the existing kernel code. Thanks, M. -- Without deviation from the norm, progress is not possible.