public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Adhemerval Zanella <adhemerval.zanella@linaro.org>
To: Florian Weimer <fw@deneb.enyo.de>
Cc: Adhemerval Zanella via Libc-alpha <libc-alpha@sourceware.org>
Subject: Re: [PATCH 03/10] linux: Add __readdir_unlocked
Date: Tue, 21 Apr 2020 10:00:16 -0300	[thread overview]
Message-ID: <4a96fd5e-7a06-90c8-c2f2-8e39246d15ce@linaro.org> (raw)
In-Reply-To: <87blnlrq3r.fsf@mid.deneb.enyo.de>



On 21/04/2020 09:16, Florian Weimer wrote:
> * Adhemerval Zanella:
> 
>> On 21/04/2020 07:41, Florian Weimer wrote:
>>> * Adhemerval Zanella via Libc-alpha:
>>>
>>>> And use it on readdir_r implementation.
>>>
>>> I think __readdir_unlocked will not really be async-signal-safe if we
>>> have to call malloc during readdir, to allocate the long-to-off64_t
>>> translation table for the real fix for bug 23960 (when the kernel does
>>> not provide 32-bit off64_t values).
>>>
>>> That's why I think this change does not go in the right direction, sorry.
>>>
>>
>> I am not following, the whole set explicit avoid malloc by using a reserved
>> space in the allocated but on opendir.  Even on mips64, which requires
>> a special getdents64 implementation, avoid memory allocation.
> 
> The fix for bug 23960 needs to introduce some kind of memory
> allocation to store the mapping.  We can avoid it in most cases, but I
> think the readdir interface has too much baggage to make it
> async-signal-safe.

My proposed solution is to first set readdir use internally off64_t (even
for non-LFS) and whence he memory allocation is done on telldir which
will try to first allocate an entry on the dynamic array embedded on
allocated opendir __dirstream.

> 
>> And it is for both non-LFS and LFS interfaces.  The only issue for 
>> async-signal-safeness is on telldir for non-LFS mode which might require
>> to extend the dynamic array internal buffer if the entry could not be
>> added in the internal pre-allocated buffer (and this could be mitigated
>> if we added a mmap variant of dynamic array). 
> 
> telldir cannot allocate because there is no way to return error.  The
> allocation has to happen in readdir.
> 
> We should be able to optimize out allocations if telldir is never
> called (basically, pre-allocate one slot to be used for telldir).  But
> all this is really tricky.  I do wonder if it makes more sense to call
> getdents64 directly if one needs async-signal-safety.

This is essentially what the patchset does: it uses gendents64 on
both LFS and non-LFS interface, the position is already place on
the __dirstream and a default dynamic array of off64_t is used to
map internal directory offset to long int.

The problem is not really telldir, but rather seekdir that does not
allow to signal an invalid position. The standard states that any
value obtained from telldir is valid. 

My proposed fix uses a dynamic array with the default pre-defined size 
to  allocated the off64_t mapping and returns -1 if the dynamic array 
could not be extended.  Although it is not what POSIX state, man-pages
documents as a possible return error. 

But even not allowing the pre-allocated buffer to extend over its
initial size, we will still need to handle a telldir call with the
buffer full.  So I see we might have two options:

  1. Just abort on telldir once it requires an additional entry in the
     pre-allocated mapping buffer.

  2. Return -1 on failure and handle it as special sentinel value that
     does not update the directory position.  So user might still try
     to check if seekdir does succeeded by:

       long int pre = telldir (dirp);
       seek (dirp, value);
       if (pre == telldir (dirp)
         // position not updated, invalid value

In any case, this fix is only for *no-LFS* which should be considered
a legacy interface a long time ago.  The default LFS interface does not
suffer with this limitation.

  reply	other threads:[~2020-04-21 13:00 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-17 13:22 [PATCH 01/10] linux: Move posix dir implementations to Linux Adhemerval Zanella
2020-04-17 13:22 ` [PATCH 02/10] linux: Simplify opendir buffer allocation Adhemerval Zanella
2020-04-21 10:28   ` Florian Weimer
2020-04-23 21:27     ` Rafal Luzynski
2020-04-29 17:09       ` Adhemerval Zanella
2020-04-23 21:39     ` Adhemerval Zanella
2020-04-24 10:11       ` Florian Weimer
2020-04-24 12:08         ` Adhemerval Zanella
2020-04-24 13:08           ` Florian Weimer
2020-04-17 13:22 ` [PATCH 03/10] linux: Add __readdir_unlocked Adhemerval Zanella
2020-04-21 10:41   ` Florian Weimer
2020-04-21 12:03     ` Adhemerval Zanella
2020-04-21 12:16       ` Florian Weimer
2020-04-21 13:00         ` Adhemerval Zanella [this message]
2020-05-27 16:38           ` Adhemerval Zanella
2020-04-17 13:22 ` [PATCH 04/10] linux: Use internal DIR locks when accessing filepos on telldir Adhemerval Zanella
2020-04-21 10:33   ` Florian Weimer
2020-04-17 13:22 ` [PATCH 05/10] linux: Use getdents64 on non-LFS readdir Adhemerval Zanella
2020-04-17 13:22 ` [PATCH 06/10] linux: Set internal DIR filepos as off64_t [BZ #23960, BZ #24050] Adhemerval Zanella
2020-04-20 15:01   ` Andreas Schwab
2020-04-20 15:02     ` Florian Weimer
2020-04-20 15:06       ` Andreas Schwab
2020-04-21 12:04         ` Adhemerval Zanella
2020-04-17 13:22 ` [PATCH 07/10] linux: Add __readdir64_unlocked Adhemerval Zanella
2020-04-17 13:22 ` [PATCH 08/10] linux: Add __old_readdir64_unlocked Adhemerval Zanella
2020-04-17 13:22 ` [PATCH 09/10] linux: Use getdents64 on readdir64 compat implementation Adhemerval Zanella
2020-04-17 13:22 ` [PATCH 10/10] dirent: Deprecate getdirentries Adhemerval Zanella
2020-04-22 10:10   ` Florian Weimer
2020-04-20 14:53 ` [PATCH 01/10] linux: Move posix dir implementations to Linux Andreas Schwab
2020-04-21 10:15   ` Florian Weimer
2020-04-21 11:51   ` Adhemerval Zanella
2020-05-27 16:35 ` Adhemerval Zanella

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4a96fd5e-7a06-90c8-c2f2-8e39246d15ce@linaro.org \
    --to=adhemerval.zanella@linaro.org \
    --cc=fw@deneb.enyo.de \
    --cc=libc-alpha@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).