Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp708909pxb; Wed, 11 Nov 2020 14:23:28 -0800 (PST) X-Google-Smtp-Source: ABdhPJzZdyEH85aKYAP8gSWnwcynH/YbY8yzfkQJeHGOJz2AZPMojwJAAYyxvLU+cJtSX0mulY3E X-Received: by 2002:aa7:d9d3:: with SMTP id v19mr1823100eds.67.1605133408578; Wed, 11 Nov 2020 14:23:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1605133408; cv=none; d=google.com; s=arc-20160816; b=OXfUD6UIFD4wH6TH9TSMcdvbpK+F9AOaRmbmB8ITa9Z4SQ2qFtDB22vyiISaw/i4RS 6bPXAEzQeP38HD/TQzE5aBY8ul9DrdSbifmNlxAxUheLLKWwyqCR6bN3oaaEz99kUeuG 2fGtG6Gw+GEyPpmVeIpy/ILp9yhM14GgHTqVmGIIxpRdDRTGpMuxbjS4xD3ZLKRyfcTw M0haCsugZhZmzu0HQPOUKUiMibVvtgQ4tWz159ZlHEyDGELIoLmQv4gp//EfMslFVhKF B/GtHoZnaxhqE1iT6PpsW8RvNQsk3pYtEqnxnNwa0Vx7SMwapfRpi2Wqvxq5CkOQUYbk nCDg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date; bh=BaMlEZeq7hTwz83Y9R5i/wWk7ELKXIL1gHRQxcz0UDU=; b=j8biJ9QnYCtq+VR/ht88o0IE0Mhq6QYkeAClH/KDHXI0EbVAxPi/tYHeZ1wYC+IAIc 5KJSINt1GIGqr1PcNRl+nsyf5aNM3CBHY88FKa8AZmsUjaxyqVoJn/UxezCFD3CptbVG H4UCsx4s4xLMVKA9ExVkM4B83aEv5asTRNqOqbLJLK8DgANj5eMtZbi1Ya8HcYHrvmdD Ai3Gar7Cfo0JPfvtm/U9NPflKjPz3DtvwD88sD7WTDoqLYVUkSg6R086htDtSupKVKdQ CqHfZ7I4InsaKmsh59UxJJyfCiClZPGTM/Vqy8en98fA7dd8G5zxXJwa+eHRPjNuM2qY Dzew== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ce10si2871039edb.515.2020.11.11.14.23.01; Wed, 11 Nov 2020 14:23:28 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727188AbgKKWVZ (ORCPT + 99 others); Wed, 11 Nov 2020 17:21:25 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51170 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725996AbgKKWVZ (ORCPT ); Wed, 11 Nov 2020 17:21:25 -0500 Received: from ZenIV.linux.org.uk (zeniv.linux.org.uk [IPv6:2002:c35c:fd02::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7A981C0613D1; Wed, 11 Nov 2020 14:21:25 -0800 (PST) Received: from viro by ZenIV.linux.org.uk with local (Exim 4.92.3 #3 (Red Hat Linux)) id 1kcyUO-003rM8-2z; Wed, 11 Nov 2020 22:21:16 +0000 Date: Wed, 11 Nov 2020 22:21:16 +0000 From: Al Viro To: Linus Torvalds Cc: Christoph Hellwig , Greg KH , Alexey Dobriyan , linux-fsdevel , Linux Kernel Mailing List Subject: Re: [PATCH 1/6] seq_file: add seq_read_iter Message-ID: <20201111222116.GA919131@ZenIV.linux.org.uk> References: <20201104082738.1054792-1-hch@lst.de> <20201104082738.1054792-2-hch@lst.de> <20201110213253.GV3576660@ZenIV.linux.org.uk> <20201110213511.GW3576660@ZenIV.linux.org.uk> <20201110232028.GX3576660@ZenIV.linux.org.uk> <20201111215220.GA3576660@ZenIV.linux.org.uk> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20201111215220.GA3576660@ZenIV.linux.org.uk> Sender: Al Viro Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Nov 11, 2020 at 09:52:20PM +0000, Al Viro wrote: > That can be done, but I would rather go with > n = copy_to_iter(m->buf + m->from, m->count, iter); > m->count -= n; > m->from += n; > copied += n; > if (!size) > goto Done; > if (m->count) > goto Efault; > if we do it that way. Let me see if I can cook something > reasonable along those lines... Something like below (build-tested only): diff --git a/fs/seq_file.c b/fs/seq_file.c index 3b20e21604e7..07b33c1f34a9 100644 --- a/fs/seq_file.c +++ b/fs/seq_file.c @@ -168,7 +168,6 @@ EXPORT_SYMBOL(seq_read); ssize_t seq_read_iter(struct kiocb *iocb, struct iov_iter *iter) { struct seq_file *m = iocb->ki_filp->private_data; - size_t size = iov_iter_count(iter); size_t copied = 0; size_t n; void *p; @@ -208,14 +207,11 @@ ssize_t seq_read_iter(struct kiocb *iocb, struct iov_iter *iter) } /* if not empty - flush it first */ if (m->count) { - n = min(m->count, size); - if (copy_to_iter(m->buf + m->from, n, iter) != n) - goto Efault; + n = copy_to_iter(m->buf + m->from, m->count, iter); m->count -= n; m->from += n; - size -= n; copied += n; - if (!size) + if (!iov_iter_count(iter) || m->count) goto Done; } /* we need at least one record in buffer */ @@ -249,6 +245,7 @@ ssize_t seq_read_iter(struct kiocb *iocb, struct iov_iter *iter) goto Done; Fill: /* they want more? let's try to get some more */ + /* m->count is positive and there's space left in iter */ while (1) { size_t offs = m->count; loff_t pos = m->index; @@ -263,7 +260,7 @@ ssize_t seq_read_iter(struct kiocb *iocb, struct iov_iter *iter) err = PTR_ERR(p); break; } - if (m->count >= size) + if (m->count >= iov_iter_count(iter)) break; err = m->op->show(m, p); if (seq_has_overflowed(m) || err) { @@ -273,16 +270,14 @@ ssize_t seq_read_iter(struct kiocb *iocb, struct iov_iter *iter) } } m->op->stop(m, p); - n = min(m->count, size); - if (copy_to_iter(m->buf, n, iter) != n) - goto Efault; + n = copy_to_iter(m->buf, m->count, iter); copied += n; m->count -= n; m->from = n; Done: - if (!copied) - copied = err; - else { + if (unlikely(!copied)) { + copied = m->count ? -EFAULT : err; + } else { iocb->ki_pos += copied; m->read_pos += copied; } @@ -291,9 +286,6 @@ ssize_t seq_read_iter(struct kiocb *iocb, struct iov_iter *iter) Enomem: err = -ENOMEM; goto Done; -Efault: - err = -EFAULT; - goto Done; } EXPORT_SYMBOL(seq_read_iter);