From patchwork Mon Apr 27 07:42:14 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martijn Coenen X-Patchwork-Id: 11511357 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 82F96912 for ; Mon, 27 Apr 2020 07:42:40 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 628772087E for ; Mon, 27 Apr 2020 07:42:40 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=android.com header.i=@android.com header.b="oGC3hWAK" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726699AbgD0Hmj (ORCPT ); Mon, 27 Apr 2020 03:42:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55232 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725785AbgD0Hmg (ORCPT ); Mon, 27 Apr 2020 03:42:36 -0400 Received: from mail-wr1-x441.google.com (mail-wr1-x441.google.com [IPv6:2a00:1450:4864:20::441]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E1674C061A41 for ; Mon, 27 Apr 2020 00:42:35 -0700 (PDT) Received: by mail-wr1-x441.google.com with SMTP id k13so19293928wrw.7 for ; Mon, 27 Apr 2020 00:42:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=android.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=qpydbUORH6esAS5gi+cl+9hT95SHJdDoYerkBD0BMSQ=; b=oGC3hWAK5PSMXwewUJVSLEn3ogH9c7x5GKyIHrpt9Un7/DBlDORpuf62nVnG49Srxq CBPr6rjegtfdy4HmaesOJcdbArkYcmrSclkKdby2Szkv2vZysdmhW/l8xsDWAw2iyPnq jfboZFx7R4wbNNSHjoAWW5eMpNn76nkOspz55Y5+OFrUkDYZ8p0wXuqCEi45cqbxXUBG TeFDLDb0OfgPptEIe47XWWQm/bN+bVLTlgFBc4gekSc6hGd2QXs2dBk0V1KbvDBHqQW+ 3g1EWvZ+w2B1/oRR4QmdxkGkGcsOVE2LCqMRuxP+CV8IuHcnno6CdC3EKkUvtBdSkHpL gEZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=qpydbUORH6esAS5gi+cl+9hT95SHJdDoYerkBD0BMSQ=; b=beX5YpgTD8ozmwn2pFv6L8P7H+25x/Ew/4qgQJ/0mUgnil8QKc3vsoAb0Gk78ZYCkb 17L7+FV/mR3ECHETLkTtjWBLHJH3DzZEnpZ+0xzJ2OMdHmDG2cZL/+bBylSZ2MI2a0Uu Z9H0RDCmDPoAWJGXcl4k9MXa3kw/HPjMr4SR42poc1SNuZwtKnikWzP7FFUnye9nXxWY UTn8DVpULuW7sf2KuuNRpbo+BIRDoEAYTp9McPTXPY1wMkU4AKV5aoSsZfs3liqkFZmP sP2NFW3lk10wRpLb/k7j9Be1voi1KYOoHTxI3zrIPdClODap3KXCtY2M/h3Gnhdys7ua +n2w== X-Gm-Message-State: AGi0PuYVdfKH3ZSH52FodnotaU5INtgfip0dDQKUz5+s132ldNOrCgN6 5obmPC+r754vW4tEGqhaEn+9kg== X-Google-Smtp-Source: APiQypLFrOdL4tryNZXtOTQNslh1ABY9mm0tKM345BBzQpNwqTXF9WQtd8sATSSGF3XCbbu98PFwRA== X-Received: by 2002:a05:6000:12c7:: with SMTP id l7mr26755993wrx.239.1587973354636; Mon, 27 Apr 2020 00:42:34 -0700 (PDT) Received: from maco2.ams.corp.google.com (a83-162-234-235.adsl.xs4all.nl. [83.162.234.235]) by smtp.gmail.com with ESMTPSA id x132sm15091658wmg.33.2020.04.27.00.42.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Apr 2020 00:42:34 -0700 (PDT) From: Martijn Coenen To: axboe@kernel.dk, hch@lst.de, ming.lei@redhat.com Cc: narayan@google.com, zezeozue@google.com, kernel-team@android.com, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, maco@google.com, bvanassche@acm.org, Chaitanya.Kulkarni@wdc.com, jaegeuk@kernel.org, Martijn Coenen Subject: [PATCH v3 1/9] loop: Factor out loop size validation Date: Mon, 27 Apr 2020 09:42:14 +0200 Message-Id: <20200427074222.65369-2-maco@android.com> X-Mailer: git-send-email 2.26.2.303.gf8c07b1a785-goog In-Reply-To: <20200427074222.65369-1-maco@android.com> References: <20200427074222.65369-1-maco@android.com> MIME-Version: 1.0 Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org Ensuring we don't truncate loff_t when casting to sector_t is done in multiple places; factor it out. Signed-off-by: Martijn Coenen Reviewed-by: Christoph Hellwig --- drivers/block/loop.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index f1754262fc94..f812f11649d3 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -228,15 +228,30 @@ static void __loop_update_dio(struct loop_device *lo, bool dio) blk_mq_unfreeze_queue(lo->lo_queue); } +/** + * loop_validate_size() - validates that the passed in size fits in a sector_t + * @size: size to validate + */ +static int +loop_validate_size(loff_t size) +{ + if ((loff_t)(sector_t)size != size) + return -EFBIG; + else + return 0; +} + static int figure_loop_size(struct loop_device *lo, loff_t offset, loff_t sizelimit) { + int err; loff_t size = get_size(offset, sizelimit, lo->lo_backing_file); - sector_t x = (sector_t)size; struct block_device *bdev = lo->lo_device; - if (unlikely((loff_t)x != size)) - return -EFBIG; + err = loop_validate_size(size); + if (err) + return err; + if (lo->lo_offset != offset) lo->lo_offset = offset; if (lo->lo_sizelimit != sizelimit) @@ -1003,9 +1018,9 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode, !file->f_op->write_iter) lo_flags |= LO_FLAGS_READ_ONLY; - error = -EFBIG; size = get_loop_size(lo, file); - if ((loff_t)(sector_t)size != size) + error = loop_validate_size(size); + if (error) goto out_unlock; error = loop_prepare_queue(lo); if (error) From patchwork Mon Apr 27 07:42:15 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martijn Coenen X-Patchwork-Id: 11511375 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id DB3C81667 for ; Mon, 27 Apr 2020 07:43:20 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C2B1F21835 for ; Mon, 27 Apr 2020 07:43:20 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=android.com header.i=@android.com header.b="arVADTHp" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726680AbgD0Hmi (ORCPT ); Mon, 27 Apr 2020 03:42:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55234 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726661AbgD0Hmh (ORCPT ); Mon, 27 Apr 2020 03:42:37 -0400 Received: from mail-wm1-x341.google.com (mail-wm1-x341.google.com [IPv6:2a00:1450:4864:20::341]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E54CCC0610D5 for ; Mon, 27 Apr 2020 00:42:36 -0700 (PDT) Received: by mail-wm1-x341.google.com with SMTP id 188so18383689wmc.2 for ; Mon, 27 Apr 2020 00:42:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=android.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=WklQum+x0z5V9R5tHm2+hiPAk8mSt3Evh9Pc0IUIHmE=; b=arVADTHpoWcouRgjhe2ruxU3zURDN9Du+CEQ3uvkL+e/yzk8bWfu5JZuOvtahupy+Z 8vuK68H2XLgMvUUZhwopsrfOgJX2mXCEoxIfDMQketXBXKOXXgUgV5R1RvJZYDWCY1uA tCWS4wIePmfaRCnzzQ4iZxcu3tQyS0pydLrasKCdnnlRffNYZFauFEADls78CJvfTadT C4ACE26NgonIzFM0LtE2vjET/1mIet+4YB+wtRIE/05eTFrQYYM9HA+wJyrCDHybnnm2 3wDqJiAG8oYB9ATo4V9C27V1VHEKXvx4VTykGP3TMIIQoQND7zhBVs6v6Yfm6PUB5s3w vGQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=WklQum+x0z5V9R5tHm2+hiPAk8mSt3Evh9Pc0IUIHmE=; b=d8HO7R6TjO0r5/MUWp321K+r24hOkrDDD1g5WBlchGRdWiHh5OEOFqrQqRE1Kvilqx bURtWhTcVX/YOCryGXYtLmoiI6OnpT/DsVhRKqnxtvFuss+8pZXSb92381Rp0r+RValo 3xkKSmusrUvCwB6WSJDy64g4yn6MeSWQ0GpC/GktgOBJuNXmUUJ0E6QVp4KwllCs3tiR tHRPZqr5inmpvwDL3KDIFXAKw4X1ZF4v0JLhtRRkTfP9eY/5uAcrXGdIXXGQ7fA2ieUq y1CZKtPdyY/EpyK/W0Abs/M2hTCKo3dRTZvd9ZJs/7VjapFwqCuYcE9Uwd1bfFFXCfYw PJ5Q== X-Gm-Message-State: AGi0PuYP+gPRsVnLkAZ7gcizOxPpLvZsvbDkj7Qg7jtUGEVRITLGqoTC yZAFFHEjzUOAwAlGPhMADXVd3g== X-Google-Smtp-Source: APiQypJ83XmOkAsrqagWwyC9NoAKpSuxzYwOYns2jlzw0+6U75xJxgkndNV5WRm/GeYCC+L0SLqcFA== X-Received: by 2002:a1c:7c18:: with SMTP id x24mr23185171wmc.146.1587973355743; Mon, 27 Apr 2020 00:42:35 -0700 (PDT) Received: from maco2.ams.corp.google.com (a83-162-234-235.adsl.xs4all.nl. [83.162.234.235]) by smtp.gmail.com with ESMTPSA id x132sm15091658wmg.33.2020.04.27.00.42.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Apr 2020 00:42:35 -0700 (PDT) From: Martijn Coenen To: axboe@kernel.dk, hch@lst.de, ming.lei@redhat.com Cc: narayan@google.com, zezeozue@google.com, kernel-team@android.com, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, maco@google.com, bvanassche@acm.org, Chaitanya.Kulkarni@wdc.com, jaegeuk@kernel.org, Martijn Coenen Subject: [PATCH v3 2/9] loop: Factor out setting loop device size Date: Mon, 27 Apr 2020 09:42:15 +0200 Message-Id: <20200427074222.65369-3-maco@android.com> X-Mailer: git-send-email 2.26.2.303.gf8c07b1a785-goog In-Reply-To: <20200427074222.65369-1-maco@android.com> References: <20200427074222.65369-1-maco@android.com> MIME-Version: 1.0 Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org This code is used repeatedly. Signed-off-by: Martijn Coenen Reviewed-by: Christoph Hellwig --- drivers/block/loop.c | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index f812f11649d3..4630d098cc54 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -241,12 +241,29 @@ loop_validate_size(loff_t size) return 0; } +/** + * loop_set_size() - sets device size and notifies userspace + * @lo: struct loop_device to set the size for + * @size: new size of the loop device + * + * Callers must validate that the size passed into this function fits into + * a sector_t, eg using loop_validate_size() + */ +static void loop_set_size(struct loop_device *lo, loff_t size) +{ + struct block_device *bdev = lo->lo_device; + + set_capacity(lo->lo_disk, size); + bd_set_size(bdev, size << SECTOR_SHIFT); + /* let user-space know about the new size */ + kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); +} + static int figure_loop_size(struct loop_device *lo, loff_t offset, loff_t sizelimit) { int err; loff_t size = get_size(offset, sizelimit, lo->lo_backing_file); - struct block_device *bdev = lo->lo_device; err = loop_validate_size(size); if (err) @@ -256,10 +273,9 @@ figure_loop_size(struct loop_device *lo, loff_t offset, loff_t sizelimit) lo->lo_offset = offset; if (lo->lo_sizelimit != sizelimit) lo->lo_sizelimit = sizelimit; - set_capacity(lo->lo_disk, x); - bd_set_size(bdev, (loff_t)get_capacity(bdev->bd_disk) << 9); - /* let user-space know about the new size */ - kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); + + loop_set_size(lo, size); + return 0; } @@ -1055,11 +1071,8 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode, loop_update_rotational(lo); loop_update_dio(lo); - set_capacity(lo->lo_disk, size); - bd_set_size(bdev, size << 9); loop_sysfs_init(lo); - /* let user-space know about the new size */ - kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); + loop_set_size(lo, size); set_blocksize(bdev, S_ISBLK(inode->i_mode) ? block_size(inode->i_bdev) : PAGE_SIZE); From patchwork Mon Apr 27 07:42:16 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martijn Coenen X-Patchwork-Id: 11511369 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id EA23F912 for ; Mon, 27 Apr 2020 07:43:15 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D2C2C2087E for ; Mon, 27 Apr 2020 07:43:15 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=android.com header.i=@android.com header.b="qiWr5IRy" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726700AbgD0Hmj (ORCPT ); Mon, 27 Apr 2020 03:42:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55244 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726690AbgD0Hmi (ORCPT ); Mon, 27 Apr 2020 03:42:38 -0400 Received: from mail-wm1-x344.google.com (mail-wm1-x344.google.com [IPv6:2a00:1450:4864:20::344]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 33B1CC061A10 for ; Mon, 27 Apr 2020 00:42:38 -0700 (PDT) Received: by mail-wm1-x344.google.com with SMTP id u16so19264929wmc.5 for ; Mon, 27 Apr 2020 00:42:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=android.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=8xcqA+A5T/eSVdFtVfWGXjYGiqSe96zXgzrOUnJqE+k=; b=qiWr5IRyG5B4RKrJoDDtT8SjaYb8O6m4vlG/UDbEJ+kzh9zuT8Kr48zBapp3HkOhdt c/b7+BF3iGfc6P/0+/VJSbLn1rBPNWxkB1Mt92wUX/UR4JSxKE0g2SjorK89CNnzZuqe xeGdXtKQ4IjckhaETkRgjTP4x2Xf0qutb2DD6Ha81BDA2YynHDUQpauF4So5yeP1uKng 7/ZaauHsfmDsK9Skv1adaZEg6pN7W00u/ftK1FybB5FymVSnUyTfhQC2TbtH3/G26gxw 0V+WKXeEoUijb53PjRyjSH5fBlR0/nrD/hw2PVJAmiz4veXYrbEPNLgbvS016EpWgMET F5CQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=8xcqA+A5T/eSVdFtVfWGXjYGiqSe96zXgzrOUnJqE+k=; b=F0aAAYiZ6q/rknQ3K4mU3QbgOVj9gOPtHkXJHtC5rEXsAfRujB2RmdNq3ibZyx9XAL dGcJ1sF90DRdlR3zM0eMbooih1IY/RZ8BVG+fg8X/G7vaVYO/RHHd6e7G5IzGZ7c0zdE pxWzx3Y32eByXZzBbQIAMvVNauCZNdyjSv/aTHDNRpfyU3qNKqNW6IG5T+WhlxXHSUDg dxzC73xUrmTHy8JROxg2Rc+p+ITbb03OvBrSbbfa48h5l2q8BqZC9Kyw/ozfg+qt8VfY JxzPcFY9sWfIr/C1yJvGZ9CJ9j5jKkeOE32s+Z2ZVbfN9kmPTd9Dr7Zl+EY7Q19oBy+J ai/Q== X-Gm-Message-State: AGi0PuYDXzzN/kDTHjJmWKV8gCA31eTdWJLFWMrRjHwSRSM04ibuKHD3 3j8EGQ7FZ1Wvv/eAJkhE2K2vpw== X-Google-Smtp-Source: APiQypIah0sHrAJ/x6WbRfPBVzVuW7jH0seRoMM3/pJfodEWhuoSFpkQDKte+QdQW/Q4yzWxxZyiBg== X-Received: by 2002:a1c:f418:: with SMTP id z24mr24440234wma.122.1587973357000; Mon, 27 Apr 2020 00:42:37 -0700 (PDT) Received: from maco2.ams.corp.google.com (a83-162-234-235.adsl.xs4all.nl. [83.162.234.235]) by smtp.gmail.com with ESMTPSA id x132sm15091658wmg.33.2020.04.27.00.42.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Apr 2020 00:42:36 -0700 (PDT) From: Martijn Coenen To: axboe@kernel.dk, hch@lst.de, ming.lei@redhat.com Cc: narayan@google.com, zezeozue@google.com, kernel-team@android.com, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, maco@google.com, bvanassche@acm.org, Chaitanya.Kulkarni@wdc.com, jaegeuk@kernel.org, Martijn Coenen Subject: [PATCH v3 3/9] loop: Switch to set_capacity_revalidate_and_notify() Date: Mon, 27 Apr 2020 09:42:16 +0200 Message-Id: <20200427074222.65369-4-maco@android.com> X-Mailer: git-send-email 2.26.2.303.gf8c07b1a785-goog In-Reply-To: <20200427074222.65369-1-maco@android.com> References: <20200427074222.65369-1-maco@android.com> MIME-Version: 1.0 Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org This was recently added to block/genhd.c, and takes care of both updating the capacity and notifying userspace of the new size. Signed-off-by: Martijn Coenen Reviewed-by: Christoph Hellwig --- drivers/block/loop.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index 4630d098cc54..2e2874318393 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -253,10 +253,10 @@ static void loop_set_size(struct loop_device *lo, loff_t size) { struct block_device *bdev = lo->lo_device; - set_capacity(lo->lo_disk, size); bd_set_size(bdev, size << SECTOR_SHIFT); + /* let user-space know about the new size */ - kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); + set_capacity_revalidate_and_notify(lo->lo_disk, size, false); } static int From patchwork Mon Apr 27 07:42:17 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martijn Coenen X-Patchwork-Id: 11511371 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 904411575 for ; Mon, 27 Apr 2020 07:43:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 74F2D21835 for ; Mon, 27 Apr 2020 07:43:16 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=android.com header.i=@android.com header.b="fnexYmog" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726829AbgD0HnP (ORCPT ); Mon, 27 Apr 2020 03:43:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55248 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726710AbgD0Hmj (ORCPT ); Mon, 27 Apr 2020 03:42:39 -0400 Received: from mail-wm1-x343.google.com (mail-wm1-x343.google.com [IPv6:2a00:1450:4864:20::343]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6FC95C061A10 for ; Mon, 27 Apr 2020 00:42:39 -0700 (PDT) Received: by mail-wm1-x343.google.com with SMTP id u16so19265001wmc.5 for ; Mon, 27 Apr 2020 00:42:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=android.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=LlF9hQrtqAVkh9UpGKlmxEFlHX/UD9uxV1eHlcxAJ3U=; b=fnexYmogjOXup/e3tYS2gP/6MnVAfVxckYSj8bpFo/B7M3YWZgyo8m8WwEIDajR5bi crzfuWav0zcI76Y+F0wc3xFuRGtCWuxBTLfhPWoJKVDH+PAdMVwIIiTVapIEro0FTPTp lyFX2fVqx24UivdL9QiebFgeoriTR/NOp/wWnAbTDGXWyPuAmy7QJnph0uwo8C1EaWfs FcgV5HBMNtD9mjZekcaB3Y0NdMac/bg1hhyEblwD/+X3FtemNqJO3C/+udCIqsFakKRS 4zO/cLKGFqu3EcorY5TXwjBtYzIw+AcepWUJdPa8UHZJwK8HjbZ76IyT6fqSYWhvdF4s yDIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=LlF9hQrtqAVkh9UpGKlmxEFlHX/UD9uxV1eHlcxAJ3U=; b=p7/fL5o+XXGN/X2eFD1IXeDTQQTsuDL41oRIAzvg18XuiPN1YN6ZrU/0m6sx/fo5FY L5DO3jlUVpFrYHBgtCHjKr3UnBDKQnHYnuhiNs1LNFtyyakyuWSKQPKi34S2Dn+mpQJP EXWg8q+gs4lpqELUvEPPXOcjuIwoBKYoiavVhwdCtQ2MHRz60vkqRRfjpFT2pGmGCoYs 7z+QXEKpyiIDnBM88G2vsORk+7rAB1cuQo4axVAha6ceQueKNV+198gT/NbZVno4k0af fzNmSHNuyYj8TZpOAbntDmuGMS8zFlwj8br9mE5W603we2igRE/iP9fSAVoXQbRiC+e8 pC2w== X-Gm-Message-State: AGi0Puaf7dkoe2DlGy1PX3G5Qb7gv+opukr7u46HNFlrelaSuegvFb1t 73sy85KJvHGpdwP3vmdCrv31Wg== X-Google-Smtp-Source: APiQypKxaIEIrAbS4VD6DPhHwA8Z9c0Iikjybz7Gls4+S+TFILJ5eZ7Rv3r3svM3sG1cy2rxDanIYg== X-Received: by 2002:a05:600c:414b:: with SMTP id h11mr24580948wmm.9.1587973358112; Mon, 27 Apr 2020 00:42:38 -0700 (PDT) Received: from maco2.ams.corp.google.com (a83-162-234-235.adsl.xs4all.nl. [83.162.234.235]) by smtp.gmail.com with ESMTPSA id x132sm15091658wmg.33.2020.04.27.00.42.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Apr 2020 00:42:37 -0700 (PDT) From: Martijn Coenen To: axboe@kernel.dk, hch@lst.de, ming.lei@redhat.com Cc: narayan@google.com, zezeozue@google.com, kernel-team@android.com, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, maco@google.com, bvanassche@acm.org, Chaitanya.Kulkarni@wdc.com, jaegeuk@kernel.org, Martijn Coenen Subject: [PATCH v3 4/9] loop: Refactor loop_set_status() size calculation Date: Mon, 27 Apr 2020 09:42:17 +0200 Message-Id: <20200427074222.65369-5-maco@android.com> X-Mailer: git-send-email 2.26.2.303.gf8c07b1a785-goog In-Reply-To: <20200427074222.65369-1-maco@android.com> References: <20200427074222.65369-1-maco@android.com> MIME-Version: 1.0 Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org figure_loop_size() calculates the loop size based on the passed in parameters, but at the same time it updates the offset and sizelimit parameters in the loop device configuration. That is a somewhat unexpected side effect of a function with this name, and it is only only needed by one of the two callers of this function - loop_set_status(). Move the lo_offset and lo_sizelimit assignment back into loop_set_status(), and use the newly factored out functions to validate and apply the newly calculated size. This allows us to get rid of figure_loop_size in a follow-up commit. Signed-off-by: Martijn Coenen Reviewed-by: Christoph Hellwig --- drivers/block/loop.c | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index 2e2874318393..d0f17ee1e29b 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -1295,6 +1295,8 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info) kuid_t uid = current_uid(); struct block_device *bdev; bool partscan = false; + bool size_changed = false; + loff_t validated_size; err = mutex_lock_killable(&loop_ctl_mutex); if (err) @@ -1316,6 +1318,13 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info) if (lo->lo_offset != info->lo_offset || lo->lo_sizelimit != info->lo_sizelimit) { + loff_t size = get_size(info->lo_offset, info->lo_sizelimit, + lo->lo_backing_file); + err = loop_validate_size(size); + if (err) + goto out_unlock; + size_changed = true; + validated_size = size; sync_blockdev(lo->lo_device); kill_bdev(lo->lo_device); } @@ -1323,6 +1332,15 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info) /* I/O need to be drained during transfer transition */ blk_mq_freeze_queue(lo->lo_queue); + if (size_changed && lo->lo_device->bd_inode->i_mapping->nrpages) { + /* If any pages were dirtied after kill_bdev(), try again */ + err = -EAGAIN; + pr_warn("%s: loop%d (%s) has still dirty pages (nrpages=%lu)\n", + __func__, lo->lo_number, lo->lo_file_name, + lo->lo_device->bd_inode->i_mapping->nrpages); + goto out_unfreeze; + } + err = loop_release_xfer(lo); if (err) goto out_unfreeze; @@ -1346,22 +1364,8 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info) if (err) goto out_unfreeze; - if (lo->lo_offset != info->lo_offset || - lo->lo_sizelimit != info->lo_sizelimit) { - /* kill_bdev should have truncated all the pages */ - if (lo->lo_device->bd_inode->i_mapping->nrpages) { - err = -EAGAIN; - pr_warn("%s: loop%d (%s) has still dirty pages (nrpages=%lu)\n", - __func__, lo->lo_number, lo->lo_file_name, - lo->lo_device->bd_inode->i_mapping->nrpages); - goto out_unfreeze; - } - if (figure_loop_size(lo, info->lo_offset, info->lo_sizelimit)) { - err = -EFBIG; - goto out_unfreeze; - } - } - + lo->lo_offset = info->lo_offset; + lo->lo_sizelimit = info->lo_sizelimit; memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE); memcpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE); lo->lo_file_name[LO_NAME_SIZE-1] = 0; @@ -1385,6 +1389,9 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info) lo->lo_key_owner = uid; } + if (size_changed) + loop_set_size(lo, validated_size); + loop_config_discard(lo); /* update dio if lo_offset or transfer is changed */ From patchwork Mon Apr 27 07:42:18 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martijn Coenen X-Patchwork-Id: 11511367 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 3768F912 for ; Mon, 27 Apr 2020 07:43:07 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1EECD21D7A for ; Mon, 27 Apr 2020 07:43:07 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=android.com header.i=@android.com header.b="vsDvwOEJ" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726731AbgD0Hml (ORCPT ); Mon, 27 Apr 2020 03:42:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55256 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726722AbgD0Hml (ORCPT ); Mon, 27 Apr 2020 03:42:41 -0400 Received: from mail-wm1-x341.google.com (mail-wm1-x341.google.com [IPv6:2a00:1450:4864:20::341]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A936FC061A0F for ; Mon, 27 Apr 2020 00:42:40 -0700 (PDT) Received: by mail-wm1-x341.google.com with SMTP id v8so15256991wma.0 for ; Mon, 27 Apr 2020 00:42:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=android.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=J2ueFYfJbnvHeCjbZWouiTCT/5yxNTxRW64HSkOHS6k=; b=vsDvwOEJ4ADtwAftYHFSGr7eBIujIoTjebnwYye1XpqKCZSfYnAOH7Cf97R/2uLbnb JBWFiDcW1XLYhlp3+3xx6qpbdMCcCzISQUNLQn5V5r5eVxUwEDL7jeBGpQMzPcWpI+S5 D9Au/BHAZkiwzA/a+CYZBYvVx0yLdfpxoXubCjvQAUcxc/0iYSGepnCTGTQtx0fj/YPM 3xcF1tgHw8rbdb5u8fz0PjvwmjumdZu+YXalSfKfb+PRtbXJYLUPtR8zgzeGUT/bA4UE QNhsXd0HnLuSuR0NAWfTfJJ9NBzya/1L9ejLH/TzbGP/g6yQfD6LTLRvV2lp4a/FFgXc ydQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=J2ueFYfJbnvHeCjbZWouiTCT/5yxNTxRW64HSkOHS6k=; b=Bqe4jsMEW/rgnpOLoeNQNJQZdHMumWtlFlQIWu6iGPvO3Ew3GwBoxE2AFVPmBzHOAl kOkfwWligVC69ciV6kpJSdmmLyhBzJ9mzPTev7UnKTiwAUihhlQmAKVoJp3pBY3h/JyV f1GfBdD3FQobJk2vHdGrP5KmCOI1UOg7hwMJEfZd2AyoaGunvEOcpiOTOwiLggGrHo5r E/sFmWNAkI7w4dbjL7E0xZL/Zhdlm/UHtfaIzlgXB+lYeTXHtgUtTvvo4SgdOMGQt2Ii kVCb9l8C2vuI95Y5U78XE7Lb6u53Nu4aXt9PcMXyaMKBSKrNCmZhHvVaR/k3QklzpFwu 0i3A== X-Gm-Message-State: AGi0PuaGPnRJMDazjmFsqClzz4ri4FxHTPFKT6GSymw1QeWVVsc/y+rq L1rgeblnXfWk6F59NI3gVOKw9g== X-Google-Smtp-Source: APiQypLAVK+627PiCW2wwBfbRkTRj2/fkVJ1Yx3bN4CYoMqpPxAa2E0f2Q6Ce/yiBmIBM5BPj0wBZQ== X-Received: by 2002:a1c:96c6:: with SMTP id y189mr26153350wmd.106.1587973359410; Mon, 27 Apr 2020 00:42:39 -0700 (PDT) Received: from maco2.ams.corp.google.com (a83-162-234-235.adsl.xs4all.nl. [83.162.234.235]) by smtp.gmail.com with ESMTPSA id x132sm15091658wmg.33.2020.04.27.00.42.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Apr 2020 00:42:38 -0700 (PDT) From: Martijn Coenen To: axboe@kernel.dk, hch@lst.de, ming.lei@redhat.com Cc: narayan@google.com, zezeozue@google.com, kernel-team@android.com, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, maco@google.com, bvanassche@acm.org, Chaitanya.Kulkarni@wdc.com, jaegeuk@kernel.org, Martijn Coenen Subject: [PATCH v3 5/9] loop: Remove figure_loop_size() Date: Mon, 27 Apr 2020 09:42:18 +0200 Message-Id: <20200427074222.65369-6-maco@android.com> X-Mailer: git-send-email 2.26.2.303.gf8c07b1a785-goog In-Reply-To: <20200427074222.65369-1-maco@android.com> References: <20200427074222.65369-1-maco@android.com> MIME-Version: 1.0 Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org This function was now only used by loop_set_capacity(), and updating the offset and sizelimit is no longer necessary in that case. Just open code the remaining code in the caller instead. Signed-off-by: Martijn Coenen Reviewed-by: Christoph Hellwig --- drivers/block/loop.c | 33 ++++++++++++--------------------- 1 file changed, 12 insertions(+), 21 deletions(-) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index d0f17ee1e29b..d9a1a7e8b192 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -259,26 +259,6 @@ static void loop_set_size(struct loop_device *lo, loff_t size) set_capacity_revalidate_and_notify(lo->lo_disk, size, false); } -static int -figure_loop_size(struct loop_device *lo, loff_t offset, loff_t sizelimit) -{ - int err; - loff_t size = get_size(offset, sizelimit, lo->lo_backing_file); - - err = loop_validate_size(size); - if (err) - return err; - - if (lo->lo_offset != offset) - lo->lo_offset = offset; - if (lo->lo_sizelimit != sizelimit) - lo->lo_sizelimit = sizelimit; - - loop_set_size(lo, size); - - return 0; -} - static inline int lo_do_transfer(struct loop_device *lo, int cmd, struct page *rpage, unsigned roffs, @@ -1566,10 +1546,21 @@ loop_get_status64(struct loop_device *lo, struct loop_info64 __user *arg) { static int loop_set_capacity(struct loop_device *lo) { + int err; + loff_t size; + if (unlikely(lo->lo_state != Lo_bound)) return -ENXIO; - return figure_loop_size(lo, lo->lo_offset, lo->lo_sizelimit); + size = get_loop_size(lo, lo->lo_backing_file); + + err = loop_validate_size(size); + if (err) + return err; + + loop_set_size(lo, size); + + return 0; } static int loop_set_dio(struct loop_device *lo, unsigned long arg) From patchwork Mon Apr 27 07:42:19 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martijn Coenen X-Patchwork-Id: 11511365 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 906281575 for ; Mon, 27 Apr 2020 07:43:05 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 78BDA21835 for ; Mon, 27 Apr 2020 07:43:05 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=android.com header.i=@android.com header.b="gVLXq5UT" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726757AbgD0HnE (ORCPT ); Mon, 27 Apr 2020 03:43:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55262 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726752AbgD0Hmn (ORCPT ); Mon, 27 Apr 2020 03:42:43 -0400 Received: from mail-wm1-x344.google.com (mail-wm1-x344.google.com [IPv6:2a00:1450:4864:20::344]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D267CC061A10 for ; Mon, 27 Apr 2020 00:42:41 -0700 (PDT) Received: by mail-wm1-x344.google.com with SMTP id x4so18395548wmj.1 for ; Mon, 27 Apr 2020 00:42:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=android.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=BPVCBcZ+vxq/vE4PZ944u0smAG80GpkLUfGejPD/RBc=; b=gVLXq5UTLy0Ky0X9RXtF7PmsN+x21+1ldkywUzJDwSyOyLmpHneDPQ2I+VDgFJ/mn7 XuC4a8Kaiz+KkMu4MSvu+lqv+7aY3X1rc96lLpuBhsKGWTWEa6voZHHh8/5dA0q65fVd dX4V2Yt1Z+y9vkCTwk4Q7IJ1JD6U/4ggynoMWpUsIdYeWwzLRp+N1MwjaJEQuRxxTrx5 AKz5M/dllwEAHos70YtDbiHgGSaS3ll5T6UzwFIS7FvC1Icfo4sENqlzM80dgdXYkpPm OFpfaCmFtir8qeTArrTCXBCySxDTy2jyHHXn25HSYy8CoHx48mnCNNTq3duMpcfsjV1H iVZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=BPVCBcZ+vxq/vE4PZ944u0smAG80GpkLUfGejPD/RBc=; b=IvdOt37KTcs9IJceVN4a/H3Ak5hckYxI63z9w2JIvEojWxXcBwS+0drLDb/gKrJKHh ullCE8WKR7v8vfn0mS9L70ufoK+cXMwon4JP/MkXwkZ8mQ4r4vF5i3eqgsLEcBRsa50v Rf5X4SWk1PzpKgU6bwoFN1+pGVOMXvqir8QJKRljyvzkqCk561I5uER70T+QD+UHmn1G F/cQOSv2dPifLS3TR3w3sQcoNn7WCcmFra5sb8HUgeeZoqGLP3SDRFGtj2IMXTOP2bJf eW0FkXscDafHlmjUODCxGw5qPKKG1gOt8IKV+xpRNDVN/DlKnUOQxWIg7RzUOgmDgy0K u4Rw== X-Gm-Message-State: AGi0PuZ+G/cXjXwTTNHQ5/nA7MxgCJC1PzSHRho++Un903Z/oUltsyi2 NqnfUCIBtDloKJf27QU9SKhhrQ== X-Google-Smtp-Source: APiQypLpcwPdjusrbnmQUpHn2u+iuCu0JjOhCwJKZM9cdOEV08wmidKyKi1vkkGnkx6Ew9J/oslyXQ== X-Received: by 2002:a1c:49:: with SMTP id 70mr23488300wma.184.1587973360626; Mon, 27 Apr 2020 00:42:40 -0700 (PDT) Received: from maco2.ams.corp.google.com (a83-162-234-235.adsl.xs4all.nl. [83.162.234.235]) by smtp.gmail.com with ESMTPSA id x132sm15091658wmg.33.2020.04.27.00.42.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Apr 2020 00:42:39 -0700 (PDT) From: Martijn Coenen To: axboe@kernel.dk, hch@lst.de, ming.lei@redhat.com Cc: narayan@google.com, zezeozue@google.com, kernel-team@android.com, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, maco@google.com, bvanassche@acm.org, Chaitanya.Kulkarni@wdc.com, jaegeuk@kernel.org, Martijn Coenen Subject: [PATCH v3 6/9] loop: Factor out configuring loop from status Date: Mon, 27 Apr 2020 09:42:19 +0200 Message-Id: <20200427074222.65369-7-maco@android.com> X-Mailer: git-send-email 2.26.2.303.gf8c07b1a785-goog In-Reply-To: <20200427074222.65369-1-maco@android.com> References: <20200427074222.65369-1-maco@android.com> MIME-Version: 1.0 Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org Factor out this code into a separate function, so it can be reused by other code more easily. Signed-off-by: Martijn Coenen Reviewed-by: Christoph Hellwig --- drivers/block/loop.c | 117 +++++++++++++++++++++++++------------------ 1 file changed, 67 insertions(+), 50 deletions(-) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index d9a1a7e8b192..b55569fce975 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -1267,13 +1267,78 @@ static int loop_clr_fd(struct loop_device *lo) return __loop_clr_fd(lo, false); } +/** + * loop_set_status_from_info - configure device from loop_info + * @lo: struct loop_device to configure + * @info: struct loop_info64 to configure the device with + * + * Configures the loop device parameters according to the passed + * in loop_info64 configuration. + */ static int -loop_set_status(struct loop_device *lo, const struct loop_info64 *info) +loop_set_status_from_info(struct loop_device *lo, + const struct loop_info64 *info) { int err; struct loop_func_table *xfer; kuid_t uid = current_uid(); + + if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE) + return -EINVAL; + + err = loop_release_xfer(lo); + if (err) + return err; + + if (info->lo_encrypt_type) { + unsigned int type = info->lo_encrypt_type; + + if (type >= MAX_LO_CRYPT) + return -EINVAL; + xfer = xfer_funcs[type]; + if (xfer == NULL) + return -EINVAL; + } else + xfer = NULL; + + err = loop_init_xfer(lo, xfer, info); + if (err) + return err; + + lo->lo_offset = info->lo_offset; + lo->lo_sizelimit = info->lo_sizelimit; + memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE); + memcpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE); + lo->lo_file_name[LO_NAME_SIZE-1] = 0; + lo->lo_crypt_name[LO_NAME_SIZE-1] = 0; + + if (!xfer) + xfer = &none_funcs; + lo->transfer = xfer->transfer; + lo->ioctl = xfer->ioctl; + + if ((lo->lo_flags & LO_FLAGS_AUTOCLEAR) != + (info->lo_flags & LO_FLAGS_AUTOCLEAR)) + lo->lo_flags ^= LO_FLAGS_AUTOCLEAR; + + lo->lo_encrypt_key_size = info->lo_encrypt_key_size; + lo->lo_init[0] = info->lo_init[0]; + lo->lo_init[1] = info->lo_init[1]; + if (info->lo_encrypt_key_size) { + memcpy(lo->lo_encrypt_key, info->lo_encrypt_key, + info->lo_encrypt_key_size); + lo->lo_key_owner = uid; + } + + return 0; +} + +static int +loop_set_status(struct loop_device *lo, const struct loop_info64 *info) +{ + int err; struct block_device *bdev; + kuid_t uid = current_uid(); bool partscan = false; bool size_changed = false; loff_t validated_size; @@ -1291,10 +1356,6 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info) err = -ENXIO; goto out_unlock; } - if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE) { - err = -EINVAL; - goto out_unlock; - } if (lo->lo_offset != info->lo_offset || lo->lo_sizelimit != info->lo_sizelimit) { @@ -1321,54 +1382,10 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info) goto out_unfreeze; } - err = loop_release_xfer(lo); + err = loop_set_status_from_info(lo, info); if (err) goto out_unfreeze; - if (info->lo_encrypt_type) { - unsigned int type = info->lo_encrypt_type; - - if (type >= MAX_LO_CRYPT) { - err = -EINVAL; - goto out_unfreeze; - } - xfer = xfer_funcs[type]; - if (xfer == NULL) { - err = -EINVAL; - goto out_unfreeze; - } - } else - xfer = NULL; - - err = loop_init_xfer(lo, xfer, info); - if (err) - goto out_unfreeze; - - lo->lo_offset = info->lo_offset; - lo->lo_sizelimit = info->lo_sizelimit; - memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE); - memcpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE); - lo->lo_file_name[LO_NAME_SIZE-1] = 0; - lo->lo_crypt_name[LO_NAME_SIZE-1] = 0; - - if (!xfer) - xfer = &none_funcs; - lo->transfer = xfer->transfer; - lo->ioctl = xfer->ioctl; - - if ((lo->lo_flags & LO_FLAGS_AUTOCLEAR) != - (info->lo_flags & LO_FLAGS_AUTOCLEAR)) - lo->lo_flags ^= LO_FLAGS_AUTOCLEAR; - - lo->lo_encrypt_key_size = info->lo_encrypt_key_size; - lo->lo_init[0] = info->lo_init[0]; - lo->lo_init[1] = info->lo_init[1]; - if (info->lo_encrypt_key_size) { - memcpy(lo->lo_encrypt_key, info->lo_encrypt_key, - info->lo_encrypt_key_size); - lo->lo_key_owner = uid; - } - if (size_changed) loop_set_size(lo, validated_size); From patchwork Mon Apr 27 07:42:20 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martijn Coenen X-Patchwork-Id: 11511363 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 1482B912 for ; Mon, 27 Apr 2020 07:43:05 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id EC5C72098B for ; Mon, 27 Apr 2020 07:43:04 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=android.com header.i=@android.com header.b="j1gbTsin" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726779AbgD0HnD (ORCPT ); Mon, 27 Apr 2020 03:43:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55270 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726757AbgD0Hmn (ORCPT ); Mon, 27 Apr 2020 03:42:43 -0400 Received: from mail-wm1-x343.google.com (mail-wm1-x343.google.com [IPv6:2a00:1450:4864:20::343]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 39438C061A41 for ; Mon, 27 Apr 2020 00:42:43 -0700 (PDT) Received: by mail-wm1-x343.google.com with SMTP id z6so19301579wml.2 for ; Mon, 27 Apr 2020 00:42:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=android.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=z6AdbaZw5amSpR1OTBUs1mGG2xOgZkTKod8u323wdnU=; b=j1gbTsinaBOHHCiTpWUuIBw9z4yKIJrmeAQ+2qtSbwgf64HKByFWL1UsWv/dpI2Iuy 5IsrEQhr+7buKe6hGb3g07bYK+h2iESqSLVYBcv+v61glE4FygZFrR3zoevQdul6rcrz YFel6MbOauDSI8Tnd1TKpKl+GdhPB9RoALzH4jiFfSMVHm0JJqgmI6WMIoReOL7MZovg cShdx7UyPklTnp4u6n5kx0cFAmYxIiMIYvEYSCU/ZxNisjPnJKipycXLG8Fx1OZU9od1 37rfY10qYzd3Afk+ZskE2EjLJInDJYuX1a/D7d1KMQx8JaUgmUngxCLHhtUYii1eiSWj 4+dw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=z6AdbaZw5amSpR1OTBUs1mGG2xOgZkTKod8u323wdnU=; b=JVBCMI0ncUiFxdr5tsceNaHu6SHMwj80uJStR5MUIbkSGObW3qtA+hvPN5DeBokL1D jkK/+Xn6/vEUUcYsQ4SBhe1RMWEI9kX0k/D1L6IwNZYELEXhyfvA0dNKSbERYV6QY1by j0i+31DVn6546YbxokrDwN7z4tG1zuXMufHZh3vruhLZmGrqksYAOZ+CR1iNLGoIdtn9 DzwYUqUtcan6kWz3QTPz+SKLElQi3CcDsJ3NYRd59qMeSw2fLz390PT2o1DAu4rI4Y80 SlyYm84yHn5UCiV0ti2v14QyHTQNjwEbbj3ooARfG6OKTlFWW2j7kFuhBsKLN2WYkko3 X7pg== X-Gm-Message-State: AGi0Pubb1ibxNdo6O+ka0PAlm99CN3R3/2EIxHw7X+Z6mN4WdPT6LM9s xfpPW2V+MN+i86rdd23q+nCK9w== X-Google-Smtp-Source: APiQypKz0yDPUhaz7yPws4piP/BM54W17wyzklqe4uMkwsqVg9PlCsrCQpVHVAsiyoFRLvd1PTXldw== X-Received: by 2002:a7b:ce09:: with SMTP id m9mr24019050wmc.156.1587973361938; Mon, 27 Apr 2020 00:42:41 -0700 (PDT) Received: from maco2.ams.corp.google.com (a83-162-234-235.adsl.xs4all.nl. [83.162.234.235]) by smtp.gmail.com with ESMTPSA id x132sm15091658wmg.33.2020.04.27.00.42.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Apr 2020 00:42:41 -0700 (PDT) From: Martijn Coenen To: axboe@kernel.dk, hch@lst.de, ming.lei@redhat.com Cc: narayan@google.com, zezeozue@google.com, kernel-team@android.com, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, maco@google.com, bvanassche@acm.org, Chaitanya.Kulkarni@wdc.com, jaegeuk@kernel.org, Martijn Coenen Subject: [PATCH v3 7/9] loop: Move loop_set_status_from_info() and friends up Date: Mon, 27 Apr 2020 09:42:20 +0200 Message-Id: <20200427074222.65369-8-maco@android.com> X-Mailer: git-send-email 2.26.2.303.gf8c07b1a785-goog In-Reply-To: <20200427074222.65369-1-maco@android.com> References: <20200427074222.65369-1-maco@android.com> MIME-Version: 1.0 Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org So we can use it without forward declaration. This is a separate commit to make it easier to verify that this is just a move, without functional modifications. Signed-off-by: Martijn Coenen Reviewed-by: Christoph Hellwig --- drivers/block/loop.c | 206 +++++++++++++++++++++---------------------- 1 file changed, 103 insertions(+), 103 deletions(-) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index b55569fce975..cd1efe0eec5a 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -963,6 +963,109 @@ static void loop_update_rotational(struct loop_device *lo) blk_queue_flag_clear(QUEUE_FLAG_NONROT, q); } +static int +loop_release_xfer(struct loop_device *lo) +{ + int err = 0; + struct loop_func_table *xfer = lo->lo_encryption; + + if (xfer) { + if (xfer->release) + err = xfer->release(lo); + lo->transfer = NULL; + lo->lo_encryption = NULL; + module_put(xfer->owner); + } + return err; +} + +static int +loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer, + const struct loop_info64 *i) +{ + int err = 0; + + if (xfer) { + struct module *owner = xfer->owner; + + if (!try_module_get(owner)) + return -EINVAL; + if (xfer->init) + err = xfer->init(lo, i); + if (err) + module_put(owner); + else + lo->lo_encryption = xfer; + } + return err; +} + +/** + * loop_set_status_from_info - configure device from loop_info + * @lo: struct loop_device to configure + * @info: struct loop_info64 to configure the device with + * + * Configures the loop device parameters according to the passed + * in loop_info64 configuration. + */ +static int +loop_set_status_from_info(struct loop_device *lo, + const struct loop_info64 *info) +{ + int err; + struct loop_func_table *xfer; + kuid_t uid = current_uid(); + + if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE) + return -EINVAL; + + err = loop_release_xfer(lo); + if (err) + return err; + + if (info->lo_encrypt_type) { + unsigned int type = info->lo_encrypt_type; + + if (type >= MAX_LO_CRYPT) + return -EINVAL; + xfer = xfer_funcs[type]; + if (xfer == NULL) + return -EINVAL; + } else + xfer = NULL; + + err = loop_init_xfer(lo, xfer, info); + if (err) + return err; + + lo->lo_offset = info->lo_offset; + lo->lo_sizelimit = info->lo_sizelimit; + memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE); + memcpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE); + lo->lo_file_name[LO_NAME_SIZE-1] = 0; + lo->lo_crypt_name[LO_NAME_SIZE-1] = 0; + + if (!xfer) + xfer = &none_funcs; + lo->transfer = xfer->transfer; + lo->ioctl = xfer->ioctl; + + if ((lo->lo_flags & LO_FLAGS_AUTOCLEAR) != + (info->lo_flags & LO_FLAGS_AUTOCLEAR)) + lo->lo_flags ^= LO_FLAGS_AUTOCLEAR; + + lo->lo_encrypt_key_size = info->lo_encrypt_key_size; + lo->lo_init[0] = info->lo_init[0]; + lo->lo_init[1] = info->lo_init[1]; + if (info->lo_encrypt_key_size) { + memcpy(lo->lo_encrypt_key, info->lo_encrypt_key, + info->lo_encrypt_key_size); + lo->lo_key_owner = uid; + } + + return 0; +} + static int loop_set_fd(struct loop_device *lo, fmode_t mode, struct block_device *bdev, unsigned int arg) { @@ -1086,43 +1189,6 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode, return error; } -static int -loop_release_xfer(struct loop_device *lo) -{ - int err = 0; - struct loop_func_table *xfer = lo->lo_encryption; - - if (xfer) { - if (xfer->release) - err = xfer->release(lo); - lo->transfer = NULL; - lo->lo_encryption = NULL; - module_put(xfer->owner); - } - return err; -} - -static int -loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer, - const struct loop_info64 *i) -{ - int err = 0; - - if (xfer) { - struct module *owner = xfer->owner; - - if (!try_module_get(owner)) - return -EINVAL; - if (xfer->init) - err = xfer->init(lo, i); - if (err) - module_put(owner); - else - lo->lo_encryption = xfer; - } - return err; -} - static int __loop_clr_fd(struct loop_device *lo, bool release) { struct file *filp = NULL; @@ -1267,72 +1333,6 @@ static int loop_clr_fd(struct loop_device *lo) return __loop_clr_fd(lo, false); } -/** - * loop_set_status_from_info - configure device from loop_info - * @lo: struct loop_device to configure - * @info: struct loop_info64 to configure the device with - * - * Configures the loop device parameters according to the passed - * in loop_info64 configuration. - */ -static int -loop_set_status_from_info(struct loop_device *lo, - const struct loop_info64 *info) -{ - int err; - struct loop_func_table *xfer; - kuid_t uid = current_uid(); - - if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE) - return -EINVAL; - - err = loop_release_xfer(lo); - if (err) - return err; - - if (info->lo_encrypt_type) { - unsigned int type = info->lo_encrypt_type; - - if (type >= MAX_LO_CRYPT) - return -EINVAL; - xfer = xfer_funcs[type]; - if (xfer == NULL) - return -EINVAL; - } else - xfer = NULL; - - err = loop_init_xfer(lo, xfer, info); - if (err) - return err; - - lo->lo_offset = info->lo_offset; - lo->lo_sizelimit = info->lo_sizelimit; - memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE); - memcpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE); - lo->lo_file_name[LO_NAME_SIZE-1] = 0; - lo->lo_crypt_name[LO_NAME_SIZE-1] = 0; - - if (!xfer) - xfer = &none_funcs; - lo->transfer = xfer->transfer; - lo->ioctl = xfer->ioctl; - - if ((lo->lo_flags & LO_FLAGS_AUTOCLEAR) != - (info->lo_flags & LO_FLAGS_AUTOCLEAR)) - lo->lo_flags ^= LO_FLAGS_AUTOCLEAR; - - lo->lo_encrypt_key_size = info->lo_encrypt_key_size; - lo->lo_init[0] = info->lo_init[0]; - lo->lo_init[1] = info->lo_init[1]; - if (info->lo_encrypt_key_size) { - memcpy(lo->lo_encrypt_key, info->lo_encrypt_key, - info->lo_encrypt_key_size); - lo->lo_key_owner = uid; - } - - return 0; -} - static int loop_set_status(struct loop_device *lo, const struct loop_info64 *info) { From patchwork Mon Apr 27 07:42:21 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martijn Coenen X-Patchwork-Id: 11511361 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 1415E912 for ; Mon, 27 Apr 2020 07:43:03 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id F097D21835 for ; Mon, 27 Apr 2020 07:43:02 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=android.com header.i=@android.com header.b="Fpxnl/UA" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726777AbgD0Hmp (ORCPT ); Mon, 27 Apr 2020 03:42:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55278 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726770AbgD0Hmo (ORCPT ); Mon, 27 Apr 2020 03:42:44 -0400 Received: from mail-wr1-x442.google.com (mail-wr1-x442.google.com [IPv6:2a00:1450:4864:20::442]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 53BC7C061A0F for ; Mon, 27 Apr 2020 00:42:44 -0700 (PDT) Received: by mail-wr1-x442.google.com with SMTP id d15so17655954wrx.3 for ; Mon, 27 Apr 2020 00:42:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=android.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=rCmoWGSBp0bkzJg26kKRHWQrBipKAajMKVFOcPDe3LA=; b=Fpxnl/UAJRSu0MlJX/eTcPVQag3sJsu1tNWj/IhwcCrALMssjX1D99A/8Pf06ZRJm6 DfAGzVGkwco3mmYXL0yWR54tkAPJAAf+yQcA1MEaQn2Jf3OxWsY6W0gxjuIw4e7zuTWo RNniWSsJ3DojLB7zizi27lOPQZjG8Kn9AsJgdziHXKnnFPx/MioQSmvhTjNTVFZqxG4I WTT34OXOr8Nqhtc0wj175eXmlHwJFehGqmLASAfuw+xxfuipMG5hrfxmUv3sj6Ke569A H1jV+WQj8KCiVKzn63y86pH9x1SNV73l4zPWXeWLV/O5ZwOeChPNcp7YKR05Bdf33brv juiQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=rCmoWGSBp0bkzJg26kKRHWQrBipKAajMKVFOcPDe3LA=; b=NodWh/owyunxmlWRlcnPWA7c7e2EQsNlkJ2j6qKDXguu6SivBtLgtuEo66bFvKITjl OWDj2VvKqBqVGr2/aasmmruksI/OEt51DjVFtWbh3XEepcgbDtW0rE945OQkB76PBo18 VxkRtXHMEm8g5UQoHnAlkRCp9j74P6ViRwlFmNflNgca6kPWA+3VqZV4OuZ9+hpu8sUF b2weqMu2VSmMC9qEABgo8psePcqFfQzlisFP2LfIS1Xe0jAiopqgmhnHRVqdr7w47rVc ZpDj4dUf5EAOdg8jEPrS5Y+HutcM4zjfwxUkCh0+JSHUrDETd5/CsO6awMUcnFF0k8OM EMjw== X-Gm-Message-State: AGi0PuYYERTSoIhb/J66zkd4SnrylYkvpB75pwaKW6I6ZT9ry1YUmN1d W8yoSO+II1lDmOdf4qsjQt0EkQ== X-Google-Smtp-Source: APiQypIeVxgjC84n/+iNIZdbu7Zqu5t8RkSwPK2GboDSMrHKbOfN6W3WuwmPkwZd9nt91ItTKfwsaw== X-Received: by 2002:adf:cd0a:: with SMTP id w10mr25344895wrm.404.1587973363097; Mon, 27 Apr 2020 00:42:43 -0700 (PDT) Received: from maco2.ams.corp.google.com (a83-162-234-235.adsl.xs4all.nl. [83.162.234.235]) by smtp.gmail.com with ESMTPSA id x132sm15091658wmg.33.2020.04.27.00.42.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Apr 2020 00:42:42 -0700 (PDT) From: Martijn Coenen To: axboe@kernel.dk, hch@lst.de, ming.lei@redhat.com Cc: narayan@google.com, zezeozue@google.com, kernel-team@android.com, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, maco@google.com, bvanassche@acm.org, Chaitanya.Kulkarni@wdc.com, jaegeuk@kernel.org, Martijn Coenen Subject: [PATCH v3 8/9] loop: Rework lo_ioctl() __user argument casting Date: Mon, 27 Apr 2020 09:42:21 +0200 Message-Id: <20200427074222.65369-9-maco@android.com> X-Mailer: git-send-email 2.26.2.303.gf8c07b1a785-goog In-Reply-To: <20200427074222.65369-1-maco@android.com> References: <20200427074222.65369-1-maco@android.com> MIME-Version: 1.0 Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org In preparation for a new ioctl that needs to copy_from_user(); makes the code easier to read as well. Signed-off-by: Martijn Coenen Reviewed-by: Christoph Hellwig --- drivers/block/loop.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index cd1efe0eec5a..92bbe368ab62 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -1660,6 +1660,7 @@ static int lo_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd, unsigned long arg) { struct loop_device *lo = bdev->bd_disk->private_data; + void __user *argp = (void __user *) arg; int err; switch (cmd) { @@ -1672,21 +1673,19 @@ static int lo_ioctl(struct block_device *bdev, fmode_t mode, case LOOP_SET_STATUS: err = -EPERM; if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) { - err = loop_set_status_old(lo, - (struct loop_info __user *)arg); + err = loop_set_status_old(lo, argp); } break; case LOOP_GET_STATUS: - return loop_get_status_old(lo, (struct loop_info __user *) arg); + return loop_get_status_old(lo, argp); case LOOP_SET_STATUS64: err = -EPERM; if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) { - err = loop_set_status64(lo, - (struct loop_info64 __user *) arg); + err = loop_set_status64(lo, argp); } break; case LOOP_GET_STATUS64: - return loop_get_status64(lo, (struct loop_info64 __user *) arg); + return loop_get_status64(lo, argp); case LOOP_SET_CAPACITY: case LOOP_SET_DIRECT_IO: case LOOP_SET_BLOCK_SIZE: From patchwork Mon Apr 27 07:42:22 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martijn Coenen X-Patchwork-Id: 11511359 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 68FF2912 for ; Mon, 27 Apr 2020 07:43:01 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4A9EC2087E for ; Mon, 27 Apr 2020 07:43:01 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=android.com header.i=@android.com header.b="GT76iOHW" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726792AbgD0Hmr (ORCPT ); Mon, 27 Apr 2020 03:42:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55282 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726781AbgD0Hmr (ORCPT ); Mon, 27 Apr 2020 03:42:47 -0400 Received: from mail-wm1-x344.google.com (mail-wm1-x344.google.com [IPv6:2a00:1450:4864:20::344]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BAB02C061A0F for ; Mon, 27 Apr 2020 00:42:45 -0700 (PDT) Received: by mail-wm1-x344.google.com with SMTP id r26so19279653wmh.0 for ; Mon, 27 Apr 2020 00:42:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=android.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=X5F50p+vZUxpVs6G1Sij0eAs6fVJw9OhKD5dcn7Nhqk=; b=GT76iOHWX38pFtAPcXhMHsG0oXaug+HDpaN2ONpegjBg+3dLkZFpxCr/kfZYDXO8VF GEOToh+uwVDBj22S3PsngayTgpk3D5yHevxw9m7+UqRsbGyYFjCHGTZd7oERXdLMhHwW yCIth/TR3VZMwAtdHoRuOvENcw5jXjN13Fq9xDHFD/HM4Gn3wdNsoNFKFOJdnOy6MTPt pR8pXfQsBiqshnrj1BTgXSVh9Mbd/wyJE4oE6IIs34vOGWERjRKXpEi/Q+YUzvSJpSio 65ITJiTs/MZWd7Hx5qtzuA7iPgXhxWEHez0S4VZwsG/JcUkrIc+11UwlTSULIwKDg45W 11qw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=X5F50p+vZUxpVs6G1Sij0eAs6fVJw9OhKD5dcn7Nhqk=; b=aZB/eW23J7PC0WzBJcNhzwQu/bPiQK5BJvxBpLHH33nwrvUUcLgcvPe99XSG4QJD4J KGVc5Jhy+3l0o8n403hCfP1FkSMNC4cf783rsCLrRHZYdW3YUVeSCDQy8LveS+2yKF00 tvISM8hMlhswIDtjxbeaec8vAXg7CGji4QUI7mB8K7B6BIXvehJz0HMVtuN3d8avrmce p8Y/+apLtedsWAqvgxp1vyQmrjruUISGJyR6cIQ2Z4mnGjpOy6tenWslRZQIdD9zlKRh 37BpqkDAYFDOYawyD+PCB5XBQVsfzrWVWdPNxKk1emanguMt5SfiYLxqgHDHEoQbWD6j U4ig== X-Gm-Message-State: AGi0PuYnRxfxa2roGDBON2TfsrGBDdCvd+fvgdfUKxBTu2nJ7EfUXS8/ ++V/HBySPpFPhCHKtnEwFbZJPw== X-Google-Smtp-Source: APiQypLMsONfT/ydInrYnq3ss7muzDFkNCSsJMBb0dPqQNbOekEWl0Ts3hcELQB/bsXzFc92NOIx3w== X-Received: by 2002:a1c:4e16:: with SMTP id g22mr23666626wmh.157.1587973364411; Mon, 27 Apr 2020 00:42:44 -0700 (PDT) Received: from maco2.ams.corp.google.com (a83-162-234-235.adsl.xs4all.nl. [83.162.234.235]) by smtp.gmail.com with ESMTPSA id x132sm15091658wmg.33.2020.04.27.00.42.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Apr 2020 00:42:43 -0700 (PDT) From: Martijn Coenen To: axboe@kernel.dk, hch@lst.de, ming.lei@redhat.com Cc: narayan@google.com, zezeozue@google.com, kernel-team@android.com, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, maco@google.com, bvanassche@acm.org, Chaitanya.Kulkarni@wdc.com, jaegeuk@kernel.org, Martijn Coenen , Linux API Subject: [PATCH v3 9/9] loop: Add LOOP_SET_FD_AND_STATUS ioctl Date: Mon, 27 Apr 2020 09:42:22 +0200 Message-Id: <20200427074222.65369-10-maco@android.com> X-Mailer: git-send-email 2.26.2.303.gf8c07b1a785-goog In-Reply-To: <20200427074222.65369-1-maco@android.com> References: <20200427074222.65369-1-maco@android.com> MIME-Version: 1.0 Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org This allows userspace to completely setup a loop device with a single ioctl, removing the in-between state where the device can be partially configured - eg the loop device has a backing file associated with it, but is reading from the wrong offset. Besides removing the intermediate state, another big benefit of this ioctl is that LOOP_SET_STATUS can be slow; the main reason for this slowness is that LOOP_SET_STATUS(64) calls blk_mq_freeze_queue() to freeze the associated queue; this requires waiting for RCU synchronization, which I've measured can take about 15-20ms on this device on average. Here's setting up ~70 regular loop devices with an offset on an x86 Android device, using LOOP_SET_FD and LOOP_SET_STATUS: vsoc_x86:/system/apex # time for i in `seq 30 100`; do losetup -r -o 4096 /dev/block/loop$i com.android.adbd.apex; done 0m03.40s real 0m00.02s user 0m00.03s system Here's configuring ~70 devices in the same way, but using a modified losetup that uses the new LOOP_SET_FD_AND_STATUS ioctl: vsoc_x86:/system/apex # time for i in `seq 30 100`; do losetup -r -o 4096 /dev/block/loop$i com.android.adbd.apex; done 0m01.94s real 0m00.01s user 0m00.01s system Signed-off-by: Martijn Coenen Reviewed-by: Christoph Hellwig --- drivers/block/loop.c | 45 +++++++++++++++++++++++++++++---------- include/uapi/linux/loop.h | 7 ++++++ 2 files changed, 41 insertions(+), 11 deletions(-) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index 92bbe368ab62..3c9b5d469ded 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -1066,8 +1066,9 @@ loop_set_status_from_info(struct loop_device *lo, return 0; } -static int loop_set_fd(struct loop_device *lo, fmode_t mode, - struct block_device *bdev, unsigned int arg) +static int loop_set_fd_and_status(struct loop_device *lo, fmode_t mode, + struct block_device *bdev, + const struct loop_fd_and_status *fds) { struct file *file; struct inode *inode; @@ -1082,7 +1083,7 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode, __module_get(THIS_MODULE); error = -EBADF; - file = fget(arg); + file = fget(fds->fd); if (!file) goto out; @@ -1091,7 +1092,7 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode, * here to avoid changing device under exclusive owner. */ if (!(mode & FMODE_EXCL)) { - claimed_bdev = bd_start_claiming(bdev, loop_set_fd); + claimed_bdev = bd_start_claiming(bdev, loop_set_fd_and_status); if (IS_ERR(claimed_bdev)) { error = PTR_ERR(claimed_bdev); goto out_putf; @@ -1121,6 +1122,11 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode, error = loop_validate_size(size); if (error) goto out_unlock; + + error = loop_set_status_from_info(lo, &fds->info); + if (error) + goto out_unlock; + error = loop_prepare_queue(lo); if (error) goto out_unlock; @@ -1133,9 +1139,6 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode, lo->lo_device = bdev; lo->lo_flags = lo_flags; lo->lo_backing_file = file; - lo->transfer = NULL; - lo->ioctl = NULL; - lo->lo_sizelimit = 0; lo->old_gfp_mask = mapping_gfp_mask(mapping); mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); @@ -1173,14 +1176,14 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode, if (partscan) loop_reread_partitions(lo, bdev); if (claimed_bdev) - bd_abort_claiming(bdev, claimed_bdev, loop_set_fd); + bd_abort_claiming(bdev, claimed_bdev, loop_set_fd_and_status); return 0; out_unlock: mutex_unlock(&loop_ctl_mutex); out_bdev: if (claimed_bdev) - bd_abort_claiming(bdev, claimed_bdev, loop_set_fd); + bd_abort_claiming(bdev, claimed_bdev, loop_set_fd_and_status); out_putf: fput(file); out: @@ -1664,8 +1667,27 @@ static int lo_ioctl(struct block_device *bdev, fmode_t mode, int err; switch (cmd) { - case LOOP_SET_FD: - return loop_set_fd(lo, mode, bdev, arg); + case LOOP_SET_FD: { + /* + * Legacy case - pass in a struct loop_fd_and_status with + * a zeroed out loop_info64, which corresponds with the default + * parameters we'd have used otherwise. + */ + struct loop_fd_and_status fds; + + memset(&fds, 0, sizeof(fds)); + fds.fd = arg; + + return loop_set_fd_and_status(lo, mode, bdev, &fds); + } + case LOOP_SET_FD_AND_STATUS: { + struct loop_fd_and_status fds; + + if (copy_from_user(&fds, argp, sizeof(fds))) + return -EFAULT; + + return loop_set_fd_and_status(lo, mode, bdev, &fds); + } case LOOP_CHANGE_FD: return loop_change_fd(lo, bdev, arg); case LOOP_CLR_FD: @@ -1837,6 +1859,7 @@ static int lo_compat_ioctl(struct block_device *bdev, fmode_t mode, case LOOP_CLR_FD: case LOOP_GET_STATUS64: case LOOP_SET_STATUS64: + case LOOP_SET_FD_AND_STATUS: arg = (unsigned long) compat_ptr(arg); /* fall through */ case LOOP_SET_FD: diff --git a/include/uapi/linux/loop.h b/include/uapi/linux/loop.h index 080a8df134ef..05ab625c40db 100644 --- a/include/uapi/linux/loop.h +++ b/include/uapi/linux/loop.h @@ -60,6 +60,12 @@ struct loop_info64 { __u64 lo_init[2]; }; +struct loop_fd_and_status { + struct loop_info64 info; + __u32 fd; + __u32 __pad; +}; + /* * Loop filter types */ @@ -90,6 +96,7 @@ struct loop_info64 { #define LOOP_SET_CAPACITY 0x4C07 #define LOOP_SET_DIRECT_IO 0x4C08 #define LOOP_SET_BLOCK_SIZE 0x4C09 +#define LOOP_SET_FD_AND_STATUS 0x4C0A /* /dev/loop-control interface */ #define LOOP_CTL_ADD 0x4C80