Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp623159iog; Mon, 13 Jun 2022 09:20:21 -0700 (PDT) X-Google-Smtp-Source: AGRyM1urS8ueZenNQ/UTNO/3J4HT2xgfQGCdoAO4j+xFYc6jADWvVdEmXUBbIWnUs3h8cyuttjVv X-Received: by 2002:a17:90a:e2c2:b0:1ea:788a:e951 with SMTP id fr2-20020a17090ae2c200b001ea788ae951mr561633pjb.155.1655137220766; Mon, 13 Jun 2022 09:20:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655137220; cv=none; d=google.com; s=arc-20160816; b=PXhG48fsmWLNBXeScG7jgvWdOBcog53ydlJDfDdmpkz8/9hP+8WOC2/GBc2rF2gRzS f4agpnPb3ULLRPywQAMdKVnLA316lKsUr03DrGfiyS3ErTrroiMY+AN4ffRPsUUQ1v2v io+I9f6PqIMcvrqdbtOG3lqsLAST5IZ7rzLM6RoCKJ0+QHIc0efB9OTdfGFv6JPIdka8 8F+BroI/4iin1IJ7AUPyPIvTjfhvijHDQxT7xqEDG0IQW8Uj1Ae/WDPRn9mytOCkL4WX 2m/rz11UeQcN4rKvA3F7i3FW07Z0ECR/uHH2YVAJ+PJ+yx3r7j7q6Z85TtWhN/yP/UYs GEmg== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=Pw2hHCxeeSzvGK3B3wbSFRlCuqevaPcS54eWU1BY44o=; b=yab8HlME/udI56n6YaZ2YfaFWLstJuvdc/5OrVIzso9xGdjHaJf171V8goOVwCGEVf qYxtD6x7vHGuCBd2GBpiyKnonB9/bNvL9keNx1//Bl50BWh3xi6cy6JJhoaX3pF64K2T 0toT4+vh19BRAS6PaT9M3x19Sb8ao1qnGR8ucbjMuBSfmc4BR3Acfsj/ZwV5F1JuLa1F FEnbYgsGcaGha6vzS1jpZ1HMzIvXvoX7gaXJmY57Obtx4gHbqhMNJyTg6dZqQ/zsaDD1 c7V6W6eRNKaeGaEfWUUe/2GgCVjPcrLg4dwrMU86eFfYp+AP229dSArpbAjV8OEFtVhS bcjA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=aUphnpHk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t7-20020a634607000000b003fadcc14015si10106053pga.15.2022.06.13.09.20.06; Mon, 13 Jun 2022 09:20:20 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=aUphnpHk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1359470AbiFMNP6 (ORCPT + 99 others); Mon, 13 Jun 2022 09:15:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37018 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1359257AbiFMNJo (ORCPT ); Mon, 13 Jun 2022 09:09:44 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9FF5E28E04; Mon, 13 Jun 2022 04:19:47 -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 3C13860F2B; Mon, 13 Jun 2022 11:19:47 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2422FC34114; Mon, 13 Jun 2022 11:19:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1655119186; bh=VDEMdKayyI9mHvjQiuo3UP5gP86R8hGnkAqeLT6SQmc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=aUphnpHkeiwAoEJ/p8vT5D+KX5PuNdMmyVw1T+WW0YoYkAXoe5UIYO25XR/O4RCFk BOZwRoTreLZlx2xJ9Uee8lxlos9w3aqtMCDVsk32al/tAXuasg6YNqOvmA9bgnY9vP VEXuy3v+N5DK0qzYmsGcagcnkPOnSi2+1nd5wzQU= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jeff Layton , David Howells , Alexander Viro , Dominique Martinet , Mike Marshall , Gao Xiang , linux-afs@lists.infradead.org, v9fs-developer@lists.sourceforge.net, devel@lists.orangefs.org, linux-erofs@lists.ozlabs.org, linux-cachefs@redhat.com, linux-fsdevel@vger.kernel.org, Sasha Levin Subject: [PATCH 5.15 164/247] iov_iter: Fix iter_xarray_get_pages{,_alloc}() Date: Mon, 13 Jun 2022 12:11:06 +0200 Message-Id: <20220613094927.932137292@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220613094922.843438024@linuxfoundation.org> References: <20220613094922.843438024@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-8.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: David Howells [ Upstream commit 6c77676645ad42993e0a8bdb8dafa517851a352a ] The maths at the end of iter_xarray_get_pages() to calculate the actual size doesn't work under some circumstances, such as when it's been asked to extract a partial single page. Various terms of the equation cancel out and you end up with actual == offset. The same issue exists in iter_xarray_get_pages_alloc(). Fix these to just use min() to select the lesser amount from between the amount of page content transcribed into the buffer, minus the offset, and the size limit specified. This doesn't appear to have caused a problem yet upstream because network filesystems aren't getting the pages from an xarray iterator, but rather passing it directly to the socket, which just iterates over it. Cachefiles *does* do DIO from one to/from ext4/xfs/btrfs/etc. but it always asks for whole pages to be written or read. Fixes: 7ff5062079ef ("iov_iter: Add ITER_XARRAY") Reported-by: Jeff Layton Signed-off-by: David Howells cc: Alexander Viro cc: Dominique Martinet cc: Mike Marshall cc: Gao Xiang cc: linux-afs@lists.infradead.org cc: v9fs-developer@lists.sourceforge.net cc: devel@lists.orangefs.org cc: linux-erofs@lists.ozlabs.org cc: linux-cachefs@redhat.com cc: linux-fsdevel@vger.kernel.org Signed-off-by: Al Viro Signed-off-by: Sasha Levin --- lib/iov_iter.c | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/lib/iov_iter.c b/lib/iov_iter.c index 6d146f77601d..be07eb24ab2f 100644 --- a/lib/iov_iter.c +++ b/lib/iov_iter.c @@ -1436,7 +1436,7 @@ static ssize_t iter_xarray_get_pages(struct iov_iter *i, { unsigned nr, offset; pgoff_t index, count; - size_t size = maxsize, actual; + size_t size = maxsize; loff_t pos; if (!size || !maxpages) @@ -1463,13 +1463,7 @@ static ssize_t iter_xarray_get_pages(struct iov_iter *i, if (nr == 0) return 0; - actual = PAGE_SIZE * nr; - actual -= offset; - if (nr == count && size > 0) { - unsigned last_offset = (nr > 1) ? 0 : offset; - actual -= PAGE_SIZE - (last_offset + size); - } - return actual; + return min(nr * PAGE_SIZE - offset, maxsize); } /* must be done on non-empty ITER_IOVEC one */ @@ -1604,7 +1598,7 @@ static ssize_t iter_xarray_get_pages_alloc(struct iov_iter *i, struct page **p; unsigned nr, offset; pgoff_t index, count; - size_t size = maxsize, actual; + size_t size = maxsize; loff_t pos; if (!size) @@ -1633,13 +1627,7 @@ static ssize_t iter_xarray_get_pages_alloc(struct iov_iter *i, if (nr == 0) return 0; - actual = PAGE_SIZE * nr; - actual -= offset; - if (nr == count && size > 0) { - unsigned last_offset = (nr > 1) ? 0 : offset; - actual -= PAGE_SIZE - (last_offset + size); - } - return actual; + return min(nr * PAGE_SIZE - offset, maxsize); } ssize_t iov_iter_get_pages_alloc(struct iov_iter *i, -- 2.35.1