On Mon, 9 Apr 2018, Sebastian Ott wrote:
> On Fri, 6 Apr 2018, Kees Cook wrote:
> > On Fri, Apr 6, 2018 at 2:47 AM, Sebastian Ott <seb...@linux.vnet.ibm.com> 
> > wrote:
> > > Today's kernel oopsed on s390. Bisect points to:
> > > 3c8ba0d61d04 ("kernel.h: Retain constant expression output for 
> > > max()/min()")
> > >
> > > [    1.898277] dasd-eckd 0.0.3304: DASD with 4 KB/block, 21636720 KB 
> > > total size, 48 KB/track, compatible disk layout
> > > [    1.898308] ------------[ cut here ]------------
> > > [    1.898310] kernel BUG at block/bio.c:1798!
> > 
> > Well that's extremely bad. :(
> 
> What happened is that the bio build by the partition detection code was
> attempted to be split by the block layer because the block queue had a
> max_sector setting of 0. blk_queue_max_hw_sectors uses min_not_zero.
> 
> Both of the following return 0 on my machine:
> +       pr_warn("%u\n", min_not_zero(100, 1000));
> +       pr_warn("%u\n", min_not_zero(1000, 100));
> 
> So, we now know what failed...the question is why?

I copied these macros to a userspace program to easily test it on other
machines/compilers. ....maybe I did something wrong but min_not_zero did
not work - even on fedora on an x86 laptop.

Sebastian
#include <stdio.h>

#define __typecheck(x, y)                               \
        (!!(sizeof((typeof(x) *)1 == (typeof(y) *)1)))

#define __is_constexpr(x)                                               \
        (sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8)))

#define __no_side_effects(x, y)                         \
        (__is_constexpr(x) && __is_constexpr(y))

#define __safe_cmp(x, y)                                                \
        (__typecheck(x, y) && __no_side_effects(x, y))

#define __cmp(x, y, op)        ((x) op (y) ? (x) : (y))

#define __cmp_once(x, y, op) ({                 \
                        typeof(x) __x = (x);    \
                        typeof(y) __y = (y);    \
                        __cmp(__x, __y, op); })

#define __careful_cmp(x, y, op)                                         \
        __builtin_choose_expr(__safe_cmp(x, y),                         \
                              __cmp(x, y, op), __cmp_once(x, y, op))

#define min(x, y)      __careful_cmp(x, y, <)
#define max(x, y)      __careful_cmp(x, y, >)

#define min_t(type, x, y)      __careful_cmp((type)(x), (type)(y), <)
#define max_t(type, x, y)      __careful_cmp((type)(x), (type)(y), >)


#define min_not_zero(x, y) ({                                           \
                        typeof(x) __x = (x);                            \
                        typeof(y) __y = (y);                            \
                        __x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); })

int main()
{
        printf("test %u\n", min_not_zero(100, 1000));
        return 0;
}

Reply via email to