From patchwork Mon Mar 19 15:49:10 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sasha Levin X-Patchwork-Id: 132043 Delivered-To: patch@linaro.org Received: by 10.80.152.129 with SMTP id j1csp2969619edb; Mon, 19 Mar 2018 08:50:40 -0700 (PDT) X-Google-Smtp-Source: AG47ELvYhHirMJVq+RaC9qnl6iZxVhWgVefauQ+zUzJPwtRkSpAj0J9yYRI3wNDzWNxD+h7MNAU7 X-Received: by 10.101.97.15 with SMTP id z15mr7584338pgu.393.1521474639990; Mon, 19 Mar 2018 08:50:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521474639; cv=none; d=google.com; s=arc-20160816; b=OilcY9pkzZea/fCyU+TjBclHwramR2UTyhAzm+axxlgvIFLNVmCQSvyJErsIFIbYgZ OON+pKMSv2r+ZygHrIRAyx1Nid+2xSB5He2mYfHF9HtA3bMnFPiPbSSx86O+uEKoZ5Nn WJFAdfd4+vFuIoQrhQ13q3J77UiWLxA6Y9yJ0y60VT7XIsV9Djm6Pwnn2pgUqeF0AOe8 c+jLLdUsgnywodF5Aidha9V0UxPyHp8/y9nwgOvrebbgbOLZ7gf7+xetEGPEh9lCoBTC MGdXQFI22SmmgtB1joEpeyiz/blnprbuqIO00zPHk339/hB0fsyMiHn4D9tDVYpwvwMb h9UA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:content-transfer-encoding :spamdiagnosticmetadata:spamdiagnosticoutput:content-language :accept-language:in-reply-to:references:message-id:date:thread-index :thread-topic:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=XjTb+FV6a+ei1GqZhrkj6xRWOwKJxuIEkv0VdfFt09U=; b=nA/mh9HzkPMlPJddPb+x17gtIA/wOIjDgc8OorNzvYT17wtPJoKmBXfmASv/6MHEqK 6o0qYZEtL9Ox4OXaCRUzEESX/ygmnBAB5fu8S2Iw4xnCKfTN/06GUQ3j9TjNXkjmSi7+ C1wVXF3KNnUJPNVayXh+AmLGLyQ+wGmojF0kb1kU+aEsiT8PBE4hCQNn7vuxB+6CoeDr Z1YrQTO/xBaWFIpTTGKSdacMrLqyWbjBPysnHlYldSamodiiq6S2jlW5AdxQtAn4B7cv vbUkBJhOtSY7wGXA6cuD1U/edPRAKNs99zI8edYKfvYEzzytBJgC7sQQ8EWKt8nQt4Si CDYA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@microsoft.com header.s=selector1 header.b=Ge/24d2t; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=microsoft.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r8si157186pgp.468.2018.03.19.08.50.39; Mon, 19 Mar 2018 08:50:39 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@microsoft.com header.s=selector1 header.b=Ge/24d2t; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=microsoft.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S934121AbeCSPuf (ORCPT + 28 others); Mon, 19 Mar 2018 11:50:35 -0400 Received: from mail-by2nam01on0114.outbound.protection.outlook.com ([104.47.34.114]:57771 "EHLO NAM01-BY2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S934070AbeCSPuP (ORCPT ); Mon, 19 Mar 2018 11:50:15 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=XjTb+FV6a+ei1GqZhrkj6xRWOwKJxuIEkv0VdfFt09U=; b=Ge/24d2thO9Myr/ddg4U8bUhTKifhEHPbab8nB8A8FObORqRtjTseuUKhRaK6ILhedz+jmPtTiTzePdjDfqhFs158Y2JUnQN5GoFx36fgE+v1mn23xIfI+ThDP7HtE4MNg1xDZHdd4oXCQiTWOXL40wlh0INLNGf/Mk+nd+d8tM= Received: from DM5PR2101MB1032.namprd21.prod.outlook.com (52.132.128.13) by DM5PR2101MB1109.namprd21.prod.outlook.com (52.132.130.19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.631.0; Mon, 19 Mar 2018 15:50:01 +0000 Received: from DM5PR2101MB1032.namprd21.prod.outlook.com ([fe80::3d9b:79e7:94eb:5d62]) by DM5PR2101MB1032.namprd21.prod.outlook.com ([fe80::3d9b:79e7:94eb:5d62%5]) with mapi id 15.20.0631.004; Mon, 19 Mar 2018 15:50:01 +0000 From: Sasha Levin To: "linux-kernel@vger.kernel.org" , "stable@vger.kernel.org" CC: Arnd Bergmann , Richard Biener , Jakub Jelinek , Ard Biesheuvel , Herbert Xu , Sasha Levin Subject: [PATCH AUTOSEL for 4.15 115/124] crypto: aes-generic - build with -Os on gcc-7+ Thread-Topic: [PATCH AUTOSEL for 4.15 115/124] crypto: aes-generic - build with -Os on gcc-7+ Thread-Index: AQHTv5nSHXcUmL9aeUm4bSTOGIaRSA== Date: Mon, 19 Mar 2018 15:49:10 +0000 Message-ID: <20180319154645.11350-115-alexander.levin@microsoft.com> References: <20180319154645.11350-1-alexander.levin@microsoft.com> In-Reply-To: <20180319154645.11350-1-alexander.levin@microsoft.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [52.168.54.252] x-ms-publictraffictype: Email x-microsoft-exchange-diagnostics: 1; DM5PR2101MB1109; 7:OTKngZ/CJbR2Fc+yZmoB3wnaFLtsWKabse4hQx+q+OgGgu9pADE9ETrNlPknUGQwpKhdEhD0BM0x28hqaV27kfYSmDGeVx1IlXIL//E7fE6JasWmAj/GUTwsSXjBYQ4WZaf0qhvpehsJwdHOhrX6noFtGrpAWMktU+8kg26krteGeda6Z2ElhZX8m/t83PH6a7e2TF0zBinA2yWu7YSgHGzfL3xdKJf7sklY5Lzb84Y1yX12V5mZxEYyebv1Y9Qa; 20:fZOmj1AnYqloz4EAKbdX/cqFheE+Ff/AapMNZqtfPeABi4CgmSfBdcOGjzNaKfXOQn+EsEq6FPWbNfykxp4zsQanjD40Omz/bcxLKQo+PjwJlWtyi8hga42PdWSrza4MIoGi1C7xKwUoZTtopzi8Zcmb+NL7ZUnkn4PvzQLUDX4= x-ms-office365-filtering-ht: Tenant x-ms-office365-filtering-correlation-id: 7c30531d-b251-4c46-8df7-08d58db112fe x-microsoft-antispam: UriScan:; BCL:0; PCL:0; RULEID:(7020095)(4652020)(48565401081)(5600026)(4604075)(3008032)(4534165)(4627221)(201703031133081)(201702281549075)(2017052603328)(7193020); SRVR:DM5PR2101MB1109; x-ms-traffictypediagnostic: DM5PR2101MB1109: authentication-results: spf=none (sender IP is ) smtp.mailfrom=Alexander.Levin@microsoft.com; x-microsoft-antispam-prvs: x-exchange-antispam-report-test: UriScan:(28532068793085)(22074186197030)(89211679590171)(183786458502308); x-exchange-antispam-report-cfa-test: BCL:0; PCL:0; RULEID:(8211001083)(61425038)(6040522)(2401047)(5005006)(8121501046)(3231221)(944501300)(52105095)(3002001)(93006095)(93001095)(10201501046)(6055026)(61426038)(61427038)(6041310)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123560045)(20161123562045)(20161123564045)(20161123558120)(6072148)(201708071742011); SRVR:DM5PR2101MB1109; BCL:0; PCL:0; RULEID:; SRVR:DM5PR2101MB1109; x-forefront-prvs: 06167FAD59 x-forefront-antispam-report: SFV:NSPM; SFS:(10019020)(366004)(396003)(39860400002)(39380400002)(376002)(346002)(199004)(189003)(81166006)(10290500003)(107886003)(81156014)(106356001)(6506007)(8676002)(3846002)(36756003)(8936002)(97736004)(6116002)(1076002)(102836004)(68736007)(6666003)(3280700002)(2950100002)(3660700001)(478600001)(72206003)(59450400001)(2906002)(14454004)(966005)(76176011)(66066001)(6512007)(6306002)(5250100002)(10090500001)(2501003)(53936002)(6486002)(6436002)(26005)(316002)(22452003)(305945005)(7736002)(4326008)(25786009)(86362001)(86612001)(5660300001)(2900100001)(99286004)(54906003)(186003)(105586002)(110136005)(22906009)(41533002)(217873001); DIR:OUT; SFP:1102; SCL:1; SRVR:DM5PR2101MB1109; H:DM5PR2101MB1032.namprd21.prod.outlook.com; FPR:; SPF:None; LANG:en; PTR:InfoNoRecords; A:1; MX:1; received-spf: None (protection.outlook.com: microsoft.com does not designate permitted sender hosts) x-microsoft-antispam-message-info: piRyZvFOhXs4vK6OFS3+WfvUAVWk6IqenPbaBe2QLpFFB9oWxx9zaGM6wIDFP1wYc8vtM/uTCqR8CcIkIe5wCFJEX4QZPG5Fg7MSw5f0uNP9HHStVdRqhjWsnnglVdd7xBHL3vup6gS0X0CKQ/7uxhchphg+B3Vlg6sitVY/gA35yMsk0/YzvxURwN59EpJWfBH+/KXhwIVZ/ZHGwQNN+x/UZ3es29WRyUnC9R/AqjXd1ecWISnP1U/WPibqzErPAqHYEx/SsPLEmqq7OiJVItsiqrAruUwpbltZ73dcfhgshjllFFdJwL4L6G1Upq7PdcGzLQ8BlZ+uAr8Bh79/Vg== spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM MIME-Version: 1.0 X-OriginatorOrg: microsoft.com X-MS-Exchange-CrossTenant-Network-Message-Id: 7c30531d-b251-4c46-8df7-08d58db112fe X-MS-Exchange-CrossTenant-originalarrivaltime: 19 Mar 2018 15:49:10.6131 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 72f988bf-86f1-41af-91ab-2d7cd011db47 X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR2101MB1109 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Arnd Bergmann [ Upstream commit 148b974deea927f5dbb6c468af2707b488bfa2de ] While testing other changes, I discovered that gcc-7.2.1 produces badly optimized code for aes_encrypt/aes_decrypt. This is especially true when CONFIG_UBSAN_SANITIZE_ALL is enabled, where it leads to extremely large stack usage that in turn might cause kernel stack overflows: crypto/aes_generic.c: In function 'aes_encrypt': crypto/aes_generic.c:1371:1: warning: the frame size of 4880 bytes is larger than 2048 bytes [-Wframe-larger-than=] crypto/aes_generic.c: In function 'aes_decrypt': crypto/aes_generic.c:1441:1: warning: the frame size of 4864 bytes is larger than 2048 bytes [-Wframe-larger-than=] I verified that this problem exists on all architectures that are supported by gcc-7.2, though arm64 in particular is less affected than the others. I also found that gcc-7.1 and gcc-8 do not show the extreme stack usage but still produce worse code than earlier versions for this file, apparently because of optimization passes that generally provide a substantial improvement in object code quality but understandably fail to find any shortcuts in the AES algorithm. Possible workarounds include a) disabling -ftree-pre and -ftree-sra optimizations, this was an earlier patch I tried, which reliably fixed the stack usage, but caused a serious performance regression in some versions, as later testing found. b) disabling UBSAN on this file or all ciphers, as suggested by Ard Biesheuvel. This would lead to massively better crypto performance in UBSAN-enabled kernels and avoid the stack usage, but there is a concern over whether we should exclude arbitrary files from UBSAN at all. c) Forcing the optimization level in a different way. Similar to a), but rather than deselecting specific optimization stages, this now uses "gcc -Os" for this file, regardless of the CONFIG_CC_OPTIMIZE_FOR_PERFORMANCE/SIZE option. This is a reliable workaround for the stack consumption on all architecture, and I've retested the performance results now on x86, cycles/byte (lower is better) for cbc(aes-generic) with 256 bit keys: -O2 -Os gcc-6.3.1 14.9 15.1 gcc-7.0.1 14.7 15.3 gcc-7.1.1 15.3 14.7 gcc-7.2.1 16.8 15.9 gcc-8.0.0 15.5 15.6 This implements the option c) by enabling forcing -Os on all compiler versions starting with gcc-7.1. As a workaround for PR83356, it would only be needed for gcc-7.2+ with UBSAN enabled, but since it also shows better performance on gcc-7.1 without UBSAN, it seems appropriate to use the faster version here as well. Side note: during testing, I also played with the AES code in libressl, which had a similar performance regression from gcc-6 to gcc-7.2, but was three times slower overall. It might be interesting to investigate that further and possibly port the Linux implementation into that. Link: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83356 Link: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83651 Cc: Richard Biener Cc: Jakub Jelinek Cc: Ard Biesheuvel Signed-off-by: Arnd Bergmann Acked-by: Ard Biesheuvel Signed-off-by: Herbert Xu Signed-off-by: Sasha Levin --- crypto/Makefile | 1 + 1 file changed, 1 insertion(+) -- 2.14.1 diff --git a/crypto/Makefile b/crypto/Makefile index d674884b2d51..daa69360e054 100644 --- a/crypto/Makefile +++ b/crypto/Makefile @@ -99,6 +99,7 @@ obj-$(CONFIG_CRYPTO_TWOFISH_COMMON) += twofish_common.o obj-$(CONFIG_CRYPTO_SERPENT) += serpent_generic.o CFLAGS_serpent_generic.o := $(call cc-option,-fsched-pressure) # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79149 obj-$(CONFIG_CRYPTO_AES) += aes_generic.o +CFLAGS_aes_generic.o := $(call cc-ifversion, -ge, 0701, -Os) # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83356 obj-$(CONFIG_CRYPTO_AES_TI) += aes_ti.o obj-$(CONFIG_CRYPTO_CAMELLIA) += camellia_generic.o obj-$(CONFIG_CRYPTO_CAST_COMMON) += cast_common.o