Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp997422pxb; Tue, 9 Nov 2021 03:32:57 -0800 (PST) X-Google-Smtp-Source: ABdhPJziG07EUAHotHoo2ugFypyDFp4VnNtbtX6B1cy8eYLfc+2pTzgDZW/IjOVqFdkJHf9Do2Xq X-Received: by 2002:a05:6638:349e:: with SMTP id t30mr4960653jal.49.1636457577170; Tue, 09 Nov 2021 03:32:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1636457577; cv=none; d=google.com; s=arc-20160816; b=vwHTXoZZwsNlgYAF2GWSYWYmkDS2w9Nv33kV4SmaEmGMrMifFBa+pZnAQ7E5grc1Y4 wFR3V/xoZL0Rx/Z8/WguOm24UWzApD2uA95wy7q4g81sKNnWw21lcw/4ZXtqalYBxDNR sYO99L4HfpsqxHKJFLE37aS+giQ3CMOrQX5ypzKCzptLXTSxBUb4Nxd7Nhl+/Irkj+kH ras3fvPpLHQOoygdU+ek0bZd8pL5u8Ic1ibHE9AzH8ahzyNmrmFh63jODM2VaxbZVS+O uXR/rGJntdd/q+lpqzI9G33lcvKCGHXFfebraDD7lUfDfeqDj6jBSBE38cfsb35YBbpc ljrg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=gT3MUMtIZ0PS90yl0lE9QBzqwoRSMBq2QOt4vNKzTys=; b=j9RCbZEpiHqjXp1TrFDgvcnGYR0aeuwEmGcDkh+ulqMJRaQpWV9pdXvmygNlBwDCqj +iD7ahSaC7dxWdzwWMr7A+jt7+mc4wd3VRIRmnIaXU8ENexJqhmQBpoQEe9/fRhEL9Vh QHSMKOcfCmTir06NpLid7Ehk0bnVyHCsa2PC4BVND1SSypJVpJlqCYRNTBIdMy39z4px G2QidCimZPjc4zYOdMtqXW8RdikjxylnmlqAbuJrmqoQeWnuYFEHO4ZqbV35R7z15ESd Q7mWPBiZh8WPVRzi06kmPFs002cSy2aEueAlUScV5JJIEFzGbWeollj1bkjAHO4l1mZu d8Ng== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=b13sxWM9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p11si1264580jal.39.2021.11.09.03.32.44; Tue, 09 Nov 2021 03:32:57 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=b13sxWM9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239254AbhKIBlU (ORCPT + 99 others); Mon, 8 Nov 2021 20:41:20 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50642 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234461AbhKIBlO (ORCPT ); Mon, 8 Nov 2021 20:41:14 -0500 Received: from mail-pl1-x630.google.com (mail-pl1-x630.google.com [IPv6:2607:f8b0:4864:20::630]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ADD7BC04CAC4 for ; Mon, 8 Nov 2021 17:17:44 -0800 (PST) Received: by mail-pl1-x630.google.com with SMTP id y7so812678plp.0 for ; Mon, 08 Nov 2021 17:17:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=gT3MUMtIZ0PS90yl0lE9QBzqwoRSMBq2QOt4vNKzTys=; b=b13sxWM9K5iisnYPadl4iOffaoys9RijOi3mTsZncT79yh0EYcQ5zP36xW3B53gbfI hf38uXQxIRmk/GO8JXWSnV+gRSRIGZYtVYUdIsHAt6HO9GbiW9mBH/HQFPbdGVhmJzAE d+5xCaoOiMJhRE25WFHguzeVSIifxnExemfwDQtGBdPF4S9WTr8CvKXghy1m1SqKVnwm EERujOqfJ1YA7bmWxiB6FDxmsEbHoK9/PEVJSGjKx5CzfV6eZDPa/bWo0671IUu3X0m3 XIR3TxcculNhN3pd4DHjS7KL7tM8G3YWRmURA4LG43nJsj/84/En3IvJRGamYai7QoJh 2EpQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=gT3MUMtIZ0PS90yl0lE9QBzqwoRSMBq2QOt4vNKzTys=; b=MhURqb3P0uE3AF+k4Qds/1ra3U3el7Abf+tYGRU2RVwUdtxw2yBSCKvl2BiuyL4Giw eWxZtJAqZJ221oFsfqvHP65q/Mgn63f1kfk9P9WKV3TnOFTgtMSS/SqXk3vvc4NuY9BV 0vKw26xDugdMm+on5ghUdz+0ORErpXl7U+cd+H7OidXvNhq1OvkDLQhOV40qGa6HH1u3 e4aL5799uPHcV/AyqiVPS3nINwxQKNsiifTtbDc7wENpAVZl3Gyg/hRKfQ5JgBccwmdU N1CObjdGuQWirtPRmH9mmjzBUwnGsJAw9JR51HVHKejeu4o5yhfyLEOVKLP/ZCroIR3e yZ6g== X-Gm-Message-State: AOAM532sijwk59L3HaVtxOHieCJZ0ma5vYhJN/MwNLKiV+F5L5hyxIAA DplyrYw4OI9KCA3uaYr6Hs0Amg== X-Received: by 2002:a17:902:6acb:b0:142:76c3:d35f with SMTP id i11-20020a1709026acb00b0014276c3d35fmr3494468plt.89.1636420663931; Mon, 08 Nov 2021 17:17:43 -0800 (PST) Received: from google.com (157.214.185.35.bc.googleusercontent.com. [35.185.214.157]) by smtp.gmail.com with ESMTPSA id q13sm18256433pfj.26.2021.11.08.17.17.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Nov 2021 17:17:43 -0800 (PST) Date: Tue, 9 Nov 2021 01:17:39 +0000 From: Sean Christopherson To: "Maciej S. Szmigiero" Cc: James Morse , Alexandru Elisei , Suzuki K Poulose , Atish Patra , David Hildenbrand , Cornelia Huck , Claudio Imbrenda , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-mips@vger.kernel.org, kvm@vger.kernel.org, kvm-ppc@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Ben Gardon , Marc Zyngier , Huacai Chen , Aleksandar Markovic , Paul Mackerras , Anup Patel , Palmer Dabbelt , Paul Walmsley , Albert Ou , Christian Borntraeger , Janosch Frank , Paolo Bonzini Subject: Re: [PATCH v5.5 01/30] KVM: Ensure local memslot copies operate on up-to-date arch-specific data Message-ID: References: <20211104002531.1176691-1-seanjc@google.com> <20211104002531.1176691-2-seanjc@google.com> <6407c2d3-854b-edf6-9990-b54a5baedd0a@oracle.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <6407c2d3-854b-edf6-9990-b54a5baedd0a@oracle.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Nov 09, 2021, Maciej S. Szmigiero wrote: > On 04.11.2021 01:25, Sean Christopherson wrote: > > @@ -1597,6 +1596,26 @@ static int kvm_set_memslot(struct kvm *kvm, > > kvm_copy_memslots(slots, __kvm_memslots(kvm, as_id)); > > } > > + /* > > + * Make a full copy of the old memslot, the pointer will become stale > > + * when the memslots are re-sorted by update_memslots(), and the old > > + * memslot needs to be referenced after calling update_memslots(), e.g. > > + * to free its resources and for arch specific behavior. This needs to > > + * happen *after* (re)acquiring slots_arch_lock. > > + */ > > + slot = id_to_memslot(slots, new->id); > > + if (slot) { > > + old = *slot; > > + } else { > > + WARN_ON_ONCE(change != KVM_MR_CREATE); > > + memset(&old, 0, sizeof(old)); > > + old.id = new->id; > > + old.as_id = as_id; > > + } > > + > > + /* Copy the arch-specific data, again after (re)acquiring slots_arch_lock. */ > > + memcpy(&new->arch, &old.arch, sizeof(old.arch)); > > Had "new" been zero-initialized completely in __kvm_set_memory_region() > for safety (so it does not contain stack garbage - I don't mean just the > new.arch field in the "if (!old.npages)" branch in that function but the > whole struct) this line would be needed only in the "if (slot)" branch > above (as Ben said). > > Also, when patch 7 from this series removes this memcpy(), > kvm_arch_prepare_memory_region() does indeed receive this field > uninitialized - I know only x86 and ppcHV care > and kvm_alloc_memslot_metadata() or kvmppc_core_prepare_memory_region_hv() > then overwrites it unconditionally but it feels a bit wrong. > > I am almost certain that compiler would figure out to only actually > zero the fields that wouldn't be overwritten immediately anyway. > > But on the other hand, this patch is only a fix for code that's going > to be replaced anyway so perfection here probably isn't that important. Yeah, that about sums up my feelings about the existing code. That said, an individual memslot isn't _that_ big, and memslot updates without the scalable implementation are dreadfully slow anyways, so I'm leaning strongly toward your suggestion of zeroing all of new as part of this fix.