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 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3326DC433FE for ; Thu, 17 Nov 2022 13:25:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8D32D6B0072; Thu, 17 Nov 2022 08:25:09 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8834B6B0073; Thu, 17 Nov 2022 08:25:09 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 724F56B0074; Thu, 17 Nov 2022 08:25:09 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 6399F6B0072 for ; Thu, 17 Nov 2022 08:25:09 -0500 (EST) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 293ADABB06 for ; Thu, 17 Nov 2022 13:25:09 +0000 (UTC) X-FDA: 80143005138.09.734B13E Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by imf28.hostedemail.com (Postfix) with ESMTP id 30465C0012 for ; Thu, 17 Nov 2022 13:25:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1668691508; x=1700227508; h=date:from:to:cc:subject:message-id:reply-to:references: mime-version:in-reply-to; bh=5axGlSLhPtmuHt4PaXeWSd8Q7yJdQcmlwNx/L+JedqY=; b=RPEOkxW6umJDslOCkIX7Gh1PlAYXX78lqbcC2oK5GXIjP4GKilkZcLSs RPQf4l2GWfTnYwq55pyGMW0QzZpU5WGT3iRVF6UD3WAQkmK1a4560StPK AY7RCkYsY4A1efPZTw8dfGtosYYeRLz/8Yfc2H4y3UceWXL+wGcw0BU1e Hh0dW9p2kpi2EkOo9o+ySCZP/0ReIftwWs/NHf+oj4ZGYj5ar7sPs6O8f FqBzpC62bNnsPEqx0ioUN66S1iKqo8RSy4LN/QKmDtjVwMEaf3jx6+cyN vXCmCEkmL6tmCsCeUT7XbImHVuZ2xnlh5P410coHjGY9hBlKNqIp5c4/B A==; X-IronPort-AV: E=McAfee;i="6500,9779,10533"; a="310478132" X-IronPort-AV: E=Sophos;i="5.96,171,1665471600"; d="scan'208";a="310478132" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Nov 2022 05:25:06 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10533"; a="703331041" X-IronPort-AV: E=Sophos;i="5.96,171,1665471600"; d="scan'208";a="703331041" Received: from chaop.bj.intel.com (HELO localhost) ([10.240.193.75]) by fmsmga008.fm.intel.com with ESMTP; 17 Nov 2022 05:24:56 -0800 Date: Thu, 17 Nov 2022 21:20:32 +0800 From: Chao Peng To: Sean Christopherson Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-arch@vger.kernel.org, linux-api@vger.kernel.org, linux-doc@vger.kernel.org, qemu-devel@nongnu.org, Paolo Bonzini , Jonathan Corbet , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H . Peter Anvin" , Hugh Dickins , Jeff Layton , "J . Bruce Fields" , Andrew Morton , Shuah Khan , Mike Rapoport , Steven Price , "Maciej S . Szmigiero" , Vlastimil Babka , Vishal Annapurve , Yu Zhang , "Kirill A . Shutemov" , luto@kernel.org, jun.nakajima@intel.com, dave.hansen@intel.com, ak@linux.intel.com, david@redhat.com, aarcange@redhat.com, ddutile@redhat.com, dhildenb@redhat.com, Quentin Perret , tabba@google.com, Michael Roth , mhocko@suse.com, Muchun Song , wei.w.wang@intel.com Subject: Re: [PATCH v9 5/8] KVM: Register/unregister the guest private memory regions Message-ID: <20221117132032.GA422408@chaop.bj.intel.com> Reply-To: Chao Peng References: <20221025151344.3784230-1-chao.p.peng@linux.intel.com> <20221025151344.3784230-6-chao.p.peng@linux.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1668691508; a=rsa-sha256; cv=none; b=YkmBhuRztAGWcjfr9zxttJZvrlewujMXhgHByINMsw1jNQY6KubsTFlV0V0UrYfoiYIGNB ueqA+qiE9rK9hxpG9otg2l/rIxpDeu564TbUkK8pKEIvZ7aDUthPmW+sD3v3Zi1d9bM8gH 7xEI54YW5GgWrjAiW36hsJ2O/y/EcJ4= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=none ("invalid DKIM record") header.d=intel.com header.s=Intel header.b=RPEOkxW6; dmarc=fail reason="No valid SPF" header.from=intel.com (policy=none); spf=none (imf28.hostedemail.com: domain of chao.p.peng@linux.intel.com has no SPF policy when checking 192.55.52.93) smtp.mailfrom=chao.p.peng@linux.intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1668691508; h=from:from:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=NtlVk2nIHazi8XeZyHzFxeazJTDn2BKjtvSMrkOqx8Y=; b=dwHq9jpHurXNs7H3KSQwXN18h6er4/N1kLVJGtQLS43ZXLBQTud3Wd3rgTxe/jC6sVQXSt r/HlBMDGEk6WIbn/hs4psRFHq96bqbhhPE3K+UtdOo8F2jNs1Q0QpjD0pwIlG0LhBkLxos YAFs+geXYD2MnsxESuLu8ghDTvIIT08= X-Rspam-User: X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 30465C0012 Authentication-Results: imf28.hostedemail.com; dkim=none ("invalid DKIM record") header.d=intel.com header.s=Intel header.b=RPEOkxW6; dmarc=fail reason="No valid SPF" header.from=intel.com (policy=none); spf=none (imf28.hostedemail.com: domain of chao.p.peng@linux.intel.com has no SPF policy when checking 192.55.52.93) smtp.mailfrom=chao.p.peng@linux.intel.com X-Stat-Signature: zzk4jrp5fdrgsmsie6zz1jw7tft16dps X-HE-Tag: 1668691507-534922 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Wed, Nov 16, 2022 at 10:24:11PM +0000, Sean Christopherson wrote: > On Tue, Oct 25, 2022, Chao Peng wrote: > > +static int kvm_vm_ioctl_set_mem_attr(struct kvm *kvm, gpa_t gpa, gpa_t size, > > + bool is_private) > > +{ > > + gfn_t start, end; > > + unsigned long i; > > + void *entry; > > + int idx; > > + int r = 0; > > + > > + if (size == 0 || gpa + size < gpa) > > + return -EINVAL; > > + if (gpa & (PAGE_SIZE - 1) || size & (PAGE_SIZE - 1)) > > + return -EINVAL; > > + > > + start = gpa >> PAGE_SHIFT; > > + end = (gpa + size - 1 + PAGE_SIZE) >> PAGE_SHIFT; > > + > > + /* > > + * Guest memory defaults to private, kvm->mem_attr_array only stores > > + * shared memory. > > + */ > > + entry = is_private ? NULL : xa_mk_value(KVM_MEM_ATTR_SHARED); > > + > > + idx = srcu_read_lock(&kvm->srcu); > > + KVM_MMU_LOCK(kvm); > > + kvm_mmu_invalidate_begin(kvm, start, end); > > + > > + for (i = start; i < end; i++) { > > + r = xa_err(xa_store(&kvm->mem_attr_array, i, entry, > > + GFP_KERNEL_ACCOUNT)); > > + if (r) > > + goto err; > > + } > > + > > + kvm_unmap_mem_range(kvm, start, end); > > + > > + goto ret; > > +err: > > + for (; i > start; i--) > > + xa_erase(&kvm->mem_attr_array, i); > > I don't think deleting previous entries is correct. To unwind, the correct thing > to do is restore the original values. E.g. if userspace space is mapping a large > range as shared, and some of the previous entries were shared, deleting them would > incorrectly "convert" those entries to private. Ah, right! > > Tracking the previous state likely isn't the best approach, e.g. it would require > speculatively allocating extra memory for a rare condition that is likely going to > lead to OOM anyways. Agree. > > Instead of trying to unwind, what about updating the ioctl() params such that > retrying with the updated addr+size would Just Work? E.g. Looks good to me. Thanks! Chao > > diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c > index 55b07aae67cc..f1de592a1a06 100644 > --- a/virt/kvm/kvm_main.c > +++ b/virt/kvm/kvm_main.c > @@ -1015,15 +1015,12 @@ static int kvm_vm_ioctl_set_mem_attr(struct kvm *kvm, gpa_t gpa, gpa_t size, > > kvm_unmap_mem_range(kvm, start, end, attr); > > - goto ret; > -err: > - for (; i > start; i--) > - xa_erase(&kvm->mem_attr_array, i); > -ret: > kvm_mmu_invalidate_end(kvm, start, end); > KVM_MMU_UNLOCK(kvm); > srcu_read_unlock(&kvm->srcu, idx); > > + > + > return r; > } > #endif /* CONFIG_KVM_GENERIC_PRIVATE_MEM */ > @@ -4989,6 +4986,8 @@ static long kvm_vm_ioctl(struct file *filp, > > r = kvm_vm_ioctl_set_mem_attr(kvm, region.addr, > region.size, set); > + if (copy_to_user(argp, ®ion, sizeof(region)) && !r) > + r = -EFAULT > break; > } > #endif