clean-up: fixes "comparison between signed
Quoted 25 lines · Jesper Juhl
Quoted 12 lines · Riina Kikas
This patch fixes warning "comparison between signed and unsigned" occuring on line 308
Signed-off-by Riina Kikas <[email protected]>arch/i386/mm/fault.c 2004-12-02 21:30:59.000000000 +0000 renamed from arch/i386/mm/fault.c 2004-12-02 21:30:30.000000000 +0000 +0
-1@@ -302,7 +302,13 @@ * pusha) doing post-decrement on the stack and that * doesn't show up until later.. */ - if (address + 32 < regs->esp)arch/i386/mm/fault.c 2004-12-02 21:30:59.000000000 +0000 line 314
Quoted 10 lines
arch/i386/mm/fault.c 2004-12-02 21:30:59.000000000 +0000 renamed from arch/i386/mm/fault.c 2004-12-02 21:30:30.000000000 +0000 +7
-0+ unsigned long regs_esp; + if (regs->esp < 0) { + regs_esp = 0; + } else { + regs_esp = regs->esp; + } + if (address + 32 < regs_esp) goto bad_area; } if (expand_stack(vma, address))
Quoted 7 lines
This seems a bit silly. If the stack pointer (esp) is ever negative that's clearly a bug somewhere. So instead of testing it for <0 and then setting your
regs_espvariable to 0 it would make more sense to me to justBUG_ON(regs->esp < 0)or something, if you want to do anything at all. And if you want to silence the warning a exlicit cast to unsigned long should do I'd say, but I have a feeling the best thing is to just leave that warning alone, the code seems to be fine.
regs->esp is < 0 almost always - user stack starts at 0xBFFFFFFF, which is negative number when you treat it as 'long'. Change is wrong, now when esp is in top 2GB 'address' is never evaluated as invalid, and it was definitely not intention.
Correct is (if any fix is needed at all) typecast regs->esp to unsigned long, eventually with check that address is less than (unsigned long)-32, as area at VA 0 is not going to grow "down" to 0xFFFFFxxx, even if you nicely ask.
Best regards,
Petr Vandrovec
Quoted 34 lines · Petr Vandrovec
Quoted 25 lines · Jesper Juhl
Quoted 12 lines · Riina Kikas
This patch fixes warning "comparison between signed and unsigned" occuring on line 308
Signed-off-by Riina Kikas <[email protected]>arch/i386/mm/fault.c 2004-12-02 21:30:59.000000000 +0000 renamed from arch/i386/mm/fault.c 2004-12-02 21:30:30.000000000 +0000 +0
-1@@ -302,7 +302,13 @@ * pusha) doing post-decrement on the stack and that * doesn't show up until later.. */ - if (address + 32 < regs->esp)arch/i386/mm/fault.c 2004-12-02 21:30:59.000000000 +0000 line 314
Quoted 10 lines
arch/i386/mm/fault.c 2004-12-02 21:30:59.000000000 +0000 renamed from arch/i386/mm/fault.c 2004-12-02 21:30:30.000000000 +0000 +7
-0+ unsigned long regs_esp; + if (regs->esp < 0) { + regs_esp = 0; + } else { + regs_esp = regs->esp; + } + if (address + 32 < regs_esp) goto bad_area; } if (expand_stack(vma, address))Quoted 7 lines
This seems a bit silly. If the stack pointer (esp) is ever negative that's clearly a bug somewhere. So instead of testing it for <0 and then setting your
regs_espvariable to 0 it would make more sense to me to justBUG_ON(regs->esp < 0)or something, if you want to do anything at all. And if you want to silence the warning a exlicit cast to unsigned long should do I'd say, but I have a feeling the best thing is to just leave that warning alone, the code seems to be fine.
Quoted 2 lines
regs->espis < 0 almost always - user stack starts at0xBFFFFFFF, which is negative number when you treat it as 'long'.
True, if you treat that nr as signed long it's clearly negative. I guess what twisted my head there was thinking about a register as a signed C type.
Why are the registers not defined as unsigned types in the struct in the first place?
Quoted 6 lines
Change is wrong, now
when esp is in top 2GB 'address' is never evaluated as invalid, and it was definitely not intention.Correct is (if any fix is needed at all) typecast
regs->espto unsigned long,
That would have been my suggestion as well.
Quoted 3 lines
eventually with check that address is less than (unsigned long)-32, as area at VA 0 is not going to grow "down" to 0xFFFFFxxx, even if you nicely ask.
you mean something like this - right?
arch/i386/mm/fault.c +1 -1
@@ -305,7 +305,7 @@ fastcall void do_page_fault(struct pt_re* pusha) doing post-decrement on the stack and that * doesn't show up until later.. */ -if (address +32 < regs->esp) +if (address +32 < (unsigned long )regs->esp || address >= (~0UL -32 )) goto bad_area; }if (expand_stack (vma, address))
Quoted 11 lines · Jesper Juhl
Quoted 2 lines · Petr Vandrovec
Correct is (if any fix is needed at all) typecast
regs->espto unsigned long,That would have been my suggestion as well.
Quoted 3 lines
eventually with check that address is less than (unsigned long)-32, as area at VA 0 is not going to grow "down" to 0xFFFFFxxx, even if you nicely ask.
you mean something like this - right?
Yes. Though I believe that we already take vma == NULL path when address is that big.
Petr Vandrovec
Quoted 17 lines
Signed-off-by Jesper Juhl <[email protected]>diff -up
linux-2.6.10-rc3-bk2-orig/arch/i386/mm/fault.clinux-2.6.10-rc3-bk2/arch/i386/mm/fault.carch/i386/mm/fault.c +1
-1@@ -305,7 +305,7 @@ fastcall void do_page_fault(struct pt_re* pusha) doing post-decrement on the stack and that * doesn't show up until later.. */ -if (address +32 < regs->esp) +if (address +32 < (unsigned long )regs->esp || address >= (~0UL -32 )) goto bad_area; }if (expand_stack (vma, address))
Quoted 7 lines · Petr Vandrovec
Quoted 6 lines · Jesper Juhl
Quoted 2 lines · Petr Vandrovec
Correct is (if any fix is needed at all) typecast
regs->espto unsigned long,That would have been my suggestion as well.
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
eventually with check that address is less than (unsigned long)-32, as area at VA 0 is not going to grow "down" to 0xFFFFFxxx, even if you nicely ask.
you mean something like this - right?
Yes. Though I believe that we already take vma == NULL path when address is that big.
Hmm, where? - maybe I'm blind or just stupid, but I don't seem to be able to find where we do that.
And would it hurt to have that additional check there as well in case address was modified after the previous check and before being passed to do_page_fault ? (note: I'm writing this last bit without having mined the source for info yet).
Signature · 1 line
Jesper
Quoted 15 lines · Jesper Juhl
Quoted 13 lines · Petr Vandrovec
Quoted 11 lines · Jesper Juhl
Quoted 2 lines · Petr Vandrovec
Correct is (if any fix is needed at all) typecast
regs->espto unsigned long,That would have been my suggestion as well.
Quoted 3 lines
eventually with check that address is less than (unsigned long)-32, as area at VA 0 is not going to grow "down" to 0xFFFFFxxx, even if you nicely ask.
you mean something like this - right?
Quoted 8 lines
Quoted 1 line
Yes. Though I believe that we already take vma == NULL path when address is that big.
Hmm, where? - maybe I'm blind or just stupid, but I don't seem to be able to find where we do that.
And would it hurt to have that additional check there as well in case address was modified after the previous check and before being passed todo_page_fault? (note: I'm writing this last bit without having mined the source for info yet).
If find_vma() returns NULL, it is bad_area, and no further tests occur. Otherwise if vma->vm_start <= address, it is good area.
Only when these two conditions are satisifed (find_vma found vma, and this vma begins above vma's vm_start, regs->esp is checked. And as vma->vm_start can be at most 0xFFFFF000 (it is page aligned, and you cannot have vma at 4GB - actually you cannot have vma above 3GB on normal kernel, or 4GB-<whatever>MB on 4G/4G kernel), there is no way how 'address' could be in top 4KB, and so adding 32 to it cannot overflow 32bit variable.
At least I believe this...
Petr Vandrovec
Quoted 28 lines · Petr Vandrovec
Quoted 15 lines · Jesper Juhl
Quoted 13 lines · Petr Vandrovec
Quoted 11 lines · Jesper Juhl
Quoted 2 lines · Petr Vandrovec
Correct is (if any fix is needed at all) typecast
regs->espto unsigned long,That would have been my suggestion as well.
Quoted 3 lines
eventually with check that address is less than (unsigned long)-32, as area at VA 0 is not going to grow "down" to 0xFFFFFxxx, even if you nicely ask.
you mean something like this - right?
Quoted 8 lines
Quoted 1 line
Yes. Though I believe that we already take vma == NULL path when address is that big.
Hmm, where? - maybe I'm blind or just stupid, but I don't seem to be able to find where we do that.
And would it hurt to have that additional check there as well in case address was modified after the previous check and before being passed todo_page_fault? (note: I'm writing this last bit without having mined the source for info yet).If
find_vma()returns NULL, it isbad_area, and no further tests occur. Otherwise ifvma->vm_start<= address, it is good area.
Quoted 8 lines
Only when these two conditions are satisifed (
find_vmafound vma, and this vma begins above vma'svm_start,regs->espis checked. And asvma->vm_startcan be at most0xFFFFF000(it is page aligned, and you cannot have vma at 4GB - actually you cannot have vma above 3GB on normal kernel, or 4GB-<whatever>MB on 4G/4G kernel), there is no way how 'address' could be in top 4KB, and so adding 32 to it cannot overflow 32bit variable.At least I believe this...
Having read your explanation (thank you) and re-read the code I think you are right. If that's indeed the case, then it would seem that the code is just fine and the best we can do is just add an explicit cast to unsigned long to shut up the compiler.
arch/i386/mm/fault.c +1 -1
@@ -305,7 +305,7 @@ fastcall void do_page_fault(struct pt_re* pusha) doing post-decrement on the stack and that * doesn't show up until later.. */ -if (address +32 < regs->esp) +if (address +32 < (unsigned long )regs->esp) goto bad_area; }if (expand_stack (vma, address))
If you agree I'll forward the patch to Andrew.
Signature · 1 line
Jesper Juhl