Received: by 10.223.185.111 with SMTP id b44csp823993wrg; Fri, 9 Mar 2018 14:35:07 -0800 (PST) X-Google-Smtp-Source: AG47ELuFzC6F53nxdyzKM/xxzXjNUdMHIUcumSfZWGqZUfm0YqDPzv9s/u+jBFHCkvz7rHFJ5SrW X-Received: by 2002:a17:902:529:: with SMTP id 38-v6mr49213plf.327.1520634907466; Fri, 09 Mar 2018 14:35:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1520634907; cv=none; d=google.com; s=arc-20160816; b=nkQuGOb7I/dP0gBS9014YTHwUhNDn/jewlR31t1/VA3uy0lzj360xJKs4ooEPYeoCZ k4JuXJx0ilAemSvFNGHMb1HJBt9Tdo+UkbysUrFjJRhwmPo+TnrNSv16stT0xYBFji/M 90ZCQ3akeoy11sp/r9Uz7vKOYA5LjA1ipgc5JxYcn6cP4AJRztJOcV3x/5MHWvgHwk8i BlUIek2c2lcv4cdYuBUev169klxf1pdOTDXWviZbf5g+MZgmN+a/RW8TzHMWZ134+Q7q aB3TxzPJG1oLm6B3JWzQHv8wG119xEIYg9PzdBx7GkBR4QWHsoxrDKMZYz5A99yDsOUL ek+w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=l6Sp2j82YKDbdBpdeR8DFzN4G5+CLK3hdH3aENbgfsA=; b=MJJ+XND9bFj1z8utrDaL7y96PMw77xK5ucux1uziresb8Rok0MiyJv9vZEzRZ39fSL q+9IdreMUrAByQIUdVNTS2TRNH9CBjXEo6BtrucQO3J4GtDmHMAUJ6N6EHIFPqubx0pU iwOnURjbGev1yLCJC6o6eezBJYi4fkwUFmpwD3mBBJqGH+UVsrLtOai/vgs6vZ9rgGmO MQTltf2yM4fPN1sOKB2Pzxfz2bXWCEEjKIN3qfDHb9M6EkQdneiE2e/IceZ3lbXskaSe t1TxCJckf4anCUn44tHwchU5I2MjVfJRwxyoRJI1lQSD3ZTnmZ/IUpRgnY+rUsEF8cqr Llmw== ARC-Authentication-Results: i=1; mx.google.com; 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 u80si1570574pfg.402.2018.03.09.14.34.52; Fri, 09 Mar 2018 14:35:07 -0800 (PST) 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; 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 S932921AbeCIWeD (ORCPT + 99 others); Fri, 9 Mar 2018 17:34:03 -0500 Received: from smtp5-g21.free.fr ([212.27.42.5]:45342 "EHLO smtp5-g21.free.fr" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932405AbeCIWd7 (ORCPT ); Fri, 9 Mar 2018 17:33:59 -0500 Received: from heffalump.sk2.org (unknown [88.186.243.14]) by smtp5-g21.free.fr (Postfix) with ESMTPS id 418C75FF95; Fri, 9 Mar 2018 23:33:58 +0100 (CET) Received: from steve by heffalump.sk2.org with local (Exim 4.89) (envelope-from ) id 1euQaL-0005XC-I6; Fri, 09 Mar 2018 23:33:57 +0100 From: Stephen Kitt To: hare@suse.com, axboe@kernel.dk, jejb@linux.vnet.ibm.com, martin.petersen@oracle.com Cc: linux-scsi@vger.kernel.org, kernel-hardening@lists.openwall.com, linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, Stephen Kitt Subject: [PATCH] scsi: resolve COMMAND_SIZE at compile time Date: Fri, 9 Mar 2018 23:33:55 +0100 Message-Id: <20180309223355.21222-1-steve@sk2.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20180309232933.14e39858@heffalump.sk2.org> References: <20180309232933.14e39858@heffalump.sk2.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org COMMAND_SIZE currently uses an array of values in block/scsi_ioctl.c. A number of device_handler functions use this to initialise arrays, and this is flagged by -Wvla. This patch replaces COMMAND_SIZE with a variant using a formula which can be resolved at compile time in cases where the opcode is fixed, resolving the array size and avoiding the VLA. The downside is that the code is less maintainable and that some call sites end up having more complex generated code. Since scsi_command_size_tbl is dropped, we can remove the dependency on BLK_SCSI_REQUEST from drivers/target/Kconfig. This was prompted by https://lkml.org/lkml/2018/3/7/621 Signed-off-by: Stephen Kitt --- block/scsi_ioctl.c | 8 -------- drivers/target/Kconfig | 1 - include/scsi/scsi_common.h | 13 +++++++++++-- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/block/scsi_ioctl.c b/block/scsi_ioctl.c index 60b471f8621b..b9e176e537be 100644 --- a/block/scsi_ioctl.c +++ b/block/scsi_ioctl.c @@ -41,14 +41,6 @@ struct blk_cmd_filter { static struct blk_cmd_filter blk_default_cmd_filter; -/* Command group 3 is reserved and should never be used. */ -const unsigned char scsi_command_size_tbl[8] = -{ - 6, 10, 10, 12, - 16, 12, 10, 10 -}; -EXPORT_SYMBOL(scsi_command_size_tbl); - #include static int sg_get_version(int __user *p) diff --git a/drivers/target/Kconfig b/drivers/target/Kconfig index 4c44d7bed01a..b5f05b60cf06 100644 --- a/drivers/target/Kconfig +++ b/drivers/target/Kconfig @@ -4,7 +4,6 @@ menuconfig TARGET_CORE depends on SCSI && BLOCK select CONFIGFS_FS select CRC_T10DIF - select BLK_SCSI_REQUEST # only for scsi_command_size_tbl.. select SGL_ALLOC default n help diff --git a/include/scsi/scsi_common.h b/include/scsi/scsi_common.h index 731ac09ed231..48d950666376 100644 --- a/include/scsi/scsi_common.h +++ b/include/scsi/scsi_common.h @@ -15,8 +15,17 @@ scsi_varlen_cdb_length(const void *hdr) return ((struct scsi_varlen_cdb_hdr *)hdr)->additional_cdb_length + 8; } -extern const unsigned char scsi_command_size_tbl[8]; -#define COMMAND_SIZE(opcode) scsi_command_size_tbl[((opcode) >> 5) & 7] +/* + * SCSI command sizes are as follows, in bytes, for fixed size commands, per + * group: 6, 10, 10, 12, 16, 12, 10, 10. The top three bits of an opcode + * determine its group. + * The size table is encoded into a 32-bit value by subtracting each value + * from 16, resulting in a value of 1715488362 + * (6 << 28 + 6 << 24 + 4 << 20 + 0 << 16 + 4 << 12 + 6 << 8 + 6 << 4 + 10). + * Command group 3 is reserved and should never be used. + */ +#define COMMAND_SIZE(opcode) \ + (16 - (15 & (1715488362 >> (4 * (((opcode) >> 5) & 7))))) static inline unsigned scsi_command_size(const unsigned char *cmnd) -- 2.11.0