From nobody Fri Dec 19 06:16:03 2025 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 22FA9ECAAA1 for ; Mon, 24 Oct 2022 14:21:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235701AbiJXOVl (ORCPT ); Mon, 24 Oct 2022 10:21:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55790 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237129AbiJXOP5 (ORCPT ); Mon, 24 Oct 2022 10:15:57 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BD35F2AC67; Mon, 24 Oct 2022 05:55:35 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 85195612E6; Mon, 24 Oct 2022 12:55:33 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 923C1C433C1; Mon, 24 Oct 2022 12:55:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666616132; bh=7amcw60WT4FfsU+UZhY0SmndoGQTOG4dNSqmVeJCEuM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ByYhLL0flCkrgu+Lrv6ZfgC9TDKBSdLQPDgEqB+wrDeUIFFI6R8jWeiaJy6xB/4La 8w4kjlFJJaYOzbmBs/BK2j0vryY2o8rm4EXQPzpN/R7JaEoCIxL+xHOrpGVKivQeEd G4H2QEmoWeYTobo00QF5P+Hc5aH0PBH+OJPlUYVY= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Greg Kroah-Hartman , Pavel Begunkov Subject: [PATCH 5.15 513/530] io_uring/rw: fix short rw error handling Date: Mon, 24 Oct 2022 13:34:17 +0200 Message-Id: <20221024113108.241023895@linuxfoundation.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221024113044.976326639@linuxfoundation.org> References: <20221024113044.976326639@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Pavel Begunkov [ upstream commit 89473c1a9205760c4fa6d158058da7b594a815f0 ] We have a couple of problems, first reports of unexpected link breakage for reads when cqe->res indicates that the IO was done in full. The reason here is partial IO with retries. TL;DR; we compare the result in __io_complete_rw_common() against req->cqe.res, but req->cqe.res doesn't store the full length but rather the length left to be done. So, when we pass the full corrected result via kiocb_done() -> __io_complete_rw_common(), it fails. The second problem is that we don't try to correct res in io_complete_rw(), which, for instance, might be a problem for O_DIRECT but when a prefix of data was cached in the page cache. We also definitely don't want to pass a corrected result into io_rw_done(). The fix here is to leave __io_complete_rw_common() alone, always pass not corrected result into it and fix it up as the last step just before actually finishing the I/O. Cc: stable@vger.kernel.org Signed-off-by: Pavel Begunkov Signed-off-by: Greg Kroah-Hartman --- fs/io_uring.c | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) --- a/fs/io_uring.c +++ b/fs/io_uring.c @@ -2701,6 +2701,20 @@ static bool __io_complete_rw_common(stru return false; } =20 +static inline unsigned io_fixup_rw_res(struct io_kiocb *req, unsigned res) +{ + struct io_async_rw *io =3D req->async_data; + + /* add previously done IO, if any */ + if (io && io->bytes_done > 0) { + if (res < 0) + res =3D io->bytes_done; + else + res +=3D io->bytes_done; + } + return res; +} + static void io_req_task_complete(struct io_kiocb *req, bool *locked) { unsigned int cflags =3D io_put_rw_kbuf(req); @@ -2724,7 +2738,7 @@ static void __io_complete_rw(struct io_k { if (__io_complete_rw_common(req, res)) return; - __io_req_complete(req, issue_flags, req->result, io_put_rw_kbuf(req)); + __io_req_complete(req, issue_flags, io_fixup_rw_res(req, res), io_put_rw_= kbuf(req)); } =20 static void io_complete_rw(struct kiocb *kiocb, long res, long res2) @@ -2733,7 +2747,7 @@ static void io_complete_rw(struct kiocb =20 if (__io_complete_rw_common(req, res)) return; - req->result =3D res; + req->result =3D io_fixup_rw_res(req, res); req->io_task_work.func =3D io_req_task_complete; io_req_task_work_add(req); } @@ -2979,15 +2993,6 @@ static void kiocb_done(struct kiocb *kio unsigned int issue_flags) { struct io_kiocb *req =3D container_of(kiocb, struct io_kiocb, rw.kiocb); - struct io_async_rw *io =3D req->async_data; - - /* add previously done IO, if any */ - if (io && io->bytes_done > 0) { - if (ret < 0) - ret =3D io->bytes_done; - else - ret +=3D io->bytes_done; - } =20 if (req->flags & REQ_F_CUR_POS) req->file->f_pos =3D kiocb->ki_pos; @@ -3004,6 +3009,7 @@ static void kiocb_done(struct kiocb *kio unsigned int cflags =3D io_put_rw_kbuf(req); struct io_ring_ctx *ctx =3D req->ctx; =20 + ret =3D io_fixup_rw_res(req, ret); req_set_fail(req); if (!(issue_flags & IO_URING_F_NONBLOCK)) { mutex_lock(&ctx->uring_lock);