Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp3716902pxj; Tue, 11 May 2021 10:19:21 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx4ZZPm+RnSv27zLJwDEf6iTRMJASS/t0wwl06NCXOmXuSFnaJfIBtTlgdGhw4jpC+6orxS X-Received: by 2002:a05:6808:8ee:: with SMTP id d14mr4380105oic.18.1620753561399; Tue, 11 May 2021 10:19:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620753561; cv=none; d=google.com; s=arc-20160816; b=jcEVDwcUEV3B78eW3KrKlAQFkcFiNjWnFEEHFyYsPdye1VOiU5bkvSZNcqz0FMDqia BUMNTwz4a71aaqDbCu+sE7Kn5RHYVCUdXgxDsSzP7n+gNJ8iU2oXdjjyvqBd6cIfZ6Ts YLwWZzdXln3cR54NZ/0a7NShO13k2/gBLG8zhECjwN9OJpZi7c4YG6VDkb7erzKa9jZD 2Ew/gjosA6XH1ot90v1XP0DnP8G3mPRLR8UqpDjeQz2QNMrOwTeJcVUQ8s2RgDAl367x qTIvXeoQA8AjFsqKIsJYXYsuJr8X4iBHCFuVbeGblmT5l6qgxmQIg0J65s5uuObRRbLg qgqA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=vif4ChP6To9MWPr0GWQdazmHQ6h3mcBFCGbXkm6wl94=; b=krQclK3wjxGn6y75vO2kS+Xyh7k78wNHBoo9zqrgsa0S4lA5eD+CFapDU7bBjRoFGD tSFwLdo699gN2zhBdeKgPbhEyA6yaYwPgBAU3ysHS4uFkpmbRB6ma9lhUKLbkHPcKO/R aJ8pQYKOjghiqQ0xlnlpvbVw9vQP3q+zXC0VtT25TnBHKIiYbX+a6tRrzk0ZzhKdHESe Ib/TWTY4lZyCAX64EVvFNz7m0zGOVudMI3PXNjicE1L5CweMt9NJ2WhQFjMU+BlVpJ1F BIBKXLl0UCzrZend1pxyFHld64n2cbTNYyNq+Ac+I/1zgo9reTFW3T1VtzE9ATJ0G5Aj 29FQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b="P/9QDdCt"; 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 f30si9613649oij.34.2021.05.11.10.19.06; Tue, 11 May 2021 10:19:21 -0700 (PDT) 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=20161025 header.b="P/9QDdCt"; 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 S231964AbhEKRRr (ORCPT + 99 others); Tue, 11 May 2021 13:17:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58736 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231951AbhEKRRb (ORCPT ); Tue, 11 May 2021 13:17:31 -0400 Received: from mail-qt1-x849.google.com (mail-qt1-x849.google.com [IPv6:2607:f8b0:4864:20::849]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 57297C06138A for ; Tue, 11 May 2021 10:16:24 -0700 (PDT) Received: by mail-qt1-x849.google.com with SMTP id j12-20020ac8550c0000b02901dae492d1f2so7384793qtq.0 for ; Tue, 11 May 2021 10:16:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=vif4ChP6To9MWPr0GWQdazmHQ6h3mcBFCGbXkm6wl94=; b=P/9QDdCt1kHWngOw1na6Leu2JD7lekSdtGS7VAKUvXhmzY9JyF+b20NH7WSINqjtY9 TVgyzyuMtSqoZuS2N0QU8d8wK1wW+eRtmwMPCQPR6cRllkjzF2UAXY/EkKKXbzxMYf6Q yqzL9wOyPj6AoF5thN1a/FveVoDQIAFnA6kkUED7+aZvNwn++Yv5Kzp0qCAli/OFoMN/ Fj5IaZKVYkrTwW5/SVayGF5zZDZXIecK89NpPRjEui1GpmU/BionmPZyNqcH1t9dAS2d 46FzS8hsuleBN/9yoFR+8OotcHAR04y4AMJoI3pRyzNOy9qkgxtJqzjTqGxfzf2tRwc/ AkbQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=vif4ChP6To9MWPr0GWQdazmHQ6h3mcBFCGbXkm6wl94=; b=QfTGnaofzulX81i37Q0qx7y74qFr4cJtjfZh2bpaZ/QtoBxNp1XoVM5OQDGi4m4CBR 5uBxY6IHwHCNEfNeAKhra4GDIHEBN4tpk2D3OWldmih4yheoXXvSWKCmHm4aUeChiXzp C/xKObJWiiwwBd8VXPxVRX2XuybekhLJWDhhQIOTJXR6KWfCafqGlq0az2WqKNfi9VeW 9ySKbyMU4ORChgBLaeaapl/L87fMIux0Wiy0I/iTQ014GfRNvfqE3MgGp+MIWKstFjXO Jk1NpZyKirner7lYwRIa/Qk/vv7wNLX2k8cq+SP7SCmRBlb/uz5xg3jsaVKtEiya7yeF XTrQ== X-Gm-Message-State: AOAM533jWcTIQQJNtj4WKQoSWxvw2DGg8NSW1a9XqOVrWUGiwltsZ9Ft Vde1l0FhDRrj5O9ukwjbIQ0ZXXTeYM2TUO0SFf7Zds8Az1r0QnAiPu5Obz8dl6fthV/p7t8WE6t VhaJzj8/eNJ4CFW9ZfXtOWESExXJhicN41IyDUEkzISGvv0W5emRbgsw5DTIg1bQ3pRtXEBef X-Received: from bgardon.sea.corp.google.com ([2620:15c:100:202:e050:3342:9ea6:6859]) (user=bgardon job=sendgmr) by 2002:ad4:4dc8:: with SMTP id cw8mr29820482qvb.16.1620753383411; Tue, 11 May 2021 10:16:23 -0700 (PDT) Date: Tue, 11 May 2021 10:16:06 -0700 In-Reply-To: <20210511171610.170160-1-bgardon@google.com> Message-Id: <20210511171610.170160-4-bgardon@google.com> Mime-Version: 1.0 References: <20210511171610.170160-1-bgardon@google.com> X-Mailer: git-send-email 2.31.1.607.g51e8a6a459-goog Subject: [PATCH v4 3/7] KVM: mmu: Refactor memslot copy From: Ben Gardon To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Paolo Bonzini , Peter Xu , Sean Christopherson , Peter Shier , Yulei Zhang , Wanpeng Li , Xiao Guangrong , Kai Huang , Keqian Zhu , David Hildenbrand , Ben Gardon Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Factor out copying kvm_memslots from allocating the memory for new ones in preparation for adding a new lock to protect the arch-specific fields of the memslots. No functional change intended. Reviewed-by: David Hildenbrand Signed-off-by: Ben Gardon --- virt/kvm/kvm_main.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 6b4feb92dc79..9e106742b388 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -1306,6 +1306,18 @@ static struct kvm_memslots *install_new_memslots(struct kvm *kvm, return old_memslots; } +static size_t kvm_memslots_size(int slots) +{ + return sizeof(struct kvm_memslots) + + (sizeof(struct kvm_memory_slot) * slots); +} + +static void kvm_copy_memslots(struct kvm_memslots *from, + struct kvm_memslots *to) +{ + memcpy(to, from, kvm_memslots_size(from->used_slots)); +} + /* * Note, at a minimum, the current number of used slots must be allocated, even * when deleting a memslot, as we need a complete duplicate of the memslots for @@ -1315,19 +1327,16 @@ static struct kvm_memslots *kvm_dup_memslots(struct kvm_memslots *old, enum kvm_mr_change change) { struct kvm_memslots *slots; - size_t old_size, new_size; - - old_size = sizeof(struct kvm_memslots) + - (sizeof(struct kvm_memory_slot) * old->used_slots); + size_t new_size; if (change == KVM_MR_CREATE) - new_size = old_size + sizeof(struct kvm_memory_slot); + new_size = kvm_memslots_size(old->used_slots + 1); else - new_size = old_size; + new_size = kvm_memslots_size(old->used_slots); slots = kvzalloc(new_size, GFP_KERNEL_ACCOUNT); if (likely(slots)) - memcpy(slots, old, old_size); + kvm_copy_memslots(old, slots); return slots; } -- 2.31.1.607.g51e8a6a459-goog