lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date:   Mon, 7 Sep 2020 08:50:53 +0200
From:   Rasmus Villemoes <linux@...musvillemoes.dk>
To:     Christoph Hellwig <hch@....de>,
        Rasmus Villemoes <linux@...musvillemoes.dk>
Cc:     arnd@...db.de, gregkh@...uxfoundation.org,
        christophe.leroy@...roup.eu, linux-kernel@...r.kernel.org
Subject: Re: [PATCH] /dev/zero: also implement ->read

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

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