code: plan9front

Download patch

ref: 0d34984734c0feceaab774d401a09e9333f81991
parent: f0084afefa334fb2e905017c5b4eda6b23e77ec3
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Fri May 26 18:08:53 EDT 2023

sdiahci, sdodin, sdide, devsd: get it right...

previous commit was typoed, and the changes are even wrong.
we actually need to allocate individual SDev's for the
unregistration to happen.

also, sdide's ataclear() was trying to free sdev->name,
which isnt even dynamically allocated. the clear routine
should only get rid of the controller specific stuff,
not touch the sdev at all.

devsd was also not freeing the unitflag and unit pointer
arrays...

--- a/sys/src/9/pc/sdiahci.c
+++ b/sys/src/9/pc/sdiahci.c
@@ -170,7 +170,6 @@
 };
 
 static	Ctlr	iactlr[NCtlr];
-static	SDev	*sdevs;
 static	int	niactlr;
 
 static	Drive	*iadrive[NDrive];
@@ -2167,9 +2166,6 @@
 	}
 
 	memset(olds, 0xff, sizeof olds);
-	sdevs = malloc(sizeof(SDev)*NCtlr);
-	if(sdevs == nil)
-		return nil;
 	p = nil;
 	while((p = pcimatch(p, 0, 0)) != nil){
 		if((type = didtype(p)) == -1)
@@ -2183,9 +2179,13 @@
 			break;
 		}
 		c = iactlr + niactlr;
-		s = sdevs + niactlr;
-		memset(c, 0, sizeof *c);
+		s = malloc(sizeof(SDev));
+		if(s == nil){
+			print("iapnp: %s: can't allocate SDev\n", tname[type]);
+			break;
+		}
 		memset(s, 0, sizeof *s);
+		memset(c, 0, sizeof *c);
 		c->mmio = vmap(io, p->mem[Abar].size);
 		if(c->mmio == nil){
 			print("%s: can't map %llux\n", Tname(c), io);
--- a/sys/src/9/pc/sdide.c
+++ b/sys/src/9/pc/sdide.c
@@ -782,12 +782,6 @@
 		free(ctlr->drive[0]);
 	if (ctlr->drive[1])
 		free(ctlr->drive[1]);
-	if (sdev->name)
-		free(sdev->name);
-	if (sdev->unitflg)
-		free(sdev->unitflg);
-	if (sdev->unit)
-		free(sdev->unit);
 	free(ctlr);
 }
 
--- a/sys/src/9/pc/sdodin.c
+++ b/sys/src/9/pc/sdodin.c
@@ -543,7 +543,6 @@
 };
 
 static	Ctlr	msctlr[Nctlr];
-static	SDev	*sdevs;
 static	uint	nmsctlr;
 static	Drive	*msdrive[Ndrive];
 static	uint	nmsdrive;
@@ -2646,9 +2645,6 @@
 		return nil;
 	s0 = nil;
 	ll = &s0;
-	sdevs = malloc(sizeof(SDev)*NCtlr);
-	if(sdevs == nil)
-		return nil;
 	for(p = nil; (p = pcimatch(p, 0x11ab, 0x6485)) != nil; ){
 		if(nmsctlr == Nctlr){
 			print("sdodin: too many controllers\n");
@@ -2655,7 +2651,11 @@
 			break;
 		}
 		c = msctlr + nmsctlr;
-		s = sdevs + nmsctlr;
+		s = malloc(sizeof(SDev));
+		if(s == nil){
+			print("sdodin: can't allocate SDev\n");
+			break;
+		}
 		memset(c, 0, sizeof *c);
 		memset(s, 0, sizeof *s);
 		if((c->reg = map(p, Mebar)) == nil){
--- a/sys/src/9/port/devsd.c
+++ b/sys/src/9/port/devsd.c
@@ -356,10 +356,10 @@
 		if(sdev->unit == nil || sdev->unitflg == nil){
 			print("sdadddevs: out of memory\n");
 		giveup:
-			free(sdev->unit);
-			free(sdev->unitflg);
 			if(sdev->ifc->clear)
 				sdev->ifc->clear(sdev);
+			free(sdev->unit);
+			free(sdev->unitflg);
 			free(sdev);
 			continue;
 		}
@@ -1630,6 +1630,10 @@
 	if(sdev->enabled && sdev->ifc->disable)
 		sdev->ifc->disable(sdev);
 
+	/* free controller specific info */
+	if(sdev->ifc->clear)
+		sdev->ifc->clear(sdev);
+
 	for(i = 0; i < sdev->nunit; i++){
 		if((unit = sdev->unit[i]) != nil){
 			free(unit->name);
@@ -1637,10 +1641,8 @@
 			free(unit);
 		}
 	}
-
-	if(sdev->ifc->clear)
-		sdev->ifc->clear(sdev);
-
+	free(sdev->unit);
+	free(sdev->unitflg);
 	free(sdev);
 	return 0;
 }