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=-2.2 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 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 C33B5C33CAC for ; Thu, 6 Feb 2020 16:28:22 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id A4BCD218AC for ; Thu, 6 Feb 2020 16:28:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727753AbgBFQ2T (ORCPT ); Thu, 6 Feb 2020 11:28:19 -0500 Received: from mga04.intel.com ([192.55.52.120]:30727 "EHLO mga04.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727358AbgBFQ2T (ORCPT ); Thu, 6 Feb 2020 11:28:19 -0500 X-Amp-Result: UNKNOWN X-Amp-Original-Verdict: FILE UNKNOWN X-Amp-File-Uploaded: False Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga104.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 06 Feb 2020 08:28:19 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.70,410,1574150400"; d="scan'208";a="344968003" Received: from sjchrist-coffee.jf.intel.com (HELO linux.intel.com) ([10.54.74.202]) by fmsmga001.fm.intel.com with ESMTP; 06 Feb 2020 08:28:18 -0800 Date: Thu, 6 Feb 2020 08:28:18 -0800 From: Sean Christopherson To: Peter Xu 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 =?iso-8859-1?Q?Mathieu-Daud=E9?= Subject: Re: [PATCH v5 12/19] KVM: Move memslot deletion to helper function Message-ID: <20200206162818.GD13067@linux.intel.com> References: <20200121223157.15263-1-sean.j.christopherson@intel.com> <20200121223157.15263-13-sean.j.christopherson@intel.com> <20200206161415.GA695333@xz-x1> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200206161415.GA695333@xz-x1> User-Agent: Mutt/1.5.24 (2015-08-30) Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org 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.