From owner-svn-src-head@freebsd.org Thu Mar 9 00:45:17 2017 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 21E8BD031AE; Thu, 9 Mar 2017 00:45:17 +0000 (UTC) (envelope-from glebius@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id CBFBE17A3; Thu, 9 Mar 2017 00:45:16 +0000 (UTC) (envelope-from glebius@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id v290jFO9046347; Thu, 9 Mar 2017 00:45:15 GMT (envelope-from glebius@FreeBSD.org) Received: (from glebius@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id v290jFOF046346; Thu, 9 Mar 2017 00:45:15 GMT (envelope-from glebius@FreeBSD.org) Message-Id: <201703090045.v290jFOF046346@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: glebius set sender to glebius@FreeBSD.org using -f From: Gleb Smirnoff Date: Thu, 9 Mar 2017 00:45:15 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r314929 - head/sys/kern X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 09 Mar 2017 00:45:17 -0000 Author: glebius Date: Thu Mar 9 00:45:15 2017 New Revision: 314929 URL: https://svnweb.freebsd.org/changeset/base/314929 Log: Reduce stack usage in link_elf_load_file(), allocating struct nameidata. This function may be called recursively, when a module pulls its dependencies. Under certain circumstances, e.g. quad chain of dependencies and presence of dtrace we may run out of stack. Modified: head/sys/kern/link_elf_obj.c Modified: head/sys/kern/link_elf_obj.c ============================================================================== --- head/sys/kern/link_elf_obj.c Thu Mar 9 00:33:38 2017 (r314928) +++ head/sys/kern/link_elf_obj.c Thu Mar 9 00:45:15 2017 (r314929) @@ -476,7 +476,7 @@ static int link_elf_load_file(linker_class_t cls, const char *filename, linker_file_t *result) { - struct nameidata nd; + struct nameidata *nd; struct thread *td = curthread; /* XXX */ Elf_Ehdr *hdr; Elf_Shdr *shdr; @@ -501,18 +501,21 @@ link_elf_load_file(linker_class_t cls, c mapsize = 0; hdr = NULL; - NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, filename, td); + nd = malloc(sizeof(struct nameidata), M_TEMP, M_WAITOK); + NDINIT(nd, LOOKUP, FOLLOW, UIO_SYSSPACE, filename, td); flags = FREAD; - error = vn_open(&nd, &flags, 0, NULL); - if (error) + error = vn_open(nd, &flags, 0, NULL); + if (error) { + free(nd, M_TEMP); return error; - NDFREE(&nd, NDF_ONLY_PNBUF); - if (nd.ni_vp->v_type != VREG) { + } + NDFREE(nd, NDF_ONLY_PNBUF); + if (nd->ni_vp->v_type != VREG) { error = ENOEXEC; goto out; } #ifdef MAC - error = mac_kld_check_load(td->td_ucred, nd.ni_vp); + error = mac_kld_check_load(td->td_ucred, nd->ni_vp); if (error) { goto out; } @@ -520,7 +523,7 @@ link_elf_load_file(linker_class_t cls, c /* Read the elf header from the file. */ hdr = malloc(sizeof(*hdr), M_LINKER, M_WAITOK); - error = vn_rdwr(UIO_READ, nd.ni_vp, (void *)hdr, sizeof(*hdr), 0, + error = vn_rdwr(UIO_READ, nd->ni_vp, (void *)hdr, sizeof(*hdr), 0, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, &resid, td); if (error) @@ -577,8 +580,9 @@ link_elf_load_file(linker_class_t cls, c } shdr = malloc(nbytes, M_LINKER, M_WAITOK); ef->e_shdr = shdr; - error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)shdr, nbytes, hdr->e_shoff, - UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, &resid, td); + error = vn_rdwr(UIO_READ, nd->ni_vp, (caddr_t)shdr, nbytes, + hdr->e_shoff, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, + NOCRED, &resid, td); if (error) goto out; if (resid) { @@ -653,7 +657,7 @@ link_elf_load_file(linker_class_t cls, c /* Allocate space for and load the symbol table */ ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym); ef->ddbsymtab = malloc(shdr[symtabindex].sh_size, M_LINKER, M_WAITOK); - error = vn_rdwr(UIO_READ, nd.ni_vp, (void *)ef->ddbsymtab, + error = vn_rdwr(UIO_READ, nd->ni_vp, (void *)ef->ddbsymtab, shdr[symtabindex].sh_size, shdr[symtabindex].sh_offset, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, &resid, td); @@ -672,7 +676,7 @@ link_elf_load_file(linker_class_t cls, c /* Allocate space for and load the symbol strings */ ef->ddbstrcnt = shdr[symstrindex].sh_size; ef->ddbstrtab = malloc(shdr[symstrindex].sh_size, M_LINKER, M_WAITOK); - error = vn_rdwr(UIO_READ, nd.ni_vp, ef->ddbstrtab, + error = vn_rdwr(UIO_READ, nd->ni_vp, ef->ddbstrtab, shdr[symstrindex].sh_size, shdr[symstrindex].sh_offset, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, &resid, td); @@ -691,7 +695,7 @@ link_elf_load_file(linker_class_t cls, c ef->shstrcnt = shdr[shstrindex].sh_size; ef->shstrtab = malloc(shdr[shstrindex].sh_size, M_LINKER, M_WAITOK); - error = vn_rdwr(UIO_READ, nd.ni_vp, ef->shstrtab, + error = vn_rdwr(UIO_READ, nd->ni_vp, ef->shstrtab, shdr[shstrindex].sh_size, shdr[shstrindex].sh_offset, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, &resid, td); @@ -826,7 +830,7 @@ link_elf_load_file(linker_class_t cls, c || shdr[i].sh_type == SHT_X86_64_UNWIND #endif ) { - error = vn_rdwr(UIO_READ, nd.ni_vp, + error = vn_rdwr(UIO_READ, nd->ni_vp, ef->progtab[pb].addr, shdr[i].sh_size, shdr[i].sh_offset, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, @@ -867,7 +871,7 @@ link_elf_load_file(linker_class_t cls, c M_WAITOK); ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel); ef->reltab[rl].sec = shdr[i].sh_info; - error = vn_rdwr(UIO_READ, nd.ni_vp, + error = vn_rdwr(UIO_READ, nd->ni_vp, (void *)ef->reltab[rl].rel, shdr[i].sh_size, shdr[i].sh_offset, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, @@ -886,7 +890,7 @@ link_elf_load_file(linker_class_t cls, c ef->relatab[ra].nrela = shdr[i].sh_size / sizeof(Elf_Rela); ef->relatab[ra].sec = shdr[i].sh_info; - error = vn_rdwr(UIO_READ, nd.ni_vp, + error = vn_rdwr(UIO_READ, nd->ni_vp, (void *)ef->relatab[ra].rela, shdr[i].sh_size, shdr[i].sh_offset, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, @@ -932,9 +936,9 @@ link_elf_load_file(linker_class_t cls, c goto out; /* Pull in dependencies */ - VOP_UNLOCK(nd.ni_vp, 0); + VOP_UNLOCK(nd->ni_vp, 0); error = linker_load_dependencies(lf); - vn_lock(nd.ni_vp, LK_EXCLUSIVE | LK_RETRY); + vn_lock(nd->ni_vp, LK_EXCLUSIVE | LK_RETRY); if (error) goto out; @@ -954,8 +958,9 @@ link_elf_load_file(linker_class_t cls, c *result = lf; out: - VOP_UNLOCK(nd.ni_vp, 0); - vn_close(nd.ni_vp, FREAD, td->td_ucred, td); + VOP_UNLOCK(nd->ni_vp, 0); + vn_close(nd->ni_vp, FREAD, td->td_ucred, td); + free(nd, M_TEMP); if (error && lf) linker_file_unload(lf, LINKER_UNLOAD_FORCE); free(hdr, M_LINKER);