From patchwork Fri Sep 6 22:57:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Begunkov X-Patchwork-Id: 13794836 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 5D985E6FE4B for ; Fri, 6 Sep 2024 22:57:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B5C8C6B0099; Fri, 6 Sep 2024 18:57:09 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AE7B36B009A; Fri, 6 Sep 2024 18:57:09 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 913356B009B; Fri, 6 Sep 2024 18:57:09 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 71E376B0099 for ; Fri, 6 Sep 2024 18:57:09 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 1B4601C5E11 for ; Fri, 6 Sep 2024 22:57:09 +0000 (UTC) X-FDA: 82535825778.14.6092425 Received: from mail-ed1-f50.google.com (mail-ed1-f50.google.com [209.85.208.50]) by imf20.hostedemail.com (Postfix) with ESMTP id 2DE3C1C0009 for ; Fri, 6 Sep 2024 22:57:06 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Vzn20Iqd; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf20.hostedemail.com: domain of asml.silence@gmail.com designates 209.85.208.50 as permitted sender) smtp.mailfrom=asml.silence@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725663401; a=rsa-sha256; cv=none; b=RBF56qj5m/HUsQEasa+HeJmO8Q+ILwTr7YbvbzsrcdYE8OjrcyG/fawxwaivGbMUJfatiT rFrq0trVl1jaRxq5oZmn+Em+8DthByi4Ar5+wLsCHCVdYehR/0m7ykTyxVCt4kID4HC+Lm q6Vdj/xEa6PUtwL/jlkErRZv+hJoCe4= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Vzn20Iqd; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf20.hostedemail.com: domain of asml.silence@gmail.com designates 209.85.208.50 as permitted sender) smtp.mailfrom=asml.silence@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1725663401; 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=OmWgVJIDPmIThlp0MynhFwx87UMO7KCui+lMbfwnd/es0UYpPGpKtdMfbvUePMGtIGmMl/ PpNJHjODRKHs0C8a2wwQydYH6f/gJqtPxaFo3n4EFVkws6HisU/DLjthSxNdU87kiRpz3w 7IPBllo8ZrTM6owIGxKnABluuSSH6WU= Received: by mail-ed1-f50.google.com with SMTP id 4fb4d7f45d1cf-5c25554ec1eso2739555a12.1 for ; Fri, 06 Sep 2024 15:57:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725663426; x=1726268226; 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=Vzn20IqdI3dzQLBg2EBqECi78xXjWD9EA/zpjtS2Drf2uO6InY6TQC5A0+mrpq3gXg Y+tYjQnqbNKQ8Yj9jB2aGPlIOIrYh72lND61m8Gz7AMAyKclbHGz+bSMMxhaQH9uyNQB NZPk59x0+ZyWvSlGTBgMIvUORuzN3f+Ob50BYQCLomLjWokjX/GfIXQ+DC4q0DgwCnI3 ijQYFWu7v4cWwAI+uyLqAwSLVpj2pQWMU4UbOxiOWHA9kxMsxYSJWqCp70L2T4I8mrYd IE5DDspof2j2CfCcU+ZGxs/jTWiuwyzo8fzI3YbBIsKlmdeGtlvgHY/Zb/mwV8aJtjX5 RvPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725663426; x=1726268226; 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=AFdo28O4HhxU9qUQklBYdm9w/ovBDgUyTvMCsfNJw/jmOiNkqf/OjcTyJEdtcToSBp ItpB9iOnHeTCCsmYR+2NksvihlWmC8lemNx4tpDydSPhWOTxihvcqLkhblWT6obyOYxI PDqvReKeIJ+bF4mrVuRk2HMu0Wk6eyCawYIZrDHNV7Cy7hnkLGQZhMLETfyBfw5T9Wkw qvX0lwgJ9KSzs8zzCo3fEgAWgVMR3g1zUy9gCDOoblVJrSaXccGItwQfvfEu0AnD8j+W Il1QzRVZvDPTh8IT/Kj8MMoXyOWIEJWgcBLQe51NF4IcqtRnTaGJ1XK0UrAHFo/xxPyA R7/Q== X-Forwarded-Encrypted: i=1; AJvYcCX+r+GI3W0P8h3JkO89NVVmVSMuTxtSCcZ6tKVDTXnZWebtVy8C5EkSuVWxTbqdWwNrTVQnNaY6LA==@kvack.org X-Gm-Message-State: AOJu0Ywt5pYEO6s33Ox0hVEEsWtjX8JJJ4qcAhVNxesRfGVzlgsn1gvC x4mGI5RO6AIBBsQMuJ1DAqWT6N4+oOIfF9pzupH4bmcpkyZu1bod X-Google-Smtp-Source: AGHT+IGwtkNX+b7B+F+E65xCqdvf83CYwy/QQoEkioql8r4UWUgpbreyku1f9KDVfE9jAVw9Or4nJQ== X-Received: by 2002:a17:907:968b:b0:a86:b46b:860a with SMTP id a640c23a62f3a-a8d1c738eedmr53296966b.54.1725663425531; Fri, 06 Sep 2024 15:57:05 -0700 (PDT) Received: from 127.0.0.1localhost ([148.252.146.236]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a8d25d54978sm2679566b.199.2024.09.06.15.57.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 06 Sep 2024 15:57:05 -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 v4 6/8] block: implement async write zeroes command Date: Fri, 6 Sep 2024 23:57:23 +0100 Message-ID: <2a99dd14c2e0c1fced433822a13ff00735a84816.1725621577.git.asml.silence@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: References: MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: 2DE3C1C0009 X-Rspamd-Server: rspam01 X-Stat-Signature: rkhnhcskew3ueg81k5jz5fbhc7hwahmh X-HE-Tag: 1725663426-312056 X-HE-Meta: U2FsdGVkX199ECdiMF5fFtldTt/BDachs8JbIquqwGhnoMruWwI4c1Aszg/qvpKWzoL8jhj/0PHc3ghw8GuvxAbMJFJu03oUS1mCSuGC8+x6s7wsnvaxEZYs/uo1bfUZRSQ21HhcsASnhc8AHhMXbYjqY23xfBP3GMXYZDnlNDf2QMiXRIhi7eq9XSESvg5hP34x79hq2Sx4Jo8hVHAsB1k8yA4R+mE3mUMGPD3k2sFIRG/b4FdeD+LBr7aN5n27tup5rNkGaonqsDGb/ZpJt42+/fP8JsZqMC95xhwHcaGspNAV+y6ouNxAYXerTz7lx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Pc3Je27F1834ETOme8SwFQc1HNMKnOlR8ThzgSTHZBKvNds7QmyyasWPegRLwyQZOUo+EGiNY/uWl7a8FY6Ztvu59RaTuJK2XmQTX8hbt7P1UQUMSrDlcykR+3SM1IBMro7OcHXW+6Lv+hSj/+j6iGfZ1 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, 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 */