From patchwork Wed Sep 4 14:18:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Begunkov X-Patchwork-Id: 13790936 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id E2125CA0ED3 for ; Wed, 4 Sep 2024 14:18:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 674D76B0085; Wed, 4 Sep 2024 10:17:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5FE3B6B00A8; Wed, 4 Sep 2024 10:17:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3FE9B6B00CD; Wed, 4 Sep 2024 10:17:55 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 1F34C6B0085 for ; Wed, 4 Sep 2024 10:17:55 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id CA54581433 for ; Wed, 4 Sep 2024 14:17:54 +0000 (UTC) X-FDA: 82527259668.30.547F7C6 Received: from mail-ej1-f47.google.com (mail-ej1-f47.google.com [209.85.218.47]) by imf24.hostedemail.com (Postfix) with ESMTP id E105018001E for ; Wed, 4 Sep 2024 14:17:52 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=MGQKIrx8; spf=pass (imf24.hostedemail.com: domain of asml.silence@gmail.com designates 209.85.218.47 as permitted sender) smtp.mailfrom=asml.silence@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1725459345; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=sVNRBhtechMDGkg+NH1UKI/XUcjXx3fSa2oGaERaZjA=; b=RaqDIJsS4hcLtAQ5xqVy2TTCTVSiz/Oo36py9Yl3bjIXwtkQtU6R+AzuHtrkkzk06lD7Pw 4oZmwXg0qLWS0mdOyvXJTS94AZ4nmKFoNEwBqJR7e9ylfj800V/brSnK16BDW5qT/UMPUN epqJ1Shcso76mfsJ2CeH4JDBewFjAsg= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=MGQKIrx8; spf=pass (imf24.hostedemail.com: domain of asml.silence@gmail.com designates 209.85.218.47 as permitted sender) smtp.mailfrom=asml.silence@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725459345; a=rsa-sha256; cv=none; b=t8Mlfi4WOmN3B41rfpqF9DIM08vr+UIRQczHEzR/lc7lyjVdcVacexvHmpSYVsdYaV4nkC 7Xf5gkXIqXnyEFdtz7UaRMCLsMciLBsBkHE39emzQzE9hL2tFLGlDBJyc9JYb9FLXlPSYI deQrXUkv5T708QZELT0ZZKdevJj5vUM= Received: by mail-ej1-f47.google.com with SMTP id a640c23a62f3a-a868831216cso789463166b.3 for ; Wed, 04 Sep 2024 07:17:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725459471; x=1726064271; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=sVNRBhtechMDGkg+NH1UKI/XUcjXx3fSa2oGaERaZjA=; b=MGQKIrx8vrw8S2pu1pVGZQBpMF54/5fNzf8Fzai6VeK4ZCzZyRIINAXfrKVrobm3+t j5QrFvGp6J1jXY7u6iLHQVdVno5Qy3V/EloS+9/dHoLZSy+oPCTbgbc0znrnSp/7XGAO B7sg04ZnnhvlSSAsbTvPkoe52Hqju8vap2GGX1FUWMMC4sCezPc7FG70uF40M6GTBqqp 3z/+PEALG4wYizG6s6f7UTW2zroqVUDlbIFZV5rZQ/JPRxC4n+A92GyRkyxLMpZMG/St 3DO6lD6N9fXahZOm6YBHGx9S3oi5FLrxGbeTA6c1V3sUAqzfvuLQc6ZZzwIHx6noN61r rgVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725459471; x=1726064271; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=sVNRBhtechMDGkg+NH1UKI/XUcjXx3fSa2oGaERaZjA=; b=mMpkVqgfW/x49cI+oUdUqOjRWcVk9IzpjrqRHUHnRV9YG8SpMZRCgUG4GHHs6Pq8Ky lHPk+maP8sNdZz6I23vuLrAZaMjUt9BLFwS6TfX/06x8lti0ei2NcOCYhMdnyo7BThTX 82hx68Uq6CvSAbes5oTpYwrQP8zIyiVN/6z+YtCv12RYxaWDJKqVovq/Csq74cwZ3H6z bVRA6R4mKEStBo6KqMKLUqY22flYlwmB07dAqnXq3eUYfSs0TMbDo6j61nR9FxZv3k8t 59JtQutXhvYVvDXNOzCDqeXtsWueNfwsqfRH9Lh/m9mHhQh7uUIhWtnL0pg+J0puic9z HYyw== X-Forwarded-Encrypted: i=1; AJvYcCVMscGxoJ8Gl6Ji30bV6x7NDQGcgg/Jn1eGr7iaYK4CoQ28zuwj2L/UcJpzTNndvFFuhzZmeCIMDQ==@kvack.org X-Gm-Message-State: AOJu0Yz9eZkPuq1eDfos4JfzfgZzZslKORKbwXCmjorIR4k/l8ieOuMQ DGfQ37at2612KL1qxxt/waQ/95Ks+pYQKCnlk6w+nUExWxctnIB3 X-Google-Smtp-Source: AGHT+IHb0jlP4IioizAxaoRrXvgoAIk/f8ER3xkL75iNWvRggyazpylQKgjr9C/Pc8pltG39ZfMk2Q== X-Received: by 2002:a17:906:730a:b0:a86:a4b1:d2b8 with SMTP id a640c23a62f3a-a89b93da137mr930271466b.4.1725459471238; Wed, 04 Sep 2024 07:17:51 -0700 (PDT) Received: from 127.0.0.1localhost ([163.114.131.193]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a8989196c88sm811160766b.102.2024.09.04.07.17.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Sep 2024 07:17:50 -0700 (PDT) From: Pavel Begunkov To: io-uring@vger.kernel.org Cc: Jens Axboe , asml.silence@gmail.com, Conrad Meyer , linux-block@vger.kernel.org, linux-mm@kvack.org, Christoph Hellwig Subject: [PATCH v3 6/8] block: implement async write zeroes command Date: Wed, 4 Sep 2024 15:18:05 +0100 Message-ID: <2a99dd14c2e0c1fced433822a13ff00735a84816.1725459175.git.asml.silence@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: E105018001E X-Stat-Signature: d1nhroxhg8on8ojqbu7cmhtxj78nwyx9 X-Rspam-User: X-HE-Tag: 1725459472-898985 X-HE-Meta: U2FsdGVkX1+WyYiu0bZm/JJIUSGx8bCjN9Q5Mab7+b7549EInIWc7k60jXs9nH1j6vJYJRd5B81hLdkISJmVjORa0SauwTp2R1NAJaoEpLCnRiGY7b4jcTATHd5YkHe+qK0p45hquKKpAzxu0758nE7JwzNq6KzK0KIFwLrGG7sEPre6hJjjjvvjMUA+yrH1WfAljKeDaxh3kOpEJHxplZIoDEQQQ5cVSq0t/phMAn7mS4jmvXq8VrZJi+Fdztsn/fAylycdjD5PYbrKXKW9SktbWypKv3i+3C+e4FylOJtsf9EHZj3CpxBsRGNGKJr2SmBOQ530+bXBusW+eG8vFLzpgxyg/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vgQWa/6NHPg+7z+VHpRBd88uSOchbOuJMb+sTJHMR8MHeWN9nfcexPWdYBHQibrp7JQtE0oBXyKP+R5n3czGnVktR7TgtEUQF7j8nE15fUcARR4y3BcOJQ X-Bogosity: Ham, tests=bogofilter, spamicity=0.000003, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Add another io_uring cmd for block layer implementing asynchronous write zeroes. It reuses helpers we've added for async discards, and inherits the code structure as well as all considerations in regards to page cache races. Suggested-by: Conrad Meyer Signed-off-by: Pavel Begunkov --- block/ioctl.c | 64 +++++++++++++++++++++++++++++++++++++++++ include/uapi/linux/fs.h | 1 + 2 files changed, 65 insertions(+) diff --git a/block/ioctl.c b/block/ioctl.c index 19fba8332eee..ef4b2a90ad79 100644 --- a/block/ioctl.c +++ b/block/ioctl.c @@ -772,6 +772,67 @@ static void bio_cmd_bio_end_io(struct bio *bio) bio_put(bio); } +static int blkdev_cmd_write_zeroes(struct io_uring_cmd *cmd, + struct block_device *bdev, + uint64_t start, uint64_t len, bool nowait) +{ + + sector_t bs_mask = (bdev_logical_block_size(bdev) >> SECTOR_SHIFT) - 1; + sector_t limit = bdev_write_zeroes_sectors(bdev); + sector_t sector = start >> SECTOR_SHIFT; + sector_t nr_sects = len >> SECTOR_SHIFT; + struct bio *prev = NULL, *bio; + gfp_t gfp = nowait ? GFP_NOWAIT : GFP_KERNEL; + int err; + + if (!(file_to_blk_mode(cmd->file) & BLK_OPEN_WRITE)) + return -EBADF; + if (bdev_read_only(bdev)) + return -EPERM; + err = blk_validate_byte_range(bdev, start, len); + if (err) + return err; + + if (!limit) + return -EOPNOTSUPP; + /* + * Don't allow multi-bio non-blocking submissions as subsequent bios + * may fail but we won't get a direct indication of that. Normally, + * the caller should retry from a blocking context. + */ + if (nowait && nr_sects > limit) + return -EAGAIN; + + err = filemap_invalidate_pages(bdev->bd_mapping, start, + start + len - 1, nowait); + if (err) + return err; + + limit = min(limit, (UINT_MAX >> SECTOR_SHIFT) & ~bs_mask); + while (nr_sects) { + sector_t bio_sects = min(nr_sects, limit); + + bio = bio_alloc(bdev, 0, REQ_OP_WRITE_ZEROES|REQ_NOUNMAP, gfp); + if (!bio) + break; + if (nowait) + bio->bi_opf |= REQ_NOWAIT; + bio->bi_iter.bi_sector = sector; + bio->bi_iter.bi_size = bio_sects << SECTOR_SHIFT; + sector += bio_sects; + nr_sects -= bio_sects; + + prev = bio_chain_and_submit(prev, bio); + } + if (!prev) + return -EAGAIN; + + prev->bi_private = cmd; + prev->bi_end_io = bio_cmd_bio_end_io; + submit_bio(prev); + return -EIOCBQUEUED; +} + static int blkdev_cmd_discard(struct io_uring_cmd *cmd, struct block_device *bdev, uint64_t start, uint64_t len, bool nowait) @@ -841,6 +902,9 @@ int blkdev_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags) switch (cmd_op) { case BLOCK_URING_CMD_DISCARD: return blkdev_cmd_discard(cmd, bdev, start, len, bic->nowait); + case BLOCK_URING_CMD_WRITE_ZEROES: + return blkdev_cmd_write_zeroes(cmd, bdev, start, len, + bic->nowait); } return -EINVAL; } diff --git a/include/uapi/linux/fs.h b/include/uapi/linux/fs.h index 7ea41ca97158..68b0fccebf92 100644 --- a/include/uapi/linux/fs.h +++ b/include/uapi/linux/fs.h @@ -209,6 +209,7 @@ struct fsxattr { */ #define BLOCK_URING_CMD_DISCARD _IO(0x12,137) +#define BLOCK_URING_CMD_WRITE_ZEROES _IO(0x12,138) #define BMAP_IOCTL 1 /* obsolete - kept for compatibility */ #define FIBMAP _IO(0x00,1) /* bmap access */