From nobody@FreeBSD.org  Fri Oct 21 18:03:35 2011
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id 7D5BA106564A
	for <freebsd-gnats-submit@FreeBSD.org>; Fri, 21 Oct 2011 18:03:35 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from red.freebsd.org (red.freebsd.org [IPv6:2001:4f8:fff6::22])
	by mx1.freebsd.org (Postfix) with ESMTP id 531278FC12
	for <freebsd-gnats-submit@FreeBSD.org>; Fri, 21 Oct 2011 18:03:35 +0000 (UTC)
Received: from red.freebsd.org (localhost [127.0.0.1])
	by red.freebsd.org (8.14.4/8.14.4) with ESMTP id p9LI3Zwr006806
	for <freebsd-gnats-submit@FreeBSD.org>; Fri, 21 Oct 2011 18:03:35 GMT
	(envelope-from nobody@red.freebsd.org)
Received: (from nobody@localhost)
	by red.freebsd.org (8.14.4/8.14.4/Submit) id p9LI3Y2Q006800;
	Fri, 21 Oct 2011 18:03:34 GMT
	(envelope-from nobody)
Message-Id: <201110211803.p9LI3Y2Q006800@red.freebsd.org>
Date: Fri, 21 Oct 2011 18:03:34 GMT
From: Penta Upa <bsdboot@gmail.com>
To: freebsd-gnats-submit@FreeBSD.org
Subject: panic at vm_page_wire with FreeBSD 9.0 Beta 3
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         161887
>Category:       kern
>Synopsis:       [vm] [panic] panic at vm_page_wire with FreeBSD 9.0 Beta 3 [regression]
>Confidential:   no
>Severity:       serious
>Priority:       high
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Fri Oct 21 18:10:10 UTC 2011
>Closed-Date:    
>Last-Modified:  Wed Nov  2 09:50:04 UTC 2011
>Originator:     Penta Upa
>Release:        FreeBSD 9.0 Beta 3
>Organization:
>Environment:
FreeBSD scache 9.0-BETA3 FreeBSD 9.0-BETA3 #0: Sat Sep 24 21:31:28 UTC 2011     
root@farrell.cse.buffalo.edu:/usr/obj/usr/src/sys/GENERIC  amd64

>Description:
External modules calling vm_page_wire() even after calling vm_page_lock()
results in the following assertion failure in vm_page.c

panic: mutex page lock not owned at /usr/src/sys/vm/vm_page:1845

Interestingly check for vm page locked is also done in the external module
but there is no assertion there. More clearly within the external module

vm_page_lock(pp);
vm_page_lock_assert(pp, MA_OWNED); /* No panic here */
vm_page_wire(pp); /* Panic here for the same assertion as above, strange */
vm_page_unlock(pp);

The machine in use is a fresh install of FreeBSD 9 Beta 3. No changes
to the kernel were made. Problem however exists ever after a kernel
recompile.

I have also sent a mail to the mailing list on this.

http://lists.freebsd.org/pipermail/freebsd-current/2011-October/028550.html

I dont have a backtrace of the crash as i'm unable to configure kernel
dumps yet. Will post them once i have it.
>How-To-Repeat:
Try the attached vmtest.c

Makefile contents are

KMOD    =  vmtest

SRCS    =  vmtest.c

.include <bsd.kmod.mk>

>Fix:


Patch attached with submission follows:

#include <sys/param.h>
#include <sys/module.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/time.h>
#include <sys/systm.h>
#include <sys/lock.h>
#include <sys/mutex.h>


#include <vm/vm.h>
#include <vm/vm_extern.h>
#include <vm/vm_param.h>
#include <vm/pmap.h>
#include <vm/vm_map.h>
#include <vm/vm_page.h>
#include <vm/vm_object.h>

static void
vmtest(void) {
	vm_page_t pp;

	pp = vm_page_alloc(NULL, 0, VM_ALLOC_NOOBJ | VM_ALLOC_NORMAL);
	if (!pp) {
		printf("Page allocation failure\n");
		return;
	}
	vm_page_lock(pp);
	vm_page_lock_assert(pp, MA_OWNED);
	vm_page_wire(pp);
	vm_page_unlock(pp);
}

/* The function called at load/unload. */
static int event_handler(struct module *module, int event, void *arg) {
	switch (event) {
	case MOD_LOAD:
		vmtest();
		break;
	case MOD_UNLOAD:
		break;
	default:
		break;
	}
	return 0;
}

static moduledata_t vmtest_conf = {
    "vmtest",
     event_handler,
     NULL
};

DECLARE_MODULE(vmtest, vmtest_conf, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);


>Release-Note:
>Audit-Trail:

From: Andriy Gapon <avg@FreeBSD.org>
To: bug-followup@FreeBSD.org, bsdboot@gmail.com
Cc:  
Subject: Re: kern/161887: [vm] [panic] panic at vm_page_wire with FreeBSD
 9.0 Beta 3 [regression]
