Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp220884rwe; Fri, 26 Aug 2022 03:59:55 -0700 (PDT) X-Google-Smtp-Source: AA6agR7stPKYKSQboPGoWm6i+KIpbwXBbtUHZRjgIxZl1Pq36+TWI94gEThhhYHGRyvIz4p+0YeR X-Received: by 2002:a05:6402:156:b0:440:b458:93df with SMTP id s22-20020a056402015600b00440b45893dfmr6558143edu.337.1661511595545; Fri, 26 Aug 2022 03:59:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661511595; cv=none; d=google.com; s=arc-20160816; b=geYYgirGvm4XY3e9EZ8fGTHmLjuBVK3EktIB+htW3EJWMNWEygJ08p3jcfc25f6gMD lbVZMK17FJ2e+D9RDF9yTEytOJqFdR5wGZhwNxA7htvN8Q1iH56d0KW656cRHyWp52N6 08l/JHCZ28TTNLqD0mxNegYe8K7La9yRFkdTwcHLxAJYdIdCgnG/q7MzKqJ6+lt16s+j rKuFoXQFYqgmuXT1fQO8ZUmUDHF4Vg/yQZ4tdhEi0Q+I1/M8uJ/OS4rKxMiYaiWBpWUk k3M/aAPq91Cn/NPf272XP9knZjMuGp79Kk1ZiJDNbHzCO0Mdz1QXR6U6nB0uiXb4sb9A 611g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:subject :from:references:cc:to:content-language:user-agent:mime-version:date :message-id:dkim-signature; bh=pLii0dfj1y7lFu87Lg22T36M6OnU1zUptup5Hy8q+CA=; b=Gd58auMQKggz9gGLDLOBg9XyIsZGpNt58CvKojxpnctCzxM8Ff/PlpCboUTbgxEuvo d2DPJGEklKqSu6kjTyOVXf2Nrk97VE2NcYmZ04aXXFY7xvaiiBgPHl+L1rOQpqipIQT3 pLNVlM7/4tcu4tj+DAWQIryYGOQMR0grsYwCnvxkWaPmC0cb7/dOn2afFuYb3QUG+cZ9 6FIyDjt6501LiKjh7nbTljsmEvRX8s40nxi+qfZ6pCNtyddYON6+ZqtgtXga2Wa1M8AH WT2FsIUAetTrBgeSfVx0z7dEc429AjP6iCR4yLhmeNUKMSOpLEwbXMukd63tIbQE/Vx1 5uJw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=OO2eonGX; 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=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ho7-20020a1709070e8700b0073156b6fe1csi1273262ejc.799.2022.08.26.03.59.29; Fri, 26 Aug 2022 03:59:55 -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=@redhat.com header.s=mimecast20190719 header.b=OO2eonGX; 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=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343948AbiHZKui (ORCPT + 99 others); Fri, 26 Aug 2022 06:50:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49420 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343999AbiHZKub (ORCPT ); Fri, 26 Aug 2022 06:50:31 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 21405201BA for ; Fri, 26 Aug 2022 03:50:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1661511029; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=pLii0dfj1y7lFu87Lg22T36M6OnU1zUptup5Hy8q+CA=; b=OO2eonGXI9WqD0QmWdZnmaxtBbAOekzMpYI+f2KtcvxrIDcXWsOoK27oQWiCmnWAoRHATe FOjQM5g5kGBXFCRv7aP+wrFE0FfLVsVfdnpefR2C8q2RYyZaqpiV6wB+waagVuEv7Ux8RJ Zb71dA6MZmzsniqX861DzKSsu4NZlNM= Received: from mail-ed1-f71.google.com (mail-ed1-f71.google.com [209.85.208.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-376-9P4Rg-nQOjum7rEWsBXLew-1; Fri, 26 Aug 2022 06:50:28 -0400 X-MC-Unique: 9P4Rg-nQOjum7rEWsBXLew-1 Received: by mail-ed1-f71.google.com with SMTP id f20-20020a05640214d400b004470930f180so901213edx.10 for ; Fri, 26 Aug 2022 03:50:27 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:subject:from:references:cc:to :content-language:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc; bh=pLii0dfj1y7lFu87Lg22T36M6OnU1zUptup5Hy8q+CA=; b=C92fm4XEDhdUwohZMOFne0S9duMkHin76ZL5PBXdq+MvYATKGMzNpexvrWj6w/RCPk xiPCSwvUkX/9UIjkXxXhyBNVP8enEqEjFfvx9CcdmCw9xdq652urhs1b9nJOneOYgUmy XNa5EBshvLLO+xsbJMiQkarztnMAbuyOzYucXMssK51Sn250sQw3gDLZWGYQ9iwxA1nC 1B6JG6/v1lEZRhPLY1hXvCt8Lm+syt/PLnYEml1GXRn2usOwqwF5fQBD5pZTDkDDk9Gb C+2PJMYxmseMqDAEUdBaiZm4KdgVtENeN5KqO3oXw9eqvhEpoWforkX+W7pkPwAwKR/d GZSA== X-Gm-Message-State: ACgBeo2CxdUtbg0833w1/xYjPM0s/WcO0Gz4Q4pEWD65EvLyYsKFfVfI hofRA67YwpY+g/SYDjd+JxQ4i8gqxUD3lc9JMZlz4NzolMldVVis04xZHUG7wH1yZqDGWsvckPE oF9mQ+D7R8uH4LgLWTokOJULw X-Received: by 2002:a17:907:84a:b0:733:735:2b1a with SMTP id ww10-20020a170907084a00b0073307352b1amr5021055ejb.290.1661511026933; Fri, 26 Aug 2022 03:50:26 -0700 (PDT) X-Received: by 2002:a17:907:84a:b0:733:735:2b1a with SMTP id ww10-20020a170907084a00b0073307352b1amr5021032ejb.290.1661511026688; Fri, 26 Aug 2022 03:50:26 -0700 (PDT) Received: from ?IPV6:2001:b07:6468:f312:9af8:e5f5:7516:fa89? ([2001:b07:6468:f312:9af8:e5f5:7516:fa89]) by smtp.googlemail.com with ESMTPSA id 1-20020a170906218100b0072af4af2f46sm750652eju.74.2022.08.26.03.50.25 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 26 Aug 2022 03:50:26 -0700 (PDT) Message-ID: <9e7cb09c-82c5-9492-bccd-5511f5bede26@redhat.com> Date: Fri, 26 Aug 2022 12:50:24 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.12.0 Content-Language: en-US To: Marc Zyngier , 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, 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 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> From: Paolo Bonzini Subject: Re: [PATCH v1 1/5] KVM: arm64: Enable ring-based dirty memory tracking In-Reply-To: <878rnewpaw.wl-maz@kernel.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_NONE,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 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. 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 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). Paolo