Received: by 2002:a5d:925a:0:0:0:0:0 with SMTP id e26csp716893iol; Thu, 9 Jun 2022 12:22:35 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzj3Fu+dQrL1DQYGA6aHuMrM5PTUimnMp1exQDEVro2j9h3Ox8rDvFgEUuGTBfgQNfZ3PHp X-Received: by 2002:a17:902:e80d:b0:167:5e77:e695 with SMTP id u13-20020a170902e80d00b001675e77e695mr29940682plg.144.1654802555298; Thu, 09 Jun 2022 12:22:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654802555; cv=none; d=google.com; s=arc-20160816; b=h8hhesXbZSRk8eoHObxuQ2cx41UciMnLbaWghWBiLg4tIy/F/RuH+zvsDl/A3z+/Gs ATA+O02CRuyhYruRfH47beTrVrGltTaMA4UF3ws2LTh/wmW/O/9nZvthw+XJZvB1/xGe 6IT3xeftX/8qtPLOLb/roGbApxwXqlRcdlx6dgV0h3R02Sv4MC9TBfVyObHkPFxnBs8J o6N98QqGJI0NNWWUQ9nEMmDP4G+vJ9IgY6Zlc8sJFRP1gq1JHt9zfqfqwWFvogHFS+Qj rYjp8LOQIagEOA9bx+VfTNwBpfYtOxg3jVlIRCHvXAYT3esaBWrN0ETWgy3HTuhZfkgO 5cEA== 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:mail-followup-to:message-id:subject:cc:to:from:date :dkim-signature; bh=jxHopubkJxBq7P8N0Ankt2bLTwOWaPXg0oY0MAVJbUI=; b=KqCI5f3GnLbzKjx0dbw36fbOAxgWFRwHJnZiIf9MskIi8jzHo4c8+y7uZSfYup86Jl H8aty8U3ko4yOtV6RZkwgfd0IKWORUN1c0tDF4QM4BX8r2LvjZQv0/XqYDW2ZG/2Dfx3 2fmVzDUqFe5euwE/eZxCbWMTAX2utlqLz2ds6/IHtAHQeQAATD/ydxvc4csB+57g+P63 qxOo1bYgcKL6p0NdTQdkkGIOQAyv2T/thfRZ6csfr0erRhnmTsSr+w45WykK2CF4ayQe OE+O/gFII+INThmlRLPfarSp3rhbr65xL/9w4zjGgjeey0ZBX2nG4p3437UPCcldOxHt P28A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=DPDtpREh; 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=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k189-20020a6384c6000000b003fe0f7baa87si10420019pgd.329.2022.06.09.12.22.22; Thu, 09 Jun 2022 12:22:35 -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=@kernel.org header.s=k20201202 header.b=DPDtpREh; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237089AbiFITEJ (ORCPT + 99 others); Thu, 9 Jun 2022 15:04:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47534 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229632AbiFITEH (ORCPT ); Thu, 9 Jun 2022 15:04:07 -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 058B53F32D; Thu, 9 Jun 2022 12:04:07 -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 9485861DF3; Thu, 9 Jun 2022 19:04:06 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C24DBC34114; Thu, 9 Jun 2022 19:04:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1654801445; bh=Yxnu6+83FYCfv/3NbAs8M+FkZ+oJRuDX8jaMn/PQaZE=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=DPDtpREhdB0j8JRKxYabn/GSmhFRQx135HzaS1QEKqIVh/3OLWcUqgRccNEM25gmk kG/OrJ39Z46ozUpCD4lB0Pz1Cgw5Nr0wS2LvRNpnFS3G1qnalODO0aE13KHfgq2aVF RBiVXksCRrbDV3G+6tdZE7M8qB5p84hUf24cfmQT69pB4ht/PfI68SUxb2hijm/Alk Iy9ZomiMariI7kTxRnfRkGzQdVh+RKRQraiVWC+Xd/Y3laedlgOxz6E566fV4n10Vd 4RCPFzR938EQgajfXo30ffAkTSTF2nYqegz7D/8H6zPQrMmQALtMa5fIpBWwlTlMaX oQLbte0bNq8JA== Date: Fri, 10 Jun 2022 03:03:58 +0800 From: Gao Xiang To: David Howells Cc: jlayton@kernel.org, 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, linux-kernel@vger.kernel.org Subject: Re: [PATCH] iov_iter: Fix iter_xarray_get_pages{,_alloc}() Message-ID: Mail-Followup-To: David Howells , jlayton@kernel.org, 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, linux-kernel@vger.kernel.org References: <165476202136.3999992.433442175457370240.stgit@warthog.procyon.org.uk> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <165476202136.3999992.433442175457370240.stgit@warthog.procyon.org.uk> 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 On Thu, Jun 09, 2022 at 09:07:01AM +0100, David Howells wrote: > 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 Looks good to me, Reviewed-by: Gao Xiang Thanks, Gao Xiang > --- > > 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 834e1e268eb6..814f65fd0c42 100644 > --- a/lib/iov_iter.c > +++ b/lib/iov_iter.c > @@ -1434,7 +1434,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) > @@ -1461,13 +1461,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 */ > @@ -1602,7 +1596,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) > @@ -1631,13 +1625,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, > >