From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-0.9 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 621E6C33CAC for ; Thu, 6 Feb 2020 16:51:24 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 2A7B121775 for ; Thu, 6 Feb 2020 16:51:24 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="g08vIIU2" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727657AbgBFQvX (ORCPT ); Thu, 6 Feb 2020 11:51:23 -0500 Received: from us-smtp-1.mimecast.com ([205.139.110.61]:34295 "EHLO us-smtp-delivery-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727060AbgBFQvX (ORCPT ); Thu, 6 Feb 2020 11:51:23 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1581007882; 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: in-reply-to:in-reply-to:references:references; bh=6wEZC2xS1ICrNAGJ+8QI1N9zsFW9IzwC/0IJCgjt8nk=; b=g08vIIU2kX/ellymYRFuPRjDhweq9gwR5t8aF7Wi8t+tZo0k8UjZi/6wihOHEhNYyTOjAL mM39hGJzIWx0vpo9L0Z83RsmEMWURIfTooTkfnsvkQupjCHT1nU9mJG3kJEhYZ6p+rqqUx TiPlgCkmjtJJYPE6WEz/9F4o5oerYPA= Received: from mail-qt1-f197.google.com (mail-qt1-f197.google.com [209.85.160.197]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-308-FH7wwPA6NUOA3UrDEcUIYA-1; Thu, 06 Feb 2020 11:51:21 -0500 X-MC-Unique: FH7wwPA6NUOA3UrDEcUIYA-1 Received: by mail-qt1-f197.google.com with SMTP id n4so4255401qtv.5 for ; Thu, 06 Feb 2020 08:51:21 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=6wEZC2xS1ICrNAGJ+8QI1N9zsFW9IzwC/0IJCgjt8nk=; b=LSJz/gRh3BdnXO1Z5uFMaddTtdb0pvwecrhf8DT0bGxsuQ9HMJDMVuLILzFGz7VAAB yUShHxH3KxRAO2MDXBfPnZ1nFsvwPYCEFZiRZ5CQjpRbhdUC7SG7AALFUxg5WPArHDv5 fgkbfPg2Z8EeFHL5DlOqUYCeVKrNmz2UYEER83GI8YDXnl42FrtThWECSWU0gF6vpG0E dUCPM00HNIGPWobJ/q+B8G/Iu/8SpoigEnvBK42IADNAdXtzikrgt3aj7KFinb2atj6a F/SmrmWoIix6yEtgBmtArgE4/LZ/RxLY6XgfR/g21UHjP0zQsO3smqEhSdEOzmVFJTpc ghIA== X-Gm-Message-State: APjAAAXvXvKGdPn/LeZKQOYDQq4LXgO5rGhv3Ns2QkyVxdaZMHa+Rju7 BImIjCp8s9k75Vt6R+XhUtLGtLxtxKbjXbziye/4d7+M9DhiOEkktReV4EMGCv/SXztH3vpZRWi gm62+1Y3p1IB4Ik8TPwzmBg== X-Received: by 2002:a0c:ed22:: with SMTP id u2mr3142537qvq.13.1581007880666; Thu, 06 Feb 2020 08:51:20 -0800 (PST) X-Google-Smtp-Source: APXvYqx/jX2RKvHGf3JDTGGU+D3o2LP4p21Bk3Kl7+mbxJpFhXmYhA374b5M6RMOGMp7RFtjxv6e0g== X-Received: by 2002:a0c:ed22:: with SMTP id u2mr3142516qvq.13.1581007880314; Thu, 06 Feb 2020 08:51:20 -0800 (PST) Received: from xz-x1 ([2607:9880:19c8:32::2]) by smtp.gmail.com with ESMTPSA id v10sm1058913qtj.26.2020.02.06.08.51.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Feb 2020 08:51:19 -0800 (PST) Date: Thu, 6 Feb 2020 11:51:16 -0500 From: Peter Xu To: Sean Christopherson Cc: Paolo Bonzini , Paul Mackerras , Christian Borntraeger , Janosch Frank , David Hildenbrand , Cornelia Huck , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Marc Zyngier , James Morse , Julien Thierry , Suzuki K Poulose , linux-mips@vger.kernel.org, kvm@vger.kernel.org, kvm-ppc@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org, Christoffer Dall , Philippe =?utf-8?Q?Mathieu-Daud=C3=A9?= Subject: Re: [PATCH v5 12/19] KVM: Move memslot deletion to helper function Message-ID: <20200206165116.GE695333@xz-x1> References: <20200121223157.15263-1-sean.j.christopherson@intel.com> <20200121223157.15263-13-sean.j.christopherson@intel.com> <20200206161415.GA695333@xz-x1> <20200206162818.GD13067@linux.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20200206162818.GD13067@linux.intel.com> Sender: linux-mips-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-mips@vger.kernel.org On Thu, Feb 06, 2020 at 08:28:18AM -0800, Sean Christopherson wrote: > On Thu, Feb 06, 2020 at 11:14:15AM -0500, Peter Xu wrote: > > On Tue, Jan 21, 2020 at 02:31:50PM -0800, Sean Christopherson wrote: > > > Move memslot deletion into its own routine so that the success path for > > > other memslot updates does not need to use kvm_free_memslot(), i.e. can > > > explicitly destroy the dirty bitmap when necessary. This paves the way > > > for dropping @dont from kvm_free_memslot(), i.e. all callers now pass > > > NULL for @dont. > > > > > > Add a comment above the code to make a copy of the existing memslot > > > prior to deletion, it is not at all obvious that the pointer will become > > > stale during sorting and/or installation of new memslots. > > > > Could you help explain a bit on this explicit comment? I can follow > > up with the patch itself which looks all correct to me, but I failed > > to catch what this extra comment wants to emphasize... > > It's tempting to write the code like this (I know, because I did it): > > if (!mem->memory_size) > return kvm_delete_memslot(kvm, mem, slot, as_id); > > new = *slot; > > Where @slot is a pointer to the memslot to be deleted. At first, second, > and third glances, this seems perfectly sane. > > The issue is that slot was pulled from struct kvm_memslots.memslots, e.g. > > slot = &slots->memslots[index]; > > Note that slots->memslots holds actual "struct kvm_memory_slot" objects, > not pointers to slots. When update_memslots() sorts the slots, it swaps > the actual slot objects, not pointers. I.e. after update_memslots(), even > though @slot points at the same address, it's could be pointing at a > different slot. As a result kvm_free_memslot() in kvm_delete_memslot() > will free the dirty page info and arch-specific points for some random > slot, not the intended slot, and will set npages=0 for that random slot. Ah I see, thanks. Another alternative is we move the "old = *slot" copy into kvm_delete_memslot(), which could be even clearer imo. However I'm not sure whether it's a good idea to drop the test-by for this. Considering that comment change should not affect it, would you mind enrich the comment into something like this (or anything better)? /* * Make a full copy of the old memslot, the pointer will become stale * when the memslots are re-sorted by update_memslots() in * kvm_delete_memslot(), while to make the kvm_free_memslot() work as * expected later on, we still need the cached memory slot. */ In all cases: Reviewed-by: Peter Xu -- Peter Xu