Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp3012831pxk; Sun, 6 Sep 2020 23:54:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwSfcQZ+t5OQw2Y/yBBeq+dGxxbpevnStZKdLZ9bkSrUVmTi628LpS37RW/F31x4alK9Vd+ X-Received: by 2002:a05:6402:1851:: with SMTP id v17mr3486977edy.46.1599461647091; Sun, 06 Sep 2020 23:54:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1599461647; cv=none; d=google.com; s=arc-20160816; b=aO7KqcAIVGGfuZtwmzY+CXQWKaRe2w1Jtv8+tinvmoNpWvMKmRxX1QNNhiFwNrwSgY 0ByEO1/LVPNYYpipvQPN81JiEROOEDLto8hdBlzLBoKFu/ZqQWBHogTN85i8eiU/T0wJ VbDLcOriZcUCGjYqLn4mZSlGVB834kmxrbLJYApTOdgxrkm50ZMRO6fojrkgFxnZvJFk 4SVNp/iXheBBDc/yOKrxclM1r2AUNlQl4ZBetpoJFXQD7uxaWZztz0Xh60xPpb+AbCZC ORtUlWiZbPzwRJzXJeMbYsYaxEYmwMtttBeyX/TSzqATIak2ZKl7wvTTuQOHNaPeq9Z9 Ru+Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:dkim-signature; bh=1wiamZiSqOZYsHvayxqUn7pqDinjO3WGVZRcAVghQGw=; b=mqtNwtcW458d0/NcYimczxvAR2jjyzFW4q0sCDbIclrRWw1C2tWt8a7rVCWZA8FVBz f8kycSR2dyIYJUAtmh39RbH6kZSVOd/WH/RzqYfieKEGFRzuBfqnJWOf+enKM45d58qf 2Tpk0zb5yhLRC622ZUG5mrJtLywOEZAKHsVeRhgrwph3aSKk+kcCwSu8lL82LT7VF6Va DL/vLvb/HvZBVkG1V6doJuN1uWAVCGR1qeYSXFrzwkJ6iB466WdZZ7TArGEbvGAc15vT FbOLJQL3lq3l0JWuLTyl5iiWi7B2NTK90dvuwlvk+tXmNQXD5EYu2SgajRXj3Oif7Tqc D+4g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rasmusvillemoes.dk header.s=google header.b=BWe9Fs7U; 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 rh24si9381954ejb.283.2020.09.06.23.53.44; Sun, 06 Sep 2020 23:54:07 -0700 (PDT) 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; dkim=pass header.i=@rasmusvillemoes.dk header.s=google header.b=BWe9Fs7U; 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 S1726424AbgIGGvC (ORCPT + 99 others); Mon, 7 Sep 2020 02:51:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57026 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726278AbgIGGu6 (ORCPT ); Mon, 7 Sep 2020 02:50:58 -0400 Received: from mail-lj1-x244.google.com (mail-lj1-x244.google.com [IPv6:2a00:1450:4864:20::244]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6432AC061573 for ; Sun, 6 Sep 2020 23:50:56 -0700 (PDT) Received: by mail-lj1-x244.google.com with SMTP id u4so13877557ljd.10 for ; Sun, 06 Sep 2020 23:50:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rasmusvillemoes.dk; s=google; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=1wiamZiSqOZYsHvayxqUn7pqDinjO3WGVZRcAVghQGw=; b=BWe9Fs7UY5hMTg1jX+30qqCiCf9RwV7KMCDkQ2cklvsiLZQPuGkE7p78Dg7U/pHo5S iIc58p8AuTtPfPQZ3Jw/ac8wEhmYoEvmcpq9tJEXjzwT3GFQvyRyv9BRBl8zXkSrxSGO UZyIASZDkT4LcKoUjauMgnTWsJwcea368uJG8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=1wiamZiSqOZYsHvayxqUn7pqDinjO3WGVZRcAVghQGw=; b=kLJL3eydMTt5pMkVZIaBWM1lQy8wK4A8I5dI5VAeSUDOy1EfKvNCdfu8StcCO5YGYZ Hj+OJSIfozNrF6VKkhVjKmopoYKOMw4KcTmwpf+jS0kOJUQ9GQozl64G6PlmSV/wyqB8 TLY+MrrML+Uj/+Zoz/Jv+OWwYCdLKMkxRcmt8O17we3tJViksyI+EIPmI668nD0xiAG8 8b2t+s/pClA01ugvgoeTifoO4jd9MmzpkE8K3pnX1al+UYIeM/C4vB9INemkp/odQzHa E8nagdJWXo+MuAzAKf84itkXVRsuDDhXVWus3PNpgFcRA67tfx2+OYD9fzZdBNER7mGh 7ybA== X-Gm-Message-State: AOAM533ngpDmki1XniTD7bRGAsWmhLAPol9HniFHd/JpiJOSZcjoS4jr u1oKfQb/oWB+VjQxzWz4aE83BXPavP/v/4awekI= X-Received: by 2002:a2e:a0d5:: with SMTP id f21mr10362793ljm.364.1599461454862; Sun, 06 Sep 2020 23:50:54 -0700 (PDT) Received: from [172.16.11.132] ([81.216.59.226]) by smtp.gmail.com with ESMTPSA id f9sm6182599ljc.129.2020.09.06.23.50.53 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 06 Sep 2020 23:50:54 -0700 (PDT) Subject: Re: [PATCH] /dev/zero: also implement ->read To: Christoph Hellwig , Rasmus Villemoes Cc: arnd@arndb.de, gregkh@linuxfoundation.org, christophe.leroy@csgroup.eu, linux-kernel@vger.kernel.org References: <20200903155922.1111551-1-hch@lst.de> <8d430999-b155-dbfa-e7db-f414b48014b1@rasmusvillemoes.dk> <20200907062026.GA19076@lst.de> From: Rasmus Villemoes Message-ID: <4a981ee8-2d57-c7ce-a1ca-43ad237471d2@rasmusvillemoes.dk> Date: Mon, 7 Sep 2020 08:50:53 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: <20200907062026.GA19076@lst.de> Content-Type: text/plain; charset=windows-1252 Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 07/09/2020 08.20, Christoph Hellwig wrote: > On Mon, Sep 07, 2020 at 12:34:37AM +0200, Rasmus Villemoes wrote: >> On 03/09/2020 17.59, Christoph Hellwig wrote: > >>> +static ssize_t read_zero(struct file *file, char __user *buf, >>> + size_t count, loff_t *ppos) >>> +{ >>> + size_t cleared = 0; >>> + >>> + while (count) { >>> + size_t chunk = min_t(size_t, count, PAGE_SIZE); >>> + >>> + if (clear_user(buf + cleared, chunk)) >>> + return cleared ? cleared : -EFAULT; >> >> Probably nobody really cares, but currently doing >> >> read(fd, &unmapped_page - 5, 123); >> >> returns 5, and those five bytes do get cleared; if I'm reading the above >> right you'd return -EFAULT for that case. >> >> >>> + cleared += chunk; >>> + count -= chunk; >>> + >>> + if (signal_pending(current)) >>> + return cleared ? cleared : -ERESTARTSYS; >> >> I can't see how we can get here without 'cleared' being positive, so >> this can just be 'return cleared' (and if you fix the above EFAULT case >> to more accurately track how much got cleared, there's probably no >> longer any code to be symmetric with anyway). > > Yeah, I'll fix these up and resend. > Actually, while you're micro-optimizing it, AFAIK VFS already handles count==0, so you can avoid the initial branch and the last cond_resched() by writing it something like while (1) { size_t chunk = min_t(size_t, count, PAGE_SIZE), c; c = chunk - clear_user(buf + cleared, chunk); if (unlikely(!c)) return cleared ?: -EFAULT; cleared += c; count -= c; if (!count || signal_pending()) return cleared; cond_resched(); } For the dd test case with the default bs=512 that avoids cond_resched() and signal_pending() altogether. Rasmus