From patchwork Fri Jun 10 09:07:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hao Xu X-Patchwork-Id: 12877050 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9E664C433EF for ; Fri, 10 Jun 2022 09:10:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348330AbiFJJKi (ORCPT ); Fri, 10 Jun 2022 05:10:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44378 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243365AbiFJJJ7 (ORCPT ); Fri, 10 Jun 2022 05:09:59 -0400 Received: from out1.migadu.com (out1.migadu.com [IPv6:2001:41d0:2:863f::]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 527F3279E42 for ; Fri, 10 Jun 2022 02:07:47 -0700 (PDT) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1654852066; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=CvrJD9gksnnXOee+F5KF9tGQ2m10atYyWkwTq+1fegY=; b=Zht1Rg9viAqdmUNmueW8Ugz5q3VzWg8vEVIfzjDAyi2hbEdSfme2p27JpEmJ0dFo3xkz13 f6YBluOMSoT0ZnrURIcBQIe7Udu9Lu7ZJk8J8E72yFvRUyQcBrYqMZOCq4VhUKynlCTyFt 2F51KmVa5KjipMJ8LjxphsZeFkdvrU8= From: Hao Xu To: io-uring@vger.kernel.org Cc: Jens Axboe , Pavel Begunkov Subject: [PATCH 1/5] io_uring: openclose: fix bug of closing wrong fixed file Date: Fri, 10 Jun 2022 17:07:30 +0800 Message-Id: <20220610090734.857067-2-hao.xu@linux.dev> In-Reply-To: <20220610090734.857067-1-hao.xu@linux.dev> References: <20220610090734.857067-1-hao.xu@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev Precedence: bulk List-ID: X-Mailing-List: io-uring@vger.kernel.org From: Hao Xu Don't update ret until fixed file is closed, otherwise the file slot becomes the error code. Fixes: a7c41b4687f5 ("io_uring: let IORING_OP_FILES_UPDATE support choosing fixed file slots") Signed-off-by: Hao Xu --- io_uring/rsrc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/io_uring/rsrc.c b/io_uring/rsrc.c index d78e7f2ea91f..cf8c85d1fb59 100644 --- a/io_uring/rsrc.c +++ b/io_uring/rsrc.c @@ -705,8 +705,8 @@ static int io_files_update_with_index_alloc(struct io_kiocb *req, if (ret < 0) break; if (copy_to_user(&fds[done], &ret, sizeof(ret))) { - ret = -EFAULT; __io_close_fixed(req, issue_flags, ret); + ret = -EFAULT; break; } } From patchwork Fri Jun 10 09:07:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hao Xu X-Patchwork-Id: 12877051 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7BED5C433EF for ; Fri, 10 Jun 2022 09:10:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243365AbiFJJKj (ORCPT ); Fri, 10 Jun 2022 05:10:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40516 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343938AbiFJJJ7 (ORCPT ); Fri, 10 Jun 2022 05:09:59 -0400 Received: from out1.migadu.com (out1.migadu.com [IPv6:2001:41d0:2:863f::]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A9BD91E82A0 for ; Fri, 10 Jun 2022 02:07:49 -0700 (PDT) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1654852068; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=L1wIvv3zLWMR31Nd8s1DbvuiWn532QeRZmT5/FrvJdE=; b=q5Y/70HuwnuIfNaffHMJEThg1cRW9iR3ImdgDhw+Tw3h88zt9AKhDchdXxxJIq7J5Cuggf Skjp2AtP5iiKmVrmk3y0gp9c6vAOFPPdjkiORZLM8TChLDtTBfOeV1Ev6mVba1cqVgGbTf oW2kDCtQKw1cWmIvR+9hM+s2pKmYCs4= From: Hao Xu To: io-uring@vger.kernel.org Cc: Jens Axboe , Pavel Begunkov Subject: [PATCH 2/5] io_uring: openclose: fix bug of unexpected return value in IORING_CLOSE_FD_AND_FILE_SLOT mode Date: Fri, 10 Jun 2022 17:07:31 +0800 Message-Id: <20220610090734.857067-3-hao.xu@linux.dev> In-Reply-To: <20220610090734.857067-1-hao.xu@linux.dev> References: <20220610090734.857067-1-hao.xu@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev Precedence: bulk List-ID: X-Mailing-List: io-uring@vger.kernel.org From: Hao Xu In IORING_CLOSE_FD_AND_FILE_SLOT mode, if we successfully close fixed file but fails in close->fd >= fdt->max_fds check, cqe-res = 0 is returned, which should be -EBADF. Fixes: a7c41b4687f5 ("io_uring: let IORING_OP_FILES_UPDATE support choosing fixed file slots") Signed-off-by: Hao Xu --- io_uring/openclose.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/io_uring/openclose.c b/io_uring/openclose.c index b35bf5f66dd9..4eb1f23e028a 100644 --- a/io_uring/openclose.c +++ b/io_uring/openclose.c @@ -248,7 +248,7 @@ int io_close(struct io_kiocb *req, unsigned int issue_flags) struct io_close *close = io_kiocb_to_cmd(req); struct fdtable *fdt; struct file *file; - int ret = -EBADF; + int ret; if (close->file_slot) { ret = io_close_fixed(req, issue_flags); @@ -256,6 +256,7 @@ int io_close(struct io_kiocb *req, unsigned int issue_flags) goto err; } + ret = -EBADF; spin_lock(&files->file_lock); fdt = files_fdtable(files); if (close->fd >= fdt->max_fds) { From patchwork Fri Jun 10 09:07:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hao Xu X-Patchwork-Id: 12877052 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 565C9C43334 for ; Fri, 10 Jun 2022 09:10:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346847AbiFJJKk (ORCPT ); Fri, 10 Jun 2022 05:10:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49754 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346452AbiFJJKB (ORCPT ); Fri, 10 Jun 2022 05:10:01 -0400 Received: from out1.migadu.com (out1.migadu.com [91.121.223.63]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B297F279E74 for ; Fri, 10 Jun 2022 02:07:52 -0700 (PDT) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1654852071; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=GoEGEMXuglk9x7fsQg2tGkCbYjjy8eCyCqhMokdQTi8=; b=bAzJfloxeG/+eEuQ7Ch+D6cuT45ZtBGxoaqvCPjIqY+nrE5MqxBbdVg4ZDFRQmYiHXMpyy 254OeWRsQmvu1ffzEEizkhaoQBVwaP0/Q4rv1eOOl7bcwhSMMeW6Oqe/9v1M0mrVW9qxZA uRVJjo11HKTEcO9s3WtCDWkqHRC8qxE= From: Hao Xu To: io-uring@vger.kernel.org Cc: Jens Axboe , Pavel Begunkov Subject: [PATCH 3/5] io_uring: openclose: support separate return value for IORING_CLOSE_FD_AND_FILE_SLOT Date: Fri, 10 Jun 2022 17:07:32 +0800 Message-Id: <20220610090734.857067-4-hao.xu@linux.dev> In-Reply-To: <20220610090734.857067-1-hao.xu@linux.dev> References: <20220610090734.857067-1-hao.xu@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev Precedence: bulk List-ID: X-Mailing-List: io-uring@vger.kernel.org From: Hao Xu In IORING_CLOSE_FD_AND_FILE_SLOT mode, we just stop and return error code if either of fixed or normal file close fails. But we can actually continue to do the close even one of them fails. What we need to do is put the two result in two place: for normal file close, put the result in cqe->res like the previous behaviour, while for fixed file close, put it in cqe->flags. Users should check both member to get the status of fixed and normal file close. Signed-off-by: Hao Xu --- io_uring/openclose.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/io_uring/openclose.c b/io_uring/openclose.c index 4eb1f23e028a..da930081c03c 100644 --- a/io_uring/openclose.c +++ b/io_uring/openclose.c @@ -248,12 +248,15 @@ int io_close(struct io_kiocb *req, unsigned int issue_flags) struct io_close *close = io_kiocb_to_cmd(req); struct fdtable *fdt; struct file *file; - int ret; + int ret, ret2; if (close->file_slot) { ret = io_close_fixed(req, issue_flags); - if (ret || !(close->flags & IORING_CLOSE_FD_AND_FILE_SLOT)) + if (!(close->flags & IORING_CLOSE_FD_AND_FILE_SLOT)) goto err; + else + ret2 = ret; + } ret = -EBADF; @@ -286,6 +289,6 @@ int io_close(struct io_kiocb *req, unsigned int issue_flags) err: if (ret < 0) req_set_fail(req); - io_req_set_res(req, ret, 0); + io_req_set_res(req, ret, ret2); return IOU_OK; } From patchwork Fri Jun 10 09:07:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hao Xu X-Patchwork-Id: 12877053 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 55029CCA47B for ; Fri, 10 Jun 2022 09:10:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346452AbiFJJKm (ORCPT ); Fri, 10 Jun 2022 05:10:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44466 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346754AbiFJJKB (ORCPT ); Fri, 10 Jun 2022 05:10:01 -0400 Received: from out1.migadu.com (out1.migadu.com [91.121.223.63]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2CDE2289A1C for ; Fri, 10 Jun 2022 02:07:55 -0700 (PDT) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1654852073; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Mpp2p0ochQc/iLJwABzIvZ+1jToYJPYHhFnUDQIAQBE=; b=B94FBUnGvURpWSEETMO8LWLhNjqk3OOsnuaV8ZEjUYQyvATxX3q6w9J60iMVdMOs3vd+ch TL0PWLdPI6Zs0HMRc85OCcICSTt0JZSHIrWdegbKiuyHej22YwaERy+itH8h0Snh84u/Uw 7Fgr7sF0ayRZjqjKrYcL/SnGnM+xTYI= From: Hao Xu To: io-uring@vger.kernel.org Cc: Jens Axboe , Pavel Begunkov Subject: [PATCH 4/5] io_uring: remove duplicate kbuf release Date: Fri, 10 Jun 2022 17:07:33 +0800 Message-Id: <20220610090734.857067-5-hao.xu@linux.dev> In-Reply-To: <20220610090734.857067-1-hao.xu@linux.dev> References: <20220610090734.857067-1-hao.xu@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev Precedence: bulk List-ID: X-Mailing-List: io-uring@vger.kernel.org From: Hao Xu __io_req_complete_put() is called by __io_req_complete_post, and before that we already put kbuf, no need to do that in __io_req_complete_put() again. Signed-off-by: Hao Xu --- io_uring/io_uring.c | 6 ------ 1 file changed, 6 deletions(-) diff --git a/io_uring/io_uring.c b/io_uring/io_uring.c index 1572ebe3cff1..a94ddd0ba507 100644 --- a/io_uring/io_uring.c +++ b/io_uring/io_uring.c @@ -1245,12 +1245,6 @@ static void __io_req_complete_put(struct io_kiocb *req) } } io_req_put_rsrc(req); - /* - * Selected buffer deallocation in io_clean_op() assumes that - * we don't hold ->completion_lock. Clean them here to avoid - * deadlocks. - */ - io_put_kbuf_comp(req); io_dismantle_req(req); io_put_task(req->task, 1); wq_list_add_head(&req->comp_list, &ctx->locked_free_list); From patchwork Fri Jun 10 09:07:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hao Xu X-Patchwork-Id: 12877054 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5DD2AC433EF for ; Fri, 10 Jun 2022 09:10:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344532AbiFJJKn (ORCPT ); Fri, 10 Jun 2022 05:10:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43488 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347155AbiFJJKD (ORCPT ); Fri, 10 Jun 2022 05:10:03 -0400 Received: from out1.migadu.com (out1.migadu.com [IPv6:2001:41d0:2:863f::]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7FE93289F06 for ; Fri, 10 Jun 2022 02:07:57 -0700 (PDT) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1654852076; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=faVsmm7SshsrJAyY/i6h9TMbvPwTJcP4C9gciMxrHYQ=; b=pvViecOYa7Lae6ATQhgwKELYBKbmMdt2pEF84TrhFGa6fv+Bi64hpBNDOdIr/yPnpqeAFF Q+tmqM1FF2ApM/vV4Qpzo53woDrmrciUcupUJyP/1SjQwOkq1ODCLT3KtPpBiGzs6CHRCk ZfegqXxVkqSU3R/+Dm+2FsuggujPKDA= From: Hao Xu To: io-uring@vger.kernel.org Cc: Jens Axboe , Pavel Begunkov Subject: [PATCH 5/5] io_uring: kbuf: fix bug of not consuming ring buffer in partial io case Date: Fri, 10 Jun 2022 17:07:34 +0800 Message-Id: <20220610090734.857067-6-hao.xu@linux.dev> In-Reply-To: <20220610090734.857067-1-hao.xu@linux.dev> References: <20220610090734.857067-1-hao.xu@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev Precedence: bulk List-ID: X-Mailing-List: io-uring@vger.kernel.org From: Hao Xu When we use ring-mapped provided buffer, we should consume it before arm poll if partial io has been done. Otherwise the buffer may be used by other requests and thus we lost the data. Fixes: c7fb19428d67 ("io_uring: add support for ring mapped supplied buffers") Signed-off-by: Hao Xu --- io_uring/kbuf.c | 9 +++++++-- io_uring/kbuf.h | 10 ++++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/io_uring/kbuf.c b/io_uring/kbuf.c index d2b2b4728381..0680b63af1d2 100644 --- a/io_uring/kbuf.c +++ b/io_uring/kbuf.c @@ -49,8 +49,13 @@ void __io_kbuf_recycle(struct io_kiocb *req, unsigned issue_flags) */ if (req->flags & REQ_F_BUFFER_RING) { if (req->buf_list) { - req->buf_index = req->buf_list->bgid; - req->flags &= ~REQ_F_BUFFER_RING; + if (req->flags & REQ_F_PARTIAL_IO) { + req->buf_list->head++; + req->buf_list = NULL; + } else { + req->buf_index = req->buf_list->bgid; + req->flags &= ~REQ_F_BUFFER_RING; + } } return; } diff --git a/io_uring/kbuf.h b/io_uring/kbuf.h index b58d9d20c97e..9ecb175e60a9 100644 --- a/io_uring/kbuf.h +++ b/io_uring/kbuf.h @@ -58,8 +58,14 @@ static inline void io_kbuf_recycle(struct io_kiocb *req, unsigned issue_flags) { if (!(req->flags & (REQ_F_BUFFER_SELECTED|REQ_F_BUFFER_RING))) return; - /* don't recycle if we already did IO to this buffer */ - if (req->flags & REQ_F_PARTIAL_IO) + /* + * For legacy provided buffer mode, don't recycle if we already did + * IO to this buffer. For ring-mapped provided buffer mode, we should + * increment ring->head to explicitly monopolize the buffer to avoid + * multiple use. + */ + if ((req->flags & REQ_F_BUFFER_SELECTED) && + (req->flags & REQ_F_PARTIAL_IO)) return; __io_kbuf_recycle(req, issue_flags); }