From: "Jan Beulich" Subject: [PATCH 1/6] gnttab: eliminate several explicit version checks Date: Fri, 12 Jun 2015 11:35:22 +0100 Message-ID: <557AD20A02000078000841DA@mail.emea.novell.com> References: <557AD03E02000078000841B4@mail.emea.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part083C2AFA.1__=" Return-path: Received: from mail6.bemta3.messagelabs.com ([195.245.230.39]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1Z3MJ3-0003bt-EU for xen-devel@lists.xenproject.org; Fri, 12 Jun 2015 10:35:25 +0000 In-Reply-To: <557AD03E02000078000841B4@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. --=__Part083C2AFA.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 @@ -319,11 +319,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, @@ -631,10 +635,6 @@ __gnttab_map_grant_ref( rgt =3D rd->grant_table; spin_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); @@ -1502,14 +1502,6 @@ gnttab_prepare_for_transfer( =20 spin_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, @@ -1905,10 +1897,6 @@ __acquire_grant_for_copy( =20 spin_lock(&rgt->lock); =20 - if ( rgt->gt_version =3D=3D 0 ) - PIN_FAIL(unlock_out, GNTST_general_error, - "remote grant table not ready\n"); - if ( unlikely(gref >=3D nr_grant_entries(rgt)) ) PIN_FAIL(unlock_out, GNTST_bad_gntref, "Bad grant reference %ld\n", gref); @@ -2395,20 +2383,16 @@ 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++ ) + act =3D &active_entry(gt, i); + if ( act->pin !=3D 0 ) { - act =3D &active_entry(gt, i); - if ( act->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 @@ -2592,9 +2576,6 @@ __gnttab_swap_grant_ref(grant_ref_t ref_ =20 spin_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); @@ -3165,9 +3146,6 @@ static void gnttab_usage_print(struct do =20 spin_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; @@ -3211,7 +3189,6 @@ static void gnttab_usage_print(struct do sha->domid, frame, status); } =20 - out: spin_unlock(>->lock); =20 if ( first ) --=__Part083C2AFA.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@@ -319,11 +319,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@@ -631,10 +635,6 @@ __gnttab_map_grant_ref(=0A rgt =3D = rd->grant_table;=0A spin_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@@ -1502,14 +1502,6 @@ gnttab_prepare_for_transfer(= =0A =0A spin_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= @@ -1905,10 +1897,6 @@ __acquire_grant_for_copy(=0A =0A spin_lock(&rgt-= >lock);=0A =0A- if ( rgt->gt_version =3D=3D 0 )=0A- PIN_FAIL(unlo= ck_out, GNTST_general_error,=0A- "remote grant table not = ready\n");=0A-=0A if ( unlikely(gref >=3D nr_grant_entries(rgt)) )=0A = PIN_FAIL(unlock_out, GNTST_bad_gntref,=0A "Bad = grant reference %ld\n", gref);=0A@@ -2395,20 +2383,16 @@ gnttab_set_version= (XEN_GUEST_HANDLE_PARA=0A change the version number, except for the = first 8 entries which=0A are allowed to be in use (xenstore/xenconso= le 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(gt); i++ = )=0A+ act =3D &active_entry(gt, i);=0A+ if ( act->pin !=3D 0 = )=0A {=0A- act =3D &active_entry(gt, i);=0A- = if ( act->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@@ -2592,9 +2576,6 @@ __gnttab_swap_grant_ref(grant_ref_t ref_=0A = =0A spin_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@@ -3165,9 = +3146,6 @@ static void gnttab_usage_print(struct do=0A =0A spin_lock(&g= t->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@@ -3211,7 +3189,6 @@ = static void gnttab_usage_print(struct do=0A sha->domid, = frame, status);=0A }=0A =0A- out:=0A spin_unlock(>->lock);=0A = =0A if ( first )=0A --=__Part083C2AFA.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 --=__Part083C2AFA.1__=--