From patchwork Tue May 9 19:09:22 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yauheni Kaliuta X-Patchwork-Id: 9718881 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 83CDB60236 for ; Tue, 9 May 2017 19:09:31 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 778182845E for ; Tue, 9 May 2017 19:09:31 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 6C74A28464; Tue, 9 May 2017 19:09:31 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0E1482845E for ; Tue, 9 May 2017 19:09:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754362AbdEITJa (ORCPT ); Tue, 9 May 2017 15:09:30 -0400 Received: from mx1.redhat.com ([209.132.183.28]:16897 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754274AbdEITJa (ORCPT ); Tue, 9 May 2017 15:09:30 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id AE875F129D; Tue, 9 May 2017 19:09:29 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com AE875F129D Authentication-Results: ext-mx10.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx10.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=yauheni.kaliuta@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com AE875F129D Received: from astarta.redhat.com (ovpn-116-244.ams2.redhat.com [10.36.116.244]) by smtp.corp.redhat.com (Postfix) with ESMTPS id CF73917B9F; Tue, 9 May 2017 19:09:28 +0000 (UTC) From: Yauheni Kaliuta To: Lucas De Marchi Cc: linux-modules Subject: [PATCHv2 2/4] depmod: rewrite depmod modules search with scratchbuf Date: Tue, 9 May 2017 22:09:22 +0300 Message-Id: <20170509190924.9087-3-yauheni.kaliuta@redhat.com> In-Reply-To: <20170509190924.9087-1-yauheni.kaliuta@redhat.com> References: <20170509190924.9087-1-yauheni.kaliuta@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Tue, 09 May 2017 19:09:29 +0000 (UTC) Sender: owner-linux-modules@vger.kernel.org Precedence: bulk List-ID: X-Virus-Scanned: ClamAV using ClamSMTP The recursive search code used used pretty big, PATH_MAX, automatic storage buffer for the module directory scanning. Some time ago there was scratchbuf implemented, which dynamically reallocates its buffer on demand. The patch takes it in use for the scanning code also. The initial size is hardcoded to 256 bytes which sounds good enough for most usecases so there should be not many reallocations. Signed-off-by: Yauheni Kaliuta --- tools/depmod.c | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/tools/depmod.c b/tools/depmod.c index cfed864ff6c1..a9a02bbb1aa7 100644 --- a/tools/depmod.c +++ b/tools/depmod.c @@ -1108,10 +1108,11 @@ add: return 0; } -static int depmod_modules_search_dir(struct depmod *depmod, DIR *d, size_t baselen, char *path) +static int depmod_modules_search_dir(struct depmod *depmod, DIR *d, size_t baselen, struct scratchbuf *s_path) { struct dirent *de; int err = 0, dfd = dirfd(d); + char *path; while ((de = readdir(d)) != NULL) { const char *name = de->d_name; @@ -1124,11 +1125,13 @@ static int depmod_modules_search_dir(struct depmod *depmod, DIR *d, size_t basel if (streq(name, "build") || streq(name, "source")) continue; namelen = strlen(name); - if (baselen + namelen + 2 >= PATH_MAX) { - path[baselen] = '\0'; - ERR("path is too long %s%s\n", path, name); + if (scratchbuf_alloc(s_path, baselen + namelen + 2) < 0) { + err = -ENOMEM; + ERR("No memory\n"); continue; } + + path = scratchbuf_str(s_path); memcpy(path + baselen, name, namelen + 1); if (de->d_type == DT_REG) @@ -1154,10 +1157,6 @@ static int depmod_modules_search_dir(struct depmod *depmod, DIR *d, size_t basel if (is_dir) { int fd; DIR *subdir; - if (baselen + namelen + 2 + NAME_MAX >= PATH_MAX) { - ERR("directory path is too long %s\n", path); - continue; - } fd = openat(dfd, name, O_RDONLY); if (fd < 0) { ERR("openat(%d, %s, O_RDONLY): %m\n", @@ -1174,7 +1173,7 @@ static int depmod_modules_search_dir(struct depmod *depmod, DIR *d, size_t basel path[baselen + namelen + 1] = '\0'; err = depmod_modules_search_dir(depmod, subdir, baselen + namelen + 1, - path); + s_path); closedir(subdir); } else { err = depmod_modules_search_file(depmod, baselen, @@ -1187,14 +1186,16 @@ static int depmod_modules_search_dir(struct depmod *depmod, DIR *d, size_t basel err = 0; /* ignore errors */ } } - return err; } static int depmod_modules_search_path(struct depmod *depmod, const char *path) { - char path_buf[PATH_MAX]; + char buf[256]; + _cleanup_(scratchbuf_release) struct scratchbuf s_path_buf = + SCRATCHBUF_INITIALIZER(buf); + char *path_buf; DIR *d; size_t baselen; int err; @@ -1207,12 +1208,20 @@ static int depmod_modules_search_path(struct depmod *depmod, } baselen = strlen(path); + + if (scratchbuf_alloc(&s_path_buf, baselen + 2) < 0) { + err = -ENOMEM; + goto out; + } + path_buf = scratchbuf_str(&s_path_buf); + memcpy(path_buf, path, baselen); path_buf[baselen] = '/'; baselen++; path_buf[baselen] = '\0'; - err = depmod_modules_search_dir(depmod, d, baselen, path_buf); + err = depmod_modules_search_dir(depmod, d, baselen, &s_path_buf); +out: closedir(d); return err; }