Received: by 2002:ab2:3319:0:b0:1ef:7a0f:c32d with SMTP id i25csp831503lqc; Fri, 8 Mar 2024 13:02:03 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWS6Xt9abvjXid+RCh9X8VB4vTGeWaDBUSCM5lP1VbDs2lxXvWFYQn+96pH8HobFODgodjHrfB5L5KExzCMxpScEZO41/H/KMpgogGQCA== X-Google-Smtp-Source: AGHT+IF2n4w2GKRoHPWyrL5lvSw1nuu8L6J+YOG1ySi4EE3HNhO6oiX7s285x9SgCAhJWuP0/Qva X-Received: by 2002:a17:906:5798:b0:a45:878e:b31c with SMTP id k24-20020a170906579800b00a45878eb31cmr108718ejq.53.1709931723659; Fri, 08 Mar 2024 13:02:03 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709931723; cv=pass; d=google.com; s=arc-20160816; b=Hq5WE11e+E6LgmEyGsQ4Awg5fMbsjB4C4jE2Q6/x8antJCn9IC4dwEONI39MXrKxoS hEyi+tWPCI705v1bsbT/J3YAJ677T7s5AAixzogWY2Hb22mM0MiBR4up3bfnLVOd9jXj 6QT4/EH6rLGTy1CQWI/mN0WpuySCYFk/StK0XfReiJHTv0spNyly7Nh9mheCpqR2UXRi A99a+hCzFBPdpEx5XsGemLGoh5Xm2LK9wc3dS0Yc4hI9yxGGFyh/uitSS5dkst9fgbpB y53Qxufo8jVaPLWk23TuvLnZndJG4guJMgpR/y8Xv8cC1yvwO8M5pgCf4tU+YWdwc/zl kxmQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=80vwuZ8ga86u6NrcDUFjU5C56fcICtYZKNZLY5/YEP0=; fh=1ttxlKSBmHmj4k/JOJ0gdwE7afKL1NGufKXmSUaqrlg=; b=dKYYPNsgS0XiP8attMxAepl7GRauvj1VIkhE8Cx8qzkZhd20q9LcCaWiieuMqNPR7n qnHgmybq/qLR2qb8bu2JlHL54MwJtVCzdv346pBovYISPWFFtqZLJl7wNe2BMeXFDkBC uKJWnlIPuwKR0BELPg+WreETYOAkCXEGBSRG0Bkwla+Tw/sItLyshIuaszueByn9niFW JzHQhonZetgmufbVZVEvFdyFXbQoXvobN0PPXo9O03k6ci2Fb0x+XOtpXST9A1twa4hF NmMZiV/aO3g4XK9256gBKSAX7E1ytoJlwxR0ymDAUxREW4iYFc2UXzzN47YKEza5v/BF Laiw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ce5BxOhb; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-97586-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-97586-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id sa9-20020a1709076d0900b00a3e50aea655si129458ejc.989.2024.03.08.13.02.03 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Mar 2024 13:02:03 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-97586-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ce5BxOhb; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-97586-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-97586-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 64BB91F212F2 for ; Fri, 8 Mar 2024 21:02:03 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id BB78E5FB9D; Fri, 8 Mar 2024 21:01:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="ce5BxOhb" Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.14]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 27B035E090; Fri, 8 Mar 2024 21:01:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.14 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709931680; cv=none; b=V3dsQKeaAAsPNtFtGQmdpxGRCSH71EpxtHEwcuegARsZqUruU6N9ATcjh8ZAxnEcIZ8oD7Y1n/pcUASXQCiiN6K7WdIuOetq3aimep52tfMAi+OobdpSh996nTgwIhzU58KRyKqTt9v2OQ/9Hq6vJ9FdABin3B1zcEEgUZNWnlg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709931680; c=relaxed/simple; bh=DDl8vWySAK96jU7FvSbeJPzo1lFEUUlodXhYmz+FK+o=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=fhIyiAxce9PN1nSerMPVcZC8lZChbuPyKjVT+5tuYqoKkXZTl492RlsJFg7N7ecOqe3kQk2hymmKrS6tPoqnbBEz5NwgBEreUp8GDKZLppw+wFiQTgN0naUbrHAn3EYrYVi6Vg3h3UuXVFTP8SXcd2ZZFkaHU07K5G5JmbqVluo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=ce5BxOhb; arc=none smtp.client-ip=198.175.65.14 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1709931678; x=1741467678; h=date:from:to:cc:subject:message-id:references: mime-version:in-reply-to; bh=DDl8vWySAK96jU7FvSbeJPzo1lFEUUlodXhYmz+FK+o=; b=ce5BxOhb7mJEsC10tFHwQb6Xc74RRP8Mu+V3LLyE8CHEUiJsh8Deo8Mn PrEki+08cVqFtIxh736+biYeSCwREScuj5UvOGDG70JRLkznHGHrV/7nM TaVoGFB7A5Z52xv+sWltN1b0Drv3GBxhoBjqtYxNY45ziL0hj1pEoL8y2 RxUCyAbtj+aH7y0YEvFONDsrYB/lQUSU9e5j3TjPmBJjsmzXU+f0H4EaP GNqyPUcCUGLTKDSjZy/QzVelGPr3AqxLPIeBrQ5Z7nGd7a7kAvauo1pgC JwcexCbEEFoBfWP65c9KvDNN+YOds26sbFW8fn0YmJJmi40iEJCxhim0p Q==; X-IronPort-AV: E=McAfee;i="6600,9927,11007"; a="8485088" X-IronPort-AV: E=Sophos;i="6.07,110,1708416000"; d="scan'208";a="8485088" Received: from fmviesa005.fm.intel.com ([10.60.135.145]) by orvoesa106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Mar 2024 13:01:18 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,110,1708416000"; d="scan'208";a="15067143" Received: from ls.sc.intel.com (HELO localhost) ([172.25.112.31]) by fmviesa005-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Mar 2024 13:01:17 -0800 Date: Fri, 8 Mar 2024 13:01:16 -0800 From: Isaku Yamahata To: Yin Fengwei Cc: isaku.yamahata@intel.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, isaku.yamahata@gmail.com, Paolo Bonzini , erdemaktas@google.com, Sean Christopherson , Sagi Shahar , Kai Huang , chen.bo@intel.com, hang.yuan@intel.com, tina.zhang@intel.com, isaku.yamahata@linux.intel.com Subject: Re: [PATCH v19 014/130] KVM: Add KVM vcpu ioctl to pre-populate guest memory Message-ID: <20240308210116.GB713729@ls.amr.corp.intel.com> References: <8b7380f1b02f8e3995f18bebb085e43165d5d682.1708933498.git.isaku.yamahata@intel.com> <6b453972-2723-47c5-981e-56c150f217d7@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <6b453972-2723-47c5-981e-56c150f217d7@intel.com> On Thu, Mar 07, 2024 at 03:01:11PM +0800, Yin Fengwei wrote: > > diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c > > index 0349e1f241d1..2f0a8e28795e 100644 > > --- a/virt/kvm/kvm_main.c > > +++ b/virt/kvm/kvm_main.c > > @@ -4409,6 +4409,62 @@ static int kvm_vcpu_ioctl_get_stats_fd(struct kvm_vcpu *vcpu) > > return fd; > > } > > > > +__weak void kvm_arch_vcpu_pre_memory_mapping(struct kvm_vcpu *vcpu) > > +{ > > +} > > + > > +__weak int kvm_arch_vcpu_memory_mapping(struct kvm_vcpu *vcpu, > > + struct kvm_memory_mapping *mapping) > > +{ > > + return -EOPNOTSUPP; > > +} > > + > > +static int kvm_vcpu_memory_mapping(struct kvm_vcpu *vcpu, > > + struct kvm_memory_mapping *mapping) > > +{ > > + bool added = false; > > + int idx, r = 0; > > + > > + /* flags isn't used yet. */ > > + if (mapping->flags) > > + return -EINVAL; > > + > > + /* Sanity check */ > > + if (!IS_ALIGNED(mapping->source, PAGE_SIZE) || > > + !mapping->nr_pages || > > + mapping->nr_pages & GENMASK_ULL(63, 63 - PAGE_SHIFT) || > > + mapping->base_gfn + mapping->nr_pages <= mapping->base_gfn) > I suppose !mapping->nr_pages can be deleted as this line can cover it. > > + return -EINVAL; > > + > > + vcpu_load(vcpu); > > + idx = srcu_read_lock(&vcpu->kvm->srcu); > > + kvm_arch_vcpu_pre_memory_mapping(vcpu); > > + > > + while (mapping->nr_pages) { > > + if (signal_pending(current)) { > > + r = -ERESTARTSYS; > > + break; > > + } > > + > > + if (need_resched()) > > + cond_resched(); > > + > > + r = kvm_arch_vcpu_memory_mapping(vcpu, mapping); > > + if (r) > > + break; > > + > > + added = true; > > + } > > + > > + srcu_read_unlock(&vcpu->kvm->srcu, idx); > > + vcpu_put(vcpu); > > + > > + if (added && mapping->nr_pages > 0) > > + r = -EAGAIN; > > + > > + return r; > > +} > > + > > static long kvm_vcpu_ioctl(struct file *filp, > > unsigned int ioctl, unsigned long arg) > > { > > @@ -4610,6 +4666,17 @@ static long kvm_vcpu_ioctl(struct file *filp, > > r = kvm_vcpu_ioctl_get_stats_fd(vcpu); > > break; > > } > > + case KVM_MEMORY_MAPPING: { > > + struct kvm_memory_mapping mapping; > > + > > + r = -EFAULT; > > + if (copy_from_user(&mapping, argp, sizeof(mapping))) > > + break; > > + r = kvm_vcpu_memory_mapping(vcpu, &mapping); > return value r should be checked before copy_to_user That's intentional to tell the mapping is partially or fully processed regardless that error happened or not. > > > Regards > Yin, Fengwei > > > + if (copy_to_user(argp, &mapping, sizeof(mapping))) > > + r = -EFAULT; > > + break; > > + } > > default: > > r = kvm_arch_vcpu_ioctl(filp, ioctl, arg); > > } > -- Isaku Yamahata