1. 13 Aug, 2021 2 commits
    • Hou Tao's avatar
      nbd: do del_gendisk() asynchronously for NBD_DESTROY_ON_DISCONNECT · 68c9417b
      Hou Tao authored
      Now open_mutex is used to synchronize partition operations (e.g,
      blk_drop_partitions() and blkdev_reread_part()), however it makes
      nbd driver broken, because nbd may call del_gendisk() in nbd_release()
      or nbd_genl_disconnect() if NBD_CFLAG_DESTROY_ON_DISCONNECT is enabled,
      and deadlock occurs, as shown below:
      
      // AB-BA dead-lock
      nbd_genl_disconnect            blkdev_open
        nbd_disconnect_and_put
                                       lock bd_mutex
        // last ref
        nbd_put
          lock nbd_index_mutex
            del_gendisk
                                         nbd_open
                                           try lock nbd_index_mutex
              try lock bd_mutex
      
       or
      
      // AA dead-lock
      nbd_release
        lock bd_mutex
          nbd_put
            try lock bd_mutex
      
      Instead of fixing block layer (e.g, introduce another lock), fixing
      the nbd driver to call del_gendisk() in a kworker when
      NBD_DESTROY_ON_DISCONNECT is enabled. When NBD_DESTROY_ON_DISCONNECT
      is disabled, nbd device will always be destroy through module removal,
      and there is no risky of deadlock.
      
      To ensure the reuse of nbd index succeeds, moving the calling of
      idr_remove() after del_gendisk(), so if the reused index is not found
      in nbd_index_idr, the old disk must have been deleted. And reusing
      the existing destroy_complete mechanism to ensure nbd_genl_connect()
      will wait for the completion of del_gendisk().
      
      Also adding a new workqueue for nbd removal, so nbd_cleanup()
      can ensure all removals complete before exits.
      
      Reported-by: syzbot+0fe7752e52337864d29b@syzkaller.appspotmail.com
      Fixes: c76f48eb ("block: take bd_mutex around delete_partitions in del_gendisk")
      Signed-off-by: default avatarHou Tao <houtao1@huawei.com>
      Signed-off-by: default avatarChristoph Hellwig <hch@lst.de>
      Link: https://lore.kernel.org/r/20210811124428.2368491-2-hch@lst.deReviewed-by: default avatarJosef Bacik <josef@toxicpanda.com>
      Signed-off-by: default avatarJens Axboe <axboe@kernel.dk>
      68c9417b
    • Baokun Li's avatar
      nbd: add the check to prevent overflow in __nbd_ioctl() · fad7cd33
      Baokun Li authored
      If user specify a large enough value of NBD blocks option, it may trigger
      signed integer overflow which may lead to nbd->config->bytesize becomes a
      large or small value, zero in particular.
      
      UBSAN: Undefined behaviour in drivers/block/nbd.c:325:31
      signed integer overflow:
      1024 * 4611686155866341414 cannot be represented in type 'long long int'
      [...]
      Call trace:
      [...]
       handle_overflow+0x188/0x1dc lib/ubsan.c:192
       __ubsan_handle_mul_overflow+0x34/0x44 lib/ubsan.c:213
       nbd_size_set drivers/block/nbd.c:325 [inline]
       __nbd_ioctl drivers/block/nbd.c:1342 [inline]
       nbd_ioctl+0x998/0xa10 drivers/block/nbd.c:1395
       __blkdev_driver_ioctl block/ioctl.c:311 [inline]
      [...]
      
      Although it is not a big deal, still silence the UBSAN by limit
      the input value.
      Reported-by: default avatarHulk Robot <hulkci@huawei.com>
      Signed-off-by: default avatarBaokun Li <libaokun1@huawei.com>
      Reviewed-by: default avatarJosef Bacik <josef@toxicpanda.com>
      Link: https://lore.kernel.org/r/20210804021212.990223-1-libaokun1@huawei.com
      [axboe: dropped unlikely()]
      Signed-off-by: default avatarJens Axboe <axboe@kernel.dk>
      fad7cd33
  2. 10 Aug, 2021 1 commit
  3. 02 Aug, 2021 37 commits