<div dir="ltr"><div dir="ltr">Hi Christian:<div><br></div><div>The warning is from the kernel test robot, I quote it here. The key point is that vm may be used in radeon_vm_fini(rdev, vm) without initialization. Although it is not possible in practice, I still add initializations to silence the warning of llvm.</div><div><br></div><div><div dir="ltr" class="gmail_attr">---------- Forwarded message ---------<br>From: <strong class="gmail_sendername" dir="auto">kernel test robot</strong> <span dir="auto"><<a href="mailto:lkp@intel.com">lkp@intel.com</a>></span><br>Date: Wed, Dec 1, 2021 at 4:32 AM<br>Subject: drivers/gpu/drm/radeon/radeon_kms.c:672:7: warning: variable 'vm' is used uninitialized whenever 'if' condition is false<br>To: Zhou Qingyang <<a href="mailto:zhou1615@umn.edu">zhou1615@umn.edu</a>><br>Cc: <<a href="mailto:llvm@lists.linux.dev">llvm@lists.linux.dev</a>>, <<a href="mailto:kbuild-all@lists.01.org">kbuild-all@lists.01.org</a>>, <<a href="mailto:linux-kernel@vger.kernel.org">linux-kernel@vger.kernel.org</a>>, 0day robot <<a href="mailto:lkp@intel.com">lkp@intel.com</a>><br></div><br><br>tree:   <a href="https://github.com/0day-ci/linux/commits/UPDATE-20211130-233655/Zhou-Qingyang/drm-radeon-radeon_kms-Fix-a-NULL-pointer-dereference-in-radeon_driver_open_kms/20211130-231509" rel="noreferrer" target="_blank">https://github.com/0day-ci/linux/commits/UPDATE-20211130-233655/Zhou-Qingyang/drm-radeon-radeon_kms-Fix-a-NULL-pointer-dereference-in-radeon_driver_open_kms/20211130-231509</a><br>head:   123fb3d217e89c4388fdb08b63991bac7c324219<br>commit: 123fb3d217e89c4388fdb08b63991bac7c324219 drm/radeon/radeon_kms: Fix a NULL pointer dereference in radeon_driver_open_kms()<br>date:   5 hours ago<br>config: mips-randconfig-r014-20211128 (<a href="https://download.01.org/0day-ci/archive/20211201/202112010420.xkXHciHS-lkp@intel.com/config" rel="noreferrer" target="_blank">https://download.01.org/0day-ci/archive/20211201/202112010420.xkXHciHS-lkp@intel.com/config</a>)<br>compiler: clang version 14.0.0 (<a href="https://github.com/llvm/llvm-project" rel="noreferrer" target="_blank">https://github.com/llvm/llvm-project</a> 25eb7fa01d7ebbe67648ea03841cda55b4239ab2)<br>reproduce (this is a W=1 build):<br>        wget <a href="https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross" rel="noreferrer" target="_blank">https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross</a> -O ~/bin/make.cross<br>        chmod +x ~/bin/make.cross<br>        # install mips cross compiling tool for clang build<br>        # apt-get install binutils-mips-linux-gnu<br>        # <a href="https://github.com/0day-ci/linux/commit/123fb3d217e89c4388fdb08b63991bac7c324219" rel="noreferrer" target="_blank">https://github.com/0day-ci/linux/commit/123fb3d217e89c4388fdb08b63991bac7c324219</a><br>        git remote add linux-review <a href="https://github.com/0day-ci/linux" rel="noreferrer" target="_blank">https://github.com/0day-ci/linux</a><br>        git fetch --no-tags linux-review UPDATE-20211130-233655/Zhou-Qingyang/drm-radeon-radeon_kms-Fix-a-NULL-pointer-dereference-in-radeon_driver_open_kms/20211130-231509<br>        git checkout 123fb3d217e89c4388fdb08b63991bac7c324219<br>        # save the config file to linux build tree<br>        mkdir build_dir<br>        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=mips SHELL=/bin/bash drivers/gpu/drm/radeon/<br><br>If you fix the issue, kindly add following tag as appropriate<br>Reported-by: kernel test robot <<a href="mailto:lkp@intel.com" target="_blank">lkp@intel.com</a>><br><br>All warnings (new ones prefixed by >>):<br><br>>> drivers/gpu/drm/radeon/radeon_kms.c:672:7: warning: variable 'vm' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized]<br>                   if (rdev->accel_working) {<br>                       ^~~~~~~~~~~~~~~~~~~<br>   drivers/gpu/drm/radeon/radeon_kms.c:705:23: note: uninitialized use occurs here<br>           radeon_vm_fini(rdev, vm);<br>                                ^~<br>   drivers/gpu/drm/radeon/radeon_kms.c:672:3: note: remove the 'if' if its condition is always true<br>                   if (rdev->accel_working) {<br>                   ^~~~~~~~~~~~~~~~~~~~~~~~~<br>   drivers/gpu/drm/radeon/radeon_kms.c:664:6: warning: variable 'vm' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized]<br>           if (rdev->family >= CHIP_CAYMAN) {<br>               ^~~~~~~~~~~~~~~~~~~~~~~~~~~<br>   drivers/gpu/drm/radeon/radeon_kms.c:705:23: note: uninitialized use occurs here<br>           radeon_vm_fini(rdev, vm);<br>                                ^~<br>   drivers/gpu/drm/radeon/radeon_kms.c:664:2: note: remove the 'if' if its condition is always true<br>           if (rdev->family >= CHIP_CAYMAN) {<br>           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br>   drivers/gpu/drm/radeon/radeon_kms.c:653:22: note: initialize the variable 'vm' to silence this warning<br>           struct radeon_vm *vm;<br>                               ^<br>                                = NULL<br>   2 warnings generated.<br><br><br>vim +672 drivers/gpu/drm/radeon/radeon_kms.c<br><br>771fe6b912fca54 Jerome Glisse      2009-06-05  638 <br>f482a1419545ded Alex Deucher       2012-07-17  639  /**<br>f482a1419545ded Alex Deucher       2012-07-17  640   * radeon_driver_open_kms - drm callback for open<br>f482a1419545ded Alex Deucher       2012-07-17  641   *<br>f482a1419545ded Alex Deucher       2012-07-17  642   * @dev: drm dev pointer<br>f482a1419545ded Alex Deucher       2012-07-17  643   * @file_priv: drm file<br>f482a1419545ded Alex Deucher       2012-07-17  644   *<br>f482a1419545ded Alex Deucher       2012-07-17  645   * On device open, init vm on cayman+ (all asics).<br>f482a1419545ded Alex Deucher       2012-07-17  646   * Returns 0 on success, error on failure.<br>f482a1419545ded Alex Deucher       2012-07-17  647   */<br>771fe6b912fca54 Jerome Glisse      2009-06-05  648  int radeon_driver_open_kms(struct drm_device *dev, struct drm_file *file_priv)<br>771fe6b912fca54 Jerome Glisse      2009-06-05  649  {<br>721604a15b934f0 Jerome Glisse      2012-01-05  650      struct radeon_device *rdev = dev->dev_private;<br>10ebc0bc09344ab Dave Airlie        2012-09-17  651      int r;<br>123fb3d217e89c4 Zhou Qingyang      2021-11-30  652      struct radeon_fpriv *fpriv;<br>123fb3d217e89c4 Zhou Qingyang      2021-11-30  653      struct radeon_vm *vm;<br>721604a15b934f0 Jerome Glisse      2012-01-05  654 <br>721604a15b934f0 Jerome Glisse      2012-01-05  655      file_priv->driver_priv = NULL;<br>721604a15b934f0 Jerome Glisse      2012-01-05  656 <br>10ebc0bc09344ab Dave Airlie        2012-09-17  657      r = pm_runtime_get_sync(dev->dev);<br>9fb10671011143d Aditya Pakki       2020-06-13  658      if (r < 0) {<br>9fb10671011143d Aditya Pakki       2020-06-13  659              pm_runtime_put_autosuspend(dev->dev);<br>10ebc0bc09344ab Dave Airlie        2012-09-17  660              return r;<br>9fb10671011143d Aditya Pakki       2020-06-13  661      }<br>10ebc0bc09344ab Dave Airlie        2012-09-17  662 <br>721604a15b934f0 Jerome Glisse      2012-01-05  663      /* new gpu have virtual address space support */<br>721604a15b934f0 Jerome Glisse      2012-01-05  664      if (rdev->family >= CHIP_CAYMAN) {<br>721604a15b934f0 Jerome Glisse      2012-01-05  665 <br>721604a15b934f0 Jerome Glisse      2012-01-05  666              fpriv = kzalloc(sizeof(*fpriv), GFP_KERNEL);<br>721604a15b934f0 Jerome Glisse      2012-01-05  667              if (unlikely(!fpriv)) {<br>32c59dc14b72803 Alex Deucher       2016-08-31  668                      r = -ENOMEM;<br>32c59dc14b72803 Alex Deucher       2016-08-31  669                      goto out_suspend;<br>721604a15b934f0 Jerome Glisse      2012-01-05  670              }<br>721604a15b934f0 Jerome Glisse      2012-01-05  671 <br>544143f9e01a60a Alex Deucher       2015-01-28 @672              if (rdev->accel_working) {<br>cc9e67e3d7000c1 Christian König    2014-07-18  673                      vm = &fpriv->vm;<br>cc9e67e3d7000c1 Christian König    2014-07-18  674                      r = radeon_vm_init(rdev, vm);<br>74073c9dd299056 Quentin Casasnovas 2014-03-18  675                      if (r) {<br>123fb3d217e89c4 Zhou Qingyang      2021-11-30  676                              goto out_fpriv;<br>74073c9dd299056 Quentin Casasnovas 2014-03-18  677                      }<br>d72d43cfc5847c1 Christian König    2012-10-09  678 <br>f1e3dc708aaadb9 Christian König    2014-02-20  679                      r = radeon_bo_reserve(rdev-><a href="http://ring_tmp_bo.bo/" rel="noreferrer" target="_blank">ring_tmp_bo.bo</a>, false);<br>74073c9dd299056 Quentin Casasnovas 2014-03-18  680                      if (r) {<br>123fb3d217e89c4 Zhou Qingyang      2021-11-30  681                              goto out_vm_fini;<br>74073c9dd299056 Quentin Casasnovas 2014-03-18  682                      }<br>f1e3dc708aaadb9 Christian König    2014-02-20  683 <br>d72d43cfc5847c1 Christian König    2012-10-09  684                      /* map the ib pool buffer read only into<br>d72d43cfc5847c1 Christian König    2012-10-09  685                       * virtual address space */<br>cc9e67e3d7000c1 Christian König    2014-07-18  686                      vm->ib_bo_va = radeon_vm_bo_add(rdev, vm,<br>d72d43cfc5847c1 Christian König    2012-10-09  687                                                      rdev-><a href="http://ring_tmp_bo.bo/" rel="noreferrer" target="_blank">ring_tmp_bo.bo</a>);<br>123fb3d217e89c4 Zhou Qingyang      2021-11-30  688                      if (!vm->ib_bo_va) {<br>123fb3d217e89c4 Zhou Qingyang      2021-11-30  689                              r = -ENOMEM;<br>123fb3d217e89c4 Zhou Qingyang      2021-11-30  690                              goto out_vm_fini;<br>123fb3d217e89c4 Zhou Qingyang      2021-11-30  691                      }<br>123fb3d217e89c4 Zhou Qingyang      2021-11-30  692 <br>cc9e67e3d7000c1 Christian König    2014-07-18  693                      r = radeon_vm_bo_set_addr(rdev, vm->ib_bo_va,<br>cc9e67e3d7000c1 Christian König    2014-07-18  694                                                RADEON_VA_IB_OFFSET,<br>d72d43cfc5847c1 Christian König    2012-10-09  695                                                RADEON_VM_PAGE_READABLE |<br>d72d43cfc5847c1 Christian König    2012-10-09  696                                                RADEON_VM_PAGE_SNOOPED);<br>721604a15b934f0 Jerome Glisse      2012-01-05  697                      if (r) {<br>123fb3d217e89c4 Zhou Qingyang      2021-11-30  698                              goto out_vm_fini;<br>721604a15b934f0 Jerome Glisse      2012-01-05  699                      }<br>24f47acc78b0ab5 Jérôme Glisse      2014-05-07  700              }<br>721604a15b934f0 Jerome Glisse      2012-01-05  701              file_priv->driver_priv = fpriv;<br>721604a15b934f0 Jerome Glisse      2012-01-05  702      }<br>10ebc0bc09344ab Dave Airlie        2012-09-17  703 <br>123fb3d217e89c4 Zhou Qingyang      2021-11-30  704  out_vm_fini:<br>123fb3d217e89c4 Zhou Qingyang      2021-11-30  705      radeon_vm_fini(rdev, vm);<br>123fb3d217e89c4 Zhou Qingyang      2021-11-30  706  out_fpriv:<br>123fb3d217e89c4 Zhou Qingyang      2021-11-30  707      kfree(fpriv);<br>32c59dc14b72803 Alex Deucher       2016-08-31  708  out_suspend:<br>10ebc0bc09344ab Dave Airlie        2012-09-17  709      pm_runtime_mark_last_busy(dev->dev);<br>10ebc0bc09344ab Dave Airlie        2012-09-17  710      pm_runtime_put_autosuspend(dev->dev);<br>32c59dc14b72803 Alex Deucher       2016-08-31  711      return r;<br>771fe6b912fca54 Jerome Glisse      2009-06-05  712  }<br>771fe6b912fca54 Jerome Glisse      2009-06-05  713 <br><br>---<br>0-DAY CI Kernel Test Service, Intel Corporation<br><a href="https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org" rel="noreferrer" target="_blank">https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org</a><br></div><div><br></div></div><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Dec 1, 2021 at 3:20 PM Christian König <<a href="mailto:christian.koenig@amd.com" target="_blank">christian.koenig@amd.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Am 01.12.21 um 04:22 schrieb Zhou Qingyang:<br>
> In radeon_driver_open_kms(), radeon_vm_bo_add() is assigned to<br>
> vm->ib_bo_va and passes and used in radeon_vm_bo_set_addr(). In<br>
> radeon_vm_bo_set_addr(), there is a dereference of vm->ib_bo_va,<br>
> which could lead to a NULL pointer dereference on failure of<br>
> radeon_vm_bo_add().<br>
><br>
> Fix this bug by adding a check of vm->ib_bo_va.<br>
><br>
> This bug was found by a static analyzer. The analysis employs<br>
> differential checking to identify inconsistent security operations<br>
> (e.g., checks or kfrees) between two code paths and confirms that the<br>
> inconsistent operations are not recovered in the current function or<br>
> the callers, so they constitute bugs.<br>
><br>
> Note that, as a bug found by static analysis, it can be a false<br>
> positive or hard to trigger. Multiple researchers have cross-reviewed<br>
> the bug.<br>
><br>
> Builds with CONFIG_DRM_RADEON=m show no new warnings,<br>
> and our static analyzer no longer warns about this code.<br>
><br>
> Fixes: cc9e67e3d700 ("drm/radeon: fix VM IB handling")<br>
> Reported-by: kernel test robot <<a href="mailto:lkp@intel.com" target="_blank">lkp@intel.com</a>><br>
> Signed-off-by: Zhou Qingyang <<a href="mailto:zhou1615@umn.edu" target="_blank">zhou1615@umn.edu</a>><br>
> ---<br>
> Changes in v2:<br>
>    -  Initialize the variables to silence warning<br>
<br>
What warning do you get? Double checking the code that shouldn't be <br>
necessary and is usually rather frowned upon.<br>
<br>
Thanks,<br>
Christian.<br>
<br>
><br>
> Changes in v3:<br>
>    -  Fix the bug that good case will also be freed<br>
>    -  Improve code style<br>
><br>
> Changes in v2:<br>
>    -  Improve the error handling into goto style<br>
><br>
>   drivers/gpu/drm/radeon/radeon_kms.c | 37 ++++++++++++++++-------------<br>
>   1 file changed, 20 insertions(+), 17 deletions(-)<br>
><br>
> diff --git a/drivers/gpu/drm/radeon/radeon_kms.c b/drivers/gpu/drm/radeon/radeon_kms.c<br>
> index 482fb0ae6cb5..9d0f840286a1 100644<br>
> --- a/drivers/gpu/drm/radeon/radeon_kms.c<br>
> +++ b/drivers/gpu/drm/radeon/radeon_kms.c<br>
> @@ -648,7 +648,9 @@ void radeon_driver_lastclose_kms(struct drm_device *dev)<br>
>   int radeon_driver_open_kms(struct drm_device *dev, struct drm_file *file_priv)<br>
>   {<br>
>       struct radeon_device *rdev = dev->dev_private;<br>
> -     int r;<br>
> +     struct radeon_fpriv *fpriv = NULL;<br>
> +     struct radeon_vm *vm = NULL;<br>
> +     int r = 0;<br>
><br>
>       file_priv->driver_priv = NULL;<br>
><br>
> @@ -660,8 +662,6 @@ int radeon_driver_open_kms(struct drm_device *dev, struct drm_file *file_priv)<br>
>   <br>
>       /* new gpu have virtual address space support */<br>
>       if (rdev->family >= CHIP_CAYMAN) {<br>
> -             struct radeon_fpriv *fpriv;<br>
> -             struct radeon_vm *vm;<br>
>   <br>
>               fpriv = kzalloc(sizeof(*fpriv), GFP_KERNEL);<br>
>               if (unlikely(!fpriv)) {<br>
> @@ -672,35 +672,38 @@ int radeon_driver_open_kms(struct drm_device *dev, struct drm_file *file_priv)<br>
>               if (rdev->accel_working) {<br>
>                       vm = &fpriv->vm;<br>
>                       r = radeon_vm_init(rdev, vm);<br>
> -                     if (r) {<br>
> -                             kfree(fpriv);<br>
> -                             goto out_suspend;<br>
> -                     }<br>
> +                     if (r)<br>
> +                             goto out_fpriv;<br>
>   <br>
>                       r = radeon_bo_reserve(rdev-><a href="http://ring_tmp_bo.bo" rel="noreferrer" target="_blank">ring_tmp_bo.bo</a>, false);<br>
> -                     if (r) {<br>
> -                             radeon_vm_fini(rdev, vm);<br>
> -                             kfree(fpriv);<br>
> -                             goto out_suspend;<br>
> -                     }<br>
> +                     if (r)<br>
> +                             goto out_vm_fini;<br>
>   <br>
>                       /* map the ib pool buffer read only into<br>
>                        * virtual address space */<br>
>                       vm->ib_bo_va = radeon_vm_bo_add(rdev, vm,<br>
>                                                       rdev-><a href="http://ring_tmp_bo.bo" rel="noreferrer" target="_blank">ring_tmp_bo.bo</a>);<br>
> +                     if (!vm->ib_bo_va) {<br>
> +                             r = -ENOMEM;<br>
> +                             goto out_vm_fini;<br>
> +                     }<br>
> +<br>
>                       r = radeon_vm_bo_set_addr(rdev, vm->ib_bo_va,<br>
>                                                 RADEON_VA_IB_OFFSET,<br>
>                                                 RADEON_VM_PAGE_READABLE |<br>
>                                                 RADEON_VM_PAGE_SNOOPED);<br>
> -                     if (r) {<br>
> -                             radeon_vm_fini(rdev, vm);<br>
> -                             kfree(fpriv);<br>
> -                             goto out_suspend;<br>
> -                     }<br>
> +                     if (r)<br>
> +                             goto out_vm_fini;<br>
>               }<br>
>               file_priv->driver_priv = fpriv;<br>
>       }<br>
>   <br>
> +out_vm_fini:<br>
> +     if (r)<br>
> +             radeon_vm_fini(rdev, vm);<br>
> +out_fpriv:<br>
> +     if (r)<br>
> +             kfree(fpriv);<br>
>   out_suspend:<br>
>       pm_runtime_mark_last_busy(dev->dev);<br>
>       pm_runtime_put_autosuspend(dev->dev);<br>
<br>
</blockquote></div>
</div>