Home | History | Annotate | Line # | Download | only in kernspace
      1  1.8     ad /*	$NetBSD: busypage.c,v 1.8 2020/03/17 18:31:39 ad Exp $	*/
      2  1.1  pooka 
      3  1.1  pooka /*-
      4  1.1  pooka  * Copyright (c) 2010 The NetBSD Foundation, Inc.
      5  1.1  pooka  * All rights reserved.
      6  1.1  pooka  *
      7  1.1  pooka  * Redistribution and use in source and binary forms, with or without
      8  1.1  pooka  * modification, are permitted provided that the following conditions
      9  1.1  pooka  * are met:
     10  1.1  pooka  * 1. Redistributions of source code must retain the above copyright
     11  1.1  pooka  *    notice, this list of conditions and the following disclaimer.
     12  1.1  pooka  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  pooka  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  pooka  *    documentation and/or other materials provided with the distribution.
     15  1.1  pooka  *
     16  1.1  pooka  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
     17  1.1  pooka  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
     18  1.1  pooka  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     19  1.1  pooka  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  1.1  pooka  * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
     21  1.1  pooka  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22  1.1  pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     23  1.1  pooka  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24  1.1  pooka  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
     25  1.1  pooka  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     26  1.1  pooka  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
     27  1.1  pooka  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  1.1  pooka  */
     29  1.1  pooka 
     30  1.1  pooka #include <sys/cdefs.h>
     31  1.1  pooka #if !defined(lint)
     32  1.8     ad __RCSID("$NetBSD: busypage.c,v 1.8 2020/03/17 18:31:39 ad Exp $");
     33  1.1  pooka #endif /* !lint */
     34  1.1  pooka 
     35  1.1  pooka #include <sys/param.h>
     36  1.1  pooka #include <sys/condvar.h>
     37  1.1  pooka #include <sys/kthread.h>
     38  1.1  pooka #include <sys/mutex.h>
     39  1.1  pooka #include <sys/proc.h>
     40  1.1  pooka 
     41  1.1  pooka #include <uvm/uvm.h>
     42  1.1  pooka 
     43  1.1  pooka #include "kernspace.h"
     44  1.1  pooka 
     45  1.1  pooka static struct uvm_object *uobj;
     46  1.1  pooka static struct vm_page *testpg;
     47  1.1  pooka static kcondvar_t tcv;
     48  1.1  pooka 
     49  1.1  pooka static bool threadrun = false;
     50  1.1  pooka 
     51  1.1  pooka static void
     52  1.1  pooka thread(void *arg)
     53  1.1  pooka {
     54  1.1  pooka 
     55  1.7     ad 	mutex_enter(&testpg->interlock);
     56  1.1  pooka 	threadrun = true;
     57  1.1  pooka 	cv_signal(&tcv);
     58  1.7     ad 	mutex_exit(&testpg->interlock);
     59  1.7     ad 
     60  1.7     ad 	rw_enter(uobj->vmobjlock, RW_READER);
     61  1.7     ad 	uvm_pagewait(testpg, uobj->vmobjlock, "tw");
     62  1.7     ad 
     63  1.1  pooka 	kthread_exit(0);
     64  1.1  pooka }
     65  1.1  pooka 
     66  1.1  pooka void
     67  1.1  pooka rumptest_busypage()
     68  1.1  pooka {
     69  1.1  pooka 	struct lwp *newl;
     70  1.1  pooka 	int rv;
     71  1.1  pooka 
     72  1.1  pooka 	cv_init(&tcv, "napina");
     73  1.1  pooka 
     74  1.1  pooka 	uobj = uao_create(1, 0);
     75  1.6     ad 	rw_enter(uobj->vmobjlock, RW_WRITER);
     76  1.1  pooka 	testpg = uvm_pagealloc(uobj, 0, NULL, 0);
     77  1.6     ad 	rw_exit(uobj->vmobjlock);
     78  1.1  pooka 	if (testpg == NULL)
     79  1.1  pooka 		panic("couldn't create vm page");
     80  1.1  pooka 
     81  1.5  rmind 	rv = kthread_create(PRI_NONE, KTHREAD_MUSTJOIN | KTHREAD_MPSAFE, NULL,
     82  1.1  pooka 	    thread, NULL, &newl, "jointest");
     83  1.1  pooka 	if (rv)
     84  1.1  pooka 		panic("thread creation failed: %d", rv);
     85  1.1  pooka 
     86  1.7     ad 	kpause("lolgic", false, mstohz(100), NULL);
     87  1.7     ad 
     88  1.7     ad 	mutex_enter(&testpg->interlock);
     89  1.6     ad 	while (!threadrun)
     90  1.7     ad 		cv_wait(&tcv, &testpg->interlock);
     91  1.7     ad 	mutex_exit(&testpg->interlock);
     92  1.1  pooka 
     93  1.7     ad 	rw_enter(uobj->vmobjlock, RW_WRITER);
     94  1.8     ad 	uvm_page_unbusy(&testpg, 1);
     95  1.6     ad 	rw_exit(uobj->vmobjlock);
     96  1.1  pooka 
     97  1.1  pooka 	rv = kthread_join(newl);
     98  1.1  pooka 	if (rv)
     99  1.1  pooka 		panic("thread join failed: %d", rv);
    100  1.1  pooka 
    101  1.1  pooka }
    102