From patchwork Wed Sep 4 14:18:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Begunkov X-Patchwork-Id: 13790949 Received: from mail-ej1-f45.google.com (mail-ej1-f45.google.com [209.85.218.45]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 96A6F1DC061; Wed, 4 Sep 2024 14:17:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725459467; cv=none; b=B/Y23Aj0yCWOEdLKEj+0Ysl2duVorsXhK5+PDbxFg7l9AE4Th/dAU9tJSyBdj+VvdJszOhLkfmY8KTKNdH07WiOmBfn6SyGzHxeQEAU88VrX4R3bYTfyuCAS/s5g8N5ZoOxXamcPikN0PpIodqZ4WC0I3FmW0zKY1jy7w6oy8FQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725459467; c=relaxed/simple; bh=2vI0bIvl9+D8oswmBaa6Rx1hzhaGczPhBrLqovoOrl0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=O1r3Sm8/ibhqnOCRRSO0thKvJSKEwf5NzpdzOi5OKKzihysffa5AlQ+cB8IVY3rJRqY31JioFaMrIsy68+eCWVUx3OylTu3sdjA65xfoPWLBTImAVwkrFkoxoRcP4RhYkaKapLTFjf2U0+OB5oPH2lWDpYYl1EcdZvqWdRAb0pM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=PpH+eCji; arc=none smtp.client-ip=209.85.218.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="PpH+eCji" Received: by mail-ej1-f45.google.com with SMTP id a640c23a62f3a-a869f6ce2b9so720257366b.2; Wed, 04 Sep 2024 07:17:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725459464; x=1726064264; darn=vger.kernel.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=KZ8pkxfeOzRC4kA801xaO2cK98vlGCE9d9luTt+J2Kg=; b=PpH+eCjiJ+OI85hYTRBQPZKPD8oOMsDSurH5VPcCZcfkrla9yAJ7JOD+onWYeRrK82 ne5T7gLEiPN0LMz0Bht1j+TKRtwX3JH8TurFBpeWHOE27E1x2XTNUBOot5HpjYXp55h+ m5APIDRChpGrl+dhubE6RwycrSwFEcIMIydAtw6H3WeqEwmjjRHzlxUryvkjMuZHPyGL Ewfp40KwqgJYySkxunGsw5TVQgd/bL9k2bWf71URY67gbc2qMYRFl4BhEPbiVZPx0Q7w vcXgzTqG4mMxbNSYut+ht3s6VXSt7u79QOCJWpfVeMySI6iUbNuLQoUg0sQNFXNjgIWA dFsQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725459464; x=1726064264; 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=KZ8pkxfeOzRC4kA801xaO2cK98vlGCE9d9luTt+J2Kg=; b=tNYuUmHz0+qEZC0NjKqAq1iD9DRst6CjqZOcq8sNHDDsPnQ4waRSNlKIVIMcPAZbDG vs2dbXfJjBoLr5/e3oTRvEjgoWcab43CqpSYncQ+2VQdQ0e+UTavrcw8ZZfA7HApiA1N rGIHEoBTQztCOffjPJmu/AU8xhxOrcgW012BSa1yU3ZeOtoG2dn8wnd5GDqEo++sAlTI VzQQ4XawsurcyuOSdTuCJ3d09NF+vE0LcwzP14KSMaDD6UbCVfO/CFS10MTjGdLufZLg B+MvLFPgbS+NtDrjhspqlsbiMLqCO7P4q5HpIhBUhdh1W6w9usw/kmqKdeQj/AE4sI7w q9cQ== X-Forwarded-Encrypted: i=1; AJvYcCWRzNWS8bBrdx7DeXNoJBMTspLqiQwJBQg2mcIeJw/sFRTdt71V2N1omG4yMQDsKBOfMRhpwFfbr8O2gg==@vger.kernel.org X-Gm-Message-State: AOJu0YyazpzdMTGdH5ACxlqS2DFdwLTeeoGoLdM0q9OLFrFAW0qH4dY1 gqQ0DmouNDHUvqZBtoB1FOA7/x7ZXCLbguF7lm8wdv1JpMbspM8kT14RxA== X-Google-Smtp-Source: AGHT+IH097ivB/9WxgCVpcO8B8ihxC+WO/gvLLOLQq9DQZmLBk0QFnqSJuSVFdgCDV3eF0HI2K1RcQ== X-Received: by 2002:a17:907:7289:b0:a86:9f56:21e6 with SMTP id a640c23a62f3a-a8a32ed6109mr297583066b.33.1725459462834; Wed, 04 Sep 2024 07:17:42 -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.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Sep 2024 07:17:42 -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 1/8] io_uring/cmd: expose iowq to cmds Date: Wed, 4 Sep 2024 15:18:00 +0100 Message-ID: X-Mailer: git-send-email 2.45.2 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 When an io_uring request needs blocking context we offload it to the io_uring's thread pool called io-wq. We can get there off ->uring_cmd by returning -EAGAIN, but there is no straightforward way of doing that from an asynchronous callback. Add a helper that would transfer a command to a blocking context. Note, we do an extra hop via task_work before io_queue_iowq(), that's a limitation of io_uring infra we have that can likely be lifted later if that would ever become a problem. Signed-off-by: Pavel Begunkov --- include/linux/io_uring/cmd.h | 6 ++++++ io_uring/io_uring.c | 11 +++++++++++ io_uring/io_uring.h | 1 + io_uring/uring_cmd.c | 7 +++++++ 4 files changed, 25 insertions(+) diff --git a/include/linux/io_uring/cmd.h b/include/linux/io_uring/cmd.h index 447fbfd32215..86ceb3383e49 100644 --- a/include/linux/io_uring/cmd.h +++ b/include/linux/io_uring/cmd.h @@ -48,6 +48,9 @@ void __io_uring_cmd_do_in_task(struct io_uring_cmd *ioucmd, void io_uring_cmd_mark_cancelable(struct io_uring_cmd *cmd, unsigned int issue_flags); +/* Execute the request from a blocking context */ +void io_uring_cmd_issue_blocking(struct io_uring_cmd *ioucmd); + #else static inline int io_uring_cmd_import_fixed(u64 ubuf, unsigned long len, int rw, struct iov_iter *iter, void *ioucmd) @@ -67,6 +70,9 @@ static inline void io_uring_cmd_mark_cancelable(struct io_uring_cmd *cmd, unsigned int issue_flags) { } +static inline void io_uring_cmd_issue_blocking(struct io_uring_cmd *ioucmd) +{ +} #endif /* diff --git a/io_uring/io_uring.c b/io_uring/io_uring.c index 1aca501efaf6..86cf31902841 100644 --- a/io_uring/io_uring.c +++ b/io_uring/io_uring.c @@ -533,6 +533,17 @@ static void io_queue_iowq(struct io_kiocb *req) io_queue_linked_timeout(link); } +static void io_req_queue_iowq_tw(struct io_kiocb *req, struct io_tw_state *ts) +{ + io_queue_iowq(req); +} + +void io_req_queue_iowq(struct io_kiocb *req) +{ + req->io_task_work.func = io_req_queue_iowq_tw; + io_req_task_work_add(req); +} + static __cold void io_queue_deferred(struct io_ring_ctx *ctx) { while (!list_empty(&ctx->defer_list)) { diff --git a/io_uring/io_uring.h b/io_uring/io_uring.h index 65078e641390..9d70b2cf7b1e 100644 --- a/io_uring/io_uring.h +++ b/io_uring/io_uring.h @@ -94,6 +94,7 @@ int io_uring_alloc_task_context(struct task_struct *task, int io_ring_add_registered_file(struct io_uring_task *tctx, struct file *file, int start, int end); +void io_req_queue_iowq(struct io_kiocb *req); int io_poll_issue(struct io_kiocb *req, struct io_tw_state *ts); int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr); diff --git a/io_uring/uring_cmd.c b/io_uring/uring_cmd.c index 8391c7c7c1ec..39c3c816ec78 100644 --- a/io_uring/uring_cmd.c +++ b/io_uring/uring_cmd.c @@ -277,6 +277,13 @@ int io_uring_cmd_import_fixed(u64 ubuf, unsigned long len, int rw, } EXPORT_SYMBOL_GPL(io_uring_cmd_import_fixed); +void io_uring_cmd_issue_blocking(struct io_uring_cmd *ioucmd) +{ + struct io_kiocb *req = cmd_to_io_kiocb(ioucmd); + + io_req_queue_iowq(req); +} + static inline int io_uring_cmd_getsockopt(struct socket *sock, struct io_uring_cmd *cmd, unsigned int issue_flags) From patchwork Wed Sep 4 14:18:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Begunkov X-Patchwork-Id: 13790950 Received: from mail-ej1-f51.google.com (mail-ej1-f51.google.com [209.85.218.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 02E021DC061; Wed, 4 Sep 2024 14:17:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725459469; cv=none; b=uQtEyH+9Zfk1oqk73puayT18SWv8KRA+8540zGJNsH8i2ynJw92fPj2pTqjhrMEwvQgBvKeEtTBF9YO+UJtLR2V0NoQg0tGak7lNuD1t9mXG94Y/jfCPBEAu7R+O0xIKtnJm/1oRnmxXNWPlmtOs9c1VANq4PE5blY3aFmDWsew= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725459469; c=relaxed/simple; bh=xgZK/jw8ZlQXj8cIxR0W7ybGVnlcP1lJNS+9Q/l9a0s=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Dh3EvDk3Lo25T1Ehc5JXhdQzNnJ7foPpANQOqYpIZRlq31RkvMD9fa3m1o3EiYtnCEaMMDZ6Brcfnh4gn2uSrVRPzqMOqGbSHkl7Yp/WAisKVqdxPI2bH7hDpUVfZpyxxMc7usHCnTBPtJemdnTh2io6YqoQpq41v9uUYFR/LZQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=ndUWntOq; arc=none smtp.client-ip=209.85.218.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ndUWntOq" Received: by mail-ej1-f51.google.com with SMTP id a640c23a62f3a-a86b46c4831so775420666b.1; Wed, 04 Sep 2024 07:17:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725459466; x=1726064266; darn=vger.kernel.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=yyZRXxiAI12SAMIbkwT8bn9l7S8e/TKEaLOeKBVDL1c=; b=ndUWntOq4XJuMpOUjj5eavzRr2ekQTut+8DfyAj0+sb33Es+BYAd6gWznWpmbnA4YN cpSuLENaxhzV90evVeXnKR+O7F5FfUroEZoZWAlXMizMbiaG20X9BvgwhQBAaXa2cRhd RoHjtjJd9xD7y+hPR6ij8pk8basOg80XDzXR4gkhq1VSV/YmWzoCLwtwg1IxXsvOn+Ch ngyWm3wZobfq5usju5jgMyzL1OIUS+rLj7+zGKV/hvTgYszIkbm84tbz/4YduStkhNei FBsi1qCSR4lPeVfMRJRakkIYcNGBAHDjuMTlez4hkiLAiRCC7brV2SUWi8F92d9J+ir6 Qmow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725459466; x=1726064266; 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=yyZRXxiAI12SAMIbkwT8bn9l7S8e/TKEaLOeKBVDL1c=; b=IMJckxDeh7ps/PdQJ4ZWgnj/kRKIfvbuTjFp3xmZ7dVhophER5BGVG602Q48O9Ti6b f9F8PEoyYUAiuG150Iir9CPqd0tDZVzBu1xC2niv6NfARZMkm9fQ4zxW5NWxafN09ad2 AV1guMHQZL5RWfqksW3cMogjEZKuXx5RAjxOwQB48LGO8yK9YHJh+CukOV/1qJLVpG3n rDMbOk8IDMlC2sc0T3BLgg221hu0gNiE3QHXc2GnxjEV62psLsHx8qW1yg13iH+LwrAc jXZFlN2D6G4gMthKbNYCadMwSUZAYdY2UxqzPIsI0RuONI1ObRfVvIf5BAVQTzdr7Fwr oXZA== X-Forwarded-Encrypted: i=1; AJvYcCUZsMD+x/QaasOIIUC3GDqg46Vyo/GqaDVdSWw9cfmW+wK1YhFVPwLSnY0P9xMgni3cEkhBmWf8lW1rZw==@vger.kernel.org X-Gm-Message-State: AOJu0YxXYMlmTAQeyKuaxlqIA6Ji2z1+N/QCaiwGNfyxf1jSdiqWM6zQ aSlpzKgyzshjGsVIx9S4WYbXDO2bjW2UCs0aQC+BbbhLZ36ooeYtmlH6lw== X-Google-Smtp-Source: AGHT+IElud/tGXrs7q4hPENcWMYIZSj7RTtRqWMNx0QoFf5mcRK9CPIkT2ZU7thyj+ZNDXYdgCuEaw== X-Received: by 2002:a17:907:a07:b0:a7d:e84c:a9e7 with SMTP id a640c23a62f3a-a89d8848bd3mr1056922166b.53.1725459466090; Wed, 04 Sep 2024 07:17:46 -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.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Sep 2024 07:17:43 -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 2/8] io_uring/cmd: give inline space in request to cmds Date: Wed, 4 Sep 2024 15:18:01 +0100 Message-ID: <7ca779a61ee5e166e535d70df9c7f07b15d8a0ce.1725459175.git.asml.silence@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Some io_uring commands can use some inline space in io_kiocb. We have 32 bytes in struct io_uring_cmd, expose it. Signed-off-by: Pavel Begunkov --- include/linux/io_uring/cmd.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/include/linux/io_uring/cmd.h b/include/linux/io_uring/cmd.h index 86ceb3383e49..c189d36ad55e 100644 --- a/include/linux/io_uring/cmd.h +++ b/include/linux/io_uring/cmd.h @@ -23,6 +23,15 @@ static inline const void *io_uring_sqe_cmd(const struct io_uring_sqe *sqe) return sqe->cmd; } +static inline void io_uring_cmd_private_sz_check(size_t cmd_sz) +{ + BUILD_BUG_ON(cmd_sz > sizeof_field(struct io_uring_cmd, pdu)); +} +#define io_uring_cmd_to_pdu(cmd, pdu_type) ( \ + io_uring_cmd_private_sz_check(sizeof(pdu_type)), \ + ((pdu_type *)&(cmd)->pdu) \ +) + #if defined(CONFIG_IO_URING) int io_uring_cmd_import_fixed(u64 ubuf, unsigned long len, int rw, struct iov_iter *iter, void *ioucmd); From patchwork Wed Sep 4 14:18:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Begunkov X-Patchwork-Id: 13790951 Received: from mail-ej1-f47.google.com (mail-ej1-f47.google.com [209.85.218.47]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D05A91E0B9C; Wed, 4 Sep 2024 14:17:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725459471; cv=none; b=QpB9K5jCw71Cobmvq1qU+Tbm9zPDD3GbZm9Rbd+A2NpjNNtMpG+yU2Qy6cDHcsP/X8UQjZCbJnxE1coiUmb8n28cY4FJm0gLVqtDT7L06hbvZoDY8eNFR1+gsISb7TdZD90Kc65mgM3o178UlKfjWE8tPBnjzJJuNv2F60aWMJs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725459471; c=relaxed/simple; bh=JrAOgMM2iAIABQmCU3ZV4QTcl0u9rm5SIWFUspGZWXA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=vAfNyCXi1FDGgK4cmwdtuSgPt6aRRnAU9Uvzwt484Y2EO55lonsyPJuvB9FZgIrrxwwoPhFV9B2iMJepgX4IsS3sNMkw1iY4bqVK8DfhFX6av2XIOCpVp71/4gmscz+urrZToyaVVS1wRfvM+5uQbvQrF51Z1Y4dM2CxfbXVrPo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=a8SvQuC4; arc=none smtp.client-ip=209.85.218.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="a8SvQuC4" Received: by mail-ej1-f47.google.com with SMTP id a640c23a62f3a-a86e9db75b9so739471266b.1; Wed, 04 Sep 2024 07:17:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725459468; x=1726064268; darn=vger.kernel.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=2XjW/1z5M+e5L1HlSBo37JIC76JYeYPBxc+HZavMul8=; b=a8SvQuC4AM1diZaMBfDKvheL4ygM5HfgLrAQTpjhmuWpypp2mcE48MQ4chmU1hpwxb FpVGiHT+56Dz/EMgnPYdlyRT+LtoFutx9O5N6cOXA4P8dIJ/YWlBkOk8fpAe/HPOeZg7 Y+r2Ak5lFUf1WQGZMorHLnJcRjL9AZii8YkWP9UalvMzM+tENiXKEcmEIdEH99fnbY+1 ZP2Vwq5zhIxJB/921wOTpx8YDjt5/hbNuhocctg0Ch3S/pQF+7yD52zzXeLjnJy0eyfR dh3pmtSGet8zG7Q0TJXzQ3Vp4VZoy/2VY5CEcesa8eCAo/Yq/D7qfptyFFMBWPSTJNDw F6GQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725459468; x=1726064268; 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=2XjW/1z5M+e5L1HlSBo37JIC76JYeYPBxc+HZavMul8=; b=xEJ+5orNtTw5rAvXEVthYYrDdBlylNtch/Xjt8Ho6OhGDvPi9O1LSeWSLLQgDrEjiX 6oVWR3s7UiFyJV9Iud6k0eugbiptj6n96485M8so/l9za5jXYyQeVhm+uAlV0r/TV0/I II4f8D9h8+s2AtygpoZ8xCckxhujMMOZ6TXYPwbaxaBzfirPbF0qdOqsUjjkzW/S3l// N6GQSo7f7EroYWC2dEtBQf3LyYlfVHhZXEcqh7N00RXr7zLBnHNHweghim5jpTCuiIHT LwYRQF+j/ZgpU6moF93bKn7NstDvts0TFKi8qPqT44bIIwErRiGbwd+foB4ya/RZ5CXX hKdw== X-Forwarded-Encrypted: i=1; AJvYcCXPemvetmqfrSdNn+jkAwws8KVrmPsKpadLjJic8z66F+w2tRbpSz2ldCXYqusfzSBz+ZlcjY4ESEhgwA==@vger.kernel.org X-Gm-Message-State: AOJu0YyIYjE1kgU0YmFhHCcvNtIPYg/bdjoGsJGTDtdFOItKjQ9/iy07 JTH27+0aIXQspcGmvTEOORtdFIckjHvQfTTsCRQOBR1Qr6mljR/oMahIJw== X-Google-Smtp-Source: AGHT+IEYnlE2jE9AeI30LzbXw8Pi928CHHZ2cfjLj3+Sl10dnyCa2YAie/Zh3flyVcceULOyKSQ8nA== X-Received: by 2002:a17:907:1ca4:b0:a77:c080:11fa with SMTP id a640c23a62f3a-a8a1d4d2201mr484969666b.48.1725459467509; Wed, 04 Sep 2024 07:17:47 -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.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Sep 2024 07:17:46 -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 3/8] filemap: introduce filemap_invalidate_pages Date: Wed, 4 Sep 2024 15:18:02 +0100 Message-ID: X-Mailer: git-send-email 2.45.2 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 kiocb_invalidate_pages() is useful for the write path, however not everything is backed by kiocb and we want to reuse the function for bio based discard implementation. Extract and and reuse a new helper called filemap_invalidate_pages(), which takes a argument indicating whether it should be non-blocking and might return -EAGAIN. Signed-off-by: Pavel Begunkov --- include/linux/pagemap.h | 2 ++ mm/filemap.c | 17 ++++++++++++----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h index d9c7edb6422b..e39c3a7ce33c 100644 --- a/include/linux/pagemap.h +++ b/include/linux/pagemap.h @@ -32,6 +32,8 @@ int invalidate_inode_pages2_range(struct address_space *mapping, pgoff_t start, pgoff_t end); int kiocb_invalidate_pages(struct kiocb *iocb, size_t count); void kiocb_invalidate_post_direct_write(struct kiocb *iocb, size_t count); +int filemap_invalidate_pages(struct address_space *mapping, + loff_t pos, loff_t end, bool nowait); int write_inode_now(struct inode *, int sync); int filemap_fdatawrite(struct address_space *); diff --git a/mm/filemap.c b/mm/filemap.c index d62150418b91..6843ed4847d4 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -2712,14 +2712,12 @@ int kiocb_write_and_wait(struct kiocb *iocb, size_t count) } EXPORT_SYMBOL_GPL(kiocb_write_and_wait); -int kiocb_invalidate_pages(struct kiocb *iocb, size_t count) +int filemap_invalidate_pages(struct address_space *mapping, + loff_t pos, loff_t end, bool nowait) { - struct address_space *mapping = iocb->ki_filp->f_mapping; - loff_t pos = iocb->ki_pos; - loff_t end = pos + count - 1; int ret; - if (iocb->ki_flags & IOCB_NOWAIT) { + if (nowait) { /* we could block if there are any pages in the range */ if (filemap_range_has_page(mapping, pos, end)) return -EAGAIN; @@ -2738,6 +2736,15 @@ int kiocb_invalidate_pages(struct kiocb *iocb, size_t count) return invalidate_inode_pages2_range(mapping, pos >> PAGE_SHIFT, end >> PAGE_SHIFT); } + +int kiocb_invalidate_pages(struct kiocb *iocb, size_t count) +{ + struct address_space *mapping = iocb->ki_filp->f_mapping; + + return filemap_invalidate_pages(mapping, iocb->ki_pos, + iocb->ki_pos + count - 1, + iocb->ki_flags & IOCB_NOWAIT); +} EXPORT_SYMBOL_GPL(kiocb_invalidate_pages); /** From patchwork Wed Sep 4 14:18:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Begunkov X-Patchwork-Id: 13790952 Received: from mail-ed1-f43.google.com (mail-ed1-f43.google.com [209.85.208.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C7A7A1DB53C; Wed, 4 Sep 2024 14:17:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725459472; cv=none; b=fCVnf2vBuKK0w1nXkwwDnblsARVHlrznDReCl8kSAi9FMAQM4cvdRx1UT4cTfc9AoubFWwikOn9FKXWjbdZSwQov70gw9+1ZaP23NBMcGYd/AuKoqPrCUiY66hS54z8Z613SaOl1lKzUYMoBcDoX/0VdRHyrO/YNObPY2Hi4Bdg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725459472; c=relaxed/simple; bh=hjZDHvkTsBuwkkkqnazOc/370IgWi6PLEmiOKefcDVg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=np+oAN9qyi7PUk7M6YU/x3xCRAPLurhywDKzEnrDE6pg0VFydjlVcix949vGt6H57YtFrFZLwiKxcBGL0chDN12saZLqKCmetDQowAt9PdErpCQnHJEGJGzdKVpnBdBKZ8BG+VcBvStnZlSqOjyar5i2eadzjYyhCdJIRLvfbWc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=fw5HBQgH; arc=none smtp.client-ip=209.85.208.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="fw5HBQgH" Received: by mail-ed1-f43.google.com with SMTP id 4fb4d7f45d1cf-58ef19aa69dso6224602a12.3; Wed, 04 Sep 2024 07:17:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725459469; x=1726064269; darn=vger.kernel.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=gCQ1sFR6q4N5GvvlFRQ23FRxGveAlTZ5FLsIiX1fOqY=; b=fw5HBQgHIRDoHiuG4hzNdI9ssLFW8Mfr97WJvinnJ6pL2ADXeXOu25NoMuosss2/4e xlFHPYar4CgBSNqX4NT/rv5yL0yz38EjwmSGTDdYZ7rQ7/NC+PZGYgc2KIkPqy45swtN u7Aq8k4DWc/xnx06Z9htIpYqq01jgkgW1B3mMymxt/viZRZfRNOYug5HSCESFADqKuOx Ex3sBBMmK0jFtu3H5iYDh+PjAcGHA5hhFRIIuLFgYPpBDA5vbgd4F0JRd+Uy+6dON8Nm /lG840iQafEHdfHDGH9A9kDioq40tWKATQy+/4hH0AHHQugOZ454Yip3xaRRY90/Ux1q Xx6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725459469; x=1726064269; 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=gCQ1sFR6q4N5GvvlFRQ23FRxGveAlTZ5FLsIiX1fOqY=; b=n0cd2UgqTaBv8Q4Wqwk8bSLJJrHEl9Ecs3KcbQIGRvflcnZKF8/tiuGKZmBMVxonSh ckY/TPop5fTxaYBK9FN3CmsDJtfjib0R8TT0uoFnP421UgN/y3BnbGBcInO1MrFuzdSb s//wVwHfjLCiBFsBFEU1X1ziA4za/Mo7aF5trqDGwcpHmOWIz4U502in5fQiG62KiS8d l6pQVsb30TIWSQ6jZJTmECZnYEV/iUR5Lnh7Gl1y0RjWS4wfG24t6PVwngX7/NNr0iKm rUrZDonCjKeTYENcRRp5NdOfQhA7JPVnQ2JkXtG4BmptW9cv3OQS2/rHT+mGg5/LaJpg tBSA== X-Forwarded-Encrypted: i=1; AJvYcCV1Za7D3JkoD2y2+b2fEh/4lzp9B8xBr9HqACdL0SaEHM+x728EnIskCIHp++lO0wSECHv9meh0XG70Kw==@vger.kernel.org X-Gm-Message-State: AOJu0YzbJ6BqcSyYW7ciaALZ1VJkFFtGq/JRHctqC/fgqmrYZ7UaTyM0 YqY5rTwEnCqhNNfdVO35Pe3A1hfHsW7tNIB6F/6ilytjjf17YsvJ22Be+QPR X-Google-Smtp-Source: AGHT+IEHqBvvipKDt3pVc4/T+6L4m8BSlDWTikVIuwjQS1tXdrsvfLbUudOAA51rWESeq0to4VuuuA== X-Received: by 2002:a17:907:6d02:b0:a86:a866:9e25 with SMTP id a640c23a62f3a-a89b9727ea8mr1148556666b.56.1725459468821; Wed, 04 Sep 2024 07:17:48 -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.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Sep 2024 07:17:47 -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 4/8] block: introduce blk_validate_byte_range() Date: Wed, 4 Sep 2024 15:18:03 +0100 Message-ID: X-Mailer: git-send-email 2.45.2 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 In preparation to further changes extract a helper function out of blk_ioctl_discard() that validates if we can do IO against the given range of disk byte addresses. Signed-off-by: Pavel Begunkov --- block/ioctl.c | 44 ++++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/block/ioctl.c b/block/ioctl.c index e8e4a4190f18..a820f692dd1c 100644 --- a/block/ioctl.c +++ b/block/ioctl.c @@ -92,38 +92,46 @@ static int compat_blkpg_ioctl(struct block_device *bdev, } #endif +static int blk_validate_byte_range(struct block_device *bdev, + uint64_t start, uint64_t len) +{ + unsigned int bs_mask = bdev_logical_block_size(bdev) - 1; + uint64_t end; + + if ((start | len) & bs_mask) + return -EINVAL; + if (!len) + return -EINVAL; + if (check_add_overflow(start, len, &end) || end > bdev_nr_bytes(bdev)) + return -EINVAL; + + return 0; +} + static int blk_ioctl_discard(struct block_device *bdev, blk_mode_t mode, unsigned long arg) { - unsigned int bs_mask = bdev_logical_block_size(bdev) - 1; - uint64_t range[2], start, len, end; + uint64_t range[2], start, len; struct bio *prev = NULL, *bio; sector_t sector, nr_sects; struct blk_plug plug; int err; - if (!(mode & BLK_OPEN_WRITE)) - return -EBADF; - - if (!bdev_max_discard_sectors(bdev)) - return -EOPNOTSUPP; - if (bdev_read_only(bdev)) - return -EPERM; - if (copy_from_user(range, (void __user *)arg, sizeof(range))) return -EFAULT; - start = range[0]; len = range[1]; - if (!len) - return -EINVAL; - if ((start | len) & bs_mask) - return -EINVAL; + if (!bdev_max_discard_sectors(bdev)) + return -EOPNOTSUPP; - if (check_add_overflow(start, len, &end) || - end > bdev_nr_bytes(bdev)) - return -EINVAL; + if (!(mode & BLK_OPEN_WRITE)) + return -EBADF; + if (bdev_read_only(bdev)) + return -EPERM; + err = blk_validate_byte_range(bdev, start, len); + if (err) + return err; filemap_invalidate_lock(bdev->bd_mapping); err = truncate_bdev_range(bdev, mode, start, start + len - 1); From patchwork Wed Sep 4 14:18:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Begunkov X-Patchwork-Id: 13790953 Received: from mail-ej1-f50.google.com (mail-ej1-f50.google.com [209.85.218.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0DB1F1E1301; Wed, 4 Sep 2024 14:17:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725459473; cv=none; b=R2iDprv0vYqsOrB9KOA/ralYmYMytR6qdE/6etaBYKlxfhiQkzBtOKXxp074K75b9XOdH4pxP798P6XaBNae5HRXccfmTm6HmoHeoGJUmXu4SE77nyLcqi04txhbxp8M7qLIR3oBXH7piDNG6T4XSA9xX+eM/oKl6z8XQXzUE9Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725459473; c=relaxed/simple; bh=j+7zM8nY7uAZN93ODz2NwhZQYv08k76ZVh99E9m/LeE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=KrnF3ix/CZQ1oRVQb0BGWjZ5ZPbeZkZWolmsrGygXigBtuDyYsJcUXi7iou4FSI7KqqF+jYskklHfQMr8mmvPB0DyfEJ11bzUcltt09fcuGQkuMOajxmHa3aANdzvUomdJYgJt5QpS3hiYkdQqkC0206hH81nkpTouIbgavFKao= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=NxtWxh9k; arc=none smtp.client-ip=209.85.218.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="NxtWxh9k" Received: by mail-ej1-f50.google.com with SMTP id a640c23a62f3a-a86e9db75b9so739476566b.1; Wed, 04 Sep 2024 07:17:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725459470; x=1726064270; darn=vger.kernel.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=v7dFMMDbVFAIV5pvquHCkne/t7NzjwwosACmczIEiUs=; b=NxtWxh9kNyK1x2XWNmtezXK7d3flh5bfYQQ+k+7QtPwoV2Ey6TpZkWtei58yvHwe6u FFa0edrHIwrSLgMYZqr7ZNSBC1c9h93Ybp1YpzmejqTlJk3LEt4gVoqk2z6JnDg2nFlh S118FvIHcD+8IPDFVvijLmYuS53iRxzKKQTmvm61hdWwLPoRwHSn0LVcGm0WRTAmJ5pS N0clmkEgmX9FcvowbhQ2qDjKP0u1J/SLRIxF2B4ukf3f1HL3FXr2kUvkb/3Bek40yjK/ sGaGwKTQZ6ML5vwLAfhIrSwirJvfdoHMb60RrRLj25q4ssIn0D5ZsF6wNmNB6l1dVkuM ljAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725459470; x=1726064270; 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=v7dFMMDbVFAIV5pvquHCkne/t7NzjwwosACmczIEiUs=; b=ozdcR9CTi01NhjimufoTWgeeeDC6cVVVCblcj1JN1b2xGVyPjSNSQjjqc1Vdjh+hSN qSADgYTZLQAzzWpIVLRhqaXQK2j3FsV20GM2fQHySfMbOUCiDoVCiXmgHJaV6dZifXL3 QXkC37WUQzSeT2ZsLkVueFc4SOZnMRwOF8Pvoo5+U5rEJt48Ttj4J9xEeZagnoqI8iTS WpdWIdbkqMhTSpMH/Tx4wziZzKVMlanG8v4jGwOvJTZNY1ngTE2kRtxNVIA0Hp7s7RVe wRmCU3RQVSxb6ies0O2AE/e2hO1wJm7SWEArMz2/p6jFJxSO7nlhCPYGWmT5X/fPYrg0 4lDw== X-Forwarded-Encrypted: i=1; AJvYcCVCbKpVeCo8tNulqzE20LeySkA95NKwkT4rPykNuWP5na98AbhpPztY8C4wyH7YFNbFajQK6w5re24D0A==@vger.kernel.org X-Gm-Message-State: AOJu0Yzx5H7PNV10UY63TsrqHflQBEXMyn8oEcMfvLkd+CXEXWtR6KJ+ IFsoRQjaCrLbBYVmxMPT8afLj8OtitYZgsFVa/3PEeG0DB1S/A2FM1/A8A== X-Google-Smtp-Source: AGHT+IE26wVQR2LgzuFQ4epQHPbTKn3Zvnc693bAIlnah2akuYCekc7BKlc1sYCNhwYDpPPSQe9AfA== X-Received: by 2002:a17:907:7e9b:b0:a86:6cb1:4d49 with SMTP id a640c23a62f3a-a8a1d29b736mr490505366b.13.1725459469967; Wed, 04 Sep 2024 07:17:49 -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.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Sep 2024 07:17:49 -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 5/8] block: implement async discard as io_uring cmd Date: Wed, 4 Sep 2024 15:18:04 +0100 Message-ID: <7fc0a61ae29190a42e958eddfefd6d44cdf372ad.1725459175.git.asml.silence@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 io_uring allows to implement custom file specific operations via fops->uring_cmd callback. Use it to wire up asynchronous discard commands. Normally, first it tries to do a non-blocking issue, and if fails we'd retry from a blocking context by returning -EAGAIN to core io_uring. Note, unlike ioctl(BLKDISCARD) with stronger guarantees against races, we only do a best effort attempt to invalidate page cache, and it can race with any writes and reads and leave page cache stale. It's the same kind of races we allow to direct writes. Suggested-by: Conrad Meyer Signed-off-by: Pavel Begunkov --- block/blk-lib.c | 3 +- block/blk.h | 1 + block/fops.c | 2 + block/ioctl.c | 102 ++++++++++++++++++++++++++++++++++++++++ include/linux/bio.h | 2 + include/uapi/linux/fs.h | 2 + 6 files changed, 111 insertions(+), 1 deletion(-) diff --git a/block/blk-lib.c b/block/blk-lib.c index 83eb7761c2bf..c94c67a75f7e 100644 --- a/block/blk-lib.c +++ b/block/blk-lib.c @@ -10,7 +10,8 @@ #include "blk.h" -static sector_t bio_discard_limit(struct block_device *bdev, sector_t sector) +/* The maximum size of a discard that can be issued from a given sector. */ +sector_t bio_discard_limit(struct block_device *bdev, sector_t sector) { unsigned int discard_granularity = bdev_discard_granularity(bdev); sector_t granularity_aligned_sector; diff --git a/block/blk.h b/block/blk.h index 32f4e9f630a3..1a1a18d118f7 100644 --- a/block/blk.h +++ b/block/blk.h @@ -605,6 +605,7 @@ blk_mode_t file_to_blk_mode(struct file *file); int truncate_bdev_range(struct block_device *bdev, blk_mode_t mode, loff_t lstart, loff_t lend); long blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg); +int blkdev_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags); long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg); extern const struct address_space_operations def_blk_aops; diff --git a/block/fops.c b/block/fops.c index 9825c1713a49..8154b10b5abf 100644 --- a/block/fops.c +++ b/block/fops.c @@ -17,6 +17,7 @@ #include #include #include +#include #include "blk.h" static inline struct inode *bdev_file_inode(struct file *file) @@ -873,6 +874,7 @@ const struct file_operations def_blk_fops = { .splice_read = filemap_splice_read, .splice_write = iter_file_splice_write, .fallocate = blkdev_fallocate, + .uring_cmd = blkdev_uring_cmd, .fop_flags = FOP_BUFFER_RASYNC, }; diff --git a/block/ioctl.c b/block/ioctl.c index a820f692dd1c..19fba8332eee 100644 --- a/block/ioctl.c +++ b/block/ioctl.c @@ -11,6 +11,8 @@ #include #include #include +#include +#include #include "blk.h" static int blkpg_do_ioctl(struct block_device *bdev, @@ -742,3 +744,103 @@ long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg) return ret; } #endif + +struct blk_iou_cmd { + int res; + bool nowait; +}; + +static void blk_cmd_complete(struct io_uring_cmd *cmd, unsigned int issue_flags) +{ + struct blk_iou_cmd *bic = io_uring_cmd_to_pdu(cmd, struct blk_iou_cmd); + + if (bic->res == -EAGAIN && bic->nowait) + io_uring_cmd_issue_blocking(cmd); + else + io_uring_cmd_done(cmd, bic->res, 0, issue_flags); +} + +static void bio_cmd_bio_end_io(struct bio *bio) +{ + struct io_uring_cmd *cmd = bio->bi_private; + struct blk_iou_cmd *bic = io_uring_cmd_to_pdu(cmd, struct blk_iou_cmd); + + if (unlikely(bio->bi_status) && !bic->res) + bic->res = blk_status_to_errno(bio->bi_status); + + io_uring_cmd_do_in_task_lazy(cmd, blk_cmd_complete); + bio_put(bio); +} + +static int blkdev_cmd_discard(struct io_uring_cmd *cmd, + struct block_device *bdev, + uint64_t start, uint64_t len, bool nowait) +{ + gfp_t gfp = nowait ? GFP_NOWAIT : GFP_KERNEL; + sector_t sector = start >> SECTOR_SHIFT; + sector_t nr_sects = len >> SECTOR_SHIFT; + struct bio *prev = NULL, *bio; + int err; + + if (!bdev_max_discard_sectors(bdev)) + return -EOPNOTSUPP; + + 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; + + /* + * 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 > bio_discard_limit(bdev, sector)) + return -EAGAIN; + + err = filemap_invalidate_pages(bdev->bd_mapping, start, + start + len - 1, nowait); + if (err) + return err; + + while ((bio = blk_alloc_discard_bio(bdev, §or, &nr_sects, gfp))) { + if (nowait) + bio->bi_opf |= REQ_NOWAIT; + 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; +} + +int blkdev_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags) +{ + struct block_device *bdev = I_BDEV(cmd->file->f_mapping->host); + struct blk_iou_cmd *bic = io_uring_cmd_to_pdu(cmd, struct blk_iou_cmd); + const struct io_uring_sqe *sqe = cmd->sqe; + u32 cmd_op = cmd->cmd_op; + uint64_t start, len; + + if (unlikely(sqe->ioprio || sqe->__pad1 || sqe->len || + sqe->rw_flags || sqe->file_index)) + return -EINVAL; + + bic->res = 0; + bic->nowait = issue_flags & IO_URING_F_NONBLOCK; + + start = READ_ONCE(sqe->addr); + len = READ_ONCE(sqe->addr3); + + switch (cmd_op) { + case BLOCK_URING_CMD_DISCARD: + return blkdev_cmd_discard(cmd, bdev, start, len, bic->nowait); + } + return -EINVAL; +} diff --git a/include/linux/bio.h b/include/linux/bio.h index faceadb040f9..78ead424484c 100644 --- a/include/linux/bio.h +++ b/include/linux/bio.h @@ -684,4 +684,6 @@ struct bio *bio_chain_and_submit(struct bio *prev, struct bio *new); struct bio *blk_alloc_discard_bio(struct block_device *bdev, sector_t *sector, sector_t *nr_sects, gfp_t gfp_mask); +sector_t bio_discard_limit(struct block_device *bdev, sector_t sector); + #endif /* __LINUX_BIO_H */ diff --git a/include/uapi/linux/fs.h b/include/uapi/linux/fs.h index 753971770733..7ea41ca97158 100644 --- a/include/uapi/linux/fs.h +++ b/include/uapi/linux/fs.h @@ -208,6 +208,8 @@ struct fsxattr { * (see uapi/linux/blkzoned.h) */ +#define BLOCK_URING_CMD_DISCARD _IO(0x12,137) + #define BMAP_IOCTL 1 /* obsolete - kept for compatibility */ #define FIBMAP _IO(0x00,1) /* bmap access */ #define FIGETBSZ _IO(0x00,2) /* get the block size used for bmap */ 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: 13790954 Received: from mail-ej1-f48.google.com (mail-ej1-f48.google.com [209.85.218.48]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 417891DB53C; Wed, 4 Sep 2024 14:17:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725459475; cv=none; b=TGQOKF/fWXzyIKgE5qw7GcfDjI2KrIyUf1hzpr0T+63fV/CaOAmGkRArDk8ev40eOSS/Ocytyeza1tbCkr2K15D03C45DRzFdd7xdLRJxw/AqFr2v1NT+s8aLLR3Xs5WV7rKYD0KmmiSgWYMYUVyyZcOnIyFFeE3+SxtSaynRmE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725459475; c=relaxed/simple; bh=lyDccPclJxGZedhLYadtZmxYdaswgU6YsCGNLhOcKgM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=elWfv3QNJHILSsao9MXLj6fsp6EVqdxmmtwUWCqcIYphGzXt0vxRdAGC880ydNWNGvFhhg3fZFYDa1m2djHxibNhOPLU/kXXRQsmm+/wiow3tuUO4+MHJJT2BM8pr71hyo5s+4GIn7DGSQNmDIjXaoHN6/Oc5dM3GL4hycG0VQk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=MSm4NMN2; arc=none smtp.client-ip=209.85.218.48 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="MSm4NMN2" Received: by mail-ej1-f48.google.com with SMTP id a640c23a62f3a-a86c476f679so804053766b.1; Wed, 04 Sep 2024 07:17:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725459471; x=1726064271; darn=vger.kernel.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=MSm4NMN27P67UAoe+oI1joUVcnV7k/TTq5jdw1sQx5ONcluJOANzau8VkEHcxL900n Q0Xg44l8DIqtYHKBdegYqlr8zaQWW/cwpZpJIJN+xqfnO7HCc2RKkCZ4dMjM+LsBngMI H4+dXPeDBb+tHJalCDwi4KXj+DOH201uQ+42+CdcgiRimEqwBoMf6O8jTMoFtGSr20T1 ShAsjWjxKh9chCXM4SAko9YGWOSjbw/bWsaqSrb6aunuLNUoS6ul/weh2PfUQSbssG1t IDwwaP1s13pR2+h+5R5ioT0R3YUfmDTehnvXIusAbLfUdv6FFaYrUPTPbetrhBQ1AjKQ Pcew== 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=w2hWYVM1pH70XBiTfno5Z1ihVt1kbDSZeWnl7m2ANE7cvFNzbR1A6nf91D0RIJ6xj6 CFjkU35YsLBEEDWRtLPkU5HR+jtkKZBfpLnvXQanO+Tk5slzhBwLhS+IKG9r8zzkpl5G 2RfD3Dm/WHWB3J9zkhzF4cW5ozz40aIJIzkUg9fGsL4dUQSrU1/OxHoDfsojFn9usCt4 FO1qKh/pISH9pMd5OFk0ScZ8rynUC5/bwOz6Dm5Qs/Gk6P7vKY9jJ+Q2qLqmF1Esp1RJ RDVIDjBuP+NXXxERj0v82VXD+0LgKT4qPAv6TnQY7hZsBBYG47G22eyt/RdRdMgvr9oa /zNQ== X-Forwarded-Encrypted: i=1; AJvYcCVQZIdK/dSFjYngejqz0LD76ZhmVGsvU/n/R8rWMzF4CL1y3Q2FkbpmaTCs5y/Sdfah8UDMfmOBFGEbvg==@vger.kernel.org X-Gm-Message-State: AOJu0YzGjAEBNzVtSJx7swD5SWlAJ8jthITixgHF52BZwF3DCTxvieoE Tp/ozz6cdWfHwSsCgQjo+0K5d+VJ8by/RR15Y3G/SqsnyDxsZc1zenpKYA== 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: Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 */ From patchwork Wed Sep 4 14:18:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Begunkov X-Patchwork-Id: 13790955 Received: from mail-lf1-f54.google.com (mail-lf1-f54.google.com [209.85.167.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8E7581E1301; Wed, 4 Sep 2024 14:17:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725459476; cv=none; b=aWAeUljmX2gBJPsZAK2Xe2qiE2dgYN46FfSxFgr60hjYJW/MlAKlfEGv8NTc/WCA1/EZgyE/KWaRCaC1tKGEwaEfiKYDCUKwX6JUVzkSiDIOQOpsyKaARUwvOOwgYXerHJcV5wCOGRRypnwNwM2vqQXgrShJnF9Xfd9Srv8JES8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725459476; c=relaxed/simple; bh=qT92kXhOb3ng0463r8HD9asnYhrFkLRqieKmlMdj6v0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=kXroC8vDFTzJx7cAVUn7PuzU+8jDNoikXq7tRcGhk3q38ZeT227I9JOQ+MqcmhSYePFVyhVlUxHJ0geMREXt3S1eBsJzwfpaygEyQ8Yu6Vof44nCJnz+cljWBawMols1omfmzUr+bi4IoeKlxPV8Eid4z6xTyZqSuIn/7RbUFtk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=et1Hv0Om; arc=none smtp.client-ip=209.85.167.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="et1Hv0Om" Received: by mail-lf1-f54.google.com with SMTP id 2adb3069b0e04-5334a8a1af7so7096188e87.2; Wed, 04 Sep 2024 07:17:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725459472; x=1726064272; darn=vger.kernel.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=/3weTv0JDO1olCzxF1klLqGqviZ0xpQPSzNHS+GA/qM=; b=et1Hv0OmkszoSyVGQsE0+ggVtHkGET0uFWXqCcboj7WSFJFg3D6ah5/FbWEV5uRJr+ B33hXqmVGgrYBegDxNFkSOK8T9dZ9qBc+G+kApdQCFMW6dOeps7JaaiDeZRJLHRe/tuv h7YzrnncgaTevmAVBQfrv8JQrdnQLu/+6Tr9aRe0kBFXNFiaOac8oTQTbFG4qK0DrH33 RcluuhyVkiGwSTM8imbwJRW1EpeDNlO2460UHvGcwVMkq44bqPBkGa16fQBo1HIH0FCc p9d73u4DcomYzx5fb+XGQk+rl7MFvWkKBclVYzMYwWfefYWvxzFcq6Gjn3ItYOkbXuc2 CDhA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725459472; x=1726064272; 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=/3weTv0JDO1olCzxF1klLqGqviZ0xpQPSzNHS+GA/qM=; b=VorrE3y/fZzwB5vsDYT4su0ccatU6BfyaDaUkNHE6RLEM7htHDKDsBA64AlHjJYjBE LO+3tm7RbSDuRJeFhoy0zsl3RSNnOxVLWivABGYszX+7q601h/GOoU2FFiBLPhNgW8ck 6D6SIXzkhIEiAj/dWcMA5JnhnDbSBzo9w8Hn7sUPaNShyrtBw5qcSZQ0Pu2edL77X7bB Fsurl+amFoMhRuWE6l9W8//lkY3XQ5xHoliCMWtgl806aOh9M5oH0ziNd7Cuvh20BSaJ k7rkkboSWJY9wt7uEBFRUjS1aM398Vm6w2/wScxmpw36to1hUqx468JtglHQqzDVBdry Fr5A== X-Forwarded-Encrypted: i=1; AJvYcCUYC6+lD+wQsg6eb+LanqXJywr/mrVO7hbuOkxxxxKeJ6l5OZWfC2QwcOyLC0lqHMwPCR2ayBG4MTIgQw==@vger.kernel.org X-Gm-Message-State: AOJu0YzCifgLpVorAxCqg5lIwVLoxuhy8Ks+5gc3QtBfXHApljkEO5Jo /4vJq2gYhV4EIssKFDGs6cop82oL9hb4burLjb9G3U//PI+q/nZnOEL7OQ== X-Google-Smtp-Source: AGHT+IHXEXHSZq9e6Nc/W/109+8Kx4Ik97uxMWG+wzLcTtKwZnA/1xjIlHHukneB6BfUKC9IqvzHsA== X-Received: by 2002:ac2:4c4f:0:b0:533:42ae:c985 with SMTP id 2adb3069b0e04-53546b2df7emr12335403e87.25.1725459472360; Wed, 04 Sep 2024 07:17:52 -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.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Sep 2024 07:17:51 -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 7/8] block: add nowait flag for __blkdev_issue_zero_pages Date: Wed, 4 Sep 2024 15:18:06 +0100 Message-ID: <292fa1c611adb064efe16ab741aad65c2128ada8.1725459175.git.asml.silence@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 To reuse __blkdev_issue_zero_pages() in the following patch, we need to make it work with non-blocking requests. Add a new nowait flag we can pass inside. Return errors if something went wrong, and check bio_alloc() for failures, which wasn't supposed to happen before because of what gfp flags the callers are passing. Note that there might be a bio passed back even when the function returned an error. To limit the scope of the patch, don't add return code handling to callers, that can be deferred to a follow up. Signed-off-by: Pavel Begunkov --- block/blk-lib.c | 22 ++++++++++++++++++---- include/linux/bio.h | 4 ++++ include/linux/blkdev.h | 1 + 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/block/blk-lib.c b/block/blk-lib.c index c94c67a75f7e..a16b7c7965e8 100644 --- a/block/blk-lib.c +++ b/block/blk-lib.c @@ -193,20 +193,32 @@ static unsigned int __blkdev_sectors_to_bio_pages(sector_t nr_sects) return min(pages, (sector_t)BIO_MAX_VECS); } -static void __blkdev_issue_zero_pages(struct block_device *bdev, +int blkdev_issue_zero_pages_bio(struct block_device *bdev, sector_t sector, sector_t nr_sects, gfp_t gfp_mask, struct bio **biop, unsigned int flags) { + blk_opf_t opf = REQ_OP_WRITE; + + if (flags & BLKDEV_ZERO_PAGES_NOWAIT) { + sector_t max_bio_sectors = BIO_MAX_VECS << PAGE_SECTORS_SHIFT; + + if (nr_sects > max_bio_sectors) + return -EAGAIN; + opf |= REQ_NOWAIT; + } + while (nr_sects) { unsigned int nr_vecs = __blkdev_sectors_to_bio_pages(nr_sects); struct bio *bio; bio = bio_alloc(bdev, nr_vecs, REQ_OP_WRITE, gfp_mask); + if (!bio) + return -ENOMEM; bio->bi_iter.bi_sector = sector; if ((flags & BLKDEV_ZERO_KILLABLE) && fatal_signal_pending(current)) - break; + return -EINTR; do { unsigned int len, added; @@ -223,6 +235,8 @@ static void __blkdev_issue_zero_pages(struct block_device *bdev, *biop = bio_chain_and_submit(*biop, bio); cond_resched(); } + + return 0; } static int blkdev_issue_zero_pages(struct block_device *bdev, sector_t sector, @@ -236,7 +250,7 @@ static int blkdev_issue_zero_pages(struct block_device *bdev, sector_t sector, return -EOPNOTSUPP; blk_start_plug(&plug); - __blkdev_issue_zero_pages(bdev, sector, nr_sects, gfp, &bio, flags); + blkdev_issue_zero_pages_bio(bdev, sector, nr_sects, gfp, &bio, flags); if (bio) { if ((flags & BLKDEV_ZERO_KILLABLE) && fatal_signal_pending(current)) { @@ -286,7 +300,7 @@ int __blkdev_issue_zeroout(struct block_device *bdev, sector_t sector, } else { if (flags & BLKDEV_ZERO_NOFALLBACK) return -EOPNOTSUPP; - __blkdev_issue_zero_pages(bdev, sector, nr_sects, gfp_mask, + blkdev_issue_zero_pages_bio(bdev, sector, nr_sects, gfp_mask, biop, flags); } return 0; diff --git a/include/linux/bio.h b/include/linux/bio.h index 78ead424484c..87d85b326e1e 100644 --- a/include/linux/bio.h +++ b/include/linux/bio.h @@ -686,4 +686,8 @@ struct bio *blk_alloc_discard_bio(struct block_device *bdev, sector_t bio_discard_limit(struct block_device *bdev, sector_t sector); +int blkdev_issue_zero_pages_bio(struct block_device *bdev, + sector_t sector, sector_t nr_sects, gfp_t gfp_mask, + struct bio **biop, unsigned int flags); + #endif /* __LINUX_BIO_H */ diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h index 643c9020a35a..bf1aa951fda2 100644 --- a/include/linux/blkdev.h +++ b/include/linux/blkdev.h @@ -1098,6 +1098,7 @@ int blkdev_issue_secure_erase(struct block_device *bdev, sector_t sector, #define BLKDEV_ZERO_NOUNMAP (1 << 0) /* do not free blocks */ #define BLKDEV_ZERO_NOFALLBACK (1 << 1) /* don't write explicit zeroes */ #define BLKDEV_ZERO_KILLABLE (1 << 2) /* interruptible by fatal signals */ +#define BLKDEV_ZERO_PAGES_NOWAIT (1 << 3) /* non-blocking submission */ extern int __blkdev_issue_zeroout(struct block_device *bdev, sector_t sector, sector_t nr_sects, gfp_t gfp_mask, struct bio **biop, From patchwork Wed Sep 4 14:18:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Begunkov X-Patchwork-Id: 13790956 Received: from mail-ej1-f43.google.com (mail-ej1-f43.google.com [209.85.218.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C995E1DB53C; Wed, 4 Sep 2024 14:17:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725459477; cv=none; b=F/K0Yk7ugL6uWhWEVNSFzpBTIA7nKcF2dJbwvry2ubcXLxdWCNYMRNw0Mh32Vju1xRNRlrcFWtyk4hTYxP7RP1YrOTHY7b5W6AsDMQ/irp3ZneoQgzRl42cyBRt0agZm1E1U7XGoJFayLgSecBHEhQkZ1rsqgBbiCUVbPtD5IJ0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725459477; c=relaxed/simple; bh=fX6prqf2VFD2UV5gx7IfCiSBWUdjjihLSk5KGUumqJA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=osx0UOhwv5rPHNeM8pvQsU9wqphewXK9EwV20Xk6UMnV2gVFW9pfLDVUpzme59mA8sf/cSuqVD67H9pydD/S0z5KzHhpPKiUriNDCjplFucAtoXHNATj63jAVGPlUWfQWb2kUiInwgADUfk7mq31V46YqjC4ZO4imsAKDFXjrpA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=XKVNNA1R; arc=none smtp.client-ip=209.85.218.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="XKVNNA1R" Received: by mail-ej1-f43.google.com with SMTP id a640c23a62f3a-a869332c2c2so142217066b.0; Wed, 04 Sep 2024 07:17:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725459474; x=1726064274; darn=vger.kernel.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=llWa+KY1CsE699NO6/Qz9OdhajA1Gh72FplwVQHN8Os=; b=XKVNNA1RzFuo0sRqQ/LxORPRf8ZexVEYz3YWZxM8WnchHpAXsspC14TUDtegrHCqMG 7wky26m5O7ZGKgOWmYUZykJgJq6DLVXGbVSU0YII+rXmvNyaGMb6KmkYe+Hoew8sgzt8 x2Y5KtW05NZIvXyWBqdoA26i+MhVVHQJ+ZMQ4duGpdmerqOzmht/B0CDNtUtdNd79ZWq +ETJ9Vp/qRY/fdA7rYWx4U/jhbWfTm8l6jEhBU3bNpa6e5hpkZPL4M031Yly2O8KTsfG QKLbN54xVNW7vK5MhMvDD9v4nRlRUE/MEs/E4FGF5jzXIwncqMMHAnqpg7UJeFVuENoW Uovg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725459474; x=1726064274; 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=llWa+KY1CsE699NO6/Qz9OdhajA1Gh72FplwVQHN8Os=; b=mF4P4hoW5kx8LPBoSxY2zjb7wqgPI9/iDCi5Oxo43lAtXSmwK7qhm76uPBTtnKyoz8 tsQlCwpP0db8kGOrBxLNv4klRq4Snm1TYeFO1Tv+fWg4u2r8vtvwQN0GJ93wV5i9VYf7 2tyXF+5vS2gHk6nJDgx6Pg/73x8zreBM11Cs9FuglKsIufO5AcipIxLIqrhmS5EiTCn6 uKCq4u7Te0DEI6oySJW/h4Dnn3TNMJcj3ROKyDxv+MpljNol9YOPn8lQTuu9bYJrfz6x +utIqtVqXidzipczLzfuBneeMQeFTlrTkdYwwCNL5vzbrTe2ixtvQ0ZJeMmBBqji8yBs dfGw== X-Forwarded-Encrypted: i=1; AJvYcCWEo6x6jOfZWzM8cXGJM0IKplnZKL4M9mrmTejf1OYZhcFF/0uNVwHDJgf9gigvAUOcH7SPeQCONmI3QQ==@vger.kernel.org X-Gm-Message-State: AOJu0Ywp36G2nyYYpJmMj6XwdhCGQBe6f8E3mzfOK3sYNqCnyusoTz/E cU3O3p/5SDF0RzhlwVggFs9UM5iv8fyyTe5eFhtyykry2fD3o4/3SZeB3F7h X-Google-Smtp-Source: AGHT+IFWD5NeSx4L5OBZcmDku0R0EHp0Mplv+v0QTrmPwvlzQ40tRMtryzZM6Z0h0n/l6uV8EPBdaQ== X-Received: by 2002:a17:907:3fa4:b0:a7a:8dcd:ffb4 with SMTP id a640c23a62f3a-a8a430913f9mr290483266b.17.1725459473656; Wed, 04 Sep 2024 07:17:53 -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.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Sep 2024 07:17:52 -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 8/8] block: implement async write zero pages command Date: Wed, 4 Sep 2024 15:18:07 +0100 Message-ID: X-Mailer: git-send-email 2.45.2 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Add a command that writes the zero page to the drive. Apart from passing the zero page instead of actual data it uses the normal write path and doesn't do any further acceleration, nor it requires any special hardware support. The indended use is to have a fallback when BLOCK_URING_CMD_WRITE_ZEROES is not supported. Signed-off-by: Pavel Begunkov --- block/ioctl.c | 24 +++++++++++++++++++++--- include/uapi/linux/fs.h | 1 + 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/block/ioctl.c b/block/ioctl.c index ef4b2a90ad79..3cb479192023 100644 --- a/block/ioctl.c +++ b/block/ioctl.c @@ -774,7 +774,8 @@ static void bio_cmd_bio_end_io(struct bio *bio) static int blkdev_cmd_write_zeroes(struct io_uring_cmd *cmd, struct block_device *bdev, - uint64_t start, uint64_t len, bool nowait) + uint64_t start, uint64_t len, + bool nowait, bool zero_pages) { sector_t bs_mask = (bdev_logical_block_size(bdev) >> SECTOR_SHIFT) - 1; @@ -793,6 +794,20 @@ static int blkdev_cmd_write_zeroes(struct io_uring_cmd *cmd, if (err) return err; + if (zero_pages) { + struct blk_iou_cmd *bic = io_uring_cmd_to_pdu(cmd, + struct blk_iou_cmd); + + err = blkdev_issue_zero_pages_bio(bdev, sector, nr_sects, + gfp, &prev, + BLKDEV_ZERO_PAGES_NOWAIT); + if (!prev) + return -EAGAIN; + if (err) + bic->res = err; + goto out_submit; + } + if (!limit) return -EOPNOTSUPP; /* @@ -826,7 +841,7 @@ static int blkdev_cmd_write_zeroes(struct io_uring_cmd *cmd, } if (!prev) return -EAGAIN; - +out_submit: prev->bi_private = cmd; prev->bi_end_io = bio_cmd_bio_end_io; submit_bio(prev); @@ -904,7 +919,10 @@ int blkdev_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags) 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); + bic->nowait, false); + case BLOCK_URING_CMD_WRITE_ZERO_PAGE: + return blkdev_cmd_write_zeroes(cmd, bdev, start, len, + bic->nowait, true); } return -EINVAL; } diff --git a/include/uapi/linux/fs.h b/include/uapi/linux/fs.h index 68b0fccebf92..f4337b87d846 100644 --- a/include/uapi/linux/fs.h +++ b/include/uapi/linux/fs.h @@ -210,6 +210,7 @@ struct fsxattr { #define BLOCK_URING_CMD_DISCARD _IO(0x12,137) #define BLOCK_URING_CMD_WRITE_ZEROES _IO(0x12,138) +#define BLOCK_URING_CMD_WRITE_ZERO_PAGE _IO(0x12,139) #define BMAP_IOCTL 1 /* obsolete - kept for compatibility */ #define FIBMAP _IO(0x00,1) /* bmap access */