public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Adhemerval Zanella <adhemerval.zanella@linaro.org>
To: Florian Weimer <fweimer@redhat.com>
Cc: libc-alpha@sourceware.org
Subject: Re: [PATCH 4/4] io: Reorganize the getcwd implementation
Date: Thu, 27 Aug 2020 10:40:08 -0300	[thread overview]
Message-ID: <716a2504-03bc-79c5-8708-1a7a7fc8d3f1@linaro.org> (raw)
In-Reply-To: <87pn7cw7ge.fsf@oldenburg2.str.redhat.com>



On 27/08/2020 10:21, Florian Weimer wrote:
> * Adhemerval Zanella:
> 
>> With this patch above applied over this set I remove the generic
>> implementation from rtld.  It allowed some code simplification and the
>> resulting ld.so size change from:
>>
>> $ size elf/ld.so
>>    text    data     bss     dec     hex filename
>>  164592    7304     392  172288   2a100 elf/ld.so
>>
>> To
>>
>> $ size elf/ld.so
>>    text    data     bss     dec     hex filename
>>  162222    7304     392  169918   297be elf/ld.so
>>
>> It also has de advantage of not pulling the generic implementation on
>> hurd build (which does not use it anyway).
> 
> Thanks for doing the experiment.
> 
>> diff --git a/io/getcwd.c b/io/getcwd.c
>> index cf7a8e1a30..574f51085b 100644
>> --- a/io/getcwd.c
>> +++ b/io/getcwd.c
>> @@ -19,6 +19,10 @@
>>  #include <unistd.h>
>>  #include <stddef.h>
>>  
>> +#if !IS_IN(rtld)
>> +#include <getcwd-generic.c>
>> +#endif
>> +
>>  /* Get the pathname of the current working directory,
>>     and put it in SIZE bytes of BUF.  Returns NULL if the
>>     directory couldn't be determined or SIZE was too small.
>> @@ -30,8 +34,10 @@ char *
>>  __getcwd (char *buf, size_t size)
>>  {
>>    char *r = __getcwd_system (buf, size);
>> +#if !IS_IN(rtld)
>>    if (r == NULL)
>>      r = __getcwd_generic (buf, size);
>> +#endif
>>    return r;
>>  }
>>  libc_hidden_def (__getcwd)
> 
> Right, that's what I had in mind.
> 
>> diff --git a/sysdeps/unix/sysv/linux/getcwd-system.c b/sysdeps/unix/sysv/linux/getcwd-system.c
>> index a7e8535b72..8526b1465b 100644
>> --- a/sysdeps/unix/sysv/linux/getcwd-system.c
>> +++ b/sysdeps/unix/sysv/linux/getcwd-system.c
>> @@ -20,18 +20,11 @@
>>  #include <unistd.h>
>>  #include <sysdep.h>
>>  
>> -/* If we compile the file for use in ld.so we don't need the feature
>> -   that getcwd() allocates the buffers itself.  */
>> -#if IS_IN (rtld)
>> -# define NO_ALLOCATION	1
>> -#endif
>> -
>>  char *
>>  __getcwd_system (char *buf, size_t size)
>>  {
>>    char *path;
>>  
>> -#ifndef NO_ALLOCATION
>>    size_t alloc_size = size;
>>    if (size == 0)
>>      {
>> @@ -51,9 +44,6 @@ __getcwd_system (char *buf, size_t size)
>>  	return NULL;
>>      }
>>    else
>> -#else
>> -# define alloc_size size
>> -#endif
>>      path = buf;
>>  
>>    int retval;
> 
> That part I'm less sure about.  I think this could allocate a 64K page
> that's never freed?  Maybe that's a bit excessive.

The original patch limits the maximum allocation to PATH_MAX and for
a successful __NR_getcwd syscall the memory will be realloced to fit the
returned size from kernel.  So the worst case of returning the maximum
allocation size would just happen if realloc fails.

Another possibility would be to use the the scratch_buffer strategy I used
on my realpath Linux optimization [1]: call __NR_getcwd in a loop and either
return the strdup if scratch_buffer did not allocated memory or the resulting
buffer otherwise.  I think once the realpath is up I can send a patch with
this strategy, but it trades some performance (to avoid calling realloc)
with some extra syscalls in case or large paths.

[1] https://sourceware.org/pipermail/libc-alpha/2020-August/116935.html


  reply	other threads:[~2020-08-27 13:40 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-26 21:02 [PATCH 1/4] Sync getcwd with gnulib Adhemerval Zanella
2020-08-26 21:02 ` [PATCH 2/4] linux: Remove __ASSUME_ATFCTS Adhemerval Zanella
2020-08-26 21:02 ` [PATCH 3/4] Use LFS readdir in generic POSIX getcwd [BZ# 22899] Adhemerval Zanella
2020-08-27  9:58   ` Florian Weimer
2020-08-26 21:02 ` [PATCH 4/4] io: Reorganize the getcwd implementation Adhemerval Zanella
2020-08-26 22:39   ` Paul Eggert
2020-08-27 12:35   ` Adhemerval Zanella
2020-08-27 13:21     ` Florian Weimer
2020-08-27 13:40       ` Adhemerval Zanella [this message]
2020-08-27 17:29     ` Adhemerval Zanella
2020-08-27 19:20   ` [PATCH v2] " Adhemerval Zanella
2020-08-27 23:44     ` Paul Eggert
2020-08-31 18:27       ` Adhemerval Zanella
2020-08-26 22:39 ` [PATCH 1/4] Sync getcwd with gnulib Paul Eggert
2020-08-27 11:07   ` Adhemerval Zanella
2020-08-27  8:14 ` Florian Weimer
2020-08-27 10:53   ` Adhemerval Zanella
2020-08-27 10:58     ` Florian Weimer
2020-08-27 11:06       ` Adhemerval Zanella
2020-08-27 11:10         ` Florian Weimer
2020-08-27 11:33           ` 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=716a2504-03bc-79c5-8708-1a7a7fc8d3f1@linaro.org \
    --to=adhemerval.zanella@linaro.org \
    --cc=fweimer@redhat.com \
    --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).