From owner-freebsd-hackers@FreeBSD.ORG Wed Jun 29 13:42:31 2005 Return-Path: X-Original-To: freebsd-hackers@freebsd.org Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 493AE16A41C for ; Wed, 29 Jun 2005 13:42:31 +0000 (GMT) (envelope-from simon@comsys.ntu-kpi.kiev.ua) Received: from comsys.ntu-kpi.kiev.ua (comsys.ntu-kpi.kiev.ua [195.245.194.142]) by mx1.FreeBSD.org (Postfix) with ESMTP id B368F43D4C for ; Wed, 29 Jun 2005 13:42:22 +0000 (GMT) (envelope-from simon@comsys.ntu-kpi.kiev.ua) Received: from pm514-9.comsys.ntu-kpi.kiev.ua (pm514-9.comsys.ntu-kpi.kiev.ua [10.18.54.109]) (authenticated bits=0) by comsys.ntu-kpi.kiev.ua (8.12.10/8.12.10) with ESMTP id j5TDmFFl010126 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Wed, 29 Jun 2005 16:48:15 +0300 (EEST) Received: by pm514-9.comsys.ntu-kpi.kiev.ua (Postfix, from userid 1000) id 71AB814F; Wed, 29 Jun 2005 16:40:29 +0300 (EEST) Date: Wed, 29 Jun 2005 16:40:29 +0300 From: Andrey Simonenko To: Seb Message-ID: <20050629134029.GA220@pm514-9.comsys.ntu-kpi.kiev.ua> References: <200506251203.13569.sebastien.b@swissinfo.org> <200506291051.01760.sebastien.b@swissinfo.org> <20050629091211.GA438@pm514-9.comsys.ntu-kpi.kiev.ua> <200506291155.50929.sebastien.b@swissinfo.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <200506291155.50929.sebastien.b@swissinfo.org> User-Agent: Mutt/1.4.2.1i X-Spam-Status: No, score=-4.5 required=5.0 tests=ALL_TRUSTED,AWL,BAYES_00 autolearn=ham version=3.0.1 X-Spam-Checker-Version: SpamAssassin 3.0.1 (2004-10-22) on comsys.ntu-kpi.kiev.ua X-Virus-Scanned: ClamAV 0.82/950/Wed Jun 22 03:48:34 2005 on comsys.ntu-kpi.kiev.ua X-Virus-Status: Clean Cc: freebsd-hackers@freebsd.org Subject: Re: Accessing filesystem from a KLD X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 29 Jun 2005 13:42:31 -0000 On Wed, Jun 29, 2005 at 11:55:50AM +0200, Seb wrote: > > Why not to use VOP_READ? See how it is called in dev/md.c:mdstart_vnode, > > check kern/vfs_vnops.c:vn_open_cred for information how to lookup a file > > name and open it. > > That's what I do, however I use the wrapper functions vn_open(), vn_rdwr() and > so. > > But I have a problem, when I call this code : > > void *request_firmware(const char *name, size_t *size) > { > int flags; > char filename[40]; > struct nameidata nd; > struct thread *td = curthread; > [...] > NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, &filename[0], td); > flags = FREAD; > vn_open(&nd, &flags, 0, -1); > [...] > } > > from the KLD handler function (for testing) it works. But when I call it from > a thread created by kthread_create() in another KLD, I have a page fault. > A few printfs show that the call to vn_open() is responsible for the fault. > I have not forgotten to lock Giant in my kernel thread. > Any ideas ? You got page fault from namei(), which is called from vn_open() to lookup a path name. namei() tries to obtain a reference on current directory for the current thread. This current directory (fd_cdir field) is NULL in your kthread. At this point a page fault in kernel address space is generated. More detail: Check in kthread_create() how new kthread is created, check flag RFFDG in fork1(). Since new kthread is created from thread0 and RFFDG is on, then new kthread will copy descriptor table from proc0. proc0 has descriptor table created by fdinit() in proc0_init(). fdinit() sets fd_cdir (current directory) to 0 (the same as NULL in /sys). Can you change fd_cdir in kthread to rootvnode I don't know, haven't checked this yet. But you can open a file in syscall and then use obtained vp in your kthread for VOP_READ call. It would be better to see backtrace of above mentioned page fault. But I guess that everything happened as I described. Hope this can help.