Prev: [RFC] [PATCH 1/2] cgroups: read-write lock CLONE_THREAD forking per threadgroup
Next: [PATCH] afs: fix possible null pointer dereference in afs_alloc_server
From: Andrew Morton on 29 May 2010 23:50 On Fri, 28 May 2010 07:53:13 -0300 Cesar Eduardo Barros <cesarb(a)cesarb.net> wrote: > kunmap_atomic() is currently at level -4 on Rusty's "Hard To Misuse" > list[1] ("Follow common convention and you'll get it wrong"), except in > some architectures when CONFIG_DEBUG_HIGHMEM is set[2][3]. > > kunmap() takes a pointer to a struct page; kunmap_atomic(), however, > takes takes a pointer to within the page itself. This seems to once in a > while trip people up (the convention they are following is the one from > kunmap()). > > Make it much harder to misuse, by moving it to level 9 on Rusty's > list[4] ("The compiler/linker won't let you get it wrong"). This is done > by refusing to build if the pointer passed to it is convertible to a > struct page * but it is not a void * (verified by trying to convert it > to a pointer to a dummy struct). > > The real kunmap_atomic() is renamed to kunmap_atomic_notypecheck() > (which is what you would call in case for some strange reason calling it > with a pointer to a struct page is not incorrect in your code). > Fair enough, that's a 99% fix. A long time ago I made kmap_atomic() return a char * (iirc) and kunmap_atomic() is passed a char*. It worked, but I ended up throwing it away. I don't precisely remember why - I think it was intrusiveness and general hassle rather than anything fundamental. > > ... > > +/* Prevent people trying to call kunmap_atomic() as if it were kunmap() */ > +struct __kunmap_atomic_dummy {}; > +#define kunmap_atomic(addr, idx) do { \ > + BUILD_BUG_ON( \ > + __builtin_types_compatible_p(typeof(addr), struct page *) && \ > + !__builtin_types_compatible_p(typeof(addr), struct __kunmap_atomic_dummy *)); \ > + kunmap_atomic_notypecheck((addr), (idx)); \ > + } while (0) <looks around> OK, it seems that __builtin_types_compatible_p() is supported on all approved gcc versions. We have a little __same_type() helper for this. __must_be_array() should be using it, too. -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo(a)vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/
From: Cesar Eduardo Barros on 30 May 2010 13:50 Em 30-05-2010 00:42, Andrew Morton escreveu: > On Fri, 28 May 2010 07:53:13 -0300 Cesar Eduardo Barros<cesarb(a)cesarb.net> wrote: >> Make it much harder to misuse, by moving it to level 9 on Rusty's >> list[4] ("The compiler/linker won't let you get it wrong"). This is done >> by refusing to build if the pointer passed to it is convertible to a >> struct page * but it is not a void * (verified by trying to convert it >> to a pointer to a dummy struct). >> >> The real kunmap_atomic() is renamed to kunmap_atomic_notypecheck() >> (which is what you would call in case for some strange reason calling it >> with a pointer to a struct page is not incorrect in your code). >> > > Fair enough, that's a 99% fix. A long time ago I made kmap_atomic() > return a char * (iirc) and kunmap_atomic() is passed a char*. It > worked, but I ended up throwing it away. I don't precisely remember > why - I think it was intrusiveness and general hassle rather than > anything fundamental. I vaguely recall reading something about that on LWN a long time ago.[1] The advantage of my __builtin_types_compatible_p approach is that it does not have to change the callers at all (except in the extremly unlikely case that someone actually meant to call it with a struct page *, which is something I did not find when looking at the whole kernel with spatch[2]). The disadvantage of my approach is that gcc's error message is absolutely atrocious: mm/swapfile.c: In function 'foo': mm/swapfile.c:2501: error: negative width in bit-field '<anonymous>' But that is a problem with BUILD_BUG_ON, not this code. >> +/* Prevent people trying to call kunmap_atomic() as if it were kunmap() */ >> +struct __kunmap_atomic_dummy {}; >> +#define kunmap_atomic(addr, idx) do { \ >> + BUILD_BUG_ON( \ >> + __builtin_types_compatible_p(typeof(addr), struct page *)&& \ >> + !__builtin_types_compatible_p(typeof(addr), struct __kunmap_atomic_dummy *)); \ >> + kunmap_atomic_notypecheck((addr), (idx)); \ >> + } while (0) > > We have a little __same_type() helper for this. __must_be_array() > should be using it, too. It would be great (shortening the long lines a lot), except that in this case it is a complete misnomer, which would probably confuse people reading the code. If __same_type(typeof(addr), void *) worked, I would not need a dummy struct; but __same_type is actually looking for compatible types, not same type (perhaps for non-pointers it actually means "same type"). In the first part of the condition, I am actually looking for "same type", but even there __same_type(void *, struct page *) would return true (which is why I need the second part). And now I am having second thoughts about the line breaks here; I should have also broken between the parameters of __builtin_types_compatible_p, to avoid long lines. If you want, I can resend the patch with it reindented. [1] Yep, there it is: https://lwn.net/Articles/111226/ [2] @@ struct page *page; expression E; @@ * kunmap_atomic(page, E) -- Cesar Eduardo Barros cesarb(a)cesarb.net cesar.barros(a)gmail.com -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo(a)vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/
From: Rusty Russell on 31 May 2010 06:20 On Sun, 30 May 2010 01:12:56 pm Andrew Morton wrote: > On Fri, 28 May 2010 07:53:13 -0300 Cesar Eduardo Barros <cesarb(a)cesarb.net> wrote: > > > kunmap_atomic() is currently at level -4 on Rusty's "Hard To Misuse" > > list[1] ("Follow common convention and you'll get it wrong"), except in > > some architectures when CONFIG_DEBUG_HIGHMEM is set[2][3]. > > > > kunmap() takes a pointer to a struct page; kunmap_atomic(), however, > > takes takes a pointer to within the page itself. This seems to once in a > > while trip people up (the convention they are following is the one from > > kunmap()). > > > > Make it much harder to misuse, by moving it to level 9 on Rusty's > > list[4] ("The compiler/linker won't let you get it wrong"). This is done > > by refusing to build if the pointer passed to it is convertible to a > > struct page * but it is not a void * (verified by trying to convert it > > to a pointer to a dummy struct). > > > > The real kunmap_atomic() is renamed to kunmap_atomic_notypecheck() > > (which is what you would call in case for some strange reason calling it > > with a pointer to a struct page is not incorrect in your code). > > > > Fair enough, that's a 99% fix. A long time ago I made kmap_atomic() > return a char * (iirc) and kunmap_atomic() is passed a char*. It > worked, but I ended up throwing it away. I don't precisely remember > why - I think it was intrusiveness and general hassle rather than > anything fundamental. > > > > > ... > > > > +/* Prevent people trying to call kunmap_atomic() as if it were kunmap() */ > > +struct __kunmap_atomic_dummy {}; > > +#define kunmap_atomic(addr, idx) do { \ > > + BUILD_BUG_ON( \ > > + __builtin_types_compatible_p(typeof(addr), struct page *) && \ > > + !__builtin_types_compatible_p(typeof(addr), struct __kunmap_atomic_dummy *)); \ > > + kunmap_atomic_notypecheck((addr), (idx)); \ > > + } while (0) > > <looks around> > > OK, it seems that __builtin_types_compatible_p() is supported on all > approved gcc versions. > > We have a little __same_type() helper for this. __must_be_array() > should be using it, too. Yep... but I think BUILD_BUG_ON(__same_type((addr), struct page *)); is sufficient; void * is not compatible in my quick tests here. Andrew, want to take this? Subject: Use __same_type() in __must_be_array() We should use the __same_type() helper in __must_be_array(). Reported-by: Andrew Morton <akpm(a)linux-foundation.org> Signed-off-by: Rusty Russell <rusty(a)rustcorp.com.au> diff --git a/include/linux/compiler-gcc.h b/include/linux/compiler-gcc.h --- a/include/linux/compiler-gcc.h +++ b/include/linux/compiler-gcc.h @@ -35,8 +35,7 @@ (typeof(ptr)) (__ptr + (off)); }) /* &a[0] degrades to a pointer: a different type from an array */ -#define __must_be_array(a) \ - BUILD_BUG_ON_ZERO(__builtin_types_compatible_p(typeof(a), typeof(&a[0]))) +#define __must_be_array(a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0])) /* * Force always-inline if the user requests it so via the .config, -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo(a)vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/
From: Cesar Eduardo Barros on 31 May 2010 06:50
Em 31-05-2010 07:15, Rusty Russell escreveu: > On Sun, 30 May 2010 01:12:56 pm Andrew Morton wrote: >> On Fri, 28 May 2010 07:53:13 -0300 Cesar Eduardo Barros<cesarb(a)cesarb.net> wrote: >>> +/* Prevent people trying to call kunmap_atomic() as if it were kunmap() */ >>> +struct __kunmap_atomic_dummy {}; >>> +#define kunmap_atomic(addr, idx) do { \ >>> + BUILD_BUG_ON( \ >>> + __builtin_types_compatible_p(typeof(addr), struct page *)&& \ >>> + !__builtin_types_compatible_p(typeof(addr), struct __kunmap_atomic_dummy *)); \ >>> + kunmap_atomic_notypecheck((addr), (idx)); \ >>> + } while (0) >> >> We have a little __same_type() helper for this. __must_be_array() >> should be using it, too. > > Yep... but I think BUILD_BUG_ON(__same_type((addr), struct page *)); is > sufficient; void * is not compatible in my quick tests here. That is what I get for only reading the manual instead of testing :( (I only tested the completed patch, not each step along the way.) I will try it later today and make a new patch if it works as expected. -- Cesar Eduardo Barros cesarb(a)cesarb.net cesar.barros(a)gmail.com -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo(a)vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/ |