From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-qk1-x743.google.com (mail-qk1-x743.google.com [IPv6:2607:f8b0:4864:20::743]) by sourceware.org (Postfix) with ESMTPS id 1C4D6385DC08 for ; Fri, 17 Apr 2020 13:22:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 1C4D6385DC08 Received: by mail-qk1-x743.google.com with SMTP id j4so2254395qkc.11 for ; Fri, 17 Apr 2020 06:22:17 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=lMxg4ody7Eg1+3MQYOdhzYeFkhv+HnTTF1yt9w+XU1Q=; b=bcRlj2bn8FBv8P0/MdjvMkU2krKiWlRE81Z1I+HVX0y3VD1TkPD/Iho1A1aFCBmc3r nln6pEUk3SAVKjWwy8jjP6br0OuvlTxbFyCP04kT4ZM3xNpuAJVgntTr9UUQ7n33iH7l kokh0UvlWmgxv3AiS9Z1ZdrleaWp18gOnI/LxT9AduJJqMO159eQkQoeefxkEJO+4d6i b39Ke/eW9LHESc2cioSh89bI+XRRfjGnzAzIjM749Yse4KkJOo69ndPoS1UYMzj/bgnE uRDfVQ+tO25GDrDvDLvoqwrOlJzmZOvy9GtcT1aMUoqAlQr9s4i50TfpcuTD77JcxmcF 0JSA== X-Gm-Message-State: AGi0PuYMhiw5xngZ51zmXluJaLzEuMq+lQskyvYP02YSddoCWE2qze6O tmPCbET5pzKUaJ74hADuMoDGw2ezxIN5xg== X-Google-Smtp-Source: APiQypJHwbh/EFG/57Fd/wtaX3KnQ1fm/Mxe9vkOB+RebwE3XROFPtpvhSqU9OCLlKQdUM6AEYQp7g== X-Received: by 2002:a37:b1c7:: with SMTP id a190mr3028927qkf.69.1587129736356; Fri, 17 Apr 2020 06:22:16 -0700 (PDT) Received: from localhost.localdomain ([177.194.48.209]) by smtp.googlemail.com with ESMTPSA id s14sm14737571qts.70.2020.04.17.06.22.15 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Apr 2020 06:22:16 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH 02/10] linux: Simplify opendir buffer allocation Date: Fri, 17 Apr 2020 10:22:01 -0300 Message-Id: <20200417132209.22065-2-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200417132209.22065-1-adhemerval.zanella@linaro.org> References: <20200417132209.22065-1-adhemerval.zanella@linaro.org> X-Spam-Status: No, score=-25.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 17 Apr 2020 13:22:18 -0000 THe fallback allocation is removed, so the possible size constraint should be analized just once; __alloc_dir assumes that 'statp' argument is non-null, and the max_buffer_size move to close its used. Checked on x86_64-linux-gnu and i686-linux-gnu. --- include/dirent.h | 3 +- sysdeps/unix/sysv/linux/opendir.c | 52 +++++++++++-------------------- 2 files changed, 21 insertions(+), 34 deletions(-) diff --git a/include/dirent.h b/include/dirent.h index 2b1cdcf8bd..fdf4c4a2f1 100644 --- a/include/dirent.h +++ b/include/dirent.h @@ -48,7 +48,8 @@ extern int __versionsort64 (const struct dirent64 **a, const struct dirent64 **b) __attribute_pure__; extern DIR *__alloc_dir (int fd, bool close_fd, int flags, - const struct stat64 *statp) attribute_hidden; + const struct stat64 *statp) + __nonnull (4) attribute_hidden; extern __typeof (rewinddir) __rewinddir; extern __typeof (seekdir) __seekdir; extern __typeof (dirfd) __dirfd; diff --git a/sysdeps/unix/sysv/linux/opendir.c b/sysdeps/unix/sysv/linux/opendir.c index c6ab79246c..765c8104b3 100644 --- a/sysdeps/unix/sysv/linux/opendir.c +++ b/sysdeps/unix/sysv/linux/opendir.c @@ -23,12 +23,6 @@ #include -/* The st_blksize value of the directory is used as a hint for the - size of the buffer which receives struct dirent values from the - kernel. st_blksize is limited to MAX_DIR_BUFFER_SIZE, in case the - file system provides a bogus value. */ -#define MAX_DIR_BUFFER_SIZE 1048576U - enum { opendir_oflags = O_RDONLY|O_NDELAY|O_DIRECTORY|O_LARGEFILE|O_CLOEXEC }; @@ -100,38 +94,30 @@ __alloc_dir (int fd, bool close_fd, int flags, const struct stat64 *statp) file descriptor. */ if (!close_fd && __glibc_unlikely (__fcntl64_nocancel (fd, F_SETFD, FD_CLOEXEC) < 0)) - goto lose; - - const size_t default_allocation = (4 * BUFSIZ < sizeof (struct dirent64) - ? sizeof (struct dirent64) : 4 * BUFSIZ); - const size_t small_allocation = (BUFSIZ < sizeof (struct dirent64) - ? sizeof (struct dirent64) : BUFSIZ); - size_t allocation = default_allocation; -#ifdef _STATBUF_ST_BLKSIZE + return NULL; + + /* The st_blksize value of the directory is used as a hint for the + size of the buffer which receives struct dirent values from the + kernel. st_blksize is limited to max_buffer_size, in case the + file system provides a bogus value. */ + enum { max_buffer_size = 1U << 20 }; + + const size_t allocation_size = 4 * BUFSIZ; + _Static_assert (allocation_size >= sizeof (struct dirent64), + "allocation_size < sizeof (struct dirent64)"); + /* Increase allocation if requested, but not if the value appears to - be bogus. */ - if (statp != NULL) - allocation = MIN (MAX ((size_t) statp->st_blksize, default_allocation), - MAX_DIR_BUFFER_SIZE); -#endif + be bogus. It will be between 32Kb (for blocksizes smaller than BUFSIZ) + up to 1Mb. */ + size_t allocation = MIN (MAX ((size_t) statp->st_blksize, allocation_size), + max_buffer_size); DIR *dirp = (DIR *) malloc (sizeof (DIR) + allocation); if (dirp == NULL) { - allocation = small_allocation; - dirp = (DIR *) malloc (sizeof (DIR) + allocation); - - if (dirp == NULL) - lose: - { - if (close_fd) - { - int save_errno = errno; - __close_nocancel_nostatus (fd); - __set_errno (save_errno); - } - return NULL; - } + if (close_fd) + __close_nocancel_nostatus (fd); + return NULL; } dirp->fd = fd; -- 2.17.1