Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp4497104imm; Wed, 30 May 2018 06:48:01 -0700 (PDT) X-Google-Smtp-Source: ADUXVKIM9VAITobYr0DFgXuc5hSGXzOOg5oO7k90W9kwwnpmNSEgCyzcR1WarAweAIeqmZOoGvCR X-Received: by 2002:a63:3759:: with SMTP id g25-v6mr2372390pgn.59.1527688081726; Wed, 30 May 2018 06:48:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527688081; cv=none; d=google.com; s=arc-20160816; b=x5AD6no3mn8AR4UVjf7gQ03c7gAhN9Ta0LWvtf2Q9RI9hykC+94HsJaqYmoQlxO28E +7olPmHBqqZ3muRDrNae993cjV3mIg/5LPSzesSqK2w1pL8XjCxvi0L7KIg6XFHxxcbz iW4IXgbeNKgFNlRx6zoRHrklBf6wd39UMJY3dM6V7zlQM1nho/Dvw1yAryucF1CxoayC neEU/Jo8zcNOPpZhB68I05n1WPlvyprMlads9ylq2CbRpTealLfjwvvU5ilYhooP0kZ/ +/uidF1TW1uM2AFrN9vT4eBHuX02UUf42XvUppVl72WOoYKkLAOYLDT8hA3kN1IoWRh/ sXyg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:spamdiagnosticmetadata :spamdiagnosticoutput:user-agent:in-reply-to :content-transfer-encoding:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature :arc-authentication-results; bh=D/yz2h6P2F5OoxBSwuEUautNb2tD+3fGUKaMbg7fnm8=; b=UtYLl+6QIGdrBxMEsvAP+yMncZoN4ppxaJOJd41uT7RXT36tZms8aIJl8WwYKO7xYv oSQWNIK87x8M3zv60dpQQ6cerBrC9q65+LpG+LmPad0v7xzpQrmbz8+s7fIPkEBcSiXB bwF8GlXTycgJDxj/b3bT3Zvgc5YtX8OqOhwa6nZQo65uyiBMPwxJwKuEhJ4RAGppwl1b 1wqfRkg2CTXn+AJhkcuuEhvw05DclP+tc4UAvsxMSVjinZwo3chPMpqudJ7duUTynmrP 7TJUrtFflDvgJq8IFduDosU9iRhmqdCe+FuMJPhuC7HXDXIVMMmH54634khQcHEBBmdo lT7g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@parrotgroup.onmicrosoft.com header.s=selector1-parrot-com header.b=SqpBn7zO; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id x70-v6si8496060pgx.576.2018.05.30.06.47.46; Wed, 30 May 2018 06:48:01 -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=@parrotgroup.onmicrosoft.com header.s=selector1-parrot-com header.b=SqpBn7zO; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753555AbeE3Nq6 (ORCPT + 99 others); Wed, 30 May 2018 09:46:58 -0400 Received: from mail-ve1eur01on0071.outbound.protection.outlook.com ([104.47.1.71]:51264 "EHLO EUR01-VE1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1750757AbeE3Nqw (ORCPT ); Wed, 30 May 2018 09:46:52 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=parrotgroup.onmicrosoft.com; s=selector1-parrot-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=D/yz2h6P2F5OoxBSwuEUautNb2tD+3fGUKaMbg7fnm8=; b=SqpBn7zO3tdBdC+A13AzWlNgqsl3rv9yrIXCv3JLe7EVpj1GNpg9JhVw6rGjlcFnTGg6vhaIobFv/N0MiaIG+KHZp8E98G1/BC/xUIktUOGyWirCi3KhFK4AZEQOop4KdeWdZlbIq59aaI9OZT10HG+wULfUeioXb7dYP7PhvVY= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=ivan.djelic@parrot.com; Received: from parrot.com (62.23.167.188) by VI1PR10MB1743.EURPRD10.PROD.OUTLOOK.COM (2603:10a6:802:33::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.20.797.11; Wed, 30 May 2018 13:46:48 +0000 Date: Wed, 30 May 2018 15:46:46 +0200 From: Ivan Djelic To: Kees Cook Cc: Boris Brezillon , Brian Norris , David Woodhouse , Marek Vasut , Richard Weinberger , linux-mtd@lists.infradead.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] lib/bch: Remove VLA usage Message-ID: <20180530134645.GA31844@parrot.com> References: <20180529224207.GA13354@beast> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20180529224207.GA13354@beast> User-Agent: Mutt/1.9.5 (2018-04-13) X-Originating-IP: [62.23.167.188] X-ClientProxiedBy: PR2P264CA0039.FRAP264.PROD.OUTLOOK.COM (2603:10a6:101:1::27) To VI1PR10MB1743.EURPRD10.PROD.OUTLOOK.COM (2603:10a6:802:33::16) X-MS-PublicTrafficType: Email X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(7020095)(4652020)(5600026)(4534165)(7168020)(4627221)(201703031133081)(201702281549075)(2017052603328)(7153060)(7193020);SRVR:VI1PR10MB1743; X-Microsoft-Exchange-Diagnostics: 1;VI1PR10MB1743;3:/H2zAANnBe9FSnYx5AdenkUsBh5ASMoUIXwUiWjiaJaWOGb76hOOVwsgcSTVrKsmiSpdcvEOVPLMNg+nrFWZMWhp+sCRj1KKWrrBON6qsbhfcHxD9gfi3bxhL3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xuXr02BLu8dmYL5Za4+rN3zJJ0XPH2Kd5k4jXTsTlwFS/dvonB0c1e3vsbT7v8zk+c7jWkfLmYHxbIu1ag8Q=;4:BMdG0hSYBeeHTcDdlKyqwGE8VoZmTR2m43v7OAlIDvupmHgkhBg5eoa58ofKBWyUgYNN1iG+KbiFqYvG8HI32UhvOEWTlLfUjRsck3Qo82nsIYyDuFKm3pmBHOB6knh/r92VOWMRi6hmIe7SYluNbOrF3vbXF0MCZpAyF/e0QhZuDoPa0+45N+LEOTrbwfzwNsEnDn7vZlSwk6zK3mGR7jqmpqa5v5fJOIitoOEx57ilj/2G9mQTkWuGmW/vd9oWXgcoVcmM9tfbzQFJxOwKPSrJTRkfC0/OvGlg7Y77BxxTpHaLFssOLXUO4ZudpPCmHqMI2tNqf75rumnbHZ86NDEYho+wvLzoiKLJBsjWIFPL3a+EGTyuLbhNH6SYfXTsafbTJGPCPxVmnocCR/LAbsTCyea8nsywKGO66UhSMfDkKb/523fhVTqQAu0cFlMI X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(192374486261705)(85827821059158)(788757137089)(42068640409301)(17755550239193); X-MS-Exchange-SenderADCheck: 1 X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(8211001083)(6040522)(2401047)(5005006)(8121501046)(10201501046)(93006095)(93001095)(3002001)(3231254)(944501410)(52105095)(149027)(150027)(6041310)(20161123562045)(20161123560045)(20161123558120)(20161123564045)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(6072148)(201708071742011)(7699016);SRVR:VI1PR10MB1743;BCL:0;PCL:0;RULEID:;SRVR:VI1PR10MB1743; X-Forefront-PRVS: 0688BF9B46 X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10009020)(396003)(39850400004)(366004)(376002)(346002)(39380400002)(189003)(199004)(2616005)(956004)(54906003)(55236004)(5660300001)(33026002)(11346002)(36756003)(81156014)(8676002)(81166006)(69596002)(86362001)(68736007)(44832011)(446003)(305945005)(6916009)(486006)(26005)(476003)(2870700001)(58126008)(66066001)(33656002)(478600001)(186003)(386003)(2906002)(59450400001)(966005)(316002)(47776003)(39060400002)(52116002)(23676004)(2486003)(52146003)(7696005)(76176011)(25786009)(21086003)(16526019)(105586002)(106356001)(53936002)(6246003)(4326008)(8936002)(6306002)(3846002)(7736002)(229853002)(1076002)(6116002)(55016002)(50466002)(97736004)(18370500001);DIR:OUT;SFP:1101;SCL:1;SRVR:VI1PR10MB1743;H:parrot.com;FPR:;SPF:None;LANG:en;PTR:InfoNoRecords;MX:1;A:1; Received-SPF: None (protection.outlook.com: parrot.com does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: =?utf-8?B?MTtWSTFQUjEwTUIxNzQzOzIzOlNUbmt2SmNzdHMvcGprUU5UUlRqVytJQ0dy?= =?utf-8?B?ditQTTgySVptbnJzeXlNNklyakl4RjkwQlorUkZRQXJ2TCtXU0U5QXFRMHFn?= =?utf-8?B?NjFpQTVURU15U3puOC9ralZ0TXJBVVUybE9DQ2NuaUljNlNrU3ZGSjB1U0NM?= =?utf-8?B?OG9nT3Vua01mTCt6Z3NVVk8xdFZ3STNqcjY0R2dsWWdxOGZoejFtUU42bmtN?= =?utf-8?B?bUt0OVh2bG1tcElaL0ZGSjNEeFhTRjVyR3F4WmltUVBrQUJzRk4vemNvOTE3?= =?utf-8?B?eUpiOFR3eWU2bDd6aGp2WkI1OVdleHFuQW1sWTM5ckNVNWZzbkF1L0M0QjR3?= =?utf-8?B?b2psOGw0UVVaVENoRm9EOFRCb013TUFwbTVEdDlmQUpScHEzcG9pSHZ5MzJH?= =?utf-8?B?YlEvS3FITnhjSVExM2ZNdHF5Q0lodzNJZ2pSRWp6LzExSDdQbzZTZVRJOThn?= =?utf-8?B?RTI0TXpxd0NaNGRMZzA4VzRLbzNveGNRcFUxRXMxcEV6dXk4WXllZ0xyTnUv?= =?utf-8?B?RHJjWUZwR1ZhMENIV01UaU1zNW1FcWtsYkZqZ1pubUpuR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xy4uhWBJjpF4GfAGT/XFTzjZX2c0eXVnKoCzUgF+wraK8zwsuYWZYwHT5DMNY8Ye/FFGDuukU0vpjnTlrBqvGXE7eWfJy0bz+LtuoFHp3co9AJklJo+QIoTIztldO6zBxDJ9Y5bn5hznFkVMimzCPl9GGtIGXGdONTQQT4tKpoZFGxZh7K9LNvw/kX5UFqqUsmENa6vhNH3+Foy4Mk+gXHptXUXbZJByz9D0SP/zlszuaNrGWN+Oaou1G9WpgtK6LzIXIL04Y3jPF2hvTuf8O9TpFWZHhItKzYgkKDT7pw3TTwmlCdfnseLA==;5:ZXmL7nfe2FkhvakfDKPeRJQO7M4n/FFiRs77pk5UnYbKaXhDlntd5CqJQ7XyZKCTSis+5bMp0E1uAB+ibkNtQopoKAl83cezEJ4oIwI4263GFhvmE2XUkXQSe6mMXwy72iQgqluKvuFcPgddFSl3kN+SELYnKwDw11HAFHaJicM=;24:lRrOUkuQTi/u/8e5lizWcTHufJC6cyfMx0gI5UwxQHc/nsoNp/0NCj9HbR9RZgO2ij/lWZOlYFUWzEzPs/Lso8kAY264FajaXfIsetjFLeQ= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1;VI1PR10MB1743;7:9USSCTraLT3BKn+OSLIqBHKzG/+n3ZyUFQpfC3kInLenwPQOUaqEhNfVjMEbpK1d/QCqgrwum0A18GPP7Lze8O/R/pZ6P/cLNCWaJ4N7jy/qIGnz3pB4oydSBl2o6pfXLpAOhOjFVxtJL7H6J9P+O66szGpA8oCbDGtsVz5abNJ/l8NYYITA7uUZ64YBM9rnjfGNrRDPwko7zagt2FAfUeREUgSPUAkhanYMxD0hGiHMZq7ALMiZWZnO8uWyb36b X-MS-Office365-Filtering-Correlation-Id: 225666ec-1568-4a0b-e786-08d5c633ca80 X-OriginatorOrg: parrot.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 May 2018 13:46:48.3839 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 225666ec-1568-4a0b-e786-08d5c633ca80 X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: ff7d991b-3922-4803-8418-ab806a3414a6 X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR10MB1743 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, May 29, 2018 at 03:42:07PM -0700, Kees Cook wrote: > In the quest to remove all stack VLA usage from the kernel[1], this removes > the on-stack working buffers in favor of pre-allocated working buffers > (which were already used in other places). Since these routines must > already be serialized (since they work on bch->ecc_buf), adding the usage > of bch->ecc_work would be similarly safe. Additionally, since "max m" is > only 15, this was adjusted to just use a fixed size array in those cases. Hi Kees, Using an on-stack buffer instead of a pre-allocated buffer was done initially for performance reasons. For "usual" (m,t) values (for instance m=13, t=4), there is a huge performance difference between the on-stack buffer version and the kmalloc version. I didn't investigate the reason for this, but I ran a quick benchmark on my PC: little-endian, type sizes: int=4 long=8 longlong=8 cpu: Intel(R) Core(TM) i5 CPU 650 @ 3.20GHz calibration: iter=4.9143µs niter=2034 nsamples=200 m=13 t=4 Buffer allocation | Encoding throughput (Mbit/s) --------------------------------------------------- on-stack, VLA | 3988 on-stack, fixed | 4494 kmalloc | 1967 The first line shows the performance of the current code, using a VLA. The second line shows the performance when r[] is allocated on the stack with a fixed, constant size (the maximum allowed value). The third line shows the performance when r is a pre-allocated working buffer. In fact, when using a pre-allocated buffer there is no need to introduce 'ecc_work': you can directly point 'r' to bch->ecc_buf and remove memcpy() surrounding the 'while (mlen--)' loop. Everything happens inside the 'bch->ecc_buf' buffer. But with a big performance penalty. Looks like declaring a temporary buffer on the stack to store ECC values allows GCC to do a better job at optimizing the loop. So rather than introducing 'ecc_work', I suggest we compute the maximum allowed size for r[] and use that: sizeof(r) = sizeof(uint32_t)*(l+1) l+1 = BCH_ECC_WORDS(bch) = DIV_ROUND_UP(m*t, 32) We also know that: m*t < 2^m - 1 (ECC maximum size) therefore: l+1 < DIV_ROUND_UP(2^m - 1, 32) < 2^(m-5) So instead of 'uint32_t r[l+1]' we could declare 'uint32_t r[1 << (BCH_MAX_M-5)]'. And replace 'sizeof(r)' with 'sizeof(*bch->ecc_buf)*(l+1)' in memset/memcpy calls. In practice the actual maximum size of r[] is (1 << (15-5))*sizeof(uint32_t) = 4096 bytes. What do you think ? -- Ivan > [1] https://lkml.kernel.org/r/CA+55aFzCG-zNmZwX4A2FQpadafLfEzK6CC=qPXydAacU1RqZWA@mail.gmail.com > > Signed-off-by: Kees Cook > --- > This is directed at linux-mtd because it's the only user of this library > and it's how it originally entered the kernel tree... > --- > include/linux/bch.h | 4 ++-- > lib/bch.c | 27 +++++++++++++++------------ > 2 files changed, 17 insertions(+), 14 deletions(-) > > diff --git a/include/linux/bch.h b/include/linux/bch.h > index 295b4ef153bb..4d46e6a73319 100644 > --- a/include/linux/bch.h > +++ b/include/linux/bch.h > @@ -39,7 +39,7 @@ > * @a_log_tab: Galois field GF(2^m) log lookup table > * @mod8_tab: remainder generator polynomial lookup tables > * @ecc_buf: ecc parity words buffer > - * @ecc_buf2: ecc parity words buffer > + * @ecc_work: ecc parity words working buffer > * @xi_tab: GF(2^m) base for solving degree 2 polynomial roots > * @syn: syndrome buffer > * @cache: log-based polynomial representation buffer > @@ -57,7 +57,7 @@ struct bch_control { > uint16_t *a_log_tab; > uint32_t *mod8_tab; > uint32_t *ecc_buf; > - uint32_t *ecc_buf2; > + uint32_t *ecc_work; > unsigned int *xi_tab; > unsigned int *syn; > int *cache; > diff --git a/lib/bch.c b/lib/bch.c > index bc89dfe4d1b3..f14eac93ecc4 100644 > --- a/lib/bch.c > +++ b/lib/bch.c > @@ -78,10 +78,12 @@ > #define GF_M(_p) (CONFIG_BCH_CONST_M) > #define GF_T(_p) (CONFIG_BCH_CONST_T) > #define GF_N(_p) ((1 << (CONFIG_BCH_CONST_M))-1) > +#define BCH_MAX_M (CONFIG_BCH_CONST_M) > #else > #define GF_M(_p) ((_p)->m) > #define GF_T(_p) ((_p)->t) > #define GF_N(_p) ((_p)->n) > +#define BCH_MAX_M 15 > #endif > > #define BCH_ECC_WORDS(_p) DIV_ROUND_UP(GF_M(_p)*GF_T(_p), 32) > @@ -187,7 +189,7 @@ void encode_bch(struct bch_control *bch, const uint8_t *data, > const unsigned int l = BCH_ECC_WORDS(bch)-1; > unsigned int i, mlen; > unsigned long m; > - uint32_t w, r[l+1]; > + uint32_t w; > const uint32_t * const tab0 = bch->mod8_tab; > const uint32_t * const tab1 = tab0 + 256*(l+1); > const uint32_t * const tab2 = tab1 + 256*(l+1); > @@ -198,7 +200,7 @@ void encode_bch(struct bch_control *bch, const uint8_t *data, > /* load ecc parity bytes into internal 32-bit buffer */ > load_ecc8(bch, bch->ecc_buf, ecc); > } else { > - memset(bch->ecc_buf, 0, sizeof(r)); > + memset(bch->ecc_work, 0, bch->ecc_bytes); > } > > /* process first unaligned data bytes */ > @@ -215,7 +217,7 @@ void encode_bch(struct bch_control *bch, const uint8_t *data, > mlen = len/4; > data += 4*mlen; > len -= 4*mlen; > - memcpy(r, bch->ecc_buf, sizeof(r)); > + memcpy(bch->ecc_work, bch->ecc_buf, bch->ecc_bytes); > > /* > * split each 32-bit word into 4 polynomials of weight 8 as follows: > @@ -229,6 +231,8 @@ void encode_bch(struct bch_control *bch, const uint8_t *data, > * xxxxxxxx yyyyyyyy zzzzzzzz tttttttt mod g = r0^r1^r2^r3 > */ > while (mlen--) { > + uint32_t *r = bch->ecc_work; > + > /* input data is read in big-endian format */ > w = r[0]^cpu_to_be32(*pdata++); > p0 = tab0 + (l+1)*((w >> 0) & 0xff); > @@ -241,7 +245,7 @@ void encode_bch(struct bch_control *bch, const uint8_t *data, > > r[l] = p0[l]^p1[l]^p2[l]^p3[l]; > } > - memcpy(bch->ecc_buf, r, sizeof(r)); > + memcpy(bch->ecc_buf, bch->ecc_work, bch->ecc_bytes); > > /* process last unaligned bytes */ > if (len) > @@ -434,7 +438,7 @@ static int solve_linear_system(struct bch_control *bch, unsigned int *rows, > { > const int m = GF_M(bch); > unsigned int tmp, mask; > - int rem, c, r, p, k, param[m]; > + int rem, c, r, p, k, param[BCH_MAX_M]; > > k = 0; > mask = 1 << m; > @@ -1009,10 +1013,10 @@ int decode_bch(struct bch_control *bch, const uint8_t *data, unsigned int len, > } > /* load received ecc or assume it was XORed in calc_ecc */ > if (recv_ecc) { > - load_ecc8(bch, bch->ecc_buf2, recv_ecc); > + load_ecc8(bch, bch->ecc_work, recv_ecc); > /* XOR received and calculated ecc */ > for (i = 0, sum = 0; i < (int)ecc_words; i++) { > - bch->ecc_buf[i] ^= bch->ecc_buf2[i]; > + bch->ecc_buf[i] ^= bch->ecc_work[i]; > sum |= bch->ecc_buf[i]; > } > if (!sum) > @@ -1114,7 +1118,7 @@ static int build_deg2_base(struct bch_control *bch) > { > const int m = GF_M(bch); > int i, j, r; > - unsigned int sum, x, y, remaining, ak = 0, xi[m]; > + unsigned int sum, x, y, remaining, ak = 0, xi[BCH_MAX_M]; > > /* find k s.t. Tr(a^k) = 1 and 0 <= k < m */ > for (i = 0; i < m; i++) { > @@ -1254,7 +1258,6 @@ struct bch_control *init_bch(int m, int t, unsigned int prim_poly) > struct bch_control *bch = NULL; > > const int min_m = 5; > - const int max_m = 15; > > /* default primitive polynomials */ > static const unsigned int prim_poly_tab[] = { > @@ -1270,7 +1273,7 @@ struct bch_control *init_bch(int m, int t, unsigned int prim_poly) > goto fail; > } > #endif > - if ((m < min_m) || (m > max_m)) > + if ((m < min_m) || (m > BCH_MAX_M)) > /* > * values of m greater than 15 are not currently supported; > * supporting m > 15 would require changing table base type > @@ -1300,7 +1303,7 @@ struct bch_control *init_bch(int m, int t, unsigned int prim_poly) > bch->a_log_tab = bch_alloc((1+bch->n)*sizeof(*bch->a_log_tab), &err); > bch->mod8_tab = bch_alloc(words*1024*sizeof(*bch->mod8_tab), &err); > bch->ecc_buf = bch_alloc(words*sizeof(*bch->ecc_buf), &err); > - bch->ecc_buf2 = bch_alloc(words*sizeof(*bch->ecc_buf2), &err); > + bch->ecc_work = bch_alloc(words*sizeof(*bch->ecc_work), &err); > bch->xi_tab = bch_alloc(m*sizeof(*bch->xi_tab), &err); > bch->syn = bch_alloc(2*t*sizeof(*bch->syn), &err); > bch->cache = bch_alloc(2*t*sizeof(*bch->cache), &err); > @@ -1349,7 +1352,7 @@ void free_bch(struct bch_control *bch) > kfree(bch->a_log_tab); > kfree(bch->mod8_tab); > kfree(bch->ecc_buf); > - kfree(bch->ecc_buf2); > + kfree(bch->ecc_work); > kfree(bch->xi_tab); > kfree(bch->syn); > kfree(bch->cache); > -- > 2.17.0 > > > -- > Kees Cook > Pixel Security