shithub: riscv

Download patch

ref: 972cd5e3fc96059548b725ed01704ae88c55e1b4
parent: d0b1db98bcade0335b7762cb76e236a483143f91
author: cinap_lenrek <[email protected]>
date: Mon Mar 16 01:46:08 EDT 2015

kernel: get rid of auxpage() and preserve cache index bits in Page.va in mount cache

the mount cache uses Page.va to store cached range offset and
limit, but mips kernel uses cache index bits from Page.va to
maintain page coloring. Page.va was not initialized by auxpage().

this change removes auxpage() which was primarily used only
by the mount cache and use newpage() with cache file offset
page as va so we will get a page of the right color.

mount cache keeps the index bits intact by only using the top
and buttom PGSHIFT bits of Page.va for the range offset/limit.

--- a/sys/src/9/bitsy/mmu.c
+++ b/sys/src/9/bitsy/mmu.c
@@ -379,9 +379,9 @@
 		if(l2pg != nil){
 			up->mmufree = l2pg->next;
 		} else {
-			l2pg = auxpage();
-			if(l2pg == nil)
-				pexit("out of memory", 1);
+			splx(s);
+			l2pg = newpage(0, 0, 0);
+			splhi();
 		}
 		l2pg->va = VA(kmap(l2pg));
 		up->l1page[va>>20] = l2pg;
--- a/sys/src/9/port/cache.c
+++ b/sys/src/9/port/cache.c
@@ -205,6 +205,11 @@
 	return nil;
 }
 
+enum {
+	VABITS	= 8*sizeof(uintptr) - 2*PGSHIFT,
+	VAMASK	= (((uintptr)1 << VABITS)-1) << PGSHIFT,
+};
+
 static Page*
 cpage(Mntcache *m, ulong pn, ulong *po, ulong *pe)
 {
@@ -219,12 +224,19 @@
 		m->bitmap[pn/MAPBITS] &= ~b;
 		return nil;
 	}
-	/* see cachedata() below */
-	*po = (ulong)p->va & (BY2PG-1);
-	*pe = (ulong)p->va >> PGSHIFT;
+	*po = p->va & (BY2PG-1);
+	*pe = 1 + (p->va >> (PGSHIFT+VABITS));
+	assert(*po < *pe);
 	return p;
 }
 
+static void
+cpageset(Page *p, ulong po, ulong pe)
+{
+	assert(po < pe);
+	p->va = po | (p->va & VAMASK) | ((uintptr)pe - 1) << (PGSHIFT+VABITS);
+}
+
 int
 cread(Chan *c, uchar *buf, int len, vlong off)
 {
@@ -253,7 +265,7 @@
 		p = cpage(m, pn, &po, &pe);
 		if(p == nil)
 			break;
-		if(po >= pe || offset < po || offset >= pe){
+		if(offset < po || offset >= pe){
 			putpage(p);
 			break;
 		}
@@ -326,8 +338,8 @@
 			l = len;
 		p = cpage(m, pn, &po, &pe);
 		if(p != nil){
-			if(po >= pe || offset > pe || (offset+l) < po){
-				/* cached range empty or not extendable, set new cached range */
+			if(offset > pe || (offset+l) < po){
+				/* cached range not extendable, set new cached range */
 				po = offset;
 				pe = offset+l;
 			} else {
@@ -338,13 +350,12 @@
 					pe = offset+l;
 			}
 		} else {
-			p = auxpage();
-			if(p == nil){
+			if(needpages(nil) || waserror()){
 				invalidate(m, offset + pn*BY2PG, len);
 				break;
 			}
-
-			p->va = 0;
+			p = newpage(0, nil, pn*BY2PG);
+			poperror();
 			p->daddr = cacheaddr(m, pn);
 			cachedel(&fscache, p->daddr);
 			cachepage(p, &fscache);
@@ -353,6 +364,7 @@
 			po = offset;
 			pe = offset+l;
 		}
+		cpageset(p, po, pe);
 
 		k = kmap(p);
 		if(waserror()) {
@@ -365,9 +377,6 @@
 		memmove((uchar*)VA(k) + offset, buf, l);
 		poperror();
 		kunmap(k);
-
-		/* update cached range */
-		p->va = po | (pe << PGSHIFT);
 		putpage(p);
 
 		offset = 0;
--- a/sys/src/9/port/page.c
+++ b/sys/src/9/port/page.c
@@ -234,26 +234,6 @@
 		freepages(p, p, 1);
 }
 
-Page*
-auxpage(void)
-{
-	Page *p;
-
-	lock(&palloc);
-	p = palloc.head;
-	if(p == nil || palloc.freecount < swapalloc.highwater) {
-		unlock(&palloc);
-		return nil;
-	}
-	palloc.head = p->next;
-	p->next = nil;
-	palloc.freecount--;
-	unlock(&palloc);
-	p->ref = 1;
-
-	return p;
-}
-
 void
 copypage(Page *f, Page *t)
 {
--- a/sys/src/9/port/portfns.h
+++ b/sys/src/9/port/portfns.h
@@ -10,7 +10,6 @@
 int		anyhigher(void);
 int		anyready(void);
 Image*		attachimage(int, Chan*, uintptr, ulong);
-Page*		auxpage(void);
 Block*		bl2mem(uchar*, Block*, int);
 int		blocklen(Block*);
 void		bootlinks(void);
@@ -185,6 +184,7 @@
 void		muxclose(Mnt*);
 Chan*		namec(char*, int, int, ulong);
 void		nameerror(char*, char*);
+int		needpages(void*);
 Chan*		newchan(void);
 int		newfd(Chan*);
 Mhead*		newmhead(Chan*);