Date: Sun, 30 Oct 2011 12:31:25 +0200

 Do you build your test module as a part of a kernel+modules build or do you
 build it in isolation?  If the latter, then this could be a known obscure problem.
 
 A standalone module build doesn't get some important definitions from kernel
 config (e.g. via  opt_global.h) and can be in a serious disagreement with the
 kernel.  In particular, if a kernel is built with SMP option, but a module build
 doesn't have SMP defined, then they will have different definitions of
 PA_LOCK_COUNT and thus would work on different actual locks when manipulating
 the same page.
 
 -- 
 Andriy Gapon

From: Penta Upa <bsdboot@gmail.com>
To: Andriy Gapon <avg@freebsd.org>
Cc: bug-followup@freebsd.org
Subject: Re: kern/161887: [vm] [panic] panic at vm_page_wire with FreeBSD 9.0
 Beta 3 [regression]
Date: Sun, 30 Oct 2011 21:47:48 +0530

 --90e6ba6e834292b19904b0867651
 Content-Type: text/plain; charset=ISO-8859-1
 
 On Sun, Oct 30, 2011 at 4:01 PM, Andriy Gapon <avg@freebsd.org> wrote:
 
 >
 > Do you build your test module as a part of a kernel+modules build or do you
 > build it in isolation?  If the latter, then this could be a known obscure
 > problem.
 >
 > External. For example built in /home/penta/vmtest
 
 
 
 > A standalone module build doesn't get some important definitions from
 > kernel
 > config (e.g. via  opt_global.h) and can be in a serious disagreement with
 > the
 > kernel.  In particular, if a kernel is built with SMP option, but a module
 > build
 > doesn't have SMP defined, then they will have different definitions of
 > PA_LOCK_COUNT and thus would work on different actual locks when
 > manipulating
 > the same page.
 >
 > Ok and it seems like they are operating on different locks then.
 vm_page_assert() succeeds in the module but immediately fails in
 vm_page_wire(). But then isn't vm_page_wire/unwire() and exported kernel
 api (i assumed it is since there is a man page entry), so shouldn't it
 succeed irrespective of the kernel config and irrespective of the location
 of the build.
 
 Regards,
 Penta
 
 --90e6ba6e834292b19904b0867651--

From: Andriy Gapon <avg@FreeBSD.org>
To: Penta Upa <bsdboot@gmail.com>
Cc: bug-followup@FreeBSD.org
Subject: Re: kern/161887: [vm] [panic] panic at vm_page_wire with FreeBSD
 9.0 Beta 3 [regression]
Date: Sun, 30 Oct 2011 19:00:28 +0200

 > Ok and it seems like they are operating on different locks then.
 > vm_page_assert() succeeds in the module but immediately fails in vm_page_wire().
 > But then isn't vm_page_wire/unwire() and exported kernel api (i assumed it is
 > since there is a man page entry), so shouldn't it succeed irrespective of the
 > kernel config and irrespective of the location of the build.
 
 You described how things should be and I described how they are at the moment.
 
 
 -- 
 Andriy Gapon

From: Penta Upa <bsdboot@gmail.com>
To: Andriy Gapon <avg@freebsd.org>
Cc: bug-followup@freebsd.org
Subject: Re: kern/161887: [vm] [panic] panic at vm_page_wire with FreeBSD 9.0
 Beta 3 [regression]
Date: Sun, 30 Oct 2011 22:41:46 +0530

 --90e6ba21219b9c283304b087375d
 Content-Type: text/plain; charset=ISO-8859-1
 
 On Sun, Oct 30, 2011 at 10:30 PM, Andriy Gapon <avg@freebsd.org> wrote:
 
 > You described how things should be and I described how they are at the
 > moment.
 >
 >
 >
 Ok :-) Would including any specific header, tweaks to the Makefile make it
 work it for me. My target environment will be release version of FreeBSD on
 amd64. For example 9.0 release  and it would never be a custom built
 kernel.
 
 Or moving the module to /usr/src/sys/modules and building from there be the
 safest way.
 
 Thanks,
 Penta
 
 --90e6ba21219b9c283304b087375d--

From: Andriy Gapon <avg@FreeBSD.org>
To: Penta Upa <bsdboot@gmail.com>
Cc: bug-followup@FreeBSD.org
Subject: Re: kern/161887: [vm] [panic] panic at vm_page_wire with FreeBSD
 9.0 Beta 3 [regression]
Date: Wed, 02 Nov 2011 11:42:59 +0200

 on 30/10/2011 19:11 Penta Upa said the following:
 
 > Ok :-) Would including any specific header, tweaks to the Makefile make it work
 > it for me. My target environment will be release version of FreeBSD on amd64.
 > For example 9.0 release  and it would never be a custom built kernel.
 > 
 > Or moving the module to /usr/src/sys/modules and building from there be the
 > safest way.
 
 Unfortunately I do not have a good answer for you.
 Ideally, this issue should be fixed somewhere on vm_page.h side.
 
 Merely moving your module sources into /usr/src/sys/modules won't change
 anything.   The only workaround I know at the moment is to build the module as a
 part of kernel build (make buildkernel).
 
 -- 
 Andriy Gapon
>Unformatted:
