Received: by 2002:a05:7412:f690:b0:e2:908c:2ebd with SMTP id ej16csp19537rdb; Wed, 18 Oct 2023 16:31:06 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHnYf++12v3k9HoZ8an7DBZZ33sTcMlB8sGJLPTKXC86qZzFZ05M2frcjPqjoFPKHngehzk X-Received: by 2002:a17:903:1383:b0:1ca:8e79:538f with SMTP id jx3-20020a170903138300b001ca8e79538fmr708542plb.25.1697671866393; Wed, 18 Oct 2023 16:31:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697671866; cv=none; d=google.com; s=arc-20160816; b=fkQpzJLWTkGtUB2gKmJseLnJ4d5wjUGr6Rh3dDBPTqdSd3bwKuMsFRnN5HEFXDg/1b Nj4MF9uzQCtMQpMi71mycvYkQQaU6Mt3vg1NViUIx45JNrR/nSY4nQMoOOl8rGIM9Of8 2W4vrhXiqHEu0zJ/oWg7fLR9OGz2twXF6AAI1OiWl4BsVw0VAaCA0BURHewji3lzqZw9 Ci8xx5no/tTfPZIfZITJdwj8it5fOZh/BxGgU+37gvEZudZBw+GRoHXWNT8BKzD8pLQt qNngUgHB1JY3N/d+EyAcSeceCvaOQE1mitumHITd8bBeUcVpIChYmv3GoExh+0K7RYgf 8O8g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=jHo2QyiEH7qEEW0RAqsZQlsvkWxLmcbcb5acVwgvCR0=; fh=75jfVXLv8lUUMwkGsfvEtJRLwze0xol72zZkEl9l+mU=; b=v4m9sW2rBuLEVYzyos+eVaAHJZMWS+iDjmXa+uM9x4wMJM+TFCNqbmAa7W0hdzxlJj d4L0wxyr9aNFqHdHUsT4o2/BXNgMOP+P4nRyrlfzFcu6brgDjJZqFHgRU2/OMU7l0I20 uhjmFPWdP16WBtNaujvKJH9oSWhKPDXquJ9hzqcIJxdpckeLUMMYS1qT97+FtLWX9Pa3 Oy0mb0oE82i6Ei6IHa5uWWVvCU1eiV0ElJzL0JYq5PX5QYxoPufYkHgb/wSGqyuXoAWX QW7Srr0XRonpxiVbDSCLzTpMmPryoBGNc6jaEpSKuB49er/lKb8ax33KuTS6v6tTDwgL mOPA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=dGTjH2Vw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id j5-20020a170902c08500b001ca6809f9e7si830414pld.261.2023.10.18.16.31.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Oct 2023 16:31:06 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=dGTjH2Vw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id E3DCD808EDE8; Wed, 18 Oct 2023 16:30:25 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229697AbjJRXaK (ORCPT + 99 others); Wed, 18 Oct 2023 19:30:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36252 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229456AbjJRXaJ (ORCPT ); Wed, 18 Oct 2023 19:30:09 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EE448FA; Wed, 18 Oct 2023 16:30:07 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9306DC433C7; Wed, 18 Oct 2023 23:30:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1697671807; bh=vsSM4qg1HgsRtYLsQXhJgkd01GgzpTXSKdRv/YTRBVw=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=dGTjH2VwPd10ZCx8hGlCUzxI1kafr0HoJxgyE11uAcheNftnKpg0VfzCLH+wqFnSv AHObOrPU152pf6MbU44AExujn2gRRO9wVE4TZMOR5ApL1OSKL+hK8Y7h8htzWNwbeg d9yyLOnQZt9r8d+8aMbtU24R1StsipIKXFWvoIiRBQqC8P6i9h0P7DPGqBt6EA20+e 1eOreH9ydaMx9fiBXwoHrsYwTG7MNDA5/Reptzt7owHYKXQpmlekQBd/A3pwVpNJQT ZjQHzdcjtTBCvue5uk2sz0mQTnd11uWj1JMnhS7s/l2FILampzkZOtD4aMzJmmn+X/ ba5xGW2QhG8DA== Date: Wed, 18 Oct 2023 16:30:06 -0700 From: "Darrick J. Wong" To: Jan Stancek Cc: willy@infradead.org, hch@lst.de, linux-xfs@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, viro@zeniv.linux.org.uk Subject: Re: [PATCH v2] iomap: fix short copy in iomap_write_iter() Message-ID: <20231018233006.GK3195650@frogsfrogsfrogs> References: <8762e91a210f4cc5713fce05fe5906c18513bd0a.1697617238.git.jstancek@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-1.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Wed, 18 Oct 2023 16:30:26 -0700 (PDT) On Wed, Oct 18, 2023 at 08:32:32PM +0200, Jan Stancek wrote: > Starting with commit 5d8edfb900d5 ("iomap: Copy larger chunks from > userspace"), iomap_write_iter() can get into endless loop. This can > be reproduced with LTP writev07 which uses partially valid iovecs: > struct iovec wr_iovec[] = { > { buffer, 64 }, > { bad_addr, 64 }, > { buffer + 64, 64 }, > { buffer + 64 * 2, 64 }, > }; > > commit bc1bb416bbb9 ("generic_perform_write()/iomap_write_actor(): > saner logics for short copy") previously introduced the logic, which > made short copy retry in next iteration with amount of "bytes" it > managed to copy: > > if (unlikely(status == 0)) { > /* > * A short copy made iomap_write_end() reject the > * thing entirely. Might be memory poisoning > * halfway through, might be a race with munmap, > * might be severe memory pressure. > */ > if (copied) > bytes = copied; > > However, since 5d8edfb900d5 "bytes" is no longer carried into next > iteration, because it is now always initialized at the beginning of > the loop. And for iov_iter_count < PAGE_SIZE, "bytes" ends up with > same value as previous iteration, making the loop retry same copy > over and over, which leads to writev07 testcase hanging. > > Make next iteration retry with amount of bytes we managed to copy. > > Fixes: 5d8edfb900d5 ("iomap: Copy larger chunks from userspace") > Signed-off-by: Jan Stancek Looks fine to me, will send it out for testing... Reviewed-by: Darrick J. Wong --D > --- > Changes in v2: > - use goto instead of new variable (suggested by Christoph Hellwig) > > fs/iomap/buffered-io.c | 10 +++++++--- > 1 file changed, 7 insertions(+), 3 deletions(-) > > diff --git a/fs/iomap/buffered-io.c b/fs/iomap/buffered-io.c > index 5db54ca29a35..2bc0aa23fde3 100644 > --- a/fs/iomap/buffered-io.c > +++ b/fs/iomap/buffered-io.c > @@ -881,8 +881,10 @@ static loff_t iomap_write_iter(struct iomap_iter *iter, struct iov_iter *i) > size_t bytes; /* Bytes to write to folio */ > size_t copied; /* Bytes copied from user */ > > + bytes = iov_iter_count(i); > +retry: > offset = pos & (chunk - 1); > - bytes = min(chunk - offset, iov_iter_count(i)); > + bytes = min(chunk - offset, bytes); > status = balance_dirty_pages_ratelimited_flags(mapping, > bdp_flags); > if (unlikely(status)) > @@ -933,10 +935,12 @@ static loff_t iomap_write_iter(struct iomap_iter *iter, struct iov_iter *i) > * halfway through, might be a race with munmap, > * might be severe memory pressure. > */ > - if (copied) > - bytes = copied; > if (chunk > PAGE_SIZE) > chunk /= 2; > + if (copied) { > + bytes = copied; > + goto retry; > + } > } else { > pos += status; > written += status; > -- > 2.31.1 >