Received: by 10.192.245.15 with SMTP id i15csp934543imn; Sat, 10 Mar 2018 13:27:08 -0800 (PST) X-Google-Smtp-Source: AG47ELsjl8NLa+MuyvvjshYzbTvRIt/muw6FxzQW7y8+l/zaCOnxN97vvd5uFxYEc7NZ1Pn56ipI X-Received: by 2002:a17:902:52a6:: with SMTP id a35-v6mr3169808pli.179.1520717227911; Sat, 10 Mar 2018 13:27:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1520717227; cv=none; d=google.com; s=arc-20160816; b=MoLzFMrVwTVzjhK9npoWSDt1omM4tgEIZvjxwDgEmYtXiqMh9GW4sdwtO/gj8+qr5J vMPCs+R56kWssOlYdxtqfO4FdIemLF5UPBLB7GM3sBm2bnw/EA7GbOYRlU61AXbfongz F6UqvcCRnconC8mSElt3CqBadK9kuCIxj+D9SzPp8ri416ZJ2gZ4skpt2YYERa279i5N HklWVXrtaRst/i9oUyTvTRiOIqLzdg3tLVQ/5SCuAP/GkWXW2reTvWrfZVWVMqfTLBuE Lri3HarRWDXqA0rwUjP9B8tnmt0m+gAj4DqpnCq4wEWSYbt7+W0E02T332GB/+MTxJFE fsig== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:reply-to :arc-authentication-results; bh=SEs06PooPZiHXaWeFrCBXaxACmMzQhvVJnxP0nV59yw=; b=ttSNQH9VxY6yTKSUQ4ohL/TY5Tq4uZcBxG6YPMxzmY0hkQh/19uPAkD/ZJ++UBC+93 bxJmOMxce+5uNolujtQzbwavyR6J+AJy+b7bd63dEMh/pMrR9D73HI59RAtVBbv/Uos8 3Js5Yf5o/dfQ9RV9wE/7KBeXGHWUwpVNBiln0XKki6Ssb4ulh5kjSz7gjszi+/rYMbCu TfjdrHU0O0ES88WiON/COwerd2UmLxHqJco1peE54gsuQ7IjKee+0jD1KZFmCV+kAZGh 2GSgq2Tclb8FamDjfiwRoEUc4KTaD08dltkMwDyWoHXUdYe7DT3zbvMbIJHtK+44GeKW nwZg== 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 1-v6si3308179pla.496.2018.03.10.13.26.52; Sat, 10 Mar 2018 13:27: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 S1751216AbeCJVZk (ORCPT + 99 others); Sat, 10 Mar 2018 16:25:40 -0500 Received: from smtp.infotech.no ([82.134.31.41]:51401 "EHLO smtp.infotech.no" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751135AbeCJVZj (ORCPT ); Sat, 10 Mar 2018 16:25:39 -0500 X-Greylist: delayed 522 seconds by postgrey-1.27 at vger.kernel.org; Sat, 10 Mar 2018 16:25:39 EST Received: from localhost (localhost [127.0.0.1]) by smtp.infotech.no (Postfix) with ESMTP id 48873204196; Sat, 10 Mar 2018 22:16:55 +0100 (CET) X-Virus-Scanned: by amavisd-new-2.6.6 (20110518) (Debian) at infotech.no Received: from smtp.infotech.no ([127.0.0.1]) by localhost (smtp.infotech.no [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id QhHzFDitlZvo; Sat, 10 Mar 2018 22:16:48 +0100 (CET) Received: from [192.168.48.23] (host-184-164-15-239.dyn.295.ca [184.164.15.239]) by smtp.infotech.no (Postfix) with ESMTPA id 468C820414B; Sat, 10 Mar 2018 22:16:46 +0100 (CET) Reply-To: dgilbert@interlog.com Subject: Re: [PATCH] scsi: resolve COMMAND_SIZE at compile time To: James Bottomley , Stephen Kitt , Bart Van Assche Cc: "hare@suse.com" , "martin.petersen@oracle.com" , "axboe@kernel.dk" , "linux-scsi@vger.kernel.org" , "linux-kernel@vger.kernel.org" , "linux-block@vger.kernel.org" , "kernel-hardening@lists.openwall.com" References: <20180309232933.14e39858@heffalump.sk2.org> <20180309223355.21222-1-steve@sk2.org> <1520635631.2907.16.camel@wdc.com> <20180310142930.0692200b@heffalump.sk2.org> <1520714957.4495.5.camel@linux.vnet.ibm.com> From: Douglas Gilbert Message-ID: Date: Sat, 10 Mar 2018 16:16:45 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0 MIME-Version: 1.0 In-Reply-To: <1520714957.4495.5.camel@linux.vnet.ibm.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-CA Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2018-03-10 03:49 PM, James Bottomley wrote: > On Sat, 2018-03-10 at 14:29 +0100, Stephen Kitt wrote: >> Hi Bart, >> >> On Fri, 9 Mar 2018 22:47:12 +0000, Bart Van Assche > c.com> >> wrote: >>> >>> On Fri, 2018-03-09 at 23:33 +0100, Stephen Kitt wrote: >>>> >>>> +/* >>>> + * 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))))) >>> >>> To me this seems hard to read and hard to verify. Could this have >>> been >>> written as a combination of ternary expressions, e.g. using a gcc >>> statement >>> expression to ensure that opcode is evaluated once? >> >> That’s what I’d tried initially, e.g. >> >> #define COMMAND_SIZE(opcode) ({ \ >> int index = ((opcode) >> 5) & 7; \ >> index == 0 ? 6 : (index == 4 ? 16 : index == 3 || index == 5 ? 12 : >> 10); \ >> }) >> >> But gcc still reckons that results in a VLA, defeating the initial >> purpose of >> the exercise. >> >> Does it help if I make the magic value construction clearer? >> >> #define SCSI_COMMAND_SIZE_TBL ( \ >>    (16 -  6) \ >> + ((16 - 10) <<  4) \ >> + ((16 - 10) <<  8) \ >> + ((16 - 12) << 12) \ >> + ((16 - 16) << 16) \ >> + ((16 - 12) << 20) \ >> + ((16 - 10) << 24) \ >> + ((16 - 10) << 28)) >> >> #define COMMAND_SIZE(opcode) >> \ >>   (16 - (15 & (SCSI_COMMAND_SIZE_TBL >> (4 * (((opcode) >> 5) & >> 7))))) > > Couldn't we do the less clever thing of making the array a static const > and moving it to a header?  That way the compiler should be able to > work it out at compile time. And maybe add a comment that as of now (SPC-5 rev 19), COMMAND_SIZE is not valid for opcodes 0x7e and 0x7f plus everything above and including 0xc0. The latter ones are vendor specific and are loosely constrained, probably all even numbered lengths in the closed range: [6,260]. If the SCSI command sets want to keep up with NVMe, they may want to think about how they can gainfully use cdb_s that are > 64 bytes long. WRITE SCATTERED got into SBC-4 but READ GATHERED didn't, due to lack of interest. The READ GATHERED proposed was a bidi command, but it could have been a a simpler data-in command with a looong cdb (holding LBA, number_of_blocks pairs). Doug Gilbert