Received: by 2002:ac0:a679:0:0:0:0:0 with SMTP id p54csp963194imp; Wed, 20 Feb 2019 12:21:58 -0800 (PST) X-Google-Smtp-Source: AHgI3IZQj5YkIzkacF7J4MlpUaHkoD50zn2+4VVBCJIauJ09T71kEpIOMUlI+e0k41gUlfbbWQz0 X-Received: by 2002:a63:7403:: with SMTP id p3mr7797948pgc.343.1550694118330; Wed, 20 Feb 2019 12:21:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1550694118; cv=none; d=google.com; s=arc-20160816; b=PxtA6OdCdFHRih3vqTMEgUPTcoumDIwQflIjbmk8Xh6dnuaJNFs3g4irDTSKZNU/+L uknhrx3nW6RhcIiUNstZM2wJKoia6Uupkk+9J3/upJkWPpSX17n1LwXt009npawpTWSh ydYSh5Xp/gghWzLHjETCGOQR/5MgXNoEdlCg5a7cmlDYMyhrhcMl4IhJO0uDM9rMS0YM Dq1V5JOPONvReOUQotTE5m9zoTb/aVJTTV3qeEVPCsCxlOFAcV0K13STvAjnQgtdWkNf X9L85ME+DJ92Jn1h1TtWfDrSL0WCspLGYkRWYw3BGw88duH+zXt5cyYVs3fiqx5SfCWH bAxg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=YZ80s2dOqvHZLSQICuv37RTvDVLvQVdgaR+I/O+Ppao=; b=Wek25YP13BWMOL41sFCgRM52CiSi+pvA790s0AW26HgIbhePdhOmPSFcnAHYBiPwwz QN2epICpbOgaElIe3X3vpMhVPGFK0IXfqxOwL166o+3mBCZtadK5wA3u2TTWoabE7Pg6 cGGAYR/kdOeLE5Yy4CcAfWyLDhn8OrZaD4OF7zwCetQes7iSykOyfcib1aURFQTp1/2u lnegKH+O4tyFto7D9ARB96vpmxvXGeCx2qPQUuzNp3s6HP6gBrQZushq5xy7nhHDAaH2 RtcCyjGYrd7o6GSKG9vh5tqGWydFXWTh2ACxOIGFRj+CSHFb0RzJXjod2E6sxl9myLxM yxXg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2018-07-02 header.b=TawXR2tZ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id s123si19013328pgs.93.2019.02.20.12.21.42; Wed, 20 Feb 2019 12:21:58 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2018-07-02 header.b=TawXR2tZ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727900AbfBTUUV (ORCPT + 99 others); Wed, 20 Feb 2019 15:20:21 -0500 Received: from userp2120.oracle.com ([156.151.31.85]:36216 "EHLO userp2120.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727972AbfBTUSg (ORCPT ); Wed, 20 Feb 2019 15:18:36 -0500 Received: from pps.filterd (userp2120.oracle.com [127.0.0.1]) by userp2120.oracle.com (8.16.0.27/8.16.0.27) with SMTP id x1KK8YIC087297; Wed, 20 Feb 2019 20:18:23 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-type : content-transfer-encoding; s=corp-2018-07-02; bh=YZ80s2dOqvHZLSQICuv37RTvDVLvQVdgaR+I/O+Ppao=; b=TawXR2tZtIHJQg0fJ3iWk9NXhfNqK5+2LgqSUautIUcoNAtdrcFluwEI6ZfWFP5EbiRY wL7/QhsLf7ie6hBe3eSbpl5bcFsLS71iLDivSHoL1rxNQYNSn6UGBJJEpIe5SvjGEYqa ZBNcyzvGETuQA9qPZ4RlU4Fy8Yi5Qj5IASIA3EaTo/AJWntzh+dekpEZLtu0LDFvrKhI hwIt+GSrLprs8zU7P7s1yGPPioYs/a5bcRCAuLgxJHyDQb3IlrlgvJdXlhPi5LuqrlM1 tuGuLCibtTpQ5jdGg+Z9gyogAFQht172H1iYiZUeFFvnHnt7futsI7F+uJ+rP9as+8Kt 1g== Received: from aserv0022.oracle.com (aserv0022.oracle.com [141.146.126.234]) by userp2120.oracle.com with ESMTP id 2qpb5rktuj-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 20 Feb 2019 20:18:23 +0000 Received: from userv0121.oracle.com (userv0121.oracle.com [156.151.31.72]) by aserv0022.oracle.com (8.14.4/8.14.4) with ESMTP id x1KKIHmo024795 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 20 Feb 2019 20:18:17 GMT Received: from abhmp0022.oracle.com (abhmp0022.oracle.com [141.146.116.28]) by userv0121.oracle.com (8.14.4/8.13.8) with ESMTP id x1KKIGjM015848; Wed, 20 Feb 2019 20:18:16 GMT Received: from paddy.lan (/94.61.137.133) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Wed, 20 Feb 2019 12:18:16 -0800 From: Joao Martins To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Ankur Arora , Boris Ostrovsky , Joao Martins , Paolo Bonzini , =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= , Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , x86@kernel.org Subject: [PATCH RFC 26/39] KVM: x86/xen: grant unmap support Date: Wed, 20 Feb 2019 20:15:56 +0000 Message-Id: <20190220201609.28290-27-joao.m.martins@oracle.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20190220201609.28290-1-joao.m.martins@oracle.com> References: <20190220201609.28290-1-joao.m.martins@oracle.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Proofpoint-Virus-Version: vendor=nai engine=5900 definitions=9173 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 priorityscore=1501 malwarescore=0 suspectscore=2 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=803 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1902200138 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Ankur Arora Grant unmap removes the grant from maptrack and marks it as not in use. We maintain a one-to-one correspondence between grant table and maptrack entries so there's no contention in allocation/free. Co-developed-by: Joao Martins Signed-off-by: Ankur Arora Signed-off-by: Joao Martins --- arch/x86/kvm/xen.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/arch/x86/kvm/xen.c b/arch/x86/kvm/xen.c index 3603645086a7..8f06924e0dfa 100644 --- a/arch/x86/kvm/xen.c +++ b/arch/x86/kvm/xen.c @@ -1684,6 +1684,69 @@ static int shim_hcall_gntmap(struct kvm_xen *ld, return 0; } +static int shim_hcall_gntunmap(struct kvm_xen *xen, + struct gnttab_unmap_grant_ref *op) +{ + struct kvm_grant_map *map, unmap; + struct grant_entry_v1 **rgt; + struct grant_entry_v1 *shah; + struct kvm *rd = NULL; + domid_t domid; + u32 ref; + + domid = handle_get_domid(op->handle); + ref = handle_get_grant(op->handle); + + + rd = kvm_xen_find_vm(domid); + if (unlikely(!rd)) { + /* We already teardown all ongoing grant maps */ + op->status = GNTST_okay; + return 0; + } + + if (unlikely(ref >= gnttab_entries(rd))) { + pr_err("gnttab: bad ref %u\n", ref); + op->status = GNTST_bad_handle; + return 0; + } + + rgt = rd->arch.xen.gnttab.frames_v1; + map = maptrack_entry(rd->arch.xen.gnttab.handle, ref); + + /* + * The test_and_clear_bit (below) serializes ownership of this + * grant-entry. After we clear it, there can be a grant-map on this + * entry. So we cache the unmap entry before relinquishing ownership. + */ + unmap = *map; + + if (!test_and_clear_bit(_KVM_GNTMAP_ACTIVE, + (unsigned long *) &map->flags)) { + pr_err("gnttab: bad flags for %u (dom %u ref %u) flags %x\n", + op->handle, domid, ref, unmap.flags); + op->status = GNTST_bad_handle; + return 0; + } + + /* Give up the reference taken in get_user_pages_remote(). */ + put_page(virt_to_page(unmap.gpa)); + + shah = shared_entry(rgt, unmap.ref); + + /* + * We have cleared _KVM_GNTMAP_ACTIVE, so a simultaneous grant-map + * could update the shah and we would stomp all over it but the + * guest deserves it. + */ + if (!(unmap.flags & GNTMAP_readonly)) + clear_bit(_GTF_writing, (unsigned long *) &shah->flags); + clear_bit(_GTF_reading, (unsigned long *) &shah->flags); + + op->status = GNTST_okay; + return 0; +} + static int shim_hcall_gnttab(int op, void *p, int count) { int ret = -ENOSYS; @@ -1698,6 +1761,16 @@ static int shim_hcall_gnttab(int op, void *p, int count) ret = 0; break; } + case GNTTABOP_unmap_grant_ref: { + struct gnttab_unmap_grant_ref *ref = p; + + for (i = 0; i < count; i++) { + shim_hcall_gntunmap(xen_shim, ref + i); + ref[i].host_addr = 0; + } + ret = 0; + break; + } default: pr_info("lcall-gnttab:op default=%d\n", op); break; -- 2.11.0