From patchwork Tue Oct 21 18:10:36 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefano Stabellini X-Patchwork-Id: 39217 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-wi0-f199.google.com (mail-wi0-f199.google.com [209.85.212.199]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id A6C3C24039 for ; Tue, 21 Oct 2014 18:14:22 +0000 (UTC) Received: by mail-wi0-f199.google.com with SMTP id d1sf1196170wiv.6 for ; Tue, 21 Oct 2014 11:14:19 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:from:to:date:message-id:in-reply-to :references:mime-version:cc:subject:precedence:list-id :list-unsubscribe:list-post:list-help:list-subscribe:sender :errors-to:x-original-sender:x-original-authentication-results :mailing-list:list-archive:content-type:content-transfer-encoding; bh=tTkhmBJwdIqY+iI3Y4N7GD7AcbIoQasUAAQTOzAdh2E=; b=ILQqOACiOry2FX4kDI+uEaj1lq9vEvLOckbND66xjoKMlHcT0h1RdORlqeUgSWERjm 0RwBWsRIhSW4PA5D55R55h383I6PvkfYomeWZxtQaEmb73eI5stTUyxs2Qy+/5h/oLLH 7klN3eNY54EzL4nw2OUeBrJc53a9YFXbuUx15fS7eBKjGSc4LDMPQwpzUqgCb83XBNfi y9am4OCztByu0el/jpL5K8C/sdzNxhk/WrN2tlafMlSeyV0bYFFKBwYXkqOkr49TwlSf tE2EYXS4TrsfP8Jhfe0doApE22lHqzxnENviVjgpP5qJnQxy0NUcYvbBA6zWohzl1l7X 8gaw== X-Gm-Message-State: ALoCoQmhypuN/jVYPHX6X2XmrAx0PuO8/MasnSMfWSzgkRziI3kkCjOOctW3Cr4nkbFVvJnEV4z7 X-Received: by 10.194.206.10 with SMTP id lk10mr850224wjc.3.1413915259358; Tue, 21 Oct 2014 11:14:19 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.23.227 with SMTP id p3ls89469laf.39.gmail; Tue, 21 Oct 2014 11:14:18 -0700 (PDT) X-Received: by 10.152.179.69 with SMTP id de5mr36877897lac.63.1413915258964; Tue, 21 Oct 2014 11:14:18 -0700 (PDT) Received: from mail-lb0-f176.google.com (mail-lb0-f176.google.com. [209.85.217.176]) by mx.google.com with ESMTPS id tj1si20208690lbb.73.2014.10.21.11.14.18 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 21 Oct 2014 11:14:18 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.176 as permitted sender) client-ip=209.85.217.176; Received: by mail-lb0-f176.google.com with SMTP id p9so1536989lbv.35 for ; Tue, 21 Oct 2014 11:14:18 -0700 (PDT) X-Received: by 10.152.5.38 with SMTP id p6mr31739468lap.44.1413915258821; Tue, 21 Oct 2014 11:14:18 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.84.229 with SMTP id c5csp556000lbz; Tue, 21 Oct 2014 11:14:17 -0700 (PDT) X-Received: by 10.224.114.135 with SMTP id e7mr30881718qaq.68.1413915256537; Tue, 21 Oct 2014 11:14:16 -0700 (PDT) Received: from lists.xen.org (lists.xen.org. [50.57.142.19]) by mx.google.com with ESMTPS id mc10si20025114qcb.13.2014.10.21.11.14.15 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Tue, 21 Oct 2014 11:14:16 -0700 (PDT) Received-SPF: none (google.com: xen-devel-bounces@lists.xen.org does not designate permitted sender hosts) client-ip=50.57.142.19; Received: from localhost ([127.0.0.1] helo=lists.xen.org) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1Xgdvv-0002B5-KI; Tue, 21 Oct 2014 18:13:23 +0000 Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1Xgdvt-000289-RD for xen-devel@lists.xensource.com; Tue, 21 Oct 2014 18:13:22 +0000 Received: from [85.158.139.211] by server-3.bemta-5.messagelabs.com id 79/AB-02953-142A6445; Tue, 21 Oct 2014 18:13:21 +0000 X-Env-Sender: Stefano.Stabellini@citrix.com X-Msg-Ref: server-9.tower-206.messagelabs.com!1413915197!10926958!2 X-Originating-IP: [66.165.176.63] X-SpamReason: No, hits=0.0 required=7.0 tests=sa_preprocessor: VHJ1c3RlZCBJUDogNjYuMTY1LjE3Ni42MyA9PiAzMDYwNDg=\n X-StarScan-Received: X-StarScan-Version: 6.12.3; banners=-,-,- X-VirusChecked: Checked Received: (qmail 10629 invoked from network); 21 Oct 2014 18:13:19 -0000 Received: from smtp02.citrix.com (HELO SMTP02.CITRIX.COM) (66.165.176.63) by server-9.tower-206.messagelabs.com with RC4-SHA encrypted SMTP; 21 Oct 2014 18:13:19 -0000 X-IronPort-AV: E=Sophos;i="5.04,763,1406592000"; d="scan'208";a="184785999" Received: from ukmail1.uk.xensource.com (10.80.16.128) by smtprelay.citrix.com (10.13.107.79) with Microsoft SMTP Server id 14.3.181.6; Tue, 21 Oct 2014 14:13:13 -0400 Received: from kaball.uk.xensource.com ([10.80.2.59]) by ukmail1.uk.xensource.com with esmtp (Exim 4.69) (envelope-from ) id 1Xgdvg-0001jK-Dv; Tue, 21 Oct 2014 19:13:08 +0100 From: Stefano Stabellini To: Date: Tue, 21 Oct 2014 19:10:36 +0100 Message-ID: <1413915038-17490-6-git-send-email-stefano.stabellini@eu.citrix.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: References: MIME-Version: 1.0 X-DLP: MIA2 Cc: julien.grall@citrix.com, Ian.Campbell@citrix.com, JBeulich@suse.com, Stefano Stabellini Subject: [Xen-devel] [PATCH v10 6/8] xen/arm: introduce GNTTABOP_cache_flush X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: List-Unsubscribe: , List-Post: , List-Help: , List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: stefano.stabellini@eu.citrix.com X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.176 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 List-Archive: Introduce a new hypercall to perform cache maintenance operation on behalf of the guest. The argument is a machine address and a size. The implementation checks that the memory range is owned by the guest or the guest has been granted access to it by another domain. Introduce grant_map_exists: an internal grant table function to check whether an mfn has been granted to a given domain on a target grant table. Check hypercall_preempt_check() every 4096 iterations in the implementation of grant_map_exists. Use the top 20 bits of the GNTTABOP cmd encoding to save the last ref across the hypercall continuation. Signed-off-by: Stefano Stabellini --- Changes in v10: - add a comment on dev_bus_addr; - remove comment on max GNTTABOP; - break long lines; - remove ref_count local variable; - use unsigned int* instead of grant_ref_t* as arguments. Changes in v9: - grant_map_exists: calculate max iteration before the loop; - do_grant_table_op: avoid shifts, use masks instead; - set GNTTABOP_CONTINUATION_ARG_SHIFT to 12; - remove MAX_GRANT_ENTRIES_ITER_SHIFT, use GNTTABOP_CONTINUATION_ARG_SHIFT; - move GNTTABOP_CMD_MASK and GNTTABOP_CONTINUATION_ARG_SHIFT to grant_table.c. Changes in v8: - avoid security issues, use two separate opaque variables to store the input argument and the output argument; - fix return values of grant_map_exists; - rename CMD_MASK to GNTTABOP_CMD_MASK; - rename OPAQUE_CONTINUATION_ARG_SHIFT to GNTTABOP_CONTINUATION_ARG_SHIFT; - save in the opaque argument the shifted ref_count; - set GNTTABOP_CONTINUATION_ARG_SHIFT to 20 and MAX_GRANT_ENTRIES_ITER_SHIFT to 12, to cover the full grant_ref_t value range; - move GNTTABOP_CONTINUATION_ARG_SHIFT and GNTTABOP_CMD_MASK to include/xen/grant_table.h; - cmd &= GNTTABOP_CMD_MASK in the compat wrapper. Changes in v7: - remove warning message; - prefix second line of the warning with XENLOG_WARNING; - do not lower DEFAULT_MAX_NR_GRANT_FRAMES; - no long lines; - interrupt loops in grant_map_exists with more than 2048 iterations, create an hypercall continuation if necessary. Changes in v6: - set DEFAULT_MAX_NR_GRANT_FRAMES to 10; - warn if max_grant_frames > 10. Changes in v5: - make mfn mfn unsigned long; - remove unhelpful error message; - handle errors returned by cache maintenance functions. Changes in v4: - ASSERT(spin_is_locked); - return instead of break in grant_map_exists; - pass a pointer to __gnttab_cache_flush; - code style; - unsigned int iterator in gnttab_cache_flush; - return ret instead -ret; - cflush.offset >= PAGE_SIZE return -EINVAL. Changes in v3: - reduce the time the grant_table lock is held; - fix warning message; - s/EFAULT/EPERM; - s/llx/PRIx64; - check offset and size independetly before checking their sum; - rcu_lock_current_domain cannot fail; - s/ENOSYS/EOPNOTSUPP; - use clean_and_invalidate_xen_dcache_va_range to do both operations at once; - fold grant_map_exists in this patch; - support "count" argument; - make correspondent changes to compat/grant_table.c; - introduce GNTTAB_CACHE_SOURCE_GREF to select the type of input in the union; - rename size field to length; - make length and offset uint16_t; - only take spin_lock if d != owner. Changes in v2: - do not check for mfn_to_page errors; - take a reference to the page; - replace printk with gdprintk; - split long line; - remove out label; - move rcu_lock_current_domain down before the loop. - move the hypercall to GNTTABOP; - take a spin_lock before calling grant_map_exists. --- xen/common/compat/grant_table.c | 15 +++- xen/common/grant_table.c | 167 +++++++++++++++++++++++++++++++++++++- xen/include/public/grant_table.h | 24 ++++++ xen/include/xlat.lst | 1 + 4 files changed, 201 insertions(+), 6 deletions(-) diff --git a/xen/common/compat/grant_table.c b/xen/common/compat/grant_table.c index 2dc1e44..0368289 100644 --- a/xen/common/compat/grant_table.c +++ b/xen/common/compat/grant_table.c @@ -51,16 +51,21 @@ CHECK_gnttab_get_version; CHECK_gnttab_swap_grant_ref; #undef xen_gnttab_swap_grant_ref +#define xen_gnttab_cache_flush gnttab_cache_flush +CHECK_gnttab_cache_flush; +#undef xen_gnttab_cache_flush + int compat_grant_table_op(unsigned int cmd, XEN_GUEST_HANDLE_PARAM(void) cmp_uop, unsigned int count) { int rc = 0; - unsigned int i; + unsigned int i, cmd_op; XEN_GUEST_HANDLE_PARAM(void) cnt_uop; set_xen_guest_handle(cnt_uop, NULL); - switch ( cmd ) + cmd_op = cmd & GNTTABOP_CMD_MASK; + switch ( cmd_op ) { #define CASE(name) \ case GNTTABOP_##name: \ @@ -106,6 +111,10 @@ int compat_grant_table_op(unsigned int cmd, CASE(swap_grant_ref); #endif +#ifndef CHECK_gnttab_cache_flush + CASE(cache_flush); +#endif + #undef CASE default: return do_grant_table_op(cmd, cmp_uop, count); @@ -132,7 +141,7 @@ int compat_grant_table_op(unsigned int cmd, } cmp; set_xen_guest_handle(nat.uop, COMPAT_ARG_XLAT_VIRT_BASE); - switch ( cmd ) + switch ( cmd_op ) { case GNTTABOP_setup_table: if ( unlikely(count > 1) ) diff --git a/xen/common/grant_table.c b/xen/common/grant_table.c index f9a9b44..c53d949 100644 --- a/xen/common/grant_table.c +++ b/xen/common/grant_table.c @@ -62,6 +62,11 @@ integer_param("gnttab_max_frames", max_grant_frames); static unsigned int __read_mostly max_maptrack_frames; integer_param("gnttab_max_maptrack_frames", max_maptrack_frames); + +#define GNTTABOP_CONTINUATION_ARG_SHIFT 12 +#define GNTTABOP_CMD_MASK ((1<lock)); + + max_iter = min(*ref_count + (1 << GNTTABOP_CONTINUATION_ARG_SHIFT), + nr_grant_entries(rgt)); + for ( ref = *ref_count; ref < max_iter; ref++ ) + { + act = &active_entry(rgt, ref); + + if ( !act->pin ) + continue; + + if ( act->domid != ld->domain_id ) + continue; + + if ( act->frame != mfn ) + continue; + + return 0; + } + + if ( ref < nr_grant_entries(rgt) ) + { + *ref_count = ref; + return 1; + } + + return -EINVAL; +} + static void mapcount( struct grant_table *lgt, struct domain *rd, unsigned long mfn, unsigned int *wrc, unsigned int *rdc) @@ -2488,16 +2530,119 @@ gnttab_swap_grant_ref(XEN_GUEST_HANDLE_PARAM(gnttab_swap_grant_ref_t) uop, return 0; } +static int __gnttab_cache_flush(gnttab_cache_flush_t *cflush, + unsigned int *ref_count) +{ + struct domain *d, *owner; + struct page_info *page; + unsigned long mfn; + void *v; + int ret = 0; + + if ( (cflush->offset >= PAGE_SIZE) || + (cflush->length > PAGE_SIZE) || + (cflush->offset + cflush->length > PAGE_SIZE) ) + return -EINVAL; + + if ( cflush->length == 0 || cflush->op == 0 ) + return 0; + + /* currently unimplemented */ + if ( cflush->op & GNTTAB_CACHE_SOURCE_GREF ) + return -EOPNOTSUPP; + + d = rcu_lock_current_domain(); + mfn = cflush->a.dev_bus_addr >> PAGE_SHIFT; + + if ( !mfn_valid(mfn) ) + { + rcu_unlock_domain(d); + return -EINVAL; + } + + page = mfn_to_page(mfn); + owner = page_get_owner_and_reference(page); + if ( !owner ) + { + rcu_unlock_domain(d); + return -EPERM; + } + + if ( d != owner ) + { + spin_lock(&owner->grant_table->lock); + + ret = grant_map_exists(d, owner->grant_table, mfn, ref_count); + if ( ret != 0 ) + { + spin_unlock(&owner->grant_table->lock); + rcu_unlock_domain(d); + put_page(page); + return ret; + } + } + + v = map_domain_page(mfn); + v += cflush->offset; + + if ( (cflush->op & GNTTAB_CACHE_INVAL) && (cflush->op & GNTTAB_CACHE_CLEAN) ) + ret = clean_and_invalidate_dcache_va_range(v, cflush->length); + else if ( cflush->op & GNTTAB_CACHE_INVAL ) + ret = invalidate_dcache_va_range(v, cflush->length); + else if ( cflush->op & GNTTAB_CACHE_CLEAN ) + ret = clean_dcache_va_range(v, cflush->length); + + if ( d != owner ) + spin_unlock(&owner->grant_table->lock); + unmap_domain_page(v); + put_page(page); + + return ret; +} + +static long +gnttab_cache_flush(XEN_GUEST_HANDLE_PARAM(gnttab_cache_flush_t) uop, + unsigned int *ref_count, + unsigned int count) +{ + int ret = 0; + unsigned int i; + gnttab_cache_flush_t op; + + for ( i = 0; i < count; i++ ) + { + if ( i && hypercall_preempt_check() ) + return i; + if ( unlikely(__copy_from_guest(&op, uop, 1)) ) + return -EFAULT; + do { + ret = __gnttab_cache_flush(&op, ref_count); + if ( ret < 0 ) + return ret; + if ( ret > 0 && hypercall_preempt_check() ) + return i; + } while ( ret > 0 ); + *ref_count = 0; + guest_handle_add_offset(uop, 1); + } + return 0; +} + + long do_grant_table_op( unsigned int cmd, XEN_GUEST_HANDLE_PARAM(void) uop, unsigned int count) { long rc; + unsigned int opaque_in = 0, opaque_out = 0; if ( (int)count < 0 ) return -EINVAL; rc = -EFAULT; + + opaque_in = cmd & GNTTABOP_ARG_MASK; + cmd &= GNTTABOP_CMD_MASK; switch ( cmd ) { case GNTTABOP_map_grant_ref: @@ -2617,17 +2762,33 @@ do_grant_table_op( } break; } + case GNTTABOP_cache_flush: + { + XEN_GUEST_HANDLE_PARAM(gnttab_cache_flush_t) cflush = + guest_handle_cast(uop, gnttab_cache_flush_t); + if ( unlikely(!guest_handle_okay(cflush, count)) ) + goto out; + rc = gnttab_cache_flush(cflush, &opaque_in, count); + if ( rc > 0 ) + { + guest_handle_add_offset(cflush, rc); + uop = guest_handle_cast(cflush, void); + } + opaque_out = opaque_in; + break; + } default: rc = -ENOSYS; break; } out: - if ( rc > 0 ) + if ( rc > 0 || opaque_out != 0 ) { ASSERT(rc < count); - rc = hypercall_create_continuation(__HYPERVISOR_grant_table_op, - "ihi", cmd, uop, count - rc); + ASSERT((opaque_out & GNTTABOP_CMD_MASK) == 0); + rc = hypercall_create_continuation(__HYPERVISOR_grant_table_op, "ihi", + opaque_out | cmd, uop, count - rc); } return rc; diff --git a/xen/include/public/grant_table.h b/xen/include/public/grant_table.h index b8a3d6c..eb56a0e 100644 --- a/xen/include/public/grant_table.h +++ b/xen/include/public/grant_table.h @@ -309,6 +309,7 @@ typedef uint16_t grant_status_t; #define GNTTABOP_get_status_frames 9 #define GNTTABOP_get_version 10 #define GNTTABOP_swap_grant_ref 11 +#define GNTTABOP_cache_flush 12 #endif /* __XEN_INTERFACE_VERSION__ */ /* ` } */ @@ -574,6 +575,29 @@ struct gnttab_swap_grant_ref { typedef struct gnttab_swap_grant_ref gnttab_swap_grant_ref_t; DEFINE_XEN_GUEST_HANDLE(gnttab_swap_grant_ref_t); +/* + * Issue one or more cache maintenance operations on a portion of a + * page granted to the calling domain by a foreign domain. + */ +struct gnttab_cache_flush { + union { + /* dev_bus_addr must refer to a granted page and match a + * previously returned dev_bus_addr by a GNTTABOP hypercall. + * As a consequence dev_bus_addr has to be page aligned. + */ + uint64_t dev_bus_addr; + grant_ref_t ref; + } a; + uint16_t offset; /* offset from start of grant */ + uint16_t length; /* size within the grant */ +#define GNTTAB_CACHE_CLEAN (1<<0) +#define GNTTAB_CACHE_INVAL (1<<1) +#define GNTTAB_CACHE_SOURCE_GREF (1<<31) + uint32_t op; +}; +typedef struct gnttab_cache_flush gnttab_cache_flush_t; +DEFINE_XEN_GUEST_HANDLE(gnttab_cache_flush_t); + #endif /* __XEN_INTERFACE_VERSION__ */ /* diff --git a/xen/include/xlat.lst b/xen/include/xlat.lst index 234b668..9ce9fee 100644 --- a/xen/include/xlat.lst +++ b/xen/include/xlat.lst @@ -51,6 +51,7 @@ ? grant_entry_header grant_table.h ? grant_entry_v2 grant_table.h ? gnttab_swap_grant_ref grant_table.h +? gnttab_cache_flush grant_table.h ? kexec_exec kexec.h ! kexec_image kexec.h ! kexec_range kexec.h