Skip to contentSkip to conversation

dpt_i2o.c memleak/incorrectness

PATCH Oleg Drokin 7 participants 15 messages Mar 13, 2003 – Mar 15, 2003 SCSI

lore
Share & tools
lore
#0 lore raw

Hello!

There is something strange going on in drivers/scsi/dpt_i2o.c in both 2.4 and 2.5. adpt_i2o_reset_hba() function allocates 4 bytes for "status" stuff, then tries to reset controller, then
if timeout on first reset stage is reached, frees "status" and returns, otherwise it proceeds to monitor "status" (which is modified by hardware now, btw), and if timeout is reached, just exits.
On the first thought I just thought it is trivial memleak that can be fixed with patch below, but after some more thining I just thought "what if after some time controller awakes and modifies status, but it is already allocated for other purposes", scary eh? So may be we shold not free those four bytes on timeout at all just for safeness reasons?

Found with help of smatch + enhanced unfree script.

Bye,
Oleg

===== drivers/scsi/dpt_i2o.c 1.9 vs edited =====

drivers/scsi/dpt_i2o.c +1 -0
@@ -1336,6 +1336,7 @@
 			}
 			if(time_after(jiffies,timeout)){
 				printk(KERN_ERR "%s:Timeout waiting for IOP Reset.\n",pHba->name);
+				kfree(status);
 				return -ETIMEDOUT;
 			}
 		} while (m == EMPTY_QUEUE);
#1 lore raw
Quoted 8 lines · Oleg Drokin

Hello!

There is something strange going on in drivers/scsi/dpt_i2o.c in both 2.4 and 2.5. adpt_i2o_reset_hba() function allocates 4 bytes for "status" stuff, then tries to reset controller, then
if timeout on first reset stage is reached, frees "status" and returns, otherwise it proceeds to monitor "status" (which is modified by hardware now, btw), and if timeout is reached, just exits.

Correctly - I2O does the same thing in this case. Its just better to throw a few bytes away than risk corruption

#2 lore raw

Re: dpt_i2o.c fix for possibly memory corruption on reset timeout

Hello!

Quoted 5 lines · Alan Cox
Quoted 3 lines

if timeout on first reset stage is reached, frees "status" and returns, otherwise it proceeds to monitor "status" (which is modified by hardware now, btw), and if timeout is reached, just exits.

Correctly - I2O does the same thing in this case. Its just better to throw a few bytes away than risk corruption

Ok, so please consider applying this patch instead (appies to both 2.4 and 2.5)

Bye,
Oleg

===== drivers/scsi/dpt_i2o.c 1.9 vs edited =====

drivers/scsi/dpt_i2o.c +6 -1
@@ -1318,7 +1318,9 @@
 	while(*status == 0){
 		if(time_after(jiffies,timeout)){
 			printk(KERN_WARNING"%s: IOP Reset Timeout\n",pHba->name);
-			kfree(status);
+			/* We loose 4 bytes of "status" here, but we cannot
+			   free these because controller may awake and corrupt
+			   those bytes at any time */
 			return -ETIMEDOUT;
 		}
 		rmb();
@@ -1336,6 +1338,9 @@
 			}
 			if(time_after(jiffies,timeout)){
 				printk(KERN_ERR "%s:Timeout waiting for IOP Reset.\n",pHba->name);
+			/* We loose 4 bytes of "status" here, but we cannot
+			   free these because controller may awake and corrupt
+			   those bytes at any time */
 				return -ETIMEDOUT;
 			}
 		} while (m == EMPTY_QUEUE);
#3 lore raw

Re: dpt_i2o.c fix for possibly memory corruption on reset timeout

On Thu, 13 Mar 2003 21:41:07 +0300 Oleg Drokin <[email protected]> wrote:

| Ok, so please consider applying this patch instead (appies to both | 2.4 and 2.5)

| + /* We loose 4 bytes of "status" here, but we cannot
lose

| @@ -1336,6 +1338,9 @@
| + /* We loose 4 bytes of "status" here, but we cannot
lose

--
~Randy

#4 lore raw

Re: dpt_i2o.c fix for possibly memory corruption on reset timeout

Hello!

Quoted 2 lines · Randy.Dunlap

| Ok, so please consider applying this patch instead (appies to both | 2.4 and 2.5)

Ok, here's the one with spelling fix from Randy ;)

Bye,
Oleg

===== drivers/scsi/dpt_i2o.c 1.9 vs edited =====

drivers/scsi/dpt_i2o.c +6 -1
@@ -1318,7 +1318,9 @@
 	while(*status == 0){
 		if(time_after(jiffies,timeout)){
 			printk(KERN_WARNING"%s: IOP Reset Timeout\n",pHba->name);
-			kfree(status);
+			/* We lose 4 bytes of "status" here, but we cannot
+			   free these because controller may awake and corrupt
+			   those bytes at any time */
 			return -ETIMEDOUT;
 		}
 		rmb();
