From patchwork Thu Feb 27 18:46:02 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Cooper X-Patchwork-Id: 11409495 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 2C11514B4 for ; Thu, 27 Feb 2020 18:47:20 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 08544246AA for ; Thu, 27 Feb 2020 18:47:19 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=citrix.com header.i=@citrix.com header.b="E7y9ygpK" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 08544246AA Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=citrix.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1j7OAi-0006Hw-GQ; Thu, 27 Feb 2020 18:46:08 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1j7OAh-0006Hr-OM for xen-devel@lists.xenproject.org; Thu, 27 Feb 2020 18:46:07 +0000 X-Inumbo-ID: 697637da-5991-11ea-83b0-bc764e2007e4 Received: from esa3.hc3370-68.iphmx.com (unknown [216.71.145.155]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 697637da-5991-11ea-83b0-bc764e2007e4; Thu, 27 Feb 2020 18:46:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=citrix.com; s=securemail; t=1582829167; h=from:to:cc:subject:date:message-id:mime-version; bh=AkgT2nv8+RRbmZx7MoRhg0SHqYJEL6hry9fiUhJ4c+Q=; b=E7y9ygpKjt76pHXFI6v+hJll2yEbcVllpoNzu65J6CLg4FrPHLoxBU/Q a24LQFuCFldR6Scy29MDXDL8ahs8bsXmyiXyXzYV+FqeYXTyN1RZyS1KO vHMQzt/J9tXFS89QTkcLnhzmKoavVZN8C3BD/sx1v+TO6gP+Afu7JnPD6 0=; Authentication-Results: esa3.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 (esa3.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=esa3.hc3370-68.iphmx.com; envelope-from="Andrew.Cooper3@citrix.com"; x-sender="andrew.cooper3@citrix.com"; x-conformance=sidf_compatible Received-SPF: Pass (esa3.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=esa3.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 ip4:168.245.78.127 ~all" Received-SPF: None (esa3.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=esa3.hc3370-68.iphmx.com; envelope-from="Andrew.Cooper3@citrix.com"; x-sender="postmaster@mail.citrix.com"; x-conformance=sidf_compatible IronPort-SDR: Y9kaHUqK1KDg0id5ghT3bs68/w7jAXvWyGj1DISbiQe2sFv3x4rrZ7FhpN/eUphI625rKt0M3h 5jsK/+dM6txrcXEiWG9+sY9oxGyUjIdBl0NtrG4T3eon2EE3DxUjjHtB/Hdxu13QcYJxQlOumI Dxf52W8ejxapXOWXO3AO54ZW0o9PYjXfWx9FMy2ADXprrclkFwQgtzG/g1t8XkqKo8t42TKRHl Bnd0S0AXw6a6+HgzZVnt20K63+p+eNCsECPOJp5u+paQM83zkAr9Q0Ej3rF/hQZJRHnm/RCRw0 cG8= X-SBRS: 2.7 X-MesageID: 13105780 X-Ironport-Server: esa3.hc3370-68.iphmx.com X-Remote-IP: 162.221.158.21 X-Policy: $RELAYED X-IronPort-AV: E=Sophos;i="5.70,493,1574139600"; d="scan'208";a="13105780" From: Andrew Cooper To: Xen-devel Date: Thu, 27 Feb 2020 18:46:02 +0000 Message-ID: <20200227184602.28282-1-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.11.0 MIME-Version: 1.0 Subject: [Xen-devel] [PATCH] xen/grant: Fix signed/unsigned comparisons issues 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 , Julien Grall , Wei Liu , Konrad Rzeszutek Wilk , George Dunlap , Andrew Cooper , Jan Beulich Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" Each function takes an unsigned count, and loops based on a signed i. This causes problems when between 2 and 4 billion operations are requested. In practice, signed-ness issues aren't possible because count exceeding INT_MAX is excluded earlier in do_grant_op(), but the code reads as if it is buggy, and GCC obviously can't spot this either. Bloat-o-meter reports: add/remove: 0/0 grow/shrink: 0/4 up/down: 0/-95 (-95) Function old new delta do_grant_table_op 7155 7140 -15 gnttab_transfer 2732 2716 -16 gnttab_unmap_grant_ref 771 739 -32 gnttab_unmap_and_replace 771 739 -32 Total: Before=2996364, After=2996269, chg -0.00% and inspection of gnttab_unmap_grant_ref() at least reveals one fewer local variables on the stack. Signed-off-by: Andrew Cooper Reviewed-by: Jan Beulich --- CC: George Dunlap CC: Jan Beulich CC: Konrad Rzeszutek Wilk CC: Stefano Stabellini CC: Wei Liu CC: Julien Grall gnttab_unmap_grant_ref()'s stack frame size is 0x740 (dropping to 0x738) which is alarmingly close to 2k. --- xen/common/grant_table.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/xen/common/grant_table.c b/xen/common/grant_table.c index bc37acae0e..0f81875bee 100644 --- a/xen/common/grant_table.c +++ b/xen/common/grant_table.c @@ -1270,7 +1270,7 @@ static long gnttab_map_grant_ref( XEN_GUEST_HANDLE_PARAM(gnttab_map_grant_ref_t) uop, unsigned int count) { - int i; + unsigned int i; struct gnttab_map_grant_ref op; for ( i = 0; i < count; i++ ) @@ -1568,13 +1568,14 @@ static long gnttab_unmap_grant_ref( XEN_GUEST_HANDLE_PARAM(gnttab_unmap_grant_ref_t) uop, unsigned int count) { - int i, c, partial_done, done = 0; + unsigned int i, partial_done, done = 0; struct gnttab_unmap_grant_ref op; struct gnttab_unmap_common common[GNTTAB_UNMAP_BATCH_SIZE]; while ( count != 0 ) { - c = min(count, (unsigned int)GNTTAB_UNMAP_BATCH_SIZE); + unsigned int c = min(count, (unsigned int)GNTTAB_UNMAP_BATCH_SIZE); + partial_done = 0; for ( i = 0; i < c; i++ ) @@ -1633,13 +1634,14 @@ static long gnttab_unmap_and_replace( XEN_GUEST_HANDLE_PARAM(gnttab_unmap_and_replace_t) uop, unsigned int count) { - int i, c, partial_done, done = 0; + unsigned int i, partial_done, done = 0; struct gnttab_unmap_and_replace op; struct gnttab_unmap_common common[GNTTAB_UNMAP_BATCH_SIZE]; while ( count != 0 ) { - c = min(count, (unsigned int)GNTTAB_UNMAP_BATCH_SIZE); + unsigned int c = min(count, (unsigned int)GNTTAB_UNMAP_BATCH_SIZE); + partial_done = 0; for ( i = 0; i < c; i++ ) @@ -2142,7 +2144,7 @@ gnttab_transfer( struct domain *d = current->domain; struct domain *e; struct page_info *page; - int i; + unsigned int i; struct gnttab_transfer gop; mfn_t mfn; unsigned int max_bitsize; @@ -3359,7 +3361,7 @@ static long gnttab_swap_grant_ref(XEN_GUEST_HANDLE_PARAM(gnttab_swap_grant_ref_t) uop, unsigned int count) { - int i; + unsigned int i; gnttab_swap_grant_ref_t op; for ( i = 0; i < count; i++ )