From patchwork Fri Jun 21 09:36:34 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrew Cooper X-Patchwork-Id: 11009157 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 4CF62186E for ; Fri, 21 Jun 2019 09:38:11 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3C931289A6 for ; Fri, 21 Jun 2019 09:38:11 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 30E8B289E5; Fri, 21 Jun 2019 09:38:11 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id B1407289A6 for ; Fri, 21 Jun 2019 09:38:10 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1heFyR-0007aN-1j; Fri, 21 Jun 2019 09:36:47 +0000 Received: from us1-rack-dfw2.inumbo.com ([104.130.134.6]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1heFyP-0007a8-QH for xen-devel@lists.xenproject.org; Fri, 21 Jun 2019 09:36:45 +0000 X-Inumbo-ID: 14bfd50c-9408-11e9-8980-bc764e045a96 Received: from esa1.hc3370-68.iphmx.com (unknown [216.71.145.142]) by us1-rack-dfw2.inumbo.com (Halon) with ESMTPS id 14bfd50c-9408-11e9-8980-bc764e045a96; Fri, 21 Jun 2019 09:36:44 +0000 (UTC) Authentication-Results: esa1.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none; spf=None smtp.pra=andrew.cooper3@citrix.com; spf=Pass smtp.mailfrom=Andrew.Cooper3@citrix.com; spf=None smtp.helo=postmaster@mail.citrix.com Received-SPF: None (esa1.hc3370-68.iphmx.com: no sender authenticity information available from domain of andrew.cooper3@citrix.com) identity=pra; client-ip=162.221.158.21; receiver=esa1.hc3370-68.iphmx.com; envelope-from="Andrew.Cooper3@citrix.com"; x-sender="andrew.cooper3@citrix.com"; x-conformance=sidf_compatible Received-SPF: Pass (esa1.hc3370-68.iphmx.com: domain of Andrew.Cooper3@citrix.com designates 162.221.158.21 as permitted sender) identity=mailfrom; client-ip=162.221.158.21; receiver=esa1.hc3370-68.iphmx.com; envelope-from="Andrew.Cooper3@citrix.com"; x-sender="Andrew.Cooper3@citrix.com"; x-conformance=sidf_compatible; x-record-type="v=spf1"; x-record-text="v=spf1 ip4:209.167.231.154 ip4:178.63.86.133 ip4:195.66.111.40/30 ip4:85.115.9.32/28 ip4:199.102.83.4 ip4:192.28.146.160 ip4:192.28.146.107 ip4:216.52.6.88 ip4:216.52.6.188 ip4:162.221.158.21 ip4:162.221.156.83 ~all" Received-SPF: None (esa1.hc3370-68.iphmx.com: no sender authenticity information available from domain of postmaster@mail.citrix.com) identity=helo; client-ip=162.221.158.21; receiver=esa1.hc3370-68.iphmx.com; envelope-from="Andrew.Cooper3@citrix.com"; x-sender="postmaster@mail.citrix.com"; x-conformance=sidf_compatible IronPort-SDR: 7163l8iTHCmy0+N2/twP80czhKbNZzTecOXN7Uy7gbWgp7Ta/bop3HWbqYlnpJtu+rqSkAT1l2 MqJZbV5/IIAzDgtzync9wotBThkpWtQEkWl4G/xd22N6seHBiJ08LLZZ6ekiO7dBoHwPkb5oHo 6922zbEYnrHmy0SW6P9wbyK0XcNlW7NR2DqRKStZjvDK5s79uvOkHdNdN8n0GV1+RUHcktAIf5 5sAbU9zihGgparQTSt4UA9xZCCIG+JRQVcF1GCcfS0ww+AW8y1olBXrdv5yvg37TRw7zZF3Muh EkQ= X-SBRS: 2.7 X-MesageID: 2060658 X-Ironport-Server: esa1.hc3370-68.iphmx.com X-Remote-IP: 162.221.158.21 X-Policy: $RELAYED X-IronPort-AV: E=Sophos;i="5.63,399,1557201600"; d="scan'208";a="2060658" From: Andrew Cooper To: Xen-devel Date: Fri, 21 Jun 2019 10:36:34 +0100 Message-ID: <1561109798-8744-2-git-send-email-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1561109798-8744-1-git-send-email-andrew.cooper3@citrix.com> References: <1561109798-8744-1-git-send-email-andrew.cooper3@citrix.com> MIME-Version: 1.0 Subject: [Xen-devel] [PATCH 1/5] xen/gnttab: Reduce complexity when reading grant_entry_header_t X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Stefano Stabellini , Wei Liu , George Dunlap , Andrew Cooper , Julien Grall , Jan Beulich , =?utf-8?q?Roger_Pau_Monn=C3=A9?= Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP _set_status_v{1,2}() and gnttab_prepare_for_transfer() read the shared header by always casting to u32. Despite grant_entry_header_t only having an alignment of 2, this is actually safe because of the grant table ABI itself. Switch to using an explicit uint32_t *, which removes all subsequent casting. Furthermore, switch to using ACCESS_ONCE() for the read. There is nothing in the _set_status_v1() and gnttab_prepare_for_transfer() which prevents the compiler from issuing multiple memory reads and creating a TOCTOU race around the sanity checks, although the worst that can happen is Xen stamping a status flag over a bad grant entry if the guest is misbehaving. _set_status_v2() does use barrier() to try avoid multiple reads, but this is overkill. All that matters is that the shared header gets read in one go, and this allows the compiler more room to optimise. Signed-off-by: Andrew Cooper --- CC: Jan Beulich CC: Wei Liu CC: Roger Pau Monné CC: Stefano Stabellini CC: Julien Grall CC: George Dunlap Dropping the barrier() in _set_status_v2() allows the optimiser to notice that initialiser for flags/id are redundant memory reads. This is fixed in the following patch. --- xen/common/grant_table.c | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/xen/common/grant_table.c b/xen/common/grant_table.c index 2bbde5c..e5d585f 100644 --- a/xen/common/grant_table.c +++ b/xen/common/grant_table.c @@ -679,6 +679,7 @@ static int _set_status_v1(const grant_entry_header_t *shah, domid_t ldomid) { int rc = GNTST_okay; + uint32_t *raw_shah = (uint32_t *)shah; union grant_combo scombo, prev_scombo, new_scombo; uint16_t mask = GTF_type_mask; @@ -697,7 +698,7 @@ static int _set_status_v1(const grant_entry_header_t *shah, if ( mapflag ) mask |= GTF_sub_page; - scombo.word = *(u32 *)shah; + scombo.word = ACCESS_ONCE(*raw_shah); /* * This loop attempts to set the access (reading/writing) flags @@ -728,7 +729,7 @@ static int _set_status_v1(const grant_entry_header_t *shah, "Attempt to write-pin a r/o grant entry\n"); } - prev_scombo.word = guest_cmpxchg(rd, (u32 *)shah, + prev_scombo.word = guest_cmpxchg(rd, raw_shah, scombo.word, new_scombo.word); if ( likely(prev_scombo.word == scombo.word) ) break; @@ -753,17 +754,13 @@ static int _set_status_v2(const grant_entry_header_t *shah, domid_t ldomid) { int rc = GNTST_okay; + uint32_t *raw_shah = (uint32_t *)shah; union grant_combo scombo; uint16_t flags = shah->flags; domid_t id = shah->domid; uint16_t mask = GTF_type_mask; - /* we read flags and domid in a single memory access. - this avoids the need for another memory barrier to - ensure access to these fields are not reordered */ - scombo.word = *(u32 *)shah; - barrier(); /* but we still need to stop the compiler from turning - it back into two reads */ + scombo.word = ACCESS_ONCE(*raw_shah); flags = scombo.shorts.flags; id = scombo.shorts.domid; @@ -797,8 +794,7 @@ static int _set_status_v2(const grant_entry_header_t *shah, still valid */ smp_mb(); - scombo.word = *(u32 *)shah; - barrier(); + scombo.word = ACCESS_ONCE(*raw_shah); flags = scombo.shorts.flags; id = scombo.shorts.domid; @@ -2041,7 +2037,7 @@ gnttab_prepare_for_transfer( struct domain *rd, struct domain *ld, grant_ref_t ref) { struct grant_table *rgt = rd->grant_table; - grant_entry_header_t *sha; + uint32_t *raw_shah; union grant_combo scombo, prev_scombo, new_scombo; int retries = 0; @@ -2055,9 +2051,8 @@ gnttab_prepare_for_transfer( goto fail; } - sha = shared_entry_header(rgt, ref); - - scombo.word = *(u32 *)&sha->flags; + raw_shah = (uint32_t *)shared_entry_header(rgt, ref); + scombo.word = ACCESS_ONCE(*raw_shah); for ( ; ; ) { @@ -2074,7 +2069,7 @@ gnttab_prepare_for_transfer( new_scombo = scombo; new_scombo.shorts.flags |= GTF_transfer_committed; - prev_scombo.word = guest_cmpxchg(rd, (u32 *)&sha->flags, + prev_scombo.word = guest_cmpxchg(rd, raw_shah, scombo.word, new_scombo.word); if ( likely(prev_scombo.word == scombo.word) ) break; From patchwork Fri Jun 21 09:36:35 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrew Cooper X-Patchwork-Id: 11009161 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id B10681398 for ; Fri, 21 Jun 2019 09:38:25 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A1237289A6 for ; Fri, 21 Jun 2019 09:38:25 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 95155289E8; Fri, 21 Jun 2019 09:38:25 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id E3E70289A6 for ; Fri, 21 Jun 2019 09:38:24 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1heFyW-0007dr-Dq; Fri, 21 Jun 2019 09:36:52 +0000 Received: from us1-rack-dfw2.inumbo.com ([104.130.134.6]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1heFyV-0007d6-5M for xen-devel@lists.xenproject.org; Fri, 21 Jun 2019 09:36:51 +0000 X-Inumbo-ID: 179222cf-9408-11e9-8980-bc764e045a96 Received: from esa4.hc3370-68.iphmx.com (unknown [216.71.155.144]) by us1-rack-dfw2.inumbo.com (Halon) with ESMTPS id 179222cf-9408-11e9-8980-bc764e045a96; Fri, 21 Jun 2019 09:36:48 +0000 (UTC) Authentication-Results: esa4.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none; spf=None smtp.pra=andrew.cooper3@citrix.com; spf=Pass smtp.mailfrom=Andrew.Cooper3@citrix.com; spf=None smtp.helo=postmaster@mail.citrix.com Received-SPF: None (esa4.hc3370-68.iphmx.com: no sender authenticity information available from domain of andrew.cooper3@citrix.com) identity=pra; client-ip=162.221.158.21; receiver=esa4.hc3370-68.iphmx.com; envelope-from="Andrew.Cooper3@citrix.com"; x-sender="andrew.cooper3@citrix.com"; x-conformance=sidf_compatible Received-SPF: Pass (esa4.hc3370-68.iphmx.com: domain of Andrew.Cooper3@citrix.com designates 162.221.158.21 as permitted sender) identity=mailfrom; client-ip=162.221.158.21; receiver=esa4.hc3370-68.iphmx.com; envelope-from="Andrew.Cooper3@citrix.com"; x-sender="Andrew.Cooper3@citrix.com"; x-conformance=sidf_compatible; x-record-type="v=spf1"; x-record-text="v=spf1 ip4:209.167.231.154 ip4:178.63.86.133 ip4:195.66.111.40/30 ip4:85.115.9.32/28 ip4:199.102.83.4 ip4:192.28.146.160 ip4:192.28.146.107 ip4:216.52.6.88 ip4:216.52.6.188 ip4:162.221.158.21 ip4:162.221.156.83 ~all" Received-SPF: None (esa4.hc3370-68.iphmx.com: no sender authenticity information available from domain of postmaster@mail.citrix.com) identity=helo; client-ip=162.221.158.21; receiver=esa4.hc3370-68.iphmx.com; envelope-from="Andrew.Cooper3@citrix.com"; x-sender="postmaster@mail.citrix.com"; x-conformance=sidf_compatible IronPort-SDR: 3QsXQ6UzxZ/RsFpDjPTRe2PUn5cTdhX1EEzut9HBlL95JtYSfIkKhdGiuXC43NDldRLzDrz3FW Gye4FySDBB7WLH+FCc5E4rw78koryUxtV4jmr5i+JMFphnSVCiRK+oq7C4gn39rbfHcHNVVV9+ g+FWRIgQhDSnCFCXYoFqlGvoTxCrVSwFPz21TLrHOVIFX1UuOAMo4W1dmUUjIsqNGHD155386n bl1ayci2B6AyZszy+10iphFG0PGqXnUaWMZnsRAa0FxWnf5jQCEjR2fZeEpYCeUV1oo+aYU2IE hZ0= X-SBRS: 2.7 X-MesageID: 2072494 X-Ironport-Server: esa4.hc3370-68.iphmx.com X-Remote-IP: 162.221.158.21 X-Policy: $RELAYED X-IronPort-AV: E=Sophos;i="5.63,399,1557201600"; d="scan'208";a="2072494" From: Andrew Cooper To: Xen-devel Date: Fri, 21 Jun 2019 10:36:35 +0100 Message-ID: <1561109798-8744-3-git-send-email-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1561109798-8744-1-git-send-email-andrew.cooper3@citrix.com> References: <1561109798-8744-1-git-send-email-andrew.cooper3@citrix.com> MIME-Version: 1.0 Subject: [Xen-devel] [PATCH 2/5] xen/gnttab: Reduce code volume when using union grant_combo X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Stefano Stabellini , Wei Liu , George Dunlap , Andrew Cooper , Julien Grall , Jan Beulich , =?utf-8?q?Roger_Pau_Monn=C3=A9?= Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP There is no need for 'struct { ... } shorts' to be named. Convert it to being an anonymous struct, and rename 'word' to the more common 'raw'. For _set_status_v1() and gnttab_prepare_for_transfer() which use a bounded cmpxchg loop, rename {prev,new}_scombo to {prev,new} and reduce their scope to within the loop. For _set_status_v2(), the flags and id variables are completely unnecessary. Drop them. No functional change. Signed-off-by: Andrew Cooper --- CC: Jan Beulich CC: Wei Liu CC: Roger Pau Monné CC: Stefano Stabellini CC: Julien Grall CC: George Dunlap --- xen/common/grant_table.c | 91 ++++++++++++++++++++++-------------------------- 1 file changed, 41 insertions(+), 50 deletions(-) diff --git a/xen/common/grant_table.c b/xen/common/grant_table.c index e5d585f..6d8f17d 100644 --- a/xen/common/grant_table.c +++ b/xen/common/grant_table.c @@ -144,11 +144,11 @@ custom_param("gnttab", parse_gnttab); * The following union allows that to happen in an endian-neutral fashion. */ union grant_combo { - uint32_t word; + uint32_t raw; struct { uint16_t flags; domid_t domid; - } shorts; + }; }; /* Used to share code between unmap_grant_ref and unmap_and_replace. */ @@ -680,7 +680,7 @@ static int _set_status_v1(const grant_entry_header_t *shah, { int rc = GNTST_okay; uint32_t *raw_shah = (uint32_t *)shah; - union grant_combo scombo, prev_scombo, new_scombo; + union grant_combo scombo; uint16_t mask = GTF_type_mask; /* @@ -698,7 +698,7 @@ static int _set_status_v1(const grant_entry_header_t *shah, if ( mapflag ) mask |= GTF_sub_page; - scombo.word = ACCESS_ONCE(*raw_shah); + scombo.raw = ACCESS_ONCE(*raw_shah); /* * This loop attempts to set the access (reading/writing) flags @@ -708,37 +708,35 @@ static int _set_status_v1(const grant_entry_header_t *shah, */ for ( ; ; ) { + union grant_combo prev, new; + /* If not already pinned, check the grant domid and type. */ - if ( !act->pin && - (((scombo.shorts.flags & mask) != - GTF_permit_access) || - (scombo.shorts.domid != ldomid)) ) + if ( !act->pin && (((scombo.flags & mask) != GTF_permit_access) || + (scombo.domid != ldomid)) ) PIN_FAIL(done, GNTST_general_error, "Bad flags (%x) or dom (%d); expected d%d\n", - scombo.shorts.flags, scombo.shorts.domid, - ldomid); + scombo.flags, scombo.domid, ldomid); - new_scombo = scombo; - new_scombo.shorts.flags |= GTF_reading; + new = scombo; + new.flags |= GTF_reading; if ( !readonly ) { - new_scombo.shorts.flags |= GTF_writing; - if ( unlikely(scombo.shorts.flags & GTF_readonly) ) + new.flags |= GTF_writing; + if ( unlikely(scombo.flags & GTF_readonly) ) PIN_FAIL(done, GNTST_general_error, "Attempt to write-pin a r/o grant entry\n"); } - prev_scombo.word = guest_cmpxchg(rd, raw_shah, - scombo.word, new_scombo.word); - if ( likely(prev_scombo.word == scombo.word) ) + prev.raw = guest_cmpxchg(rd, raw_shah, scombo.raw, new.raw); + if ( likely(prev.raw == scombo.raw) ) break; if ( retries++ == 4 ) PIN_FAIL(done, GNTST_general_error, "Shared grant entry is unstable\n"); - scombo = prev_scombo; + scombo = prev; } done: @@ -756,13 +754,9 @@ static int _set_status_v2(const grant_entry_header_t *shah, int rc = GNTST_okay; uint32_t *raw_shah = (uint32_t *)shah; union grant_combo scombo; - uint16_t flags = shah->flags; - domid_t id = shah->domid; uint16_t mask = GTF_type_mask; - scombo.word = ACCESS_ONCE(*raw_shah); - flags = scombo.shorts.flags; - id = scombo.shorts.domid; + scombo.raw = ACCESS_ONCE(*raw_shah); /* if this is a grant mapping operation we should ensure GTF_sub_page is not set */ @@ -770,13 +764,12 @@ static int _set_status_v2(const grant_entry_header_t *shah, mask |= GTF_sub_page; /* If not already pinned, check the grant domid and type. */ - if ( !act->pin && - ( (((flags & mask) != GTF_permit_access) && - ((flags & mask) != GTF_transitive)) || - (id != ldomid)) ) + if ( !act->pin && ((((scombo.flags & mask) != GTF_permit_access) && + ((scombo.flags & mask) != GTF_transitive)) || + (scombo.domid != ldomid)) ) PIN_FAIL(done, GNTST_general_error, "Bad flags (%x) or dom (%d); expected d%d, flags %x\n", - flags, id, ldomid, mask); + scombo.flags, scombo.domid, ldomid, mask); if ( readonly ) { @@ -784,7 +777,7 @@ static int _set_status_v2(const grant_entry_header_t *shah, } else { - if ( unlikely(flags & GTF_readonly) ) + if ( unlikely(scombo.flags & GTF_readonly) ) PIN_FAIL(done, GNTST_general_error, "Attempt to write-pin a r/o grant entry\n"); *status |= GTF_reading | GTF_writing; @@ -794,27 +787,25 @@ static int _set_status_v2(const grant_entry_header_t *shah, still valid */ smp_mb(); - scombo.word = ACCESS_ONCE(*raw_shah); - flags = scombo.shorts.flags; - id = scombo.shorts.domid; + scombo.raw = ACCESS_ONCE(*raw_shah); if ( !act->pin ) { - if ( (((flags & mask) != GTF_permit_access) && - ((flags & mask) != GTF_transitive)) || - (id != ldomid) || - (!readonly && (flags & GTF_readonly)) ) + if ( (((scombo.flags & mask) != GTF_permit_access) && + ((scombo.flags & mask) != GTF_transitive)) || + (scombo.domid != ldomid) || + (!readonly && (scombo.flags & GTF_readonly)) ) { gnttab_clear_flag(rd, _GTF_writing, status); gnttab_clear_flag(rd, _GTF_reading, status); PIN_FAIL(done, GNTST_general_error, "Unstable flags (%x) or dom (%d); expected d%d (r/w: %d)\n", - flags, id, ldomid, !readonly); + scombo.flags, scombo.domid, ldomid, !readonly); } } else { - if ( unlikely(flags & GTF_readonly) ) + if ( unlikely(scombo.flags & GTF_readonly) ) { gnttab_clear_flag(rd, _GTF_writing, status); PIN_FAIL(done, GNTST_general_error, @@ -2038,7 +2029,7 @@ gnttab_prepare_for_transfer( { struct grant_table *rgt = rd->grant_table; uint32_t *raw_shah; - union grant_combo scombo, prev_scombo, new_scombo; + union grant_combo scombo; int retries = 0; grant_read_lock(rgt); @@ -2052,26 +2043,26 @@ gnttab_prepare_for_transfer( } raw_shah = (uint32_t *)shared_entry_header(rgt, ref); - scombo.word = ACCESS_ONCE(*raw_shah); + scombo.raw = ACCESS_ONCE(*raw_shah); for ( ; ; ) { - if ( unlikely(scombo.shorts.flags != GTF_accept_transfer) || - unlikely(scombo.shorts.domid != ld->domain_id) ) + union grant_combo prev, new; + + if ( unlikely(scombo.flags != GTF_accept_transfer) || + unlikely(scombo.domid != ld->domain_id) ) { gdprintk(XENLOG_INFO, "Bad flags (%x) or dom (%d); expected d%d\n", - scombo.shorts.flags, scombo.shorts.domid, - ld->domain_id); + scombo.flags, scombo.domid, ld->domain_id); goto fail; } - new_scombo = scombo; - new_scombo.shorts.flags |= GTF_transfer_committed; + new = scombo; + new.flags |= GTF_transfer_committed; - prev_scombo.word = guest_cmpxchg(rd, raw_shah, - scombo.word, new_scombo.word); - if ( likely(prev_scombo.word == scombo.word) ) + prev.raw = guest_cmpxchg(rd, raw_shah, scombo.raw, new.raw); + if ( likely(prev.raw == scombo.raw) ) break; if ( retries++ == 4 ) @@ -2080,7 +2071,7 @@ gnttab_prepare_for_transfer( goto fail; } - scombo = prev_scombo; + scombo = prev; } grant_read_unlock(rgt); From patchwork Fri Jun 21 09:36:36 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Cooper X-Patchwork-Id: 11009165 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D8FFB1580 for ; Fri, 21 Jun 2019 09:38:32 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C665C289A6 for ; Fri, 21 Jun 2019 09:38:32 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id BACA3289EC; Fri, 21 Jun 2019 09:38:32 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 59B58289A6 for ; Fri, 21 Jun 2019 09:38:32 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1heFyR-0007aY-BH; Fri, 21 Jun 2019 09:36:47 +0000 Received: from us1-rack-dfw2.inumbo.com ([104.130.134.6]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1heFyP-0007a9-QI for xen-devel@lists.xenproject.org; Fri, 21 Jun 2019 09:36:45 +0000 X-Inumbo-ID: 152d0e33-9408-11e9-8980-bc764e045a96 Received: from esa1.hc3370-68.iphmx.com (unknown [216.71.145.142]) by us1-rack-dfw2.inumbo.com (Halon) with ESMTPS id 152d0e33-9408-11e9-8980-bc764e045a96; Fri, 21 Jun 2019 09:36:44 +0000 (UTC) Authentication-Results: esa1.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none; spf=None smtp.pra=andrew.cooper3@citrix.com; spf=Pass smtp.mailfrom=Andrew.Cooper3@citrix.com; spf=None smtp.helo=postmaster@mail.citrix.com Received-SPF: None (esa1.hc3370-68.iphmx.com: no sender authenticity information available from domain of andrew.cooper3@citrix.com) identity=pra; client-ip=162.221.158.21; receiver=esa1.hc3370-68.iphmx.com; envelope-from="Andrew.Cooper3@citrix.com"; x-sender="andrew.cooper3@citrix.com"; x-conformance=sidf_compatible Received-SPF: Pass (esa1.hc3370-68.iphmx.com: domain of Andrew.Cooper3@citrix.com designates 162.221.158.21 as permitted sender) identity=mailfrom; client-ip=162.221.158.21; receiver=esa1.hc3370-68.iphmx.com; envelope-from="Andrew.Cooper3@citrix.com"; x-sender="Andrew.Cooper3@citrix.com"; x-conformance=sidf_compatible; x-record-type="v=spf1"; x-record-text="v=spf1 ip4:209.167.231.154 ip4:178.63.86.133 ip4:195.66.111.40/30 ip4:85.115.9.32/28 ip4:199.102.83.4 ip4:192.28.146.160 ip4:192.28.146.107 ip4:216.52.6.88 ip4:216.52.6.188 ip4:162.221.158.21 ip4:162.221.156.83 ~all" Received-SPF: None (esa1.hc3370-68.iphmx.com: no sender authenticity information available from domain of postmaster@mail.citrix.com) identity=helo; client-ip=162.221.158.21; receiver=esa1.hc3370-68.iphmx.com; envelope-from="Andrew.Cooper3@citrix.com"; x-sender="postmaster@mail.citrix.com"; x-conformance=sidf_compatible IronPort-SDR: pJBqWnN1+Zomj8YcpNMobkrhTT26oNYxj6+fwFmgCGNca26V+TRBk9BNwD0Xq1upyfT3pZwqct H3vNTM+CbqEwOThyqzFgaPr3OMnNSd7g6ihEd8KYfEE9cvCYfMu2joQD8Zy+sI5BW5X4LDwccd YZWrpreQ6NyWJp8GiZnR+eH/nymFelEKr8tAjaaj0LXwahK17ASE4jwSWpxnqRa0EciIo6aSVE HywicuF133pwCzD2RXbUo6ljFkb6Zq3fuXJwgr5iOigqQx6cUOGDUmnVXpRl9xQNCaarbPdnZ2 5ts= X-SBRS: 2.7 X-MesageID: 2060661 X-Ironport-Server: esa1.hc3370-68.iphmx.com X-Remote-IP: 162.221.158.21 X-Policy: $RELAYED X-IronPort-AV: E=Sophos;i="5.63,399,1557201600"; d="scan'208";a="2060661" From: Andrew Cooper To: Xen-devel Date: Fri, 21 Jun 2019 10:36:36 +0100 Message-ID: <1561109798-8744-4-git-send-email-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1561109798-8744-1-git-send-email-andrew.cooper3@citrix.com> References: <1561109798-8744-1-git-send-email-andrew.cooper3@citrix.com> MIME-Version: 1.0 Subject: [Xen-devel] [PATCH 3/5] arm/gnttab: Implement stub helpers as static inlines X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Andrew Cooper , Julien Grall , Stefano Stabellini Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP It is inefficient to call into a different translation unit for a stub function, when a static inline will work fine. Replace an open-coded printk_once() while moving it. No functional change. Signed-off-by: Andrew Cooper Acked-by: Julien Grall --- CC: Stefano Stabellini CC: Julien Grall --- xen/arch/arm/mm.c | 16 ---------------- xen/include/asm-arm/grant_table.h | 17 +++++++++++++++-- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/xen/arch/arm/mm.c b/xen/arch/arm/mm.c index 35dc1f7..44258ad 100644 --- a/xen/arch/arm/mm.c +++ b/xen/arch/arm/mm.c @@ -41,7 +41,6 @@ #include #include -#include #include /* Override macros from asm/page.h to make them work with mfn_t */ @@ -1532,21 +1531,6 @@ void put_page_type(struct page_info *page) return; } -void gnttab_clear_flag(struct domain *d, unsigned long nr, uint16_t *addr) -{ - guest_clear_mask16(d, BIT(nr, UL), addr); -} - -void gnttab_mark_dirty(struct domain *d, mfn_t mfn) -{ - /* XXX: mark dirty */ - static int warning; - if (!warning) { - gdprintk(XENLOG_WARNING, "gnttab_mark_dirty not implemented yet\n"); - warning = 1; - } -} - int create_grant_host_mapping(unsigned long addr, mfn_t frame, unsigned int flags, unsigned int cache_flags) { diff --git a/xen/include/asm-arm/grant_table.h b/xen/include/asm-arm/grant_table.h index 1ed0aef..b0d673b 100644 --- a/xen/include/asm-arm/grant_table.h +++ b/xen/include/asm-arm/grant_table.h @@ -6,6 +6,8 @@ #include #include +#include + #define INITIAL_NR_GRANT_FRAMES 1U #define GNTTAB_MAX_VERSION 1 @@ -14,13 +16,24 @@ struct grant_table_arch { gfn_t *status_gfn; }; -void gnttab_clear_flag(struct domain *d, unsigned long nr, uint16_t *addr); +static inline void gnttab_clear_flag(struct domain *d, + unsigned long nr, uint16_t *addr) +{ + guest_clear_mask16(d, BIT(nr, UL), addr); +} + +static inline void gnttab_mark_dirty(struct domain *d, mfn_t mfn) +{ +#ifndef NDEBUG + printk_once(XENLOG_G_WARNING "gnttab_mark_dirty not implemented yet\n"); +#endif +} + int create_grant_host_mapping(unsigned long gpaddr, mfn_t mfn, unsigned int flags, unsigned int cache_flags); #define gnttab_host_mapping_get_page_type(ro, ld, rd) (0) int replace_grant_host_mapping(unsigned long gpaddr, mfn_t mfn, unsigned long new_gpaddr, unsigned int flags); -void gnttab_mark_dirty(struct domain *d, mfn_t mfn); #define gnttab_release_host_mappings(domain) 1 /* From patchwork Fri Jun 21 09:36:37 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrew Cooper X-Patchwork-Id: 11009155 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id A84281580 for ; Fri, 21 Jun 2019 09:38:10 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 978C1289A6 for ; Fri, 21 Jun 2019 09:38:10 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8BC21289E5; Fri, 21 Jun 2019 09:38:10 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id E6FC5289A6 for ; Fri, 21 Jun 2019 09:38:09 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1heFyR-0007au-QH; Fri, 21 Jun 2019 09:36:47 +0000 Received: from us1-rack-dfw2.inumbo.com ([104.130.134.6]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1heFyR-0007aP-4k for xen-devel@lists.xenproject.org; Fri, 21 Jun 2019 09:36:47 +0000 X-Inumbo-ID: 158a9716-9408-11e9-8980-bc764e045a96 Received: from esa1.hc3370-68.iphmx.com (unknown [216.71.145.142]) by us1-rack-dfw2.inumbo.com (Halon) with ESMTPS id 158a9716-9408-11e9-8980-bc764e045a96; Fri, 21 Jun 2019 09:36:45 +0000 (UTC) Authentication-Results: esa1.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none; spf=None smtp.pra=andrew.cooper3@citrix.com; spf=Pass smtp.mailfrom=Andrew.Cooper3@citrix.com; spf=None smtp.helo=postmaster@mail.citrix.com Received-SPF: None (esa1.hc3370-68.iphmx.com: no sender authenticity information available from domain of andrew.cooper3@citrix.com) identity=pra; client-ip=162.221.158.21; receiver=esa1.hc3370-68.iphmx.com; envelope-from="Andrew.Cooper3@citrix.com"; x-sender="andrew.cooper3@citrix.com"; x-conformance=sidf_compatible Received-SPF: Pass (esa1.hc3370-68.iphmx.com: domain of Andrew.Cooper3@citrix.com designates 162.221.158.21 as permitted sender) identity=mailfrom; client-ip=162.221.158.21; receiver=esa1.hc3370-68.iphmx.com; envelope-from="Andrew.Cooper3@citrix.com"; x-sender="Andrew.Cooper3@citrix.com"; x-conformance=sidf_compatible; x-record-type="v=spf1"; x-record-text="v=spf1 ip4:209.167.231.154 ip4:178.63.86.133 ip4:195.66.111.40/30 ip4:85.115.9.32/28 ip4:199.102.83.4 ip4:192.28.146.160 ip4:192.28.146.107 ip4:216.52.6.88 ip4:216.52.6.188 ip4:162.221.158.21 ip4:162.221.156.83 ~all" Received-SPF: None (esa1.hc3370-68.iphmx.com: no sender authenticity information available from domain of postmaster@mail.citrix.com) identity=helo; client-ip=162.221.158.21; receiver=esa1.hc3370-68.iphmx.com; envelope-from="Andrew.Cooper3@citrix.com"; x-sender="postmaster@mail.citrix.com"; x-conformance=sidf_compatible IronPort-SDR: yNk+spgbSIUyPpMJ3Bv1VgpP3fQ1ysjF1u6loCb+D51o+6dAli79oJ8rdBfcLU/N3DNIxNjXSf Z93NL2Suv9Cuw6Ki8/jMGap1R78DxxuSXutlX6LcJ/0eaSlg74oMUc0ZiQPRAW+NMAr8OoL7oi 60KhuxFu2YjQRXzwBqb8FIiXzDT0hSF+S1MFzE10wmhJaZ0tdBhgD+eVLMy3jChqgVAmAQDRbD Uvfc69iEqG5civg9U6DxobS1gpV2ZHgsNfBvPsH326+xW2cLkLsfryQpuGH1Jw41AVyLrC1rUP Oiw= X-SBRS: 2.7 X-MesageID: 2060662 X-Ironport-Server: esa1.hc3370-68.iphmx.com X-Remote-IP: 162.221.158.21 X-Policy: $RELAYED X-IronPort-AV: E=Sophos;i="5.63,399,1557201600"; d="scan'208";a="2060662" From: Andrew Cooper To: Xen-devel Date: Fri, 21 Jun 2019 10:36:37 +0100 Message-ID: <1561109798-8744-5-git-send-email-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1561109798-8744-1-git-send-email-andrew.cooper3@citrix.com> References: <1561109798-8744-1-git-send-email-andrew.cooper3@citrix.com> MIME-Version: 1.0 Subject: [Xen-devel] [PATCH 4/5] xen/gnttab: Refactor gnttab_clear_flag() to be gnttab_clear_flags() X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Stefano Stabellini , Wei Liu , George Dunlap , Andrew Cooper , Julien Grall , Jan Beulich , =?utf-8?q?Roger_Pau_Monn=C3=A9?= Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP To allow for further improvements, it is useful to be able to clear more than a single flag at once. Rework gnttab_clear_flag() into gnttab_clear_flags() which takes a bitmask rather than a bit number. No practical change yet. Signed-off-by: Andrew Cooper --- CC: Jan Beulich CC: Wei Liu CC: Roger Pau Monné CC: Stefano Stabellini CC: Julien Grall CC: George Dunlap --- xen/common/grant_table.c | 30 +++++++++++++++--------------- xen/include/asm-arm/grant_table.h | 6 +++--- xen/include/asm-x86/grant_table.h | 11 ++++------- 3 files changed, 22 insertions(+), 25 deletions(-) diff --git a/xen/common/grant_table.c b/xen/common/grant_table.c index 6d8f17d..4bd5777 100644 --- a/xen/common/grant_table.c +++ b/xen/common/grant_table.c @@ -796,8 +796,8 @@ static int _set_status_v2(const grant_entry_header_t *shah, (scombo.domid != ldomid) || (!readonly && (scombo.flags & GTF_readonly)) ) { - gnttab_clear_flag(rd, _GTF_writing, status); - gnttab_clear_flag(rd, _GTF_reading, status); + gnttab_clear_flags(rd, GTF_writing, status); + gnttab_clear_flags(rd, GTF_reading, status); PIN_FAIL(done, GNTST_general_error, "Unstable flags (%x) or dom (%d); expected d%d (r/w: %d)\n", scombo.flags, scombo.domid, ldomid, !readonly); @@ -807,7 +807,7 @@ static int _set_status_v2(const grant_entry_header_t *shah, { if ( unlikely(scombo.flags & GTF_readonly) ) { - gnttab_clear_flag(rd, _GTF_writing, status); + gnttab_clear_flags(rd, GTF_writing, status); PIN_FAIL(done, GNTST_general_error, "Unstable grant readonly flag\n"); } @@ -1220,10 +1220,10 @@ map_grant_ref( unlock_out_clear: if ( !(op->flags & GNTMAP_readonly) && !(act->pin & (GNTPIN_hstw_mask|GNTPIN_devw_mask)) ) - gnttab_clear_flag(rd, _GTF_writing, status); + gnttab_clear_flags(rd, GTF_writing, status); if ( !act->pin ) - gnttab_clear_flag(rd, _GTF_reading, status); + gnttab_clear_flags(rd, GTF_reading, status); act_release_out: active_entry_release(act); @@ -1493,10 +1493,10 @@ unmap_common_complete(struct gnttab_unmap_common *op) if ( ((act->pin & (GNTPIN_devw_mask|GNTPIN_hstw_mask)) == 0) && !(op->done & GNTMAP_readonly) ) - gnttab_clear_flag(rd, _GTF_writing, status); + gnttab_clear_flags(rd, GTF_writing, status); if ( act->pin == 0 ) - gnttab_clear_flag(rd, _GTF_reading, status); + gnttab_clear_flags(rd, GTF_reading, status); active_entry_release(act); grant_read_unlock(rgt); @@ -2354,11 +2354,11 @@ release_grant_for_copy( act->pin -= GNTPIN_hstw_inc; if ( !(act->pin & (GNTPIN_devw_mask|GNTPIN_hstw_mask)) ) - gnttab_clear_flag(rd, _GTF_writing, status); + gnttab_clear_flags(rd, GTF_writing, status); } if ( !act->pin ) - gnttab_clear_flag(rd, _GTF_reading, status); + gnttab_clear_flags(rd, GTF_reading, status); active_entry_release(act); grant_read_unlock(rgt); @@ -2385,10 +2385,10 @@ static void fixup_status_for_copy_pin(struct domain *rd, uint16_t *status) { if ( !(act->pin & (GNTPIN_hstw_mask | GNTPIN_devw_mask)) ) - gnttab_clear_flag(rd, _GTF_writing, status); + gnttab_clear_flags(rd, GTF_writing, status); if ( !act->pin ) - gnttab_clear_flag(rd, _GTF_reading, status); + gnttab_clear_flags(rd, GTF_reading, status); } /* @@ -2639,10 +2639,10 @@ acquire_grant_for_copy( unlock_out_clear: if ( !(readonly) && !(act->pin & (GNTPIN_hstw_mask | GNTPIN_devw_mask)) ) - gnttab_clear_flag(rd, _GTF_writing, status); + gnttab_clear_flags(rd, GTF_writing, status); if ( !act->pin ) - gnttab_clear_flag(rd, _GTF_reading, status); + gnttab_clear_flags(rd, GTF_reading, status); unlock_out: active_entry_release(act); @@ -3677,11 +3677,11 @@ gnttab_release_mappings( } if ( (act->pin & (GNTPIN_devw_mask|GNTPIN_hstw_mask)) == 0 ) - gnttab_clear_flag(rd, _GTF_writing, status); + gnttab_clear_flags(rd, GTF_writing, status); } if ( act->pin == 0 ) - gnttab_clear_flag(rd, _GTF_reading, status); + gnttab_clear_flags(rd, GTF_reading, status); active_entry_release(act); grant_read_unlock(rgt); diff --git a/xen/include/asm-arm/grant_table.h b/xen/include/asm-arm/grant_table.h index b0d673b..8ccad69 100644 --- a/xen/include/asm-arm/grant_table.h +++ b/xen/include/asm-arm/grant_table.h @@ -16,10 +16,10 @@ struct grant_table_arch { gfn_t *status_gfn; }; -static inline void gnttab_clear_flag(struct domain *d, - unsigned long nr, uint16_t *addr) +static inline void gnttab_clear_flags(struct domain *d, + uint16_t mask, uint16_t *addr) { - guest_clear_mask16(d, BIT(nr, UL), addr); + guest_clear_mask16(d, mask, addr); } static inline void gnttab_mark_dirty(struct domain *d, mfn_t mfn) diff --git a/xen/include/asm-x86/grant_table.h b/xen/include/asm-x86/grant_table.h index 121b33d..4691258 100644 --- a/xen/include/asm-x86/grant_table.h +++ b/xen/include/asm-x86/grant_table.h @@ -60,14 +60,11 @@ static inline int replace_grant_host_mapping(uint64_t addr, mfn_t frame, #define gnttab_mark_dirty(d, f) paging_mark_dirty(d, f) -static inline void gnttab_clear_flag(struct domain *d, unsigned int nr, - uint16_t *st) +static inline void gnttab_clear_flags(struct domain *d, + uint16_t mask, uint16_t *addr) { - /* - * Note that this cannot be clear_bit(), as the access must be - * confined to the specified 2 bytes. - */ - asm volatile ("lock btrw %w1,%0" : "+m" (*st) : "Ir" (nr)); + /* Access must be confined to the specified 2 bytes. */ + asm volatile ("lock andw %w1,%0" : "+m" (*addr) : "Ir" (~mask)); } /* Foreign mappings of HVM-guest pages do not modify the type count. */ From patchwork Fri Jun 21 09:36:38 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrew Cooper X-Patchwork-Id: 11009169 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id CC65C1580 for ; Fri, 21 Jun 2019 09:38:33 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BB488289A6 for ; Fri, 21 Jun 2019 09:38:33 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id AF5E7289E8; Fri, 21 Jun 2019 09:38:33 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 3282C289A6 for ; Fri, 21 Jun 2019 09:38:33 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1heFyU-0007cH-4b; Fri, 21 Jun 2019 09:36:50 +0000 Received: from us1-rack-dfw2.inumbo.com ([104.130.134.6]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1heFyS-0007bG-Ei for xen-devel@lists.xenproject.org; Fri, 21 Jun 2019 09:36:48 +0000 X-Inumbo-ID: 160e3fd5-9408-11e9-8980-bc764e045a96 Received: from esa2.hc3370-68.iphmx.com (unknown [216.71.145.153]) by us1-rack-dfw2.inumbo.com (Halon) with ESMTPS id 160e3fd5-9408-11e9-8980-bc764e045a96; Fri, 21 Jun 2019 09:36:46 +0000 (UTC) Authentication-Results: esa2.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none; spf=None smtp.pra=andrew.cooper3@citrix.com; spf=Pass smtp.mailfrom=Andrew.Cooper3@citrix.com; spf=None smtp.helo=postmaster@mail.citrix.com Received-SPF: None (esa2.hc3370-68.iphmx.com: no sender authenticity information available from domain of andrew.cooper3@citrix.com) identity=pra; client-ip=162.221.158.21; receiver=esa2.hc3370-68.iphmx.com; envelope-from="Andrew.Cooper3@citrix.com"; x-sender="andrew.cooper3@citrix.com"; x-conformance=sidf_compatible Received-SPF: Pass (esa2.hc3370-68.iphmx.com: domain of Andrew.Cooper3@citrix.com designates 162.221.158.21 as permitted sender) identity=mailfrom; client-ip=162.221.158.21; receiver=esa2.hc3370-68.iphmx.com; envelope-from="Andrew.Cooper3@citrix.com"; x-sender="Andrew.Cooper3@citrix.com"; x-conformance=sidf_compatible; x-record-type="v=spf1"; x-record-text="v=spf1 ip4:209.167.231.154 ip4:178.63.86.133 ip4:195.66.111.40/30 ip4:85.115.9.32/28 ip4:199.102.83.4 ip4:192.28.146.160 ip4:192.28.146.107 ip4:216.52.6.88 ip4:216.52.6.188 ip4:162.221.158.21 ip4:162.221.156.83 ~all" Received-SPF: None (esa2.hc3370-68.iphmx.com: no sender authenticity information available from domain of postmaster@mail.citrix.com) identity=helo; client-ip=162.221.158.21; receiver=esa2.hc3370-68.iphmx.com; envelope-from="Andrew.Cooper3@citrix.com"; x-sender="postmaster@mail.citrix.com"; x-conformance=sidf_compatible IronPort-SDR: N9ySbi1gz7TGtob0AL3JnQ+p1SMNIMgK6tKK9ujZ06jhgI699k6O367Pb1etQml4CaA9rgUikM FN1spb/pEixIAYwIWH5lRrwloavQAh3TpOBc9B6tM020QZBY8aX0hFfxBzJKxD1vCfkqIQj2GV ABhORl/M1dTAs6oopIWZEtSYz/J4zq+jtQNynFi3skAji/sO92iSdUhiVl2aboNRCufrWXz//r G1B9ncaz+XY1a5G0G7uqudF65OD0pXqymgBhOoCqz4Bul5jVrTSqvTt5Rmx1nI4S1zlKG2D/n3 1yg= X-SBRS: 2.7 X-MesageID: 2039644 X-Ironport-Server: esa2.hc3370-68.iphmx.com X-Remote-IP: 162.221.158.21 X-Policy: $RELAYED X-IronPort-AV: E=Sophos;i="5.63,399,1557201600"; d="scan'208";a="2039644" From: Andrew Cooper To: Xen-devel Date: Fri, 21 Jun 2019 10:36:38 +0100 Message-ID: <1561109798-8744-6-git-send-email-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1561109798-8744-1-git-send-email-andrew.cooper3@citrix.com> References: <1561109798-8744-1-git-send-email-andrew.cooper3@citrix.com> MIME-Version: 1.0 Subject: [Xen-devel] [PATCH 5/5] xen/gnttab: Fold adjacent calls to gnttab_clear_flags() X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Stefano Stabellini , Wei Liu , George Dunlap , Andrew Cooper , Julien Grall , Jan Beulich , =?utf-8?q?Roger_Pau_Monn=C3=A9?= Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP Atomic operations are expensive to use, especially following XSA-295 for ARM. It is wasteful to use two of them back-to-back when one will do. Especially for a misbehaving guest on ARM, this will reduce the system disruption required to complete the grant operations. Signed-off-by: Andrew Cooper --- CC: Jan Beulich CC: Wei Liu CC: Roger Pau Monné CC: Stefano Stabellini CC: Julien Grall CC: George Dunlap --- xen/common/grant_table.c | 54 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 39 insertions(+), 15 deletions(-) diff --git a/xen/common/grant_table.c b/xen/common/grant_table.c index 4bd5777..e6a0f30 100644 --- a/xen/common/grant_table.c +++ b/xen/common/grant_table.c @@ -796,8 +796,7 @@ static int _set_status_v2(const grant_entry_header_t *shah, (scombo.domid != ldomid) || (!readonly && (scombo.flags & GTF_readonly)) ) { - gnttab_clear_flags(rd, GTF_writing, status); - gnttab_clear_flags(rd, GTF_reading, status); + gnttab_clear_flags(rd, GTF_writing | GTF_reading, status); PIN_FAIL(done, GNTST_general_error, "Unstable flags (%x) or dom (%d); expected d%d (r/w: %d)\n", scombo.flags, scombo.domid, ldomid, !readonly); @@ -919,7 +918,7 @@ map_grant_ref( int rc = GNTST_okay; u32 old_pin; u32 act_pin; - unsigned int cache_flags, refcnt = 0, typecnt = 0; + unsigned int cache_flags, clear_flags = 0, refcnt = 0, typecnt = 0; bool host_map_created = false; struct active_grant_entry *act = NULL; struct grant_mapping *mt; @@ -1220,10 +1219,13 @@ map_grant_ref( unlock_out_clear: if ( !(op->flags & GNTMAP_readonly) && !(act->pin & (GNTPIN_hstw_mask|GNTPIN_devw_mask)) ) - gnttab_clear_flags(rd, GTF_writing, status); + clear_flags |= GTF_writing; if ( !act->pin ) - gnttab_clear_flags(rd, GTF_reading, status); + clear_flags |= GTF_reading; + + if ( clear_flags ) + gnttab_clear_flags(rd, clear_flags, status); act_release_out: active_entry_release(act); @@ -1433,6 +1435,7 @@ unmap_common_complete(struct gnttab_unmap_common *op) grant_entry_header_t *sha; struct page_info *pg; uint16_t *status; + unsigned int clear_flags = 0; if ( !op->done ) { @@ -1493,10 +1496,13 @@ unmap_common_complete(struct gnttab_unmap_common *op) if ( ((act->pin & (GNTPIN_devw_mask|GNTPIN_hstw_mask)) == 0) && !(op->done & GNTMAP_readonly) ) - gnttab_clear_flags(rd, GTF_writing, status); + clear_flags |= GTF_writing; if ( act->pin == 0 ) - gnttab_clear_flags(rd, GTF_reading, status); + clear_flags |= GTF_reading; + + if ( clear_flags ) + gnttab_clear_flags(rd, clear_flags, status); active_entry_release(act); grant_read_unlock(rgt); @@ -2324,6 +2330,7 @@ release_grant_for_copy( uint16_t *status; grant_ref_t trans_gref; struct domain *td; + unsigned int clear_flags = 0; grant_read_lock(rgt); @@ -2354,11 +2361,14 @@ release_grant_for_copy( act->pin -= GNTPIN_hstw_inc; if ( !(act->pin & (GNTPIN_devw_mask|GNTPIN_hstw_mask)) ) - gnttab_clear_flags(rd, GTF_writing, status); + clear_flags |= GTF_writing; } if ( !act->pin ) - gnttab_clear_flags(rd, GTF_reading, status); + clear_flags |= GTF_reading; + + if ( clear_flags ) + gnttab_clear_flags(rd, clear_flags, status); active_entry_release(act); grant_read_unlock(rgt); @@ -2384,11 +2394,16 @@ static void fixup_status_for_copy_pin(struct domain *rd, const struct active_grant_entry *act, uint16_t *status) { + unsigned int clear_flags = 0; + if ( !(act->pin & (GNTPIN_hstw_mask | GNTPIN_devw_mask)) ) - gnttab_clear_flags(rd, GTF_writing, status); + clear_flags |= GTF_writing; if ( !act->pin ) - gnttab_clear_flags(rd, GTF_reading, status); + clear_flags |= GTF_reading; + + if ( clear_flags ) + gnttab_clear_flags(rd, clear_flags, status); } /* @@ -2417,6 +2432,7 @@ acquire_grant_for_copy( uint16_t trans_length; bool is_sub_page; s16 rc = GNTST_okay; + unsigned int clear_flags = 0; *page = NULL; @@ -2639,10 +2655,13 @@ acquire_grant_for_copy( unlock_out_clear: if ( !(readonly) && !(act->pin & (GNTPIN_hstw_mask | GNTPIN_devw_mask)) ) - gnttab_clear_flags(rd, GTF_writing, status); + clear_flags |= GTF_writing; if ( !act->pin ) - gnttab_clear_flags(rd, GTF_reading, status); + clear_flags |= GTF_reading; + + if ( clear_flags ) + gnttab_clear_flags(rd, clear_flags, status); unlock_out: active_entry_release(act); @@ -3603,6 +3622,8 @@ gnttab_release_mappings( for ( handle = 0; handle < gt->maptrack_limit; handle++ ) { + unsigned int clear_flags = 0; + map = &maptrack_entry(gt, handle); if ( !(map->flags & (GNTMAP_device_map|GNTMAP_host_map)) ) continue; @@ -3677,11 +3698,14 @@ gnttab_release_mappings( } if ( (act->pin & (GNTPIN_devw_mask|GNTPIN_hstw_mask)) == 0 ) - gnttab_clear_flags(rd, GTF_writing, status); + clear_flags |= GTF_writing; } if ( act->pin == 0 ) - gnttab_clear_flags(rd, GTF_reading, status); + clear_flags |= GTF_reading; + + if ( clear_flags ) + gnttab_clear_flags(rd, clear_flags, status); active_entry_release(act); grant_read_unlock(rgt);