diff mbox series

drm/i915/buddy: document the unused header bits

Message ID 20210122154547.502685-1-matthew.auld@intel.com (mailing list archive)
State New, archived
Headers show
Series drm/i915/buddy: document the unused header bits | expand

Commit Message

Matthew Auld Jan. 22, 2021, 3:45 p.m. UTC
The largest possible order is (63-12), given that our min chunk size is
4K. With that we should only need at most 6 bits to represent all
possible orders, giving us back 4 bits for other potential uses. Include
a simple selftest to verify this.

Signed-off-by: Matthew Auld <matthew.auld@intel.com>
---
 drivers/gpu/drm/i915/i915_buddy.c           |  3 ++
 drivers/gpu/drm/i915/i915_buddy.h           |  7 ++-
 drivers/gpu/drm/i915/selftests/i915_buddy.c | 47 +++++++++++++++++++++
 3 files changed, 55 insertions(+), 2 deletions(-)

Comments

Chris Wilson Jan. 22, 2021, 4:14 p.m. UTC | #1
Quoting Matthew Auld (2021-01-22 15:45:46)
> The largest possible order is (63-12), given that our min chunk size is
> 4K. With that we should only need at most 6 bits to represent all
> possible orders, giving us back 4 bits for other potential uses. Include
> a simple selftest to verify this.
> 
> Signed-off-by: Matthew Auld <matthew.auld@intel.com>
> ---
>  drivers/gpu/drm/i915/i915_buddy.c           |  3 ++
>  drivers/gpu/drm/i915/i915_buddy.h           |  7 ++-
>  drivers/gpu/drm/i915/selftests/i915_buddy.c | 47 +++++++++++++++++++++
>  3 files changed, 55 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/gpu/drm/i915/i915_buddy.c b/drivers/gpu/drm/i915/i915_buddy.c
> index 20babbdb297d..650613ae2a7b 100644
> --- a/drivers/gpu/drm/i915/i915_buddy.c
> +++ b/drivers/gpu/drm/i915/i915_buddy.c
> @@ -56,6 +56,9 @@ static struct i915_buddy_block *i915_block_alloc(struct i915_buddy_block *parent
>         block->header |= order;
>         block->parent = parent;
>  
> +       GEM_BUG_ON(block->header & I915_BUDDY_HEADER_UNUSED);

Ok...

> +       GEM_BUG_ON(order > I915_BUDDY_MAX_ORDER);

But this is really a contract on the incoming parameter and so should be
at the start of the function.

GEM_BUG_ON(block->header...) is a contract on our output.

> +
>         return block;
>  }
>  
> diff --git a/drivers/gpu/drm/i915/i915_buddy.h b/drivers/gpu/drm/i915/i915_buddy.h
> index ed41f3507cdc..d37e03587637 100644
> --- a/drivers/gpu/drm/i915/i915_buddy.h
> +++ b/drivers/gpu/drm/i915/i915_buddy.h
> @@ -15,7 +15,9 @@ struct i915_buddy_block {
>  #define   I915_BUDDY_ALLOCATED    (1 << 10)
>  #define   I915_BUDDY_FREE         (2 << 10)
>  #define   I915_BUDDY_SPLIT        (3 << 10)
> -#define I915_BUDDY_HEADER_ORDER  GENMASK_ULL(9, 0)
> +/* Free to be used, if needed in the future */
> +#define I915_BUDDY_HEADER_UNUSED GENMASK_ULL(9, 6)
> +#define I915_BUDDY_HEADER_ORDER  GENMASK_ULL(5, 0)
>         u64 header;
>  
>         struct i915_buddy_block *left;
> @@ -34,7 +36,8 @@ struct i915_buddy_block {
>         struct list_head tmp_link;
>  };
>  
> -#define I915_BUDDY_MAX_ORDER  I915_BUDDY_HEADER_ORDER
> +/* Order-zero must be at least PAGE_SIZE */
> +#define I915_BUDDY_MAX_ORDER (63 - 12)

Can we replace 12 with an ilog2() ?

PAGE_SIZE is mentioned as the lower limit, so PAGE_SHIFT?

>  /*
>   * Binary Buddy System.
> diff --git a/drivers/gpu/drm/i915/selftests/i915_buddy.c b/drivers/gpu/drm/i915/selftests/i915_buddy.c
> index 632b912b0bc9..39c470a0e28d 100644
> --- a/drivers/gpu/drm/i915/selftests/i915_buddy.c
> +++ b/drivers/gpu/drm/i915/selftests/i915_buddy.c
> @@ -727,6 +727,52 @@ static int igt_buddy_alloc_range(void *arg)
>         return err;
>  }
>  
> +static int igt_buddy_alloc_limit(void *arg)
> +{
> +       struct i915_buddy_block *block;
> +       struct i915_buddy_mm mm;
> +       const u64 size = U64_MAX;
> +       int err;
> +
> +       err = i915_buddy_init(&mm, size, PAGE_SIZE);

Create the biggest region possible.

> +       if (err)
> +               return err;
> +
> +       if (mm.max_order != I915_BUDDY_MAX_ORDER) {

Check that we made it as big as possible.

> +               pr_err("mm.max_order(%d) != %d\n",
> +                      mm.max_order, I915_BUDDY_MAX_ORDER);
> +               err = -EINVAL;
> +               goto out_fini;
> +       }
> +
> +       block = i915_buddy_alloc(&mm, mm.max_order);

Allocate the largest single block.

> +       if (IS_ERR(block)) {
> +               err = PTR_ERR(block);
> +               goto out_fini;
> +       }
> +
> +       if (i915_buddy_block_order(block) != I915_BUDDY_MAX_ORDER) {

Check that it is as large we tried to allocate... But it should be
mm.max_order to check the actual limits in place.

> +               pr_err("block order(%d) != %d\n",
> +                      i915_buddy_block_order(block), I915_BUDDY_MAX_ORDER);
> +               err = -EINVAL;
> +               goto out_free;
> +       }
> +
> +       if (i915_buddy_block_size(&mm, block) != rounddown_pow_of_two(size)) {

Which is the largest buddy block within the range.

Hmm, this I don't think follows the I915_BUDDY_MAX_ORDER constraint.
What if we reduce the MAX_ORDER to be say 48? Then the buddy block will
correctly be (1<<48). So I think you just want size() == BIT_ULL(mm.max_order)

With those questions addressed however you see fit,
Reviewed-by: Chris Wilson <chris@chris-wilson.co.uk>
-Chris
kernel test robot Jan. 25, 2021, 7:40 a.m. UTC | #2
Hi Matthew,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on drm-intel/for-linux-next]
[also build test WARNING on drm-tip/drm-tip v5.11-rc5 next-20210122]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Matthew-Auld/drm-i915-buddy-document-the-unused-header-bits/20210125-111101
base:   git://anongit.freedesktop.org/drm-intel for-linux-next
config: i386-randconfig-a012-20210125 (attached as .config)
compiler: gcc-9 (Debian 9.3.0-20) 9.3.0
reproduce (this is a W=1 build):
        # https://github.com/0day-ci/linux/commit/9d12a23788459bd678c0f5979acaaf94a88d808c
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review Matthew-Auld/drm-i915-buddy-document-the-unused-header-bits/20210125-111101
        git checkout 9d12a23788459bd678c0f5979acaaf94a88d808c
        # save the attached .config to linux build tree
        make W=1 ARCH=i386 

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>

All warnings (new ones prefixed by >>):

   In file included from include/asm-generic/bug.h:5,
                    from arch/x86/include/asm/bug.h:93,
                    from include/linux/bug.h:5,
                    from include/linux/mmdebug.h:5,
                    from include/linux/gfp.h:5,
                    from include/linux/slab.h:15,
                    from include/linux/kmemleak.h:12,
                    from drivers/gpu/drm/i915/i915_buddy.c:6:
   drivers/gpu/drm/i915/selftests/i915_buddy.c: In function 'igt_buddy_alloc_limit':
>> include/linux/log2.h:194:8: warning: left shift count >= width of type [-Wshift-count-overflow]
     194 |   (1UL << ilog2(n))) :  \
         |        ^~
   include/linux/compiler.h:58:52: note: in definition of macro '__trace_if_var'
      58 | #define __trace_if_var(cond) (__builtin_constant_p(cond) ? (cond) : __trace_if_value(cond))
         |                                                    ^~~~
   drivers/gpu/drm/i915/selftests/i915_buddy.c:761:2: note: in expansion of macro 'if'
     761 |  if (i915_buddy_block_size(&mm, block) != rounddown_pow_of_two(size)) {
         |  ^~
   drivers/gpu/drm/i915/selftests/i915_buddy.c:761:43: note: in expansion of macro 'rounddown_pow_of_two'
     761 |  if (i915_buddy_block_size(&mm, block) != rounddown_pow_of_two(size)) {
         |                                           ^~~~~~~~~~~~~~~~~~~~
>> include/linux/log2.h:194:8: warning: left shift count >= width of type [-Wshift-count-overflow]
     194 |   (1UL << ilog2(n))) :  \
         |        ^~
   include/linux/compiler.h:69:3: note: in definition of macro '__trace_if_value'
      69 |  (cond) ?     \
         |   ^~~~
   include/linux/compiler.h:56:28: note: in expansion of macro '__trace_if_var'
      56 | #define if(cond, ...) if ( __trace_if_var( !!(cond , ## __VA_ARGS__) ) )
         |                            ^~~~~~~~~~~~~~
   drivers/gpu/drm/i915/selftests/i915_buddy.c:761:2: note: in expansion of macro 'if'
     761 |  if (i915_buddy_block_size(&mm, block) != rounddown_pow_of_two(size)) {
         |  ^~
   drivers/gpu/drm/i915/selftests/i915_buddy.c:761:43: note: in expansion of macro 'rounddown_pow_of_two'
     761 |  if (i915_buddy_block_size(&mm, block) != rounddown_pow_of_two(size)) {
         |                                           ^~~~~~~~~~~~~~~~~~~~
   In file included from include/linux/kernel.h:12,
                    from include/asm-generic/bug.h:20,
                    from arch/x86/include/asm/bug.h:93,
                    from include/linux/bug.h:5,
                    from include/linux/mmdebug.h:5,
                    from include/linux/gfp.h:5,
                    from include/linux/slab.h:15,
                    from include/linux/kmemleak.h:12,
                    from drivers/gpu/drm/i915/i915_buddy.c:6:
>> include/linux/log2.h:194:8: warning: left shift count >= width of type [-Wshift-count-overflow]
     194 |   (1UL << ilog2(n))) :  \
         |        ^~
   include/linux/printk.h:343:33: note: in expansion of macro 'rounddown_pow_of_two'
     343 |  printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
         |                                 ^~~~~~~~~~~
   drivers/gpu/drm/i915/selftests/i915_buddy.c:762:3: note: in expansion of macro 'pr_err'
     762 |   pr_err("block size(%llu) != %lu\n",
         |   ^~~~~~


vim +194 include/linux/log2.h

b311e921b385b5a Robert P. J. Day 2007-10-16   69  
f0d1b0b30d250a0 David Howells    2006-12-08   70  /**
dbef91ec5482239 Martin Wilck     2018-04-18   71   * const_ilog2 - log base 2 of 32-bit or a 64-bit constant unsigned value
a1c4d24e02d093e Randy Dunlap     2017-09-30   72   * @n: parameter
f0d1b0b30d250a0 David Howells    2006-12-08   73   *
dbef91ec5482239 Martin Wilck     2018-04-18   74   * Use this where sparse expects a true constant expression, e.g. for array
dbef91ec5482239 Martin Wilck     2018-04-18   75   * indices.
f0d1b0b30d250a0 David Howells    2006-12-08   76   */
dbef91ec5482239 Martin Wilck     2018-04-18   77  #define const_ilog2(n)				\
f0d1b0b30d250a0 David Howells    2006-12-08   78  (						\
f0d1b0b30d250a0 David Howells    2006-12-08   79  	__builtin_constant_p(n) ? (		\
474c90156c8dcc2 Linus Torvalds   2017-03-02   80  		(n) < 2 ? 0 :			\
f0d1b0b30d250a0 David Howells    2006-12-08   81  		(n) & (1ULL << 63) ? 63 :	\
f0d1b0b30d250a0 David Howells    2006-12-08   82  		(n) & (1ULL << 62) ? 62 :	\
f0d1b0b30d250a0 David Howells    2006-12-08   83  		(n) & (1ULL << 61) ? 61 :	\
f0d1b0b30d250a0 David Howells    2006-12-08   84  		(n) & (1ULL << 60) ? 60 :	\
f0d1b0b30d250a0 David Howells    2006-12-08   85  		(n) & (1ULL << 59) ? 59 :	\
f0d1b0b30d250a0 David Howells    2006-12-08   86  		(n) & (1ULL << 58) ? 58 :	\
f0d1b0b30d250a0 David Howells    2006-12-08   87  		(n) & (1ULL << 57) ? 57 :	\
f0d1b0b30d250a0 David Howells    2006-12-08   88  		(n) & (1ULL << 56) ? 56 :	\
f0d1b0b30d250a0 David Howells    2006-12-08   89  		(n) & (1ULL << 55) ? 55 :	\
f0d1b0b30d250a0 David Howells    2006-12-08   90  		(n) & (1ULL << 54) ? 54 :	\
f0d1b0b30d250a0 David Howells    2006-12-08   91  		(n) & (1ULL << 53) ? 53 :	\
f0d1b0b30d250a0 David Howells    2006-12-08   92  		(n) & (1ULL << 52) ? 52 :	\
f0d1b0b30d250a0 David Howells    2006-12-08   93  		(n) & (1ULL << 51) ? 51 :	\
f0d1b0b30d250a0 David Howells    2006-12-08   94  		(n) & (1ULL << 50) ? 50 :	\
f0d1b0b30d250a0 David Howells    2006-12-08   95  		(n) & (1ULL << 49) ? 49 :	\
f0d1b0b30d250a0 David Howells    2006-12-08   96  		(n) & (1ULL << 48) ? 48 :	\
f0d1b0b30d250a0 David Howells    2006-12-08   97  		(n) & (1ULL << 47) ? 47 :	\
f0d1b0b30d250a0 David Howells    2006-12-08   98  		(n) & (1ULL << 46) ? 46 :	\
f0d1b0b30d250a0 David Howells    2006-12-08   99  		(n) & (1ULL << 45) ? 45 :	\
f0d1b0b30d250a0 David Howells    2006-12-08  100  		(n) & (1ULL << 44) ? 44 :	\
f0d1b0b30d250a0 David Howells    2006-12-08  101  		(n) & (1ULL << 43) ? 43 :	\
f0d1b0b30d250a0 David Howells    2006-12-08  102  		(n) & (1ULL << 42) ? 42 :	\
f0d1b0b30d250a0 David Howells    2006-12-08  103  		(n) & (1ULL << 41) ? 41 :	\
f0d1b0b30d250a0 David Howells    2006-12-08  104  		(n) & (1ULL << 40) ? 40 :	\
f0d1b0b30d250a0 David Howells    2006-12-08  105  		(n) & (1ULL << 39) ? 39 :	\
f0d1b0b30d250a0 David Howells    2006-12-08  106  		(n) & (1ULL << 38) ? 38 :	\
f0d1b0b30d250a0 David Howells    2006-12-08  107  		(n) & (1ULL << 37) ? 37 :	\
f0d1b0b30d250a0 David Howells    2006-12-08  108  		(n) & (1ULL << 36) ? 36 :	\
f0d1b0b30d250a0 David Howells    2006-12-08  109  		(n) & (1ULL << 35) ? 35 :	\
f0d1b0b30d250a0 David Howells    2006-12-08  110  		(n) & (1ULL << 34) ? 34 :	\
f0d1b0b30d250a0 David Howells    2006-12-08  111  		(n) & (1ULL << 33) ? 33 :	\
f0d1b0b30d250a0 David Howells    2006-12-08  112  		(n) & (1ULL << 32) ? 32 :	\
f0d1b0b30d250a0 David Howells    2006-12-08  113  		(n) & (1ULL << 31) ? 31 :	\
f0d1b0b30d250a0 David Howells    2006-12-08  114  		(n) & (1ULL << 30) ? 30 :	\
f0d1b0b30d250a0 David Howells    2006-12-08  115  		(n) & (1ULL << 29) ? 29 :	\
f0d1b0b30d250a0 David Howells    2006-12-08  116  		(n) & (1ULL << 28) ? 28 :	\
f0d1b0b30d250a0 David Howells    2006-12-08  117  		(n) & (1ULL << 27) ? 27 :	\
f0d1b0b30d250a0 David Howells    2006-12-08  118  		(n) & (1ULL << 26) ? 26 :	\
f0d1b0b30d250a0 David Howells    2006-12-08  119  		(n) & (1ULL << 25) ? 25 :	\
f0d1b0b30d250a0 David Howells    2006-12-08  120  		(n) & (1ULL << 24) ? 24 :	\
f0d1b0b30d250a0 David Howells    2006-12-08  121  		(n) & (1ULL << 23) ? 23 :	\
f0d1b0b30d250a0 David Howells    2006-12-08  122  		(n) & (1ULL << 22) ? 22 :	\
f0d1b0b30d250a0 David Howells    2006-12-08  123  		(n) & (1ULL << 21) ? 21 :	\
f0d1b0b30d250a0 David Howells    2006-12-08  124  		(n) & (1ULL << 20) ? 20 :	\
f0d1b0b30d250a0 David Howells    2006-12-08  125  		(n) & (1ULL << 19) ? 19 :	\
f0d1b0b30d250a0 David Howells    2006-12-08  126  		(n) & (1ULL << 18) ? 18 :	\
f0d1b0b30d250a0 David Howells    2006-12-08  127  		(n) & (1ULL << 17) ? 17 :	\
f0d1b0b30d250a0 David Howells    2006-12-08  128  		(n) & (1ULL << 16) ? 16 :	\
f0d1b0b30d250a0 David Howells    2006-12-08  129  		(n) & (1ULL << 15) ? 15 :	\
f0d1b0b30d250a0 David Howells    2006-12-08  130  		(n) & (1ULL << 14) ? 14 :	\
f0d1b0b30d250a0 David Howells    2006-12-08  131  		(n) & (1ULL << 13) ? 13 :	\
f0d1b0b30d250a0 David Howells    2006-12-08  132  		(n) & (1ULL << 12) ? 12 :	\
f0d1b0b30d250a0 David Howells    2006-12-08  133  		(n) & (1ULL << 11) ? 11 :	\
f0d1b0b30d250a0 David Howells    2006-12-08  134  		(n) & (1ULL << 10) ? 10 :	\
f0d1b0b30d250a0 David Howells    2006-12-08  135  		(n) & (1ULL <<  9) ?  9 :	\
f0d1b0b30d250a0 David Howells    2006-12-08  136  		(n) & (1ULL <<  8) ?  8 :	\
f0d1b0b30d250a0 David Howells    2006-12-08  137  		(n) & (1ULL <<  7) ?  7 :	\
f0d1b0b30d250a0 David Howells    2006-12-08  138  		(n) & (1ULL <<  6) ?  6 :	\
f0d1b0b30d250a0 David Howells    2006-12-08  139  		(n) & (1ULL <<  5) ?  5 :	\
f0d1b0b30d250a0 David Howells    2006-12-08  140  		(n) & (1ULL <<  4) ?  4 :	\
f0d1b0b30d250a0 David Howells    2006-12-08  141  		(n) & (1ULL <<  3) ?  3 :	\
f0d1b0b30d250a0 David Howells    2006-12-08  142  		(n) & (1ULL <<  2) ?  2 :	\
474c90156c8dcc2 Linus Torvalds   2017-03-02  143  		1) :				\
dbef91ec5482239 Martin Wilck     2018-04-18  144  	-1)
dbef91ec5482239 Martin Wilck     2018-04-18  145  
dbef91ec5482239 Martin Wilck     2018-04-18  146  /**
dbef91ec5482239 Martin Wilck     2018-04-18  147   * ilog2 - log base 2 of 32-bit or a 64-bit unsigned value
dbef91ec5482239 Martin Wilck     2018-04-18  148   * @n: parameter
dbef91ec5482239 Martin Wilck     2018-04-18  149   *
dbef91ec5482239 Martin Wilck     2018-04-18  150   * constant-capable log of base 2 calculation
dbef91ec5482239 Martin Wilck     2018-04-18  151   * - this can be used to initialise global variables from constant data, hence
dbef91ec5482239 Martin Wilck     2018-04-18  152   * the massive ternary operator construction
dbef91ec5482239 Martin Wilck     2018-04-18  153   *
dbef91ec5482239 Martin Wilck     2018-04-18  154   * selects the appropriately-sized optimised version depending on sizeof(n)
dbef91ec5482239 Martin Wilck     2018-04-18  155   */
dbef91ec5482239 Martin Wilck     2018-04-18  156  #define ilog2(n) \
dbef91ec5482239 Martin Wilck     2018-04-18  157  ( \
dbef91ec5482239 Martin Wilck     2018-04-18  158  	__builtin_constant_p(n) ?	\
2f78788b55baa34 Jakub Jelinek    2020-12-15  159  	((n) < 2 ? 0 :			\
2f78788b55baa34 Jakub Jelinek    2020-12-15  160  	 63 - __builtin_clzll(n)) :	\
f0d1b0b30d250a0 David Howells    2006-12-08  161  	(sizeof(n) <= 4) ?		\
f0d1b0b30d250a0 David Howells    2006-12-08  162  	__ilog2_u32(n) :		\
f0d1b0b30d250a0 David Howells    2006-12-08  163  	__ilog2_u64(n)			\
f0d1b0b30d250a0 David Howells    2006-12-08  164   )
f0d1b0b30d250a0 David Howells    2006-12-08  165  
312a0c170945b49 David Howells    2006-12-08  166  /**
312a0c170945b49 David Howells    2006-12-08  167   * roundup_pow_of_two - round the given value up to nearest power of two
a1c4d24e02d093e Randy Dunlap     2017-09-30  168   * @n: parameter
312a0c170945b49 David Howells    2006-12-08  169   *
6fb189c2a4f3bea Robert P. J. Day 2007-02-17  170   * round the given value up to the nearest power of two
312a0c170945b49 David Howells    2006-12-08  171   * - the result is undefined when n == 0
312a0c170945b49 David Howells    2006-12-08  172   * - this can be used to initialise global variables from constant data
312a0c170945b49 David Howells    2006-12-08  173   */
312a0c170945b49 David Howells    2006-12-08  174  #define roundup_pow_of_two(n)			\
312a0c170945b49 David Howells    2006-12-08  175  (						\
312a0c170945b49 David Howells    2006-12-08  176  	__builtin_constant_p(n) ? (		\
428fc0aff4e5939 Jason Gunthorpe  2020-09-04  177  		((n) == 1) ? 1 :		\
312a0c170945b49 David Howells    2006-12-08  178  		(1UL << (ilog2((n) - 1) + 1))	\
312a0c170945b49 David Howells    2006-12-08  179  				   ) :		\
312a0c170945b49 David Howells    2006-12-08  180  	__roundup_pow_of_two(n)			\
312a0c170945b49 David Howells    2006-12-08  181   )
312a0c170945b49 David Howells    2006-12-08  182  
b311e921b385b5a Robert P. J. Day 2007-10-16  183  /**
b311e921b385b5a Robert P. J. Day 2007-10-16  184   * rounddown_pow_of_two - round the given value down to nearest power of two
a1c4d24e02d093e Randy Dunlap     2017-09-30  185   * @n: parameter
b311e921b385b5a Robert P. J. Day 2007-10-16  186   *
b311e921b385b5a Robert P. J. Day 2007-10-16  187   * round the given value down to the nearest power of two
b311e921b385b5a Robert P. J. Day 2007-10-16  188   * - the result is undefined when n == 0
b311e921b385b5a Robert P. J. Day 2007-10-16  189   * - this can be used to initialise global variables from constant data
b311e921b385b5a Robert P. J. Day 2007-10-16  190   */
b311e921b385b5a Robert P. J. Day 2007-10-16  191  #define rounddown_pow_of_two(n)			\
b311e921b385b5a Robert P. J. Day 2007-10-16  192  (						\
b311e921b385b5a Robert P. J. Day 2007-10-16  193  	__builtin_constant_p(n) ? (		\
b311e921b385b5a Robert P. J. Day 2007-10-16 @194  		(1UL << ilog2(n))) :		\
b311e921b385b5a Robert P. J. Day 2007-10-16  195  	__rounddown_pow_of_two(n)		\
b311e921b385b5a Robert P. J. Day 2007-10-16  196   )
b311e921b385b5a Robert P. J. Day 2007-10-16  197  

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org
diff mbox series

Patch

diff --git a/drivers/gpu/drm/i915/i915_buddy.c b/drivers/gpu/drm/i915/i915_buddy.c
index 20babbdb297d..650613ae2a7b 100644
--- a/drivers/gpu/drm/i915/i915_buddy.c
+++ b/drivers/gpu/drm/i915/i915_buddy.c
@@ -56,6 +56,9 @@  static struct i915_buddy_block *i915_block_alloc(struct i915_buddy_block *parent
 	block->header |= order;
 	block->parent = parent;
 
+	GEM_BUG_ON(block->header & I915_BUDDY_HEADER_UNUSED);
+	GEM_BUG_ON(order > I915_BUDDY_MAX_ORDER);
+
 	return block;
 }
 
diff --git a/drivers/gpu/drm/i915/i915_buddy.h b/drivers/gpu/drm/i915/i915_buddy.h
index ed41f3507cdc..d37e03587637 100644
--- a/drivers/gpu/drm/i915/i915_buddy.h
+++ b/drivers/gpu/drm/i915/i915_buddy.h
@@ -15,7 +15,9 @@  struct i915_buddy_block {
 #define   I915_BUDDY_ALLOCATED	   (1 << 10)
 #define   I915_BUDDY_FREE	   (2 << 10)
 #define   I915_BUDDY_SPLIT	   (3 << 10)
-#define I915_BUDDY_HEADER_ORDER  GENMASK_ULL(9, 0)
+/* Free to be used, if needed in the future */
+#define I915_BUDDY_HEADER_UNUSED GENMASK_ULL(9, 6)
+#define I915_BUDDY_HEADER_ORDER  GENMASK_ULL(5, 0)
 	u64 header;
 
 	struct i915_buddy_block *left;
@@ -34,7 +36,8 @@  struct i915_buddy_block {
 	struct list_head tmp_link;
 };
 
-#define I915_BUDDY_MAX_ORDER  I915_BUDDY_HEADER_ORDER
+/* Order-zero must be at least PAGE_SIZE */
+#define I915_BUDDY_MAX_ORDER (63 - 12)
 
 /*
  * Binary Buddy System.
diff --git a/drivers/gpu/drm/i915/selftests/i915_buddy.c b/drivers/gpu/drm/i915/selftests/i915_buddy.c
index 632b912b0bc9..39c470a0e28d 100644
--- a/drivers/gpu/drm/i915/selftests/i915_buddy.c
+++ b/drivers/gpu/drm/i915/selftests/i915_buddy.c
@@ -727,6 +727,52 @@  static int igt_buddy_alloc_range(void *arg)
 	return err;
 }
 
+static int igt_buddy_alloc_limit(void *arg)
+{
+	struct i915_buddy_block *block;
+	struct i915_buddy_mm mm;
+	const u64 size = U64_MAX;
+	int err;
+
+	err = i915_buddy_init(&mm, size, PAGE_SIZE);
+	if (err)
+		return err;
+
+	if (mm.max_order != I915_BUDDY_MAX_ORDER) {
+		pr_err("mm.max_order(%d) != %d\n",
+		       mm.max_order, I915_BUDDY_MAX_ORDER);
+		err = -EINVAL;
+		goto out_fini;
+	}
+
+	block = i915_buddy_alloc(&mm, mm.max_order);
+	if (IS_ERR(block)) {
+		err = PTR_ERR(block);
+		goto out_fini;
+	}
+
+	if (i915_buddy_block_order(block) != I915_BUDDY_MAX_ORDER) {
+		pr_err("block order(%d) != %d\n",
+		       i915_buddy_block_order(block), I915_BUDDY_MAX_ORDER);
+		err = -EINVAL;
+		goto out_free;
+	}
+
+	if (i915_buddy_block_size(&mm, block) != rounddown_pow_of_two(size)) {
+		pr_err("block size(%llu) != %lu\n",
+		       i915_buddy_block_size(&mm, block),
+		       rounddown_pow_of_two(size));
+		err = -EINVAL;
+		goto out_free;
+	}
+
+out_free:
+	i915_buddy_free(&mm, block);
+out_fini:
+	i915_buddy_fini(&mm);
+	return err;
+}
+
 int i915_buddy_mock_selftests(void)
 {
 	static const struct i915_subtest tests[] = {
@@ -735,6 +781,7 @@  int i915_buddy_mock_selftests(void)
 		SUBTEST(igt_buddy_alloc_pathological),
 		SUBTEST(igt_buddy_alloc_smoke),
 		SUBTEST(igt_buddy_alloc_range),
+		SUBTEST(igt_buddy_alloc_limit),
 	};
 
 	return i915_subtests(tests, NULL);