From: "Jan Beulich" Subject: [PATCH v2 1/6] gnttab: eliminate several explicit version checks Date: Mon, 15 Jun 2015 13:24:39 +0100 Message-ID: <557EE0270200007800084CB4@mail.emea.novell.com> References: <557EDDDD0200007800084C94@mail.emea.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part90A4B617.1__=" Return-path: Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1Z4TRW-0001ou-7P for xen-devel@lists.xenproject.org; Mon, 15 Jun 2015 12:24:46 +0000 In-Reply-To: <557EDDDD0200007800084C94@mail.emea.novell.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: xen-devel Cc: Ian Campbell , Keir Fraser , Ian Jackson , Tim Deegan List-Id: xen-devel@lists.xenproject.org This is a MIME message. If you are reading this text, you may want to consider changing to a mail reader or gateway that understands how to properly handle MIME multipart messages. --=__Part90A4B617.1__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline By having nr_grant_entries() return zero when the grant table version is still unset we can reduce the number of error paths and at once fix grant_map_exists() running into the being removed ASSERT() when called for a page owned by a domain not having its grant table set up yet. Signed-off-by: Jan Beulich --- Said ASSERT() was pointless in the grant_map_exists() case, since the function only looks at active entries, which are version independent. Hence this wasn't a security issue. --- a/xen/common/grant_table.c +++ b/xen/common/grant_table.c @@ -344,11 +344,15 @@ get_maptrack_handle( /* Number of grant table entries. Caller must hold d's grant table lock. = */ static unsigned int nr_grant_entries(struct grant_table *gt) { - ASSERT(gt->gt_version !=3D 0); - if (gt->gt_version =3D=3D 1) + switch ( gt->gt_version ) + { + case 1: return (nr_grant_frames(gt) << PAGE_SHIFT) / sizeof(grant_entry_v1= _t); - else + case 2: return (nr_grant_frames(gt) << PAGE_SHIFT) / sizeof(grant_entry_v2= _t); + } + + return 0; } =20 static int _set_status_v1(domid_t domid, @@ -668,10 +672,6 @@ __gnttab_map_grant_ref( rgt =3D rd->grant_table; read_lock(&rgt->lock); =20 - if ( rgt->gt_version =3D=3D 0 ) - PIN_FAIL(unlock_out, GNTST_general_error, - "remote grant table not yet set up\n"); - /* Bounds check on the grant ref */ if ( unlikely(op->ref >=3D nr_grant_entries(rgt))) PIN_FAIL(unlock_out, GNTST_bad_gntref, "Bad ref (%d).\n", = op->ref); @@ -1570,14 +1570,6 @@ gnttab_prepare_for_transfer( =20 read_lock(&rgt->lock); =20 - if ( rgt->gt_version =3D=3D 0 ) - { - gdprintk(XENLOG_INFO, - "Grant table not ready for transfer to domain(%d).\n", - rd->domain_id); - goto fail; - } - if ( unlikely(ref >=3D nr_grant_entries(rgt)) ) { gdprintk(XENLOG_INFO, @@ -1977,10 +1969,6 @@ __acquire_grant_for_copy( =20 read_lock(&rgt->lock); =20 - if ( rgt->gt_version =3D=3D 0 ) - PIN_FAIL(gt_unlock_out, GNTST_general_error, - "remote grant table not ready\n"); - if ( unlikely(gref >=3D nr_grant_entries(rgt)) ) PIN_FAIL(gt_unlock_out, GNTST_bad_gntref, "Bad grant reference %ld\n", gref); @@ -2482,19 +2470,15 @@ gnttab_set_version(XEN_GUEST_HANDLE_PARA change the version number, except for the first 8 entries which are allowed to be in use (xenstore/xenconsole keeps them mapped). (You need to change the version number for e.g. kexec.) */ - if ( gt->gt_version !=3D 0 ) + for ( i =3D GNTTAB_NR_RESERVED_ENTRIES; i < nr_grant_entries(gt); i++ = ) { - for ( i =3D GNTTAB_NR_RESERVED_ENTRIES; i < nr_grant_entries(gt); = i++ ) + if ( read_atomic(&_active_entry(gt, i).pin) !=3D 0 ) { - if ( read_atomic(&_active_entry(gt, i).pin) !=3D 0 ) - { - gdprintk(XENLOG_WARNING, - "tried to change grant table version from %d to = %d, but some grant entries still in use\n", - gt->gt_version, - op.version); - res =3D -EBUSY; - goto out_unlock; - } + gdprintk(XENLOG_WARNING, + "tried to change grant table version from %d to %d, = but some grant entries still in use\n", + gt->gt_version, op.version); + res =3D -EBUSY; + goto out_unlock; } } =20 @@ -2679,9 +2663,6 @@ __gnttab_swap_grant_ref(grant_ref_t ref_ =20 write_lock(>->lock); =20 - if ( gt->gt_version =3D=3D 0 ) - PIN_FAIL(out, GNTST_general_error, "grant table not yet set = up\n"); - /* Bounds check on the grant refs */ if ( unlikely(ref_a >=3D nr_grant_entries(d->grant_table))) PIN_FAIL(out, GNTST_bad_gntref, "Bad ref-a (%d).\n", ref_a); @@ -3264,9 +3245,6 @@ static void gnttab_usage_print(struct do =20 read_lock(>->lock); =20 - if ( gt->gt_version =3D=3D 0 ) - goto out; - for ( ref =3D 0; ref !=3D nr_grant_entries(gt); ref++ ) { struct active_grant_entry *act; @@ -3314,7 +3292,6 @@ static void gnttab_usage_print(struct do active_entry_release(act); } =20 - out: read_unlock(>->lock); =20 if ( first ) --=__Part90A4B617.1__= Content-Type: text/plain; name="gnttab-nr-grant-entries-v0.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="gnttab-nr-grant-entries-v0.patch" gnttab: eliminate several explicit version checks=0A=0ABy having nr_grant_e= ntries() return zero when the grant table version=0Ais still unset we can = reduce the number of error paths and at once fix=0Agrant_map_exists() = running into the being removed ASSERT() when called=0Afor a page owned by = a domain not having its grant table set up yet.=0A=0ASigned-off-by: Jan = Beulich =0A---=0ASaid ASSERT() was pointless in the = grant_map_exists() case, since the=0Afunction only looks at active = entries, which are version independent.=0AHence this wasn't a security = issue.=0A=0A--- a/xen/common/grant_table.c=0A+++ b/xen/common/grant_table.c= =0A@@ -344,11 +344,15 @@ get_maptrack_handle(=0A /* Number of grant table = entries. Caller must hold d's grant table lock. */=0A static unsigned int = nr_grant_entries(struct grant_table *gt)=0A {=0A- ASSERT(gt->gt_version = !=3D 0);=0A- if (gt->gt_version =3D=3D 1)=0A+ switch ( gt->gt_version= )=0A+ {=0A+ case 1:=0A return (nr_grant_frames(gt) << = PAGE_SHIFT) / sizeof(grant_entry_v1_t);=0A- else=0A+ case 2:=0A = return (nr_grant_frames(gt) << PAGE_SHIFT) / sizeof(grant_entry_v2_t);= =0A+ }=0A+=0A+ return 0;=0A }=0A =0A static int _set_status_v1(domid_= t domid,=0A@@ -668,10 +672,6 @@ __gnttab_map_grant_ref(=0A rgt =3D = rd->grant_table;=0A read_lock(&rgt->lock);=0A =0A- if ( rgt->gt_vers= ion =3D=3D 0 )=0A- PIN_FAIL(unlock_out, GNTST_general_error,=0A- = "remote grant table not yet set up\n");=0A-=0A /* Bounds = check on the grant ref */=0A if ( unlikely(op->ref >=3D nr_grant_entrie= s(rgt)))=0A PIN_FAIL(unlock_out, GNTST_bad_gntref, "Bad ref = (%d).\n", op->ref);=0A@@ -1570,14 +1570,6 @@ gnttab_prepare_for_transfer(= =0A =0A read_lock(&rgt->lock);=0A =0A- if ( rgt->gt_version =3D=3D = 0 )=0A- {=0A- gdprintk(XENLOG_INFO,=0A- "Grant = table not ready for transfer to domain(%d).\n",=0A- = rd->domain_id);=0A- goto fail;=0A- }=0A-=0A if ( unlikely(ref= >=3D nr_grant_entries(rgt)) )=0A {=0A gdprintk(XENLOG_INFO,=0A= @@ -1977,10 +1969,6 @@ __acquire_grant_for_copy(=0A =0A read_lock(&rgt-= >lock);=0A =0A- if ( rgt->gt_version =3D=3D 0 )=0A- PIN_FAIL(gt_u= nlock_out, GNTST_general_error,=0A- "remote grant table = not ready\n");=0A-=0A if ( unlikely(gref >=3D nr_grant_entries(rgt)) = )=0A PIN_FAIL(gt_unlock_out, GNTST_bad_gntref,=0A = "Bad grant reference %ld\n", gref);=0A@@ -2482,19 +2470,15 @@ gnttab_set_ve= rsion(XEN_GUEST_HANDLE_PARA=0A change the version number, except = for the first 8 entries which=0A are allowed to be in use (xenstore/= xenconsole keeps them mapped).=0A (You need to change the version = number for e.g. kexec.) */=0A- if ( gt->gt_version !=3D 0 )=0A+ for = ( i =3D GNTTAB_NR_RESERVED_ENTRIES; i < nr_grant_entries(gt); i++ )=0A = {=0A- for ( i =3D GNTTAB_NR_RESERVED_ENTRIES; i < nr_grant_entries(g= t); i++ )=0A+ if ( read_atomic(&_active_entry(gt, i).pin) !=3D 0 = )=0A {=0A- if ( read_atomic(&_active_entry(gt, i).pin) = !=3D 0 )=0A- {=0A- gdprintk(XENLOG_WARNING,=0A- = "tried to change grant table version from %d to %d, = but some grant entries still in use\n",=0A- = gt->gt_version,=0A- op.version);=0A- = res =3D -EBUSY;=0A- goto out_unlock;=0A- }=0A+ = gdprintk(XENLOG_WARNING,=0A+ "tried to = change grant table version from %d to %d, but some grant entries still in = use\n",=0A+ gt->gt_version, op.version);=0A+ = res =3D -EBUSY;=0A+ goto out_unlock;=0A }=0A }=0A = =0A@@ -2679,9 +2663,6 @@ __gnttab_swap_grant_ref(grant_ref_t ref_=0A =0A = write_lock(>->lock);=0A =0A- if ( gt->gt_version =3D=3D 0 )=0A- = PIN_FAIL(out, GNTST_general_error, "grant table not yet set up\n");=0A-= =0A /* Bounds check on the grant refs */=0A if ( unlikely(ref_a = >=3D nr_grant_entries(d->grant_table)))=0A PIN_FAIL(out, GNTST_bad_= gntref, "Bad ref-a (%d).\n", ref_a);=0A@@ -3264,9 +3245,6 @@ static void = gnttab_usage_print(struct do=0A =0A read_lock(>->lock);=0A =0A- = if ( gt->gt_version =3D=3D 0 )=0A- goto out;=0A-=0A for ( ref = =3D 0; ref !=3D nr_grant_entries(gt); ref++ )=0A {=0A struct = active_grant_entry *act;=0A@@ -3314,7 +3292,6 @@ static void gnttab_usage_p= rint(struct do=0A active_entry_release(act);=0A }=0A =0A- = out:=0A read_unlock(>->lock);=0A =0A if ( first )=0A --=__Part90A4B617.1__= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xen.org http://lists.xen.org/xen-devel --=__Part90A4B617.1__=--