From patchwork Mon Oct 25 14:25:03 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yongji Xie X-Patchwork-Id: 12581937 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7C72EC433EF for ; Mon, 25 Oct 2021 14:25:26 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5BD3560F46 for ; Mon, 25 Oct 2021 14:25:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230137AbhJYO1r (ORCPT ); Mon, 25 Oct 2021 10:27:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39138 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233512AbhJYO1q (ORCPT ); Mon, 25 Oct 2021 10:27:46 -0400 Received: from mail-pj1-x1030.google.com (mail-pj1-x1030.google.com [IPv6:2607:f8b0:4864:20::1030]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5069AC061745 for ; Mon, 25 Oct 2021 07:25:24 -0700 (PDT) Received: by mail-pj1-x1030.google.com with SMTP id lx5-20020a17090b4b0500b001a262880e99so107958pjb.5 for ; Mon, 25 Oct 2021 07:25:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=TqXG0/tzAMme0lqJcgyNBNW84HzqzOSloOMxwNDMqnk=; b=ci5vKvt7MPVkFEOn/vdkYki+HHpreHpMUojahpuEUMkbCwTji7lH6Vg3wrB7GgUMEB Pbft2KjLVDtLw+SaufMtTrDdh752x/eNenzHDSkp9Z86nXZOEYia/FSnnqXnQasWpeXX iGobot9Ge/UjPYLrKVrSveN3w/51aVWafnYZznqhbYkklsZCDhEJCd8hKjEFs71P1+ZM NwMKY9w43ZqCGs5981N7CdtgaOvWQAeV61HGNQ3TFKpXicjEeunTj5kKiZhNUnIuF36G P1mgt4VNT0KsnqTKqcN9a900U+bFba/aedd1voCItjyvKk6HKPW0EM6elCe99FkrrRDW drEw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=TqXG0/tzAMme0lqJcgyNBNW84HzqzOSloOMxwNDMqnk=; b=rgRDSy0WvhfKrxV/jW8evr8mEJxGbrNB9Lc0E3gEBWZxydOcv+1RWpszpnty3/Fzer JU5g8hCmmQgb2HmQhYm8pd2nuxkP2B2mVNXhQq2pQ3tKvSwXlywjt5t/w7UWVCG0ChwF QTY3jSbjyEdY19SQI4HYGEtX5/coKCQTGG1OxFl2RPhDc6t/WOJVMG42UgUPsgm1YYwB VgEYX/IVZymvZwmM8JUSgE43LhGP5LsmpqIa6+lt/KrIhmulMEybT4rI04KGBNiBWWsb IkXfzxNAqOd8FHce4CU78HSrpSqvBhmKIayJvNZ1DP95VCBqv0lgnQa1RtVOGuNhVLDP WJhw== X-Gm-Message-State: AOAM530yx5vRQUTH9d6+U5Ynswij+w0omPR+xY2KcSzjPuuwJN58LJ17 rwNQ/HTX3LHUL8klFJIi3oFE X-Google-Smtp-Source: ABdhPJzQBWheUx42D4ZY8nK9BboMGGe2OJYiojH/SBj6h2EEIRDnE81SpikbXuVrnIafRrI2f90kZA== X-Received: by 2002:a17:902:9b8a:b0:13f:c286:a060 with SMTP id y10-20020a1709029b8a00b0013fc286a060mr16366368plp.66.1635171923857; Mon, 25 Oct 2021 07:25:23 -0700 (PDT) Received: from localhost ([139.177.225.234]) by smtp.gmail.com with ESMTPSA id t3sm16393327pgo.51.2021.10.25.07.25.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Oct 2021 07:25:22 -0700 (PDT) From: Xie Yongji To: axboe@kernel.dk, hch@lst.de, josef@toxicpanda.com, mst@redhat.com, jasowang@redhat.com, stefanha@redhat.com, kwolf@redhat.com Cc: linux-block@vger.kernel.org, nbd@other.debian.org, virtualization@lists.linux-foundation.org Subject: [PATCH v2 1/4] block: Add a helper to validate the block size Date: Mon, 25 Oct 2021 22:25:03 +0800 Message-Id: <20211025142506.167-2-xieyongji@bytedance.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211025142506.167-1-xieyongji@bytedance.com> References: <20211025142506.167-1-xieyongji@bytedance.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org There are some duplicated codes to validate the block size in block drivers. This limitation actually comes from block layer, so this patch tries to add a new block layer helper for that. Signed-off-by: Xie Yongji --- include/linux/blkdev.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h index 12b9dbcc980e..805cd02d7914 100644 --- a/include/linux/blkdev.h +++ b/include/linux/blkdev.h @@ -235,6 +235,14 @@ struct request { void *end_io_data; }; +static inline int blk_validate_block_size(unsigned int bsize) +{ + if (bsize < 512 || bsize > PAGE_SIZE || !is_power_of_2(bsize)) + return -EINVAL; + + return 0; +} + static inline bool blk_op_is_passthrough(unsigned int op) { op &= REQ_OP_MASK; From patchwork Mon Oct 25 14:25:04 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yongji Xie X-Patchwork-Id: 12581939 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1739BC433F5 for ; Mon, 25 Oct 2021 14:25:30 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id EFE9960F46 for ; Mon, 25 Oct 2021 14:25:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233513AbhJYO1v (ORCPT ); Mon, 25 Oct 2021 10:27:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39158 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232487AbhJYO1u (ORCPT ); Mon, 25 Oct 2021 10:27:50 -0400 Received: from mail-pj1-x1036.google.com (mail-pj1-x1036.google.com [IPv6:2607:f8b0:4864:20::1036]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C04EFC061745 for ; Mon, 25 Oct 2021 07:25:28 -0700 (PDT) Received: by mail-pj1-x1036.google.com with SMTP id oa12-20020a17090b1bcc00b0019f715462a8so116365pjb.3 for ; Mon, 25 Oct 2021 07:25:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=z+Z7xBwuEdiDo3yfPlLotr1uBNzHqi96x2Qr+oepBsg=; b=rTjEd60jOwJMhvhEMRHrMEBAKxeXsvtSubyuLHnKWoGE2nd+sUM8esldpNB9FZyd9K gRQCeEn1RXqoxkZzazCxggbQD8onZqyIrumUA0RNpRhOUM/31Q0jULpAwPnmnaHq6asM IVZUJ3hCMlpovO0MZnb2o/8SMBwrlqyN+NapbT63b08vlGyItYPJ66jBYCb4FrpaAIwM KI0zI9GdD0ZJvD3b0rU6gDthUBHTwgraXxDkHsAEkVUzeVLVTX8Bcfrgc+xsjt4mWe8P MXJo/mpXLJJ35X4Nl6gSqLZQonxUrJq1heeyTERlrDrxbUrkip2EDzQeaeZr52tE8j35 fyVw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=z+Z7xBwuEdiDo3yfPlLotr1uBNzHqi96x2Qr+oepBsg=; b=TvIWdkblYMLMTSOZfkOhD8LE0Y6qfN/9oDm5v4ZxhMalaCQbLH6RzaWyNO+zfKGCrC mcdRxDoM93tyb5rS998IVJfadhqi/OWDZ00vSyDndhA5YePyADDuLB2wtnTcoLkw4V+F ntGP/UDEAAuH3Q09isUtLzdGwOEv+u2gqHteo/rsIDKjECgiLQ6ClVHJKwqSEbH2uNAL VWM42v3lz3s+lCRNRWoEO4cDwrqdksuDqeXOj1maABu9pziHGVKUjBDHz18bIelUIzJh R5FnX41Co1TuiM+1M6YdRROUx69ZHyKJPiYeY8uG52OpUDeFpbB03ox0158IeC1qbIH2 vgwQ== X-Gm-Message-State: AOAM533C1CMPuVUveZd1VrR+KPpGovcSUAQB0epVmaF/cnPJy6/CExmk R/beBN7PzTjjXPfYGEKBfhoK X-Google-Smtp-Source: ABdhPJxm/W5mKeg55PTl0JwWvTPTmH6uEA/7yKQ01Mx6oyo7yr2Rrwv5cHZuBMKhI387NKzPi54k5w== X-Received: by 2002:a17:90a:4598:: with SMTP id v24mr21011284pjg.5.1635171928289; Mon, 25 Oct 2021 07:25:28 -0700 (PDT) Received: from localhost ([139.177.225.234]) by smtp.gmail.com with ESMTPSA id s30sm23254567pfg.17.2021.10.25.07.25.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Oct 2021 07:25:27 -0700 (PDT) From: Xie Yongji To: axboe@kernel.dk, hch@lst.de, josef@toxicpanda.com, mst@redhat.com, jasowang@redhat.com, stefanha@redhat.com, kwolf@redhat.com Cc: linux-block@vger.kernel.org, nbd@other.debian.org, virtualization@lists.linux-foundation.org Subject: [PATCH v2 2/4] nbd: Use blk_validate_block_size() to validate block size Date: Mon, 25 Oct 2021 22:25:04 +0800 Message-Id: <20211025142506.167-3-xieyongji@bytedance.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211025142506.167-1-xieyongji@bytedance.com> References: <20211025142506.167-1-xieyongji@bytedance.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org Use the block layer helper to validate block size instead of open coding it. Signed-off-by: Xie Yongji --- drivers/block/nbd.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c index 1183f7872b71..3f58c3eb38b6 100644 --- a/drivers/block/nbd.c +++ b/drivers/block/nbd.c @@ -323,7 +323,8 @@ static int nbd_set_size(struct nbd_device *nbd, loff_t bytesize, { if (!blksize) blksize = 1u << NBD_DEF_BLKSIZE_BITS; - if (blksize < 512 || blksize > PAGE_SIZE || !is_power_of_2(blksize)) + + if (blk_validate_block_size(blksize)) return -EINVAL; nbd->config->bytesize = bytesize; From patchwork Mon Oct 25 14:25:05 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yongji Xie X-Patchwork-Id: 12581941 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 90006C433EF for ; Mon, 25 Oct 2021 14:25:34 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6A51B60EE3 for ; Mon, 25 Oct 2021 14:25:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233512AbhJYO1z (ORCPT ); Mon, 25 Oct 2021 10:27:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39180 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233514AbhJYO1z (ORCPT ); Mon, 25 Oct 2021 10:27:55 -0400 Received: from mail-pl1-x62d.google.com (mail-pl1-x62d.google.com [IPv6:2607:f8b0:4864:20::62d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 39CC2C061745 for ; Mon, 25 Oct 2021 07:25:33 -0700 (PDT) Received: by mail-pl1-x62d.google.com with SMTP id c4so4153884plg.13 for ; Mon, 25 Oct 2021 07:25:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=av0r/rjVqFrB/l7qERXJM+4hS0XcNVLfoNKN5vMbJNg=; b=x+zZ0eT2uie+47FbGcJT8mHH+XjW/4Ds3sjIAGgjAuF2E93R6vyPWhEmp75duwvgDu b9f+IjX290S6wRBGAzLK2zKpz2gMrmZkB5Wuzx67N7Qra6cOdsjog7MAk8Kveq9jeDi1 LDvOAXPjAE5GcIf6PJaWaviYiIDwnZsPxa2/x0ENnVH0C2DIpToPlsmUFg6zTjQeCkso UZjfP66tbd9MJ/aPd6ajIU8I9WaweWquuEhyWN3c3y59n3axDr9pXBsSZ8RrFbY+trpQ qqP5Xbky4kw+ORXJKs5EVwysoe7NULRJ28tlgijvLYWwwL+Z8HAJaAuZUopEXNhHiagW FXBg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=av0r/rjVqFrB/l7qERXJM+4hS0XcNVLfoNKN5vMbJNg=; b=pIwt5ijVRiyc2xbKStvN37oUQBSattTCOVeEPCbnOw/jI50aV4X+uma3hYmwQNbYP7 YtwxopQ8SxTmvaKDhqg3SiKuvg1Is75So9GW9ZYxul2CO1Oiiev344EiwBxetq5mQJ2b 2ixy2uY/1afSxDgnxt8YAmLWoxdYWA4XXPHTmVd7Sym6SlmyL4aIQ5CdIZOLVSpS6fM4 ySgFl7URCK5e/1lX6aM9RLpb5x/VBT95G2YOPTfWnFwvHe6G1aA1WmgcWc/Gs1mudbp0 I5ALQeebVvkIZx/7Eo3d2sjdTG2nYcfKuL2I1YtY380FXkqVftaWzdDgvLU0WGIdD8tQ nVWw== X-Gm-Message-State: AOAM533fOEE08ww8ppPKW6y6E7OE/jX4BrQEeJgFdJih3hjVeEedeZvu j6dEpRr3+cGyLiiVevOMKiShGMLI4uLz X-Google-Smtp-Source: ABdhPJyhiVjNl7b9fz60gTx056u54hm+xBqxLbAE1GxLDhDkOva6CNRgvBIx+2Wgf5QcgiqLOfic2w== X-Received: by 2002:a17:90b:2248:: with SMTP id hk8mr21042080pjb.102.1635171932763; Mon, 25 Oct 2021 07:25:32 -0700 (PDT) Received: from localhost ([139.177.225.234]) by smtp.gmail.com with ESMTPSA id c8sm17270047pgn.72.2021.10.25.07.25.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Oct 2021 07:25:31 -0700 (PDT) From: Xie Yongji To: axboe@kernel.dk, hch@lst.de, josef@toxicpanda.com, mst@redhat.com, jasowang@redhat.com, stefanha@redhat.com, kwolf@redhat.com Cc: linux-block@vger.kernel.org, nbd@other.debian.org, virtualization@lists.linux-foundation.org Subject: [PATCH v2 3/4] loop: Use blk_validate_block_size() to validate block size Date: Mon, 25 Oct 2021 22:25:05 +0800 Message-Id: <20211025142506.167-4-xieyongji@bytedance.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211025142506.167-1-xieyongji@bytedance.com> References: <20211025142506.167-1-xieyongji@bytedance.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org Remove loop_validate_block_size() and use the block layer helper to validate block size. Signed-off-by: Xie Yongji --- drivers/block/loop.c | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index 7bf4686af774..dfc72a1f6500 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -273,19 +273,6 @@ static void __loop_update_dio(struct loop_device *lo, bool dio) } /** - * loop_validate_block_size() - validates the passed in block size - * @bsize: size to validate - */ -static int -loop_validate_block_size(unsigned short bsize) -{ - if (bsize < 512 || bsize > PAGE_SIZE || !is_power_of_2(bsize)) - return -EINVAL; - - return 0; -} - -/** * loop_set_size() - sets device size and notifies userspace * @lo: struct loop_device to set the size for * @size: new size of the loop device @@ -1236,7 +1223,7 @@ static int loop_configure(struct loop_device *lo, fmode_t mode, } if (config->block_size) { - error = loop_validate_block_size(config->block_size); + error = blk_validate_block_size(config->block_size); if (error) goto out_unlock; } @@ -1759,7 +1746,7 @@ static int loop_set_block_size(struct loop_device *lo, unsigned long arg) if (lo->lo_state != Lo_bound) return -ENXIO; - err = loop_validate_block_size(arg); + err = blk_validate_block_size(arg); if (err) return err; From patchwork Mon Oct 25 14:25:06 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yongji Xie X-Patchwork-Id: 12581943 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id CA6B7C433F5 for ; Mon, 25 Oct 2021 14:25:38 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B192460EBB for ; Mon, 25 Oct 2021 14:25:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232487AbhJYO2A (ORCPT ); Mon, 25 Oct 2021 10:28:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39200 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233516AbhJYO17 (ORCPT ); Mon, 25 Oct 2021 10:27:59 -0400 Received: from mail-pg1-x52f.google.com (mail-pg1-x52f.google.com [IPv6:2607:f8b0:4864:20::52f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 986C0C061746 for ; Mon, 25 Oct 2021 07:25:37 -0700 (PDT) Received: by mail-pg1-x52f.google.com with SMTP id m21so11068633pgu.13 for ; Mon, 25 Oct 2021 07:25:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=bVOS8C1PlcF3MnWX9/1MofpPJpttTq8LtZjRTzxXBls=; b=8XOvJUJcgGrqXhblePt2Grl13rd/eel4oYYwA77benHFSA3WNwrGOwico2c9Rej3VA jcFVkNdJTPEuGkRo/WDQFc5zr6odlJs+tHcoxOyeS7FC6R7nZ3bsIHvx32X6EliRYYhF GStdoXbR6bJb2sRn9nyYNn54V88qmLXKkkEdFvFkzPuBXCacFIiAmVvzcQBalVIYiJkd wV9hAp8GKSi2zouCDeoAyPfSpJTO6P+xHRVDYdnRBQh+PQuftzFPE0Q6uxtYjKiHHVoQ KoWgx5Uxm473Hofh/ew0xVUWVwh9TpNm/BiXlwf6Yh77qCNIW9nBrWvWuoLu65JX5c2F IIqg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=bVOS8C1PlcF3MnWX9/1MofpPJpttTq8LtZjRTzxXBls=; b=vKso8s1wZ+71ZFi8f1gOd2+W3KfEkoMG0+K1PFD/9mZonPXbK8K7MnhI1eWeNPTxnY pHhdwPJblFi35NzxWuZlY1drDOgCGwLtK/CUDqy4nYO4ZvtlXdrkBnF+KGYuadY2YyO7 +TZ7kEU+BAX8QR7+pRD9NIa9tJQdEVPmbaFr0SD93a34+8cUyZlpjohkrVIX/JfPmcKO l2IecIPOaVMGEcoNTQh2bpj67w2JAtdzokQvHposMM2k1ECZcQacFLQlllCMybywczTM /5m8npP4x7Xda15nPP6AEt49GDpIILMK/cheKqvSmzxA4te2F+yxBAAaVFTP93oVx6Sj 9EIw== X-Gm-Message-State: AOAM533rP06PyzPcsiNWP7WP6pFVA8qLsk3Be3/rl7iJQ2WmC24RINeT yHp3oa7SxNb+tCAW1yGyG+wjRuFmjtU/ X-Google-Smtp-Source: ABdhPJxZcNLBw1oMVDvUdDwq10GKr3rkb3bdkb9Emof9ZTPBsRE168CwtZquQqWL7Ha1dg0LNbrv0g== X-Received: by 2002:a05:6a00:1901:b0:44b:e041:f07f with SMTP id y1-20020a056a00190100b0044be041f07fmr19162010pfi.52.1635171937101; Mon, 25 Oct 2021 07:25:37 -0700 (PDT) Received: from localhost ([139.177.225.234]) by smtp.gmail.com with ESMTPSA id lp9sm20678854pjb.35.2021.10.25.07.25.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Oct 2021 07:25:36 -0700 (PDT) From: Xie Yongji To: axboe@kernel.dk, hch@lst.de, josef@toxicpanda.com, mst@redhat.com, jasowang@redhat.com, stefanha@redhat.com, kwolf@redhat.com Cc: linux-block@vger.kernel.org, nbd@other.debian.org, virtualization@lists.linux-foundation.org Subject: [PATCH v2 4/4] virtio-blk: Use blk_validate_block_size() to validate block size Date: Mon, 25 Oct 2021 22:25:06 +0800 Message-Id: <20211025142506.167-5-xieyongji@bytedance.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211025142506.167-1-xieyongji@bytedance.com> References: <20211025142506.167-1-xieyongji@bytedance.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org The block layer can't support the block size larger than page size yet. If an untrusted device presents an invalid block size in configuration space, it will result in the kernel crash something like below: [ 506.154324] BUG: kernel NULL pointer dereference, address: 0000000000000008 [ 506.160416] RIP: 0010:create_empty_buffers+0x24/0x100 [ 506.174302] Call Trace: [ 506.174651] create_page_buffers+0x4d/0x60 [ 506.175207] block_read_full_page+0x50/0x380 [ 506.175798] ? __mod_lruvec_page_state+0x60/0xa0 [ 506.176412] ? __add_to_page_cache_locked+0x1b2/0x390 [ 506.177085] ? blkdev_direct_IO+0x4a0/0x4a0 [ 506.177644] ? scan_shadow_nodes+0x30/0x30 [ 506.178206] ? lru_cache_add+0x42/0x60 [ 506.178716] do_read_cache_page+0x695/0x740 [ 506.179278] ? read_part_sector+0xe0/0xe0 [ 506.179821] read_part_sector+0x36/0xe0 [ 506.180337] adfspart_check_ICS+0x32/0x320 [ 506.180890] ? snprintf+0x45/0x70 [ 506.181350] ? read_part_sector+0xe0/0xe0 [ 506.181906] bdev_disk_changed+0x229/0x5c0 [ 506.182483] blkdev_get_whole+0x6d/0x90 [ 506.183013] blkdev_get_by_dev+0x122/0x2d0 [ 506.183562] device_add_disk+0x39e/0x3c0 [ 506.184472] virtblk_probe+0x3f8/0x79b [virtio_blk] [ 506.185461] virtio_dev_probe+0x15e/0x1d0 [virtio] So this patch tries to use the block layer helper to validate the block size. Signed-off-by: Xie Yongji --- drivers/block/virtio_blk.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/drivers/block/virtio_blk.c b/drivers/block/virtio_blk.c index 303caf2d17d0..8b5833997f8e 100644 --- a/drivers/block/virtio_blk.c +++ b/drivers/block/virtio_blk.c @@ -815,9 +815,16 @@ static int virtblk_probe(struct virtio_device *vdev) err = virtio_cread_feature(vdev, VIRTIO_BLK_F_BLK_SIZE, struct virtio_blk_config, blk_size, &blk_size); - if (!err) + if (!err) { + err = blk_validate_block_size(blk_size); + if (err) { + dev_err(&vdev->dev, + "get invalid block size: %u\n", blk_size); + goto out_cleanup_disk; + } + blk_queue_logical_block_size(q, blk_size); - else + } else blk_size = queue_logical_block_size(q); /* Use topology information if available */