From patchwork Fri Oct 2 17:06:13 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 40645 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8C8AC38708C5; Fri, 2 Oct 2020 17:06:33 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8C8AC38708C5 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1601658393; bh=Hrn6bHXjH3zOReF1k+X5PgyJxMKj1lxBtrh8OdZKdoM=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=ycL2rTy7A4RFY3OQPUlkPB7Ihx5+DPao0u+Xozl3dN/IuEFpW03Bua+pvx+85nxcL 9SBCZq9oakbSFQxb2ymQ2R6J+5kpMEFUzVxn7Dd0M1LZ1B7SeL1I3rLNG2B0ZFRGyT Ox2d1qT8ZqJ+C3CT1/+NVMG61OFW/zWHPXsp0TlA= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qk1-x741.google.com (mail-qk1-x741.google.com [IPv6:2607:f8b0:4864:20::741]) by sourceware.org (Postfix) with ESMTPS id 36298398546A for ; Fri, 2 Oct 2020 17:06:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 36298398546A Received: by mail-qk1-x741.google.com with SMTP id f142so2066424qke.13 for ; Fri, 02 Oct 2020 10:06:31 -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:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Hrn6bHXjH3zOReF1k+X5PgyJxMKj1lxBtrh8OdZKdoM=; b=MtArRvgVyZjhOqTTyxe1r249KGqOuSh1dGsYEvMIXXnu2U7w0Z1dZvnlCqtjaDQv40 nxpz+DuUm+vxHjq4EQv30Bx0dT9GVlyCUnfgE88BlKq4/YFS63gnnWlU8YeGg5/h2keW eOOywL9PsLd7kRhuHY+chjnxlLmIWLnmxGD6Wd4b8WKS9AqXgEHZ0TRJ4gHXi3iwyBnB XpZ0cX8LzAdl4zTO7Vcastog1+6EYcq8hsao4ftD4v0w2UMmNn8IpDXi23kOcPC3Qpq+ LSCyyNJ1GXrJHsxLEm6QARXarE+iZZdbjr4VhCqMMQyR/OdresKDOyu7DX0dci8tAMMZ jDSQ== X-Gm-Message-State: AOAM533rMbtVWcnhXW+9r1kjESj6z/SMPiemwN2ubJ+vY0dCgG8eS/GR 7Fnum7wk7tUFaPojrgjDN5B4Pe3V5IlSIg== X-Google-Smtp-Source: ABdhPJz3w3jdF/oWLMxnkXJt77F4D4f1UazCMT4KSIh0XvyU/0jZJ+9iLWhqF2tAY15HMCRtKEMEPA== X-Received: by 2002:a37:a187:: with SMTP id k129mr3025588qke.147.1601658390568; Fri, 02 Oct 2020 10:06:30 -0700 (PDT) Received: from localhost.localdomain ([177.194.48.209]) by smtp.googlemail.com with ESMTPSA id d10sm1436891qkk.1.2020.10.02.10.06.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Oct 2020 10:06:30 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v2 2/9] linux: Simplify opendir buffer allocation Date: Fri, 2 Oct 2020 14:06:13 -0300 Message-Id: <20201002170620.1611673-3-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201002170620.1611673-1-adhemerval.zanella@linaro.org> References: <20201002170620.1611673-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-13.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, 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-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Cc: James Clarke , John Paul Adrian Glaubitz Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" The fallback allocation is removed, so the possible size constraint should be analyzed 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 | 51 +++++++++++-------------------- 2 files changed, 20 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 e89e09bfc7..2198224588 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,29 @@ __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 = 1048576 }; + + const size_t allocation_size = 32768; + _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 and 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;