From patchwork Wed Aug 9 14:11:18 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Block X-Patchwork-Id: 9890713 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 4EB0B60384 for ; Wed, 9 Aug 2017 14:12:22 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 26E5428A99 for ; Wed, 9 Aug 2017 14:12:22 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1B47A28A9D; Wed, 9 Aug 2017 14:12:22 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9750B28A99 for ; Wed, 9 Aug 2017 14:12:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753034AbdHIOMT (ORCPT ); Wed, 9 Aug 2017 10:12:19 -0400 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:37267 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752931AbdHIOMP (ORCPT ); Wed, 9 Aug 2017 10:12:15 -0400 Received: from pps.filterd (m0098413.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.21/8.16.0.21) with SMTP id v79EBMkL110716 for ; Wed, 9 Aug 2017 10:12:14 -0400 Received: from e06smtp14.uk.ibm.com (e06smtp14.uk.ibm.com [195.75.94.110]) by mx0b-001b2d01.pphosted.com with ESMTP id 2c807u6pwe-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Wed, 09 Aug 2017 10:12:14 -0400 Received: from localhost by e06smtp14.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Wed, 9 Aug 2017 15:12:12 +0100 Received: from b06cxnps3074.portsmouth.uk.ibm.com (9.149.109.194) by e06smtp14.uk.ibm.com (192.168.101.144) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Wed, 9 Aug 2017 15:12:11 +0100 Received: from d06av25.portsmouth.uk.ibm.com (d06av25.portsmouth.uk.ibm.com [9.149.105.61]) by b06cxnps3074.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id v79ECAWP24969316 for ; Wed, 9 Aug 2017 14:12:10 GMT Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id F01BE11C070 for ; Wed, 9 Aug 2017 15:09:09 +0100 (BST) Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id DF8B811C066 for ; Wed, 9 Aug 2017 15:09:09 +0100 (BST) Received: from bblock-ThinkPad-W530 (unknown [9.152.212.81]) by d06av25.portsmouth.uk.ibm.com (Postfix) with ESMTP for ; Wed, 9 Aug 2017 15:09:09 +0100 (BST) Received: from bblock (uid 1000) (envelope-from bblock@linux.vnet.ibm.com) id 312cf5e6 by bblock-ThinkPad-W530 (DragonFly Mail Agent v0.9); Wed, 09 Aug 2017 16:12:09 +0200 From: Benjamin Block To: "James E . J . Bottomley" , "Martin K . Petersen" , Jens Axboe Cc: Benjamin Block , linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, linux-scsi@vger.kernel.org, Johannes Thumshirn , Christoph Hellwig , Steffen Maier , open-iscsi@googlegroups.com Subject: [RFC PATCH 4/6] bsg: refactor ioctl to use regular BSG-command infrastructure for SG_IO Date: Wed, 9 Aug 2017 16:11:18 +0200 X-Mailer: git-send-email 2.12.2 In-Reply-To: References: In-Reply-To: References: X-TM-AS-GCONF: 00 x-cbid: 17080914-0016-0000-0000-000004E19C4A X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 17080914-0017-0000-0000-00002819E773 Message-Id: X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2017-08-09_04:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 suspectscore=2 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1706020000 definitions=main-1708090220 Sender: linux-scsi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Before, the SG_IO ioctl for BSG devices used to use its own on-stack data to assemble and send the specified command. The read and write calls use their own infrastructure build around the struct bsg_command and a custom slab-pool for that. Rafactor this, so that SG_IO ioctl also uses struct bsg_command and the surrounding infrastructure. This way we use global defines like BSG_COMMAND_REPLY_BUFFERSIZE only in one place, rather than two, the handling of BSG commands gets more consistent, and it reduces some code- duplications (the bio-pointer handling). It also reduces the stack footprint by 320 to 384 bytes (depending on how large pointers are), and uses the active slab-implementation for efficient alloc/free. There are two other side-effects: - the 'duration' field in the sg header is now also filled for SG_IO calls, unlike before were it was always zero. - the BSG device queue-limit is also applied to SG_IO, unlike before were you could flood one BSG device with as many commands as you'd like. If one can trust older SG documentation this limit is applicable to either normal writes, or SG_IO calls; but this wasn't enforced before for SG_IO. A complete unification is not possible, as it then would also enqueue SG_IO commands in the BGS devices's command list, but this is only for the read- and write-calls. Signed-off-by: Benjamin Block --- block/bsg.c | 60 ++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 36 insertions(+), 24 deletions(-) diff --git a/block/bsg.c b/block/bsg.c index b924f1c23c58..8517361a9b3f 100644 --- a/block/bsg.c +++ b/block/bsg.c @@ -320,6 +320,17 @@ static void bsg_rq_end_io(struct request *rq, blk_status_t status) wake_up(&bd->wq_done); } +static int bsg_prep_add_command(struct bsg_command *bc, struct request *rq) +{ + bc->rq = rq; + bc->bio = rq->bio; + if (rq->next_rq) + bc->bidi_bio = rq->next_rq->bio; + bc->hdr.duration = jiffies; + + return 0 == (bc->hdr.flags & BSG_FLAG_Q_AT_TAIL); +} + /* * do final setup of a 'bc' and submit the matching 'rq' to the block * layer for io @@ -327,16 +338,11 @@ static void bsg_rq_end_io(struct request *rq, blk_status_t status) static void bsg_add_command(struct bsg_device *bd, struct request_queue *q, struct bsg_command *bc, struct request *rq) { - int at_head = (0 == (bc->hdr.flags & BSG_FLAG_Q_AT_TAIL)); + int at_head = bsg_prep_add_command(bc, rq); /* * add bc command to busy queue and submit rq for io */ - bc->rq = rq; - bc->bio = rq->bio; - if (rq->next_rq) - bc->bidi_bio = rq->next_rq->bio; - bc->hdr.duration = jiffies; spin_lock_irq(&bd->lock); list_add_tail(&bc->list, &bd->busy_list); spin_unlock_irq(&bd->lock); @@ -916,31 +922,37 @@ static long bsg_ioctl(struct file *file, unsigned int cmd, unsigned long arg) return scsi_cmd_ioctl(bd->queue, NULL, file->f_mode, cmd, uarg); } case SG_IO: { - struct request *rq; - u8 reply_buffer[BSG_COMMAND_REPLY_BUFFERSIZE] = { 0, }; - struct bio *bio, *bidi_bio = NULL; - struct sg_io_v4 hdr; + struct bsg_command *bc; int at_head; - if (copy_from_user(&hdr, uarg, sizeof(hdr))) - return -EFAULT; + bc = bsg_alloc_command(bd); + if (IS_ERR(bc)) + return PTR_ERR(bc); - rq = bsg_map_hdr(bd, &hdr, file->f_mode & FMODE_WRITE, - reply_buffer); - if (IS_ERR(rq)) - return PTR_ERR(rq); + if (copy_from_user(&bc->hdr, uarg, sizeof(bc->hdr))) { + ret = -EFAULT; + goto sg_io_out; + } - bio = rq->bio; - if (rq->next_rq) - bidi_bio = rq->next_rq->bio; + bc->rq = bsg_map_hdr(bd, &bc->hdr, file->f_mode & FMODE_WRITE, + bc->reply_buffer); + if (IS_ERR(bc->rq)) { + ret = PTR_ERR(bc->rq); + goto sg_io_out; + } - at_head = (0 == (hdr.flags & BSG_FLAG_Q_AT_TAIL)); - blk_execute_rq(bd->queue, NULL, rq, at_head); - ret = blk_complete_sgv4_hdr_rq(rq, &hdr, bio, bidi_bio); + at_head = bsg_prep_add_command(bc, bc->rq); + blk_execute_rq(bd->queue, NULL, bc->rq, at_head); + bc->hdr.duration = jiffies_to_msecs(jiffies - bc->hdr.duration); - if (copy_to_user(uarg, &hdr, sizeof(hdr))) - return -EFAULT; + ret = blk_complete_sgv4_hdr_rq(bc->rq, &bc->hdr, bc->bio, + bc->bidi_bio); + if (copy_to_user(uarg, &bc->hdr, sizeof(bc->hdr))) + ret = -EFAULT; + + sg_io_out: + bsg_free_command(bc); return ret; } /*