From patchwork Wed Aug 14 10:45:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Begunkov X-Patchwork-Id: 13763327 Received: from mail-lf1-f50.google.com (mail-lf1-f50.google.com [209.85.167.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 DFF3A1AE043; Wed, 14 Aug 2024 10:45:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723632326; cv=none; b=TGcr0Yq7/94JXj4MnxviYrw/HadmR0dpz+E5Igmq9dSd5i/OoXm5Rq6jCEwvq6lqvngKCIedG0NogHzgO9UIl8VSN99FxEgWc0RkVW2wHipBxSMRez/JGm7AlcFzK2KHOV1vIFqFEIAO9UzySrD/qB28DuBZywLCXb5bF094FYw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723632326; c=relaxed/simple; bh=D5/M3jfkzlm1q0HAxOF7tvUuDwKusrC3BWOgtmCzAYo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=kZxUp9hixTquPsfj+Gp0AyElKjren+TOrLp9HeczOZLHkILW2J2iAXhr2NdtcVgw6SgY08jnKpvjUC+QXXmmLP/JarBnXEQnnirRctez6DJBgghBbGtMkmoGlTS/4MRWeKwOGjO+9n+SqfWRaj4ItnkSk1xq4fdKKEusxRMxXQw= 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=QOwHo5Sp; arc=none smtp.client-ip=209.85.167.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="QOwHo5Sp" Received: by mail-lf1-f50.google.com with SMTP id 2adb3069b0e04-530c2e5f4feso6643087e87.0; Wed, 14 Aug 2024 03:45:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723632323; x=1724237123; 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=oGCUwVa5Kyp0Pg2+kUxIsl/LPJjPrI+WqiYx+qtKk3Q=; b=QOwHo5SpGtw1GePy3ddoUvcI9fkE4lAg4+QK94F3ddC95Gs3Hd7uxxxCGf6C6VbiPs FUZCZLM94YL5roljyCcweYVXQkY/PGL7/ADLqq3l6srnoeVY5Ke+rfOsY8lUPqqMBF2p Ka9J0T9LDE3wp9AasorNIDLo4wg9F4VzSrEvu4JyoBDPammBlEweCOGyi1I7xQ4/evF/ 3fAm1dHCypNM1HXO4KuL2xqJrh16W98g86gR6OwcY5EQGA2qvHGdws0RiLROo2G6N74R Bcxbj2PA6xULzCwcT63FLdvZSy9G7C0YXWtNmFTdhZchell6eo46SdZxUiQRj4O0m+lF kbFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723632323; x=1724237123; 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=oGCUwVa5Kyp0Pg2+kUxIsl/LPJjPrI+WqiYx+qtKk3Q=; b=EVbBexynoJMbLr67yEJAIxRMvK1wiirfHEwr9O5Q11bmzt4YlzQT7c7170XIkuo2cT fowYHqCsAvDAptlZwOXO+Bl6hYugfXp5p57D6aHBz3+0VGarign2YEajyOycyHwNk8eu arccOGcAzUG9Dv9xyaWoyh/4LSx9MLu4yukuVBD35jAcr687RS/HRKIWIZuwU6F/HTir v9LJi34tSFj2FyliQKj9GufBp7KIRxJO0j0VMF7P/73gytTJ2i6dCWmqwWlkyz14DLGH uB4NZ4ql/z+jY6phwzR5Vo/GH3OaZJ8FbVDaCWXvVwX+WbA5MAa8fKvhn8Ph63+fW8rA hIIw== X-Forwarded-Encrypted: i=1; AJvYcCVHWpp5CRGs/AeWh0ZMKYAE3kX8Y+1sTHigK73RS84NY+9FCGyE/euiqKbxfTOOXte6RzxJudLog/gdOIrvJVz3HHskfCAuiSNCCRY= X-Gm-Message-State: AOJu0YyKOxvAOHdys1qkfYnTpSGF/LdRRN7f+vYRiiWyN9XcaknIJ66b M2+0aeu7iPIh5NJTroOQ50W2TWRWr272GurVsh+e20KG2LEWqKGUCPK6CosK X-Google-Smtp-Source: AGHT+IHhdpdAhXAFGJZHpDxWh8B1Ms2Ng2AgCkdonuHrS94jDeJvM4fNY2m5pVGUUhNgCJJpgyCaZQ== X-Received: by 2002:a05:6512:1591:b0:52e:be50:9c66 with SMTP id 2adb3069b0e04-532edbbe548mr1283237e87.53.1723632322432; Wed, 14 Aug 2024 03:45:22 -0700 (PDT) Received: from 127.0.0.1localhost ([148.252.132.251]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a80f418692asm157212766b.224.2024.08.14.03.45.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Aug 2024 03:45:22 -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 Subject: [RFC 1/5] io_uring/cmd: expose iowq to cmds Date: Wed, 14 Aug 2024 11:45:50 +0100 Message-ID: <0e94fb94c3d627dbd5c210d00bd6cc3e644d00ff.1723601134.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 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 3942db160f18..da819018088d 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 c2acf6180845..2896587a5221 100644 --- a/io_uring/io_uring.h +++ b/io_uring/io_uring.h @@ -90,6 +90,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 Aug 14 10:45:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Begunkov X-Patchwork-Id: 13763328 Received: from mail-ed1-f44.google.com (mail-ed1-f44.google.com [209.85.208.44]) (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 5F63E1448D4; Wed, 14 Aug 2024 10:45:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723632327; cv=none; b=ed+TT9i/xz0+sRmcJAcYPLagctFQC2+4VdjlT0m68fXuOqZK3hOPo7dHmqe2qDg1CdO61CHG0VMaSACwAQS2IyaV+gN/9uLK8UYzsgZP+sRFKC0FK7Emjr1PewQimWchdKprzHvdsAdrOGP3fUQiTTMbyXFf2j4ChnDDlaCmDSg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723632327; c=relaxed/simple; bh=xgZK/jw8ZlQXj8cIxR0W7ybGVnlcP1lJNS+9Q/l9a0s=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=LWn/Ya61A61X+tMxdjfTQV8buC8ewMq29qQmxnpMNppqR7I4UfWDeaDE8A0aEIIsY4a05L8JzBXLz8v+xyRnaZIfTK0zFYuqid/FMUKUUa//2E1rEwTQctK4POVzSkq1vMyzd66HZnD0IyXMint+GhT4F0j8f1aIe1YOCOk9rqk= 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=MYb7WkV9; arc=none smtp.client-ip=209.85.208.44 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="MYb7WkV9" Received: by mail-ed1-f44.google.com with SMTP id 4fb4d7f45d1cf-5b01af9b0c9so6814695a12.3; Wed, 14 Aug 2024 03:45:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723632324; x=1724237124; 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=MYb7WkV9t56ofqod+2slCDpOk8holAVgjR1BFVA6vgPnNPHvbK8wgKv7CLmw0L506G /KkJql3H0r/gOK/+bJXfBL9Vuwia9G8MkzIMd1UnhwWCpdBEtdk8Jg36xCETjo1fAzPx BeC2ocCpDtRp6oo0NWtrs4/pAWHDSEFB5UcGYazkGGGKQTFwxLz015FsEshzE1xSs8j4 nQLueXHKvRFzpndqRluQtD0HNE/C6+g8DCP7rdeRp+uGekDpS0NdYTuymwybgDvOgegv 7gy8h1IEakEvgaU5ATPSM0CHblXy/vdyJVpnHqAoRXq55QV67DIcp8qeCuh/u/9N/TY7 dFeg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723632324; x=1724237124; 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=I6JAM+wZY7/tqadQUOW1dK5lpWCKU6pAGscwQa8qHJU7SySBVC3rd0JQB6nD4DE7En ZVBGF7mzXbpr6FJDp5yBAfQdwnmmTM+BLXySCNSYcEj7PMvktWM5xLFF9jk2rf3U88Gs xQzKn+kbl5Pf1KE5PdS7272RdoUSY0xqRox1eXBMoz+U0BIqPNx88jLgw/5VyJG5fywa fSitsMq4vb7kpIROR22rhE+5NUjuWxV0u1dSelpyDiIOlYXd0pYBqIx7Y3RGzenxnQtK yrr0hl4IgXM+QuLhthb4mJfZGgVwcQb45/3dmRNMYPaVweHDt1XPEAYI8QW8C6dNTd53 SJ9A== X-Forwarded-Encrypted: i=1; AJvYcCW5mRbf3P2op8QmA24XJWC86dw/nBkCAZxkNHwuRUZn5QlIkvmJXM+Zo05O1wjuZx1Qq3YZxkl6Qcmp73LlKSznLS3ST+xICtnJUTo= X-Gm-Message-State: AOJu0YzEStKKsPdLqxqzY9gF8EaeoTEscnJpRwzpt1eQ1ufUmp572avy rl8HRtD3etznQ9U7ZdgTkQRYgoPUcEHtRyhy2sNG4mVakXzart88it5lGbQa X-Google-Smtp-Source: AGHT+IF0FJ+sTAvpOE9fJ0iyXXmALD7st6qSqWtNfVEYLYX577G+yNnVuj6lODfTWpASdYrnqFEhoA== X-Received: by 2002:a17:907:e6d1:b0:a77:cf9d:f495 with SMTP id a640c23a62f3a-a8366d658cfmr180030066b.40.1723632324005; Wed, 14 Aug 2024 03:45:24 -0700 (PDT) Received: from 127.0.0.1localhost ([148.252.132.251]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a80f418692asm157212766b.224.2024.08.14.03.45.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Aug 2024 03:45:23 -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 Subject: [RFC 2/5] io_uring/cmd: give inline space in request to cmds Date: Wed, 14 Aug 2024 11:45:51 +0100 Message-ID: <86ae0042c57c5904b40e19f6171f47fd3e8c6126.1723601134.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 Aug 14 10:45:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Begunkov X-Patchwork-Id: 13763329 Received: from mail-ej1-f54.google.com (mail-ej1-f54.google.com [209.85.218.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 6970B1AE86B; Wed, 14 Aug 2024 10:45:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723632329; cv=none; b=Fd4yrYpOHyfhJPhmopKISDAxu1Jyp11ZEuaJJ4z8Q2F+XLUr8ZbBMbFrdi6cSqWWsTGYbtG0bJxTRavzSq1H2msPji7ZlMAT9++snjhaNm60nqLxc6AZaqSs+pwiTmEo9yJHFaSewthLjz3mzX7zgzq/nlRlKo+N9GyHmvoXKts= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723632329; c=relaxed/simple; bh=w2+Gx2llTSCIEW4z0w0ChEqAXKpMZsr4Hk2SFKO+bsU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=kHHBjHdqMVqiUfYa7y+0/LfkhhqlWIVdNf8QUIsI1UtZPwQ1DYHi+rOCVngOqZdjtc8dTkCat3nXURIHBRyD9jmGIQvA2RmMbg6tK5mpHigsU3JKWYh+5R7T3BkK0/AgFPHOdcsqkCFStWovCRUvwFjWdqBr9R0FDWVoluoPaP4= 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=MrbT4Az/; arc=none smtp.client-ip=209.85.218.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="MrbT4Az/" Received: by mail-ej1-f54.google.com with SMTP id a640c23a62f3a-a7a9185e1c0so498025566b.1; Wed, 14 Aug 2024 03:45:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723632325; x=1724237125; 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=coYbv4PcaCiw3s7iKLGpr0JKeDXM/58rHgT2Op2hXlo=; b=MrbT4Az/JZROeAovtsLxW4rgqg1yX5ADvPTWssGYum2DsMH75V5jnEoLWKEvGAkYrc aIHJwBy3+FStC4sq8E5VGUnY0RQCoApod7FfuDasUgzPR6invPy8Aqqd4SfLnlmMRKtv KV2ssW238YDa+JlWUMLJtmXOJPdzr+M+uQOlM95cwHaTqCIjhbGkA0w46t7J9YGJEaha Dh27elv1GZOIoGU2HTQZclUaM9QTfQgu4APTUjX64bjiVdsf9V7pGcKpR2PIisEv6CzY HFyQtVrPzAdJCoknFMxl0vXvQTDfzsFPWBnNfeHpMJW6xtW6Vkv76jjcykpyn8PyaM0Z EHrA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723632325; x=1724237125; 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=coYbv4PcaCiw3s7iKLGpr0JKeDXM/58rHgT2Op2hXlo=; b=fNi4tEGDNBOqdamzVbEsJZzegd1YxYSsDfhNMiIj48ONzytBWl11csfTPkPnwLYk0G bkfkjrkMYsb+hcvm180/OtGAnoQpYZpoE8SwO9tZrgObD5BJUqYrqyGC+V2y0s7U4S2+ Qm54a8GrTkRdiR5PosrmCooqqVg+7bDhTleSVBwxvpgywabVGgzvqtVcSxfnzE1HS7J/ Ue2l5ae8owrEJnFQT8upWpTSJoexS7RKF8h+UgoBFSFbTTbD2PgKyBy92DsyB8XWNJ7z SmAmUKUU8OywAYhYh728sIIYe5O01gBynF35CsBfHX67GU/fIRpCoQItvpVaeuCYpCGQ w4cQ== X-Forwarded-Encrypted: i=1; AJvYcCU2Lnnz3/1xFQYgrzieYUxcMBV78IZz/RUjR6BlfF89XHBLUqOevRVZK7QdBoiUeaJhRWL7IOzaDFZVJeyHA0Xh51kQ2x+k2svov9o= X-Gm-Message-State: AOJu0Yxoo7L66hD92jVzZBquKX/A1hdTuN9Ubj+1emJ57yN1hKHtPHf7 eSU6SzO41/oTE5MBAi+c2MeTQKVQdNDX6wTHrPxgFJj/I8+0Y7Z1DMc02xxs X-Google-Smtp-Source: AGHT+IGfriIepvv++2PrjZ9Q9AjLepaO3ymcXMwFRj3SpDze6Sv3EXtnN23wF2rBhfZ8mcwRYC92+g== X-Received: by 2002:a17:906:cade:b0:a7a:1cfe:a262 with SMTP id a640c23a62f3a-a8367009c0emr168159466b.55.1723632325229; Wed, 14 Aug 2024 03:45:25 -0700 (PDT) Received: from 127.0.0.1localhost ([148.252.132.251]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a80f418692asm157212766b.224.2024.08.14.03.45.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Aug 2024 03:45:24 -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 Subject: [RFC 3/5] filemap: introduce filemap_invalidate_pages Date: Wed, 14 Aug 2024 11:45:52 +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 | 18 +++++++++++++----- 2 files changed, 15 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..74baec119239 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,16 @@ 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; + loff_t pos = iocb->ki_pos; + loff_t end = pos + count - 1; + + return filemap_invalidate_pages(mapping, pos, end, + iocb->ki_flags & IOCB_NOWAIT); +} EXPORT_SYMBOL_GPL(kiocb_invalidate_pages); /** From patchwork Wed Aug 14 10:45:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Begunkov X-Patchwork-Id: 13763330 Received: from mail-ej1-f46.google.com (mail-ej1-f46.google.com [209.85.218.46]) (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 A00661448D4; Wed, 14 Aug 2024 10:45:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723632330; cv=none; b=oI28P77ixu5O3VveWWvuBod5Mxl3rYjjOhHmYWEyS5UwmKGruiwdceVoTRpnmhddvURWlh86jyrivC3/EqyM2CVPraOk22kCjaC/XRQWl7/O012geSsORzZ4P80B7ojPmEzkXswNwP25Lg1XcF5EZ3FV+2hhdtuoyUBF70FvRlY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723632330; c=relaxed/simple; bh=u3n4zzz10biwWSRIUfBz8MlJPLdBSaqHZ7gqivjEoC4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=IOJXvox6oW8hM/tWxKFWG7c9EK8dl5VfWZdM1IL03Rs+ICLg/aDIOOEQw/Q1QCIU26rOYU5xUnDfbsLp1p6S/YbA/dNRfS6FYOo1EXpJXqJ8txqh/wAKo1CKMYVDqHQm2izw4YpqL+JBpmtXQA2qJxzYei3/yoipev7gb/owTv4= 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=GhBIQJ4M; arc=none smtp.client-ip=209.85.218.46 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="GhBIQJ4M" Received: by mail-ej1-f46.google.com with SMTP id a640c23a62f3a-a7ad02501c3so778841466b.2; Wed, 14 Aug 2024 03:45:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723632327; x=1724237127; 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=fvJpr7p454Y/UfNPCoQA/axDAfLDDtHMeofcmIJVphY=; b=GhBIQJ4MCSlsJF2LpP+qfSjtNFQsfpCeIVggH5KwmIjla2A5BFnLC7hv7qDbxGg2yw pbDim5hjT29SX0gQqBT680NdWwQgDQZKTS6ZG4+zexllpb6Cz8UoxW0ZC3iV5jffa5+z nUjzlEAkr2Pb5TAmV84ysPyXnOmu68erh7pQOABc0SqSlI9b/Kn7ayT1Hb6yUAyE4wTy FquT432h4R8zSDFBRAkmhrcGBQnoZ1LkeWPEA8B2xNZ+5IrIFGoV/sD/34ETlcjKv9iH liONV2NWTbup5IZyoovyI6ISi0yThmj6HnEwg/GDXOSeU80WIXJyaUYA0++rNKJ/E6qg SrRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723632327; x=1724237127; 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=fvJpr7p454Y/UfNPCoQA/axDAfLDDtHMeofcmIJVphY=; b=nmqS4Ay989UcgykCa6JtGkFg0uGrC7tOlQwve1QcOhb0ZUVJEOL4SSTeJCThfF4dVC DsQOtMfeWWNh9binZohaRT20QMJoHJR9y+6ZMAHSkGlJUGZv+hhTMGyy8opGhKU907Zw 9+Mse0Ljf8p9zEA1bMAOOe578N+Qq/504Cu+obecvn7uSFAYGfSoa83YnL/3c++ppQgM m877Rle1ax6U6RwZdwyXkiGwrzvrY3l+O8es4dRPN+TmFiTR9Gr2jsP2Cd8ddesFeSnn YX+zOC1UCnLGB7ZY5e6GbCFgMaeqrrI5hmWthhSePTrB3rIxWcufgpcPZDTmIparp4cU pBcg== X-Forwarded-Encrypted: i=1; AJvYcCWSsJqr4PSbrc78s8QR0G2+fCT0leSHcPW5eL4RT+8M8tgy7VD13a7Ry0FDWTwGRfoXNsc4ZU7XNZBk6/jVlLdPlaI4valZV/m5NSs= X-Gm-Message-State: AOJu0YwYN3rmVhF3T7m/pwr8DHGGxQaDCijqBvhvmKKVmtHWTeB0SjEY 87wvfCMwkMzsRd2WXyR3rOd5oH7DKhBaQguyEnnmWolv/oOAOCIcgpVpsk0H X-Google-Smtp-Source: AGHT+IF5x7vVq+APZiVW0Zf/kYmxy4TtcaaAPS1Fj1piCtyS5MQvRUzOqgbF+0Kt0wNs5ja3bp6E1A== X-Received: by 2002:a17:907:e601:b0:a7a:c197:8701 with SMTP id a640c23a62f3a-a8366d5b86cmr180254766b.31.1723632326621; Wed, 14 Aug 2024 03:45:26 -0700 (PDT) Received: from 127.0.0.1localhost ([148.252.132.251]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a80f418692asm157212766b.224.2024.08.14.03.45.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Aug 2024 03:45:25 -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 Subject: [RFC 4/5] block: introduce blk_validate_discard() Date: Wed, 14 Aug 2024 11:45:53 +0100 Message-ID: <41ac9ff000cbd47fd8e386ee70e8049c3ac80ead.1723601134.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 In preparation to further changes extract a helper function out of blk_ioctl_discard() that validates discard arguments. Signed-off-by: Pavel Begunkov --- block/ioctl.c | 45 ++++++++++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/block/ioctl.c b/block/ioctl.c index e8e4a4190f18..c7a3e6c6f5fa 100644 --- a/block/ioctl.c +++ b/block/ioctl.c @@ -92,39 +92,50 @@ static int compat_blkpg_ioctl(struct block_device *bdev, } #endif -static int blk_ioctl_discard(struct block_device *bdev, blk_mode_t mode, - unsigned long arg) +static int blk_validate_discard(struct block_device *bdev, blk_mode_t mode, + uint64_t start, uint64_t len) { - unsigned int bs_mask = bdev_logical_block_size(bdev) - 1; - uint64_t range[2], start, len, end; - struct bio *prev = NULL, *bio; - sector_t sector, nr_sects; - struct blk_plug plug; - int err; + unsigned int bs_mask; + uint64_t end; 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; + bs_mask = bdev_logical_block_size(bdev) - 1; 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) +{ + uint64_t range[2], start, len; + struct bio *prev = NULL, *bio; + sector_t sector, nr_sects; + struct blk_plug plug; + int err; + + if (copy_from_user(range, (void __user *)arg, sizeof(range))) + return -EFAULT; + start = range[0]; + len = range[1]; + + err = blk_validate_discard(bdev, mode, start, len); + if (err) + return err; + filemap_invalidate_lock(bdev->bd_mapping); err = truncate_bdev_range(bdev, mode, start, start + len - 1); if (err) From patchwork Wed Aug 14 10:45:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Begunkov X-Patchwork-Id: 13763331 Received: from mail-ed1-f47.google.com (mail-ed1-f47.google.com [209.85.208.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 556311AE86B; Wed, 14 Aug 2024 10:45:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723632332; cv=none; b=j2b3QwXwltRQGlopdqc3ujPqWb+oojZZ8MzPa3u0jV4rlGIaGyuS8R4J4Kb/LXRquNn/xZwS6b5vviLR11eMdGzqZJ/YWiyQS2viotT7Ztr3CLOYyf1fGxRnlZpB7/E5EO6tNRemAcZRHQh/n0wy2W3l4K1I10okpsXKcXgTZN0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723632332; c=relaxed/simple; bh=psn+4V45YBQ+6tX8s9X4iuZogIB3QAGe6mFbj/cnPUY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ME2T3SmbgUlt43mVJlGxP68nakdo75nMQo1/wkXzwKmLzpBG4WbPHJUPikwWJgJ4TsItevhdgiqDAfk1pawG2wXYgSdkkgItJjx/aXow1hAYdwkoKeN96639t/J0sQ6AODKtKNFkRcWmEeI9yfDTWwcEExUIl0/YmF0xYR+LHTU= 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=Tciig4Ci; arc=none smtp.client-ip=209.85.208.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="Tciig4Ci" Received: by mail-ed1-f47.google.com with SMTP id 4fb4d7f45d1cf-5bd13ea7604so5210258a12.1; Wed, 14 Aug 2024 03:45:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723632328; x=1724237128; 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=Tuo5pTUVgRqd7+Wu/p/7uLxEPhnfCWa8o0PxSIfrqWo=; b=Tciig4CiwyeK5rFGAdG+sTvwjGeUzncrLU32i8RXoT9A0Hdqe5dEzOKUIgAZ3HZuN9 4cC5LV69zTWPI13ZzoscZc0vQFLa7jqtfCqQvl/LzDApgI+pg/e05QRUKmTxi3Zw1Wap nTZXZsNG59ioW9wlO/w3nefOqXWg4/PGnH0dxui4AXb/nWbsdYq5ltVT5R9RieZ/1Afw lF07W1YXc328ObPZ73rUMZRiUq/mwhTC4l0kuLRVlfB3OxtfKJ+q+YRY/Spy9ZE8M941 cxXxXWiRqmdFbIe/4J7vYvfFgO42ofvLus7c0paXGOkVKX0k5BGleyKSfEEaQnPeJ+Xu SR0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723632328; x=1724237128; 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=Tuo5pTUVgRqd7+Wu/p/7uLxEPhnfCWa8o0PxSIfrqWo=; b=OLJU+1AZpOkvvN8ub/zsonwkMoO3ZVrEwYg73i5iYT8ARjtRIdcOXrIBOoGNbtaTS6 b/Dpya87ZhxeTnMLWAp31PYq4D3wUQZXSomj/3UaiKlVvN3NgCuZeLyteQHg0sJw6e9L 1SbzXJYXBpyl1Z08bp1XHUUJmMz5lM/rMAcqRpZI2HP6ySaftEYFQMBym4PXRXe8RtaO 2f51UBTB8/1JirTVU08Ui+ZkaDii+HkUA2+4pC7Ytkby9LMOj/nugnnkDlHSZ6qpexB/ 5ii4sAi4WzCwvOnZ2kxOJz/6CCfCb8NWYkChMBaPEjt/5QI50XE70YCh28L07D9I4NXO CtTQ== X-Forwarded-Encrypted: i=1; AJvYcCWUW/1T6dY0vSEn6JO5OinSIZ/LPdsg6Ae37OaJLHT1ZsO/cSg8J0c2GAbJ7nlKihmhnFoMJYbKTU27EDC47d7ETTdlTY4ytBLXk0U= X-Gm-Message-State: AOJu0YxWTpj8lhvzcwpo5MRsokYVSH4md2+AjEb+rvSFm9JNktErvhpy KF1xpNolV2xeAF7sQJwq+7wSLAs/ogXVj5Qcj+7PlXv//JoBoU9IcnDXeqlJ X-Google-Smtp-Source: AGHT+IG1KkX6wMcxGFIa+KqlcGfRZzjy0lHz5phQrjiJN6wASSWoiIqHMLk5BznC58MpKrZaWW6grA== X-Received: by 2002:a17:907:84c:b0:a7a:bc34:a4c9 with SMTP id a640c23a62f3a-a83670bf16cmr128595066b.69.1723632328176; Wed, 14 Aug 2024 03:45:28 -0700 (PDT) Received: from 127.0.0.1localhost ([148.252.132.251]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a80f418692asm157212766b.224.2024.08.14.03.45.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Aug 2024 03:45:27 -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 Subject: [RFC 5/5] block: implement io_uring discard cmd Date: Wed, 14 Aug 2024 11:45:54 +0100 Message-ID: <6ecd7ab3386f63f1656dc766c1b5b038ff5353c2.1723601134.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 ->uring_cmd callback for block device files and use it to implement asynchronous discard. Normally, it first tries to execute the command from non-blocking context, which we limit to a single bio because otherwise one of sub-bios may need to wait for other bios, and we don't want to deal with partial IO. If non-blocking attempt fails, we'll retry it in a blocking context. Suggested-by: Conrad Meyer Signed-off-by: Pavel Begunkov --- block/blk.h | 1 + block/fops.c | 2 + block/ioctl.c | 94 +++++++++++++++++++++++++++++++++++++++++ include/uapi/linux/fs.h | 2 + 4 files changed, 99 insertions(+) diff --git a/block/blk.h b/block/blk.h index e180863f918b..5178c5ba6852 100644 --- a/block/blk.h +++ b/block/blk.h @@ -571,6 +571,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 c7a3e6c6f5fa..f7f9c4c6d6b5 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, @@ -744,4 +746,96 @@ long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg) return ret; } + +struct blk_cmd { + blk_status_t status; + bool nowait; +}; + +static void blk_cmd_complete(struct io_uring_cmd *cmd, unsigned int issue_flags) +{ + struct blk_cmd *bc = io_uring_cmd_to_pdu(cmd, struct blk_cmd); + int res = blk_status_to_errno(bc->status); + + if (res == -EAGAIN && bc->nowait) + io_uring_cmd_issue_blocking(cmd); + else + io_uring_cmd_done(cmd, res, 0, issue_flags); +} + +static void bio_cmd_end(struct bio *bio) +{ + struct io_uring_cmd *cmd = bio->bi_private; + struct blk_cmd *bc = io_uring_cmd_to_pdu(cmd, struct blk_cmd); + + if (unlikely(bio->bi_status) && !bc->status) + bc->status = 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) +{ + sector_t sector = start >> SECTOR_SHIFT; + sector_t nr_sects = len >> SECTOR_SHIFT; + struct bio *prev = NULL, *bio; + int err; + + err = blk_validate_discard(bdev, file_to_blk_mode(cmd->file), + start, len); + if (err) + return err; + 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_KERNEL))) { + if (nowait) { + if (unlikely(nr_sects)) { + bio_put(bio); + return -EAGAIN; + } + bio->bi_opf |= REQ_NOWAIT; + } + prev = bio_chain_and_submit(prev, bio); + } + if (!prev) + return -EFAULT; + + prev->bi_private = cmd; + prev->bi_end_io = bio_cmd_end; + 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_cmd *bc = io_uring_cmd_to_pdu(cmd, struct blk_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; + + bc->status = BLK_STS_OK; + bc->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, bc->nowait); + } + return -EINVAL; +} + #endif diff --git a/include/uapi/linux/fs.h b/include/uapi/linux/fs.h index 753971770733..0016e38ed33c 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 0 + #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 */