Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp3995987rdg; Wed, 18 Oct 2023 11:38:27 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHB1uKVMModFP/QpNhsgJD76DhAk3ASzSlImG7JCHbDsnPnh2S1TWnRTENc65d9OVNKgdfa X-Received: by 2002:a05:6a00:1705:b0:6bd:f760:6ab1 with SMTP id h5-20020a056a00170500b006bdf7606ab1mr6666800pfc.14.1697654306640; Wed, 18 Oct 2023 11:38:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697654306; cv=none; d=google.com; s=arc-20160816; b=SpVB4Z7IARKXwE6+QUkOyNDklsnp/d21b1O2ta+RilkPmy+AakXrg5hCCLcaEdeRAG rJ43i+glFaUYH507Fv7o/RAIL5jVd2hejyRDI9n0p29ffyPY3p+ASGzbTzPltpjVdMrR EoCFy7xUFWgGWrvbsXEXLpBGpWZ7ZhryIExDsHIOrqXXa36+wXzU4mJZ8bWievx2Ws5g HPiRfBVtBFiE3uEjjjB6gGfK4WrngS4m2QhY+bwervGE2BBKgJnJYiWsy69QnQeZQYmV S+RL8ffV+6Tpt3j7TVYj/rZi3wk3jH6Dky+88IrtAHrTYRP1GRSSTEelHpZIA2mUuK8z mwZw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=9Q/0ep7eS2yn1G5ni+CIWne5Bs5Cck5auB+wlCPDOEU=; fh=tHRDMmASwjfCEWh2oGX/ehJspzCpeHUpCWZR/YkILzE=; b=n6IoRQJ5u/qnG/OkRY032BcusGbhsrGb/nG//G6Ccchh4+jQ6Op9qC3kPzosvcrNAK mN/44pIGluUW36Q31hbL00SutBNjoNfbMghXfb15LMF+RvJSZU3HWvFwYuzkANpls1gH c0K+wpWfZg6U4+mfcSgGuvpzKPmJxx9K7IMG1S6KvNttBu1VAY3HG/ev64CcoBoVAlLl NjH1CUIL70awh8q1+2jcR0o1mUMyPr74gcKlf+4/zcJpzYW3y2qRxyr+Vn3OQZGZn4V5 Bo/NP2sGnEzXUI8zvn8b6w+C1lR2zhmTAytWL3RkcL2GO+/SCnc4ALMQ/aCnCjJe//ot r5XA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=IYJkR6iy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id bk13-20020a056a02028d00b00578afd89ba6si2678321pgb.457.2023.10.18.11.38.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Oct 2023 11:38:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=IYJkR6iy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id AFB2F8028FF8; Wed, 18 Oct 2023 11:38:22 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229947AbjJRSiL (ORCPT + 99 others); Wed, 18 Oct 2023 14:38:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41060 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229702AbjJRSiK (ORCPT ); Wed, 18 Oct 2023 14:38:10 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A2829118 for ; Wed, 18 Oct 2023 11:37:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1697654241; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=9Q/0ep7eS2yn1G5ni+CIWne5Bs5Cck5auB+wlCPDOEU=; b=IYJkR6iyMA/fjujZt937TAtjPQgZGon8wXKELFxd0x7XnBlZC81c75MtSZleHEjVAPcKPi /ti5rJToAtWF8MsBSwvSxjIsSTw1uOg/hzGjLTqWB+9hlkKLCW+i7kZtFJ1MyY3cV/LTR5 BvE6Zpnnf9z0jAbYOR+wjs51NTi/cQE= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-163-Mu_eMp-RO9OeB8AD4XfzsA-1; Wed, 18 Oct 2023 14:37:18 -0400 X-MC-Unique: Mu_eMp-RO9OeB8AD4XfzsA-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 617943C0C113; Wed, 18 Oct 2023 18:37:17 +0000 (UTC) Received: from t14s.redhat.com (unknown [10.45.225.167]) by smtp.corp.redhat.com (Postfix) with ESMTP id 53C8E40C6F7D; Wed, 18 Oct 2023 18:37:15 +0000 (UTC) From: Jan Stancek To: djwong@kernel.org, willy@infradead.org, hch@lst.de, linux-xfs@vger.kernel.org, linux-fsdevel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, viro@zeniv.linux.org.uk, jstancek@redhat.com Subject: [PATCH v2] iomap: fix short copy in iomap_write_iter() Date: Wed, 18 Oct 2023 20:32:32 +0200 Message-Id: In-Reply-To: <8762e91a210f4cc5713fce05fe5906c18513bd0a.1697617238.git.jstancek@redhat.com> References: <8762e91a210f4cc5713fce05fe5906c18513bd0a.1697617238.git.jstancek@redhat.com> MIME-Version: 1.0 Content-type: text/plain Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.2 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, 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 groat.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 (groat.vger.email [0.0.0.0]); Wed, 18 Oct 2023 11:38:23 -0700 (PDT) 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 --- 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