From 4c639475ce8cf0aec59632b6f7537169f1537f13 Mon Sep 17 00:00:00 2001 From: cinap_lenrek Date: Sat, 3 May 2014 00:51:45 +0200 Subject: [PATCH] cwfs: fix 1GB memsize limitation the malloc pool allocator is limited in its allocation size. as almost all data structures in cwfs are never freed, use brk() in ialloc() instead of mallocalign(). this means memory returned by ialloc() cannot be freed! to make sure we do not call free by accident, remove the #define malloc(n) ialloc(n, 0) macro and use ialloc() directly as in the original code to show the intend of permanent allocations. --- sys/src/cmd/cwfs/all.h | 2 -- sys/src/cmd/cwfs/chk.c | 1 - sys/src/cmd/cwfs/config.c | 9 ++++----- sys/src/cmd/cwfs/cw.c | 2 +- sys/src/cmd/cwfs/iobuf.c | 2 +- sys/src/cmd/cwfs/juke.c | 2 +- sys/src/cmd/cwfs/main.c | 14 ++++++------- sys/src/cmd/cwfs/malloc.c | 40 ++++++++++++++++++++++++-------------- sys/src/cmd/cwfs/mworm.c | 2 +- sys/src/cmd/cwfs/portfns.h | 2 +- sys/src/cmd/cwfs/scsi.c | 4 ++-- sys/src/cmd/cwfs/sub.c | 22 ++++++++++----------- sys/src/cmd/cwfs/wren.c | 2 +- 13 files changed, 55 insertions(+), 49 deletions(-) diff --git a/sys/src/cmd/cwfs/all.h b/sys/src/cmd/cwfs/all.h index 89a15bea5..1cf878b04 100644 --- a/sys/src/cmd/cwfs/all.h +++ b/sys/src/cmd/cwfs/all.h @@ -10,8 +10,6 @@ #include "dat.h" #include "portfns.h" -#define malloc(n) ialloc(n, 0) - #define CHAT(cp) ((cons.flags&chatflag) || \ ((cp) && (((Chan*)(cp))->flags&chatflag))) #define QID9P1(a,b) (Qid9p1){a,b} diff --git a/sys/src/cmd/cwfs/chk.c b/sys/src/cmd/cwfs/chk.c index 6f2b437dc..1b793c69c 100644 --- a/sys/src/cmd/cwfs/chk.c +++ b/sys/src/cmd/cwfs/chk.c @@ -59,7 +59,6 @@ static void * chkalloc(ulong n) { char *p = mallocz(n, 1); - if (p == nil) panic("chkalloc: out of memory"); return p; diff --git a/sys/src/cmd/cwfs/config.c b/sys/src/cmd/cwfs/config.c index bcb8b5e58..da71fcd69 100644 --- a/sys/src/cmd/cwfs/config.c +++ b/sys/src/cmd/cwfs/config.c @@ -175,7 +175,7 @@ config1(int c) Device *d, *t; int m; - d = malloc(sizeof(Device)); + d = ialloc(sizeof(Device), 0); do { t = config(); if(d->cat.first == 0) @@ -233,8 +233,7 @@ config(void) if(f.error) return devnone; - d = malloc(sizeof(Device)); - + d = ialloc(sizeof(Device), 0); c = *f.charp++; switch(c) { default: @@ -277,7 +276,7 @@ config(void) d->wren.ctrl = -1; d->wren.targ = -1; d->wren.lun = -1; - d->wren.file = malloc((e - s) + 1); + d->wren.file = ialloc((e - s) + 1, 0); memmove(d->wren.file, s, e - s); d->wren.file[e - s] = 0; break; @@ -336,7 +335,7 @@ config(void) d->type = Devcw; d->cw.c = config(); d->cw.w = config(); - d->cw.ro = malloc(sizeof(Device)); + d->cw.ro = ialloc(sizeof(Device), 0); d->cw.ro->type = Devro; d->cw.ro->ro.parent = d; f.lastcw = d; diff --git a/sys/src/cmd/cwfs/cw.c b/sys/src/cmd/cwfs/cw.c index dad3d5a38..d4a0b9dd0 100644 --- a/sys/src/cmd/cwfs/cw.c +++ b/sys/src/cmd/cwfs/cw.c @@ -400,7 +400,7 @@ cwinit1(Device *dev) roflag = flag_install("ro", "-- ro reads and writes"); first = 1; } - cw = malloc(sizeof(Cw)); + cw = ialloc(sizeof(Cw), 0); dev->private = cw; cw->allflag = 0; diff --git a/sys/src/cmd/cwfs/iobuf.c b/sys/src/cmd/cwfs/iobuf.c index a48c7ace5..0caebf38b 100644 --- a/sys/src/cmd/cwfs/iobuf.c +++ b/sys/src/cmd/cwfs/iobuf.c @@ -1,7 +1,7 @@ #include "all.h" #include "io.h" -extern long nhiob; +extern uint nhiob; extern Hiob *hiob; Iobuf* diff --git a/sys/src/cmd/cwfs/juke.c b/sys/src/cmd/cwfs/juke.c index 25b916cfe..5deabd8a2 100644 --- a/sys/src/cmd/cwfs/juke.c +++ b/sys/src/cmd/cwfs/juke.c @@ -1166,7 +1166,7 @@ querychanger(Device *xdev) * allocate a juke structure * no locking problems. */ - w = malloc(sizeof(Juke)); + w = ialloc(sizeof(Juke), 0); w->magic = Jukemagic; w->isfixedsize = FIXEDSIZE; w->link = jukelist; diff --git a/sys/src/cmd/cwfs/main.c b/sys/src/cmd/cwfs/main.c index 8f6f2a8e5..eaba6c1c1 100644 --- a/sys/src/cmd/cwfs/main.c +++ b/sys/src/cmd/cwfs/main.c @@ -71,7 +71,7 @@ mapinit(char *mapfile) fields[0], mapfile); continue; } - map = malloc(sizeof *map); + map = ialloc(sizeof(Map), 0); map->from = strdup(fields[0]); map->to = strdup(fields[1]); map->fdev = iconfig(fields[0]); @@ -323,15 +323,15 @@ main(int argc, char **argv) netinit(); scsiinit(); - files = malloc(conf.nfile * sizeof *files); + files = ialloc(conf.nfile * sizeof(*files), 0); for(i=0; i < conf.nfile; i++) { qlock(&files[i]); qunlock(&files[i]); } - wpaths = malloc(conf.nwpath * sizeof(*wpaths)); - uid = malloc(conf.nuid * sizeof(*uid)); - gidspace = malloc(conf.gidspace * sizeof(*gidspace)); + wpaths = ialloc(conf.nwpath * sizeof(*wpaths), 0); + uid = ialloc(conf.nuid * sizeof(*uid), 0); + gidspace = ialloc(conf.gidspace * sizeof(*gidspace), 0); iobufinit(); @@ -597,11 +597,12 @@ Devsize inqsize(char *file) { int nf; - char *ln, *end, *data = malloc(strlen(file) + 5 + 1); + char *ln, *end, *data; char *fields[4]; Devsize rv = -1; Biobuf *bp; + data = malloc(strlen(file) + 5 + 1); strcpy(data, file); end = strstr(data, "/data"); if (end == nil) @@ -621,4 +622,3 @@ inqsize(char *file) free(data); return rv; } - diff --git a/sys/src/cmd/cwfs/malloc.c b/sys/src/cmd/cwfs/malloc.c index aed02b117..165599623 100644 --- a/sys/src/cmd/cwfs/malloc.c +++ b/sys/src/cmd/cwfs/malloc.c @@ -1,10 +1,12 @@ #include "all.h" #include "io.h" -static ulong +#include + +static uvlong memsize(void) { - ulong pgsize, pgmax, userpgs, userused; + ulong pgsize, userpgs, userused; char *s, *f[2]; int n, mpcnt; Biobuf *bp; @@ -37,17 +39,13 @@ memsize(void) if(mpcnt < 1) mpcnt = 1; userpgs = (userpgs*mpcnt)/100; - pgmax = (1024*1024*1024)/pgsize; /* 1GB max */ - if(userpgs > pgmax) - userpgs = pgmax; - return userpgs*pgsize; + return (uvlong)userpgs*pgsize; } return 16*MB; } - -long niob; -long nhiob; +uint niob; +uint nhiob; Hiob *hiob; /* @@ -56,14 +54,26 @@ Hiob *hiob; * end of the allocated memory. */ void* -ialloc(ulong n, int align) +ialloc(uintptr n, int align) { - void *p = mallocalign(n, align, 0, 0); + char *p; + int m; - if (p == nil) + if(align <= 0) + align = sizeof(uintptr); + + mainmem->lock(mainmem); + + p = sbrk(0); + if(m = n % align) + n += align - m; + if(m = (uintptr)p % align) + p += align - m; + if(brk(p+n) < 0) panic("ialloc: out of memory"); - setmalloctag(p, getcallerpc(&n)); - memset(p, 0, n); + + mainmem->unlock(mainmem); + return p; } @@ -89,7 +99,7 @@ iobufinit(void) while(!prime(nhiob)) nhiob++; if(chatty) - print("\t%ld buffers; %ld hashes\n", niob, nhiob); + print("\t%ud buffers; %ud hashes\n", niob, nhiob); hiob = ialloc(nhiob * sizeof(Hiob), 0); hp = hiob; for(i=0; icat.ndev++; } - list = malloc(d->cat.ndev*sizeof(Device*)); + list = ialloc(d->cat.ndev * sizeof(Device*), 0); d->private = list; for(x=d->cat.first; x; x=x->link) { *list++ = x; diff --git a/sys/src/cmd/cwfs/portfns.h b/sys/src/cmd/cwfs/portfns.h index eac4cafa0..ba584caf0 100644 --- a/sys/src/cmd/cwfs/portfns.h +++ b/sys/src/cmd/cwfs/portfns.h @@ -95,7 +95,7 @@ Chan* getlcp(uchar*, long); Off getraddr(Device*); void hexdump(void*, int); int iaccess(File*, Dentry*, int); -void* ialloc(ulong, int); +void* ialloc(uintptr, int); Off ibbpow(int); Off ibbpowsum(int); Device* iconfig(char *); diff --git a/sys/src/cmd/cwfs/scsi.c b/sys/src/cmd/cwfs/scsi.c index 3e4c90d3b..6ce7a7cd9 100644 --- a/sys/src/cmd/cwfs/scsi.c +++ b/sys/src/cmd/cwfs/scsi.c @@ -46,8 +46,8 @@ scsiinit(void) tp->ctlrno = ctlrno; tp->targetno = targetno; - tp->inquiry = malloc(Ninquiry); - tp->sense = malloc(Nsense); + tp->inquiry = ialloc(Ninquiry, 0); + tp->sense = ialloc(Nsense, 0); } } } diff --git a/sys/src/cmd/cwfs/sub.c b/sys/src/cmd/cwfs/sub.c index 3f33310fa..c73c3d3ab 100644 --- a/sys/src/cmd/cwfs/sub.c +++ b/sys/src/cmd/cwfs/sub.c @@ -38,7 +38,7 @@ fs_chaninit(int count, int data) Chan *cp, *icp; int i; - p = malloc(count * (sizeof(Chan)+data)); + p = ialloc(count * (sizeof(Chan)+data), 0); icp = (Chan*)p; for(i = 0; i < count; i++) { cp = (Chan*)p; @@ -761,17 +761,17 @@ mbinit(void) msgalloc.lmsgbuf = 0; msgalloc.smsgbuf = 0; for(i=0; imagic = Mbmagic; - mb->xdata = malloc(LARGEBUF+Slop); + mb->xdata = ialloc(LARGEBUF+Slop, 0); mb->flags = LARGE; mbfree(mb); cons.nlarge++; } for(i=0; imagic = Mbmagic; - mb->xdata = malloc(SMALLBUF+Slop); + mb->xdata = ialloc(SMALLBUF+Slop, 0); mb->flags = 0; mbfree(mb); cons.nsmall++; @@ -782,7 +782,7 @@ mbinit(void) unlock(&rabuflock); rabuffree = 0; for(i=0; i<1000; i++) { - rb = malloc(sizeof(*rb)); + rb = ialloc(sizeof(*rb), 0); rb->link = rabuffree; rabuffree = rb; } @@ -799,8 +799,8 @@ mballoc(int count, Chan *cp, int category) panic("msgbuf count"); mb = msgalloc.lmsgbuf; if(mb == nil) { - mb = malloc(sizeof(Msgbuf)); - mb->xdata = malloc(LARGEBUF+Slop); + mb = ialloc(sizeof(Msgbuf), 0); + mb->xdata = ialloc(LARGEBUF+Slop, 0); cons.nlarge++; } else msgalloc.lmsgbuf = mb->next; @@ -808,8 +808,8 @@ mballoc(int count, Chan *cp, int category) } else { mb = msgalloc.smsgbuf; if(mb == nil) { - mb = malloc(sizeof(Msgbuf)); - mb->xdata = malloc(SMALLBUF+Slop); + mb = ialloc(sizeof(Msgbuf), 0); + mb->xdata = ialloc(SMALLBUF+Slop, 0); cons.nsmall++; } else msgalloc.smsgbuf = mb->next; @@ -958,7 +958,7 @@ newqueue(int size, char *name) { Queue *q; - q = malloc(sizeof(Queue) + (size-1)*sizeof(void*)); + q = ialloc(sizeof(Queue) + (size-1)*sizeof(void*), 0); q->size = size; q->avail = size; q->count = 0; diff --git a/sys/src/cmd/cwfs/wren.c b/sys/src/cmd/cwfs/wren.c index 65338af8e..5f963a874 100644 --- a/sys/src/cmd/cwfs/wren.c +++ b/sys/src/cmd/cwfs/wren.c @@ -39,7 +39,7 @@ wreninit(Device *d) if(d->private) return; - d->private = dr = malloc(sizeof(Wren)); + d->private = dr = ialloc(sizeof(Wren), 0); if (d->wren.file) d->wren.sddata = dataof(d->wren.file);