@@ -1336,6 +1338,9 @@
 			}
 			if(time_after(jiffies,timeout)){
 				printk(KERN_ERR "%s:Timeout waiting for IOP Reset.\n",pHba->name);
+			/* We lose 4 bytes of "status" here, but we cannot
+			   free these because controller may awake and corrupt
+			   those bytes at any time */
 				return -ETIMEDOUT;
 			}
 		} while (m == EMPTY_QUEUE);
#5 lore raw

Re: dpt_i2o.c fix for possibly memory corruption on reset timeout

Quoted 15 lines · Oleg Drokin

Hello!

Quoted 2 lines · Randy.Dunlap

| Ok, so please consider applying this patch instead (appies to | both 2.4 and 2.5)

Ok, here's the one with spelling fix from Randy ;)

Bye,
Oleg

===== drivers/scsi/dpt_i2o.c 1.9 vs edited =====

drivers/scsi/dpt_i2o.c line 1323

Quoted 7 lines
drivers/scsi/dpt_i2o.c +3 -1
@@ -1318,7 +1318,9 @@
 	while(*status == 0){
 		if(time_after(jiffies,timeout)){
 			printk(KERN_WARNING"%s: IOP Reset Timeout\n",pHba->name);
-			kfree(status);
+			/* We lose 4 bytes of "status" here, but we cannot
+			   free these because controller may awake and corrupt
+			   those bytes at any time */

I'd leave kfree() inside the comment - less effort for those operating under -ENOCAFFEINE condition

			/* do NOT do kfree(status): we lose ....

I don't like the whole idea that mem leak is tolerable.

Can we just add a 4 byte scratch pad status to
struct _adpt_hba? Let it scribble there...
--
vda

#6 lore raw

Re: dpt_i2o.c fix for possibly memory corruption on reset timeout

Quoted 5 lines · Denis Vlasenko

I don't like the whole idea that mem leak is tolerable.

Can we just add a 4 byte scratch pad status to
struct _adpt_hba? Let it scribble there...

I've had the same idea, but there might be some pitfalls around. The problem boils down to the users of the buffer. Is it per-device, per-process, per-whatever?

Once this is known I'm all for putting the buffer into a per-whatever data structure. But someone has to understand the driver first. :)

J�rn

Signature · 2 lines
Everything should be made as simple as possible, but not simpler.
-- Albert Einstein
#7 lore raw

Re: dpt_i2o.c fix for possibly memory corruption on reset timeout

In reply to Denis Vlasenko
Quoted 4 lines · Denis Vlasenko

I don't like the whole idea that mem leak is tolerable.

Can we just add a 4 byte scratch pad status to
struct _adpt_hba? Let it scribble there...

Its 4 bytes (+ slab overhead), its far safer if this happens to say its gone forever. Its owned by the I2O controller now and it never gave it back

#8 lore raw

Re: dpt_i2o.c fix for possibly memory corruption on reset timeout

Quoted 9 lines · Alan Cox
Quoted 4 lines · Denis Vlasenko

I don't like the whole idea that mem leak is tolerable.

Can we just add a 4 byte scratch pad status to
struct _adpt_hba? Let it scribble there...

Its 4 bytes (+ slab overhead), its far safer if this happens to say its gone forever. Its owned by the I2O controller now and it never gave it back

How about an (optional) counter then? If you can show that this case is hit zero times during operation, noone will complain. On the other hand, if some hardware hits this problem 1000+ times, we have a good reason to find another solution.

I'd volunteer to create the patch, if the idea is accepted.

J�rn

Signature · 3 lines
Mundie uses a textbook tactic of manipulation: start with some
reasonable talk, and lead the audience to an unreasonable conclusion.
-- Bruce Perens
#9 lore raw

Re: dpt_i2o.c fix for possibly memory corruption on reset timeout

Hello!

Quoted 10 lines · Joern Engel
Quoted 5 lines
Quoted 2 lines

Can we just add a 4 byte scratch pad status to
struct _adpt_hba? Let it scribble there...

Its 4 bytes (+ slab overhead), its far safer if this happens to say its gone forever. Its owned by the I2O controller now and it never gave it back

How about an (optional) counter then? If you can show that this case is hit zero times during operation, noone will complain. On the other hand, if some hardware hits this problem 1000+ times, we have a good reason to find another solution.
I'd volunteer to create the patch, if the idea is accepted.

Well, if some hardware would do so, then users would go here and complain about kernel being noisy on certain hardware. (message is printed each time this happens). Have not happened so far.

Bye,
Oleg

#10 lore raw

Re: dpt_i2o.c fix for possibly memory corruption on reset timeout

Quoted 3 lines · Oleg Drokin

Well, if some hardware would do so, then users would go here and complain about kernel being noisy on certain hardware. (message is printed each time this happens). Have not happened so far.

Its run once per controller on board setup, and if it fails that controller is history until reboot

#11 lore raw
In reply to Alan Cox
Quoted 9 lines
Quoted 6 lines

There is something strange going on in drivers/scsi/dpt_i2o.c in both 2.4 and 2.5. adpt_i2o_reset_hba() function allocates 4 bytes for "status" stuff, then tries to reset controller, then
if timeout on first reset stage is reached, frees "status" and returns, otherwise it proceeds to monitor "status" (which is modified by hardware now, btw), and if timeout is reached, just exits.

Correctly - I2O does the same thing in this case. Its just better to throw a few bytes away than risk corruption

Better document it in the comments or it will get "corrected" by some mem leak detector. If possible try to use a static for the pointer to the status block, but that may not work. Re-enterant code and multi CPU situations likely won't allow for that. Also it might not be worth the effort to properly determin if it is safe to use only one location.

- Bryan

#12 lore raw

Bryan Andersen <[email protected]> dijo:

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

There is something strange going on in drivers/scsi/dpt_i2o.c in both 2.4 and 2.5. adpt_i2o_reset_hba() function allocates 4 bytes for "status" stuff, then tries to reset controller, then
if timeout on first reset stage is reached, frees "status" and returns, otherwise it proceeds to monitor "status" (which is modified by hardware now, btw), and if timeout is reached, just exits.

Quoted 8 lines
Quoted 2 lines

Correctly - I2O does the same thing in this case. Its just better to throw a few bytes away than risk corruption

Better document it in the comments or it will get "corrected" by some mem leak detector. If possible try to use a static for the pointer to the status block, but that may not work. Re-enterant code and multi CPU situations likely won't allow for that. Also it might not be worth the effort to properly determin if it is safe to use only one location.

A device owned area, perhaps? To set up an area that can be messed around without proper locking will get you problems down the line.

Signature · 4 lines
Dr. Horst H. von Brand                   User #22616 counter.li.org
Departamento de Informatica                     Fono: +56 32 654431
Universidad Tecnica Federico Santa Maria              +56 32 654239
Casilla 110-V, Valparaiso, Chile                Fax:  +56 32 797513
#13 lore raw

Now i2o_core.c memleak/incorrectness?

In reply to Alan Cox

Hello!

Quoted 8 lines · Alan Cox
Quoted 6 lines

There is something strange going on in drivers/scsi/dpt_i2o.c in both 2.4 and 2.5. adpt_i2o_reset_hba() function allocates 4 bytes for "status" stuff, then tries to reset controller, then
if timeout on first reset stage is reached, frees "status" and returns, otherwise it proceeds to monitor "status" (which is modified by hardware now, btw), and if timeout is reached, just exits.

Correctly - I2O does the same thing in this case. Its just better to throw a few bytes away than risk corruption

Well, it seems that i2o does not always follow this rule.
Also i2o_init_outbound_q() seems not free this "status" thing if everything went ok, is this intentional?
Or perhaps something like this patch is needed?

Bye,
Oleg

===== drivers/message/i2o/i2o_core.c 1.12 vs edited =====

drivers/message/i2o/i2o_core.c +2 -1
@@ -2217,7 +2217,7 @@
 			else  
 				printk(KERN_ERR "%s: Outbound queue initialize timeout.\n",
 					c->name);
-			kfree(status);
+			// Better leak this for safety: kfree(status);
 			return -ETIMEDOUT;
 		}  
 		schedule();
@@ -2231,6 +2231,7 @@
 		return -ETIMEDOUT;
 	}
 
+	kfree(status);
 	return 0;
 }
 
#14 lore raw

Re: Now i2o_core.c memleak/incorrectness?

Quoted 4 lines · Oleg Drokin

Well, it seems that i2o does not always follow this rule.
Also i2o_init_outbound_q() seems not free this "status" thing if everything went ok, is this intentional?
Or perhaps something like this patch is needed?

I'll take a look. I'm doing a bunch of i2o cleanup in 2.5 right now