Received: by 2002:a89:2c3:0:b0:1ed:23cc:44d1 with SMTP id d3csp209377lqs; Mon, 4 Mar 2024 22:24:21 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCU3PLDX349AcaLuQBYkLGtIFUitPtE/eMFgCsKm8cmLGQMuxYOX+EMSvWkMJRbe5CuZm5c7PronMs5d7rhfVd3nCZwqmpqcAWAdeOhG3A== X-Google-Smtp-Source: AGHT+IGaMsVfgF6GwgdPiABBoFN7vCfBKGsUMRaSXXGqbJXWyV8RtD0XBF1uSkBLruhcYAFRmbM+ X-Received: by 2002:a05:6808:90f:b0:3c1:e9c6:165a with SMTP id w15-20020a056808090f00b003c1e9c6165amr879611oih.31.1709619861103; Mon, 04 Mar 2024 22:24:21 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709619861; cv=pass; d=google.com; s=arc-20160816; b=zKUsbzVNJLkILZztC3yX8OV/5/DYSJSm1/JoXRogeAtDf2qzvN97c5OBzI+mVV7MzS YUF3sbpZWh7mLmut9RT/5dd0HY9oTGufBbhPZvDhYkUWzeDT7K2MrLNeAMVyRH/bvE8j ODsM4TTvrFFSsAktxdqfBDzEMqW5UF39XRybegI3uHPReXvE1h/koMA+ulTtgRKJ5zFZ oCV4OZyAj49CCeVQCEXOoT/tLNsvWmjc2Wq6+pvkDQTFt2dRYetmgHrGMMEqkRUUddWN X0JLO7mBniqLNWi+dX5bj4GCERF652QFlCR5IMfjzqLbZQYeLissrW8bG1hzzcC6zRqM NisA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to:subject :user-agent:mime-version:list-unsubscribe:list-subscribe:list-id :precedence:date:message-id; bh=/zOu4iVnVdJtSK37dpBiPfMlnc0bFwUe/dTBvq47qPA=; fh=ItBlbBs/Rb+JSu2Ua/ymNP50siYN/W1dBgYttqEsEGo=; b=rS2wtQQrS0B75qZswe4a5On0P8LLUY88OWdKt2Xylqirieu6V93H1r5wRxi1j24Pqd ToyLw8dC//Dhvv2chm/+osJ6ki5QXWz7CaW49f2AKv5vZkNq0hMrRQE+mizhUZiTKOpi clH1Y+m+6rDEYA/JMCOlpSuPfFQxkKowm5MJ9bery0v+wpIwj9EcGb65hJlPuhZAuxpb dHZTo5rBa2Pako+fUBqFcFB2HpupiNpSQ/lAipOYYQtwXybBCejCjvMHBDDh4BnTwffa j1sr4rCDEYYDheoxhylT8kWnP5XKpOcxw4EXOM6o1KoUa5MToVY8iodG+tty7yi9m2z9 i20w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=huaweicloud.com); spf=pass (google.com: domain of linux-kernel+bounces-91736-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-91736-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id o13-20020a65520d000000b005e42b4bb9dasi9454391pgp.213.2024.03.04.22.24.20 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Mar 2024 22:24:21 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-91736-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=huaweicloud.com); spf=pass (google.com: domain of linux-kernel+bounces-91736-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-91736-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id BE9AE2836E2 for ; Tue, 5 Mar 2024 06:24:20 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id BF56A7C6C0; Tue, 5 Mar 2024 06:24:13 +0000 (UTC) Received: from dggsgout11.his.huawei.com (unknown [45.249.212.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5A9ED29437; Tue, 5 Mar 2024 06:24:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.51 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709619853; cv=none; b=kvRtC2E8c0e8eW7hiwwyNtN/1uXwtstLGYUouJlFG/BTjvub5GOX+IwV6epJ++8ovxSONTT5+1vV5everC7/PKQP2eKlQs6RDL83nUTopMkb6GQJkcWr6L+g60yE+148gubgvK7XZFc2HbIfEdyxlvW+C8Rr15JL5EGsb2y4OMI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709619853; c=relaxed/simple; bh=7OQCuem/dH7I3j9E2bsrCLNZCmifdKDYrJY26mnm6rM=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=o1q+v7fSVd32OB68Jw2/5gl+vbNC8CeGmFUgrDLbeP13P6tU2QDwz+jiTdCEcuBF6soJ0ATLJTSZCuDmxNHlzvlp02SCA+dHsza69EtmkWWOYXvUKHeA56qerxWc9xi1Sv3qTZPH9tgxjCJObHGgClzVAPEuphRRTkFOOncNLfY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com; spf=pass smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=45.249.212.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.19.163.235]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4Tplrg1G6Pz4f3k68; Tue, 5 Mar 2024 14:24:03 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.112]) by mail.maildlp.com (Postfix) with ESMTP id A57BF1A0568; Tue, 5 Mar 2024 14:24:06 +0800 (CST) Received: from [10.174.179.155] (unknown [10.174.179.155]) by APP1 (Coremail) with SMTP id cCh0CgBXKBGEuuZlVtP5Fw--.21762S3; Tue, 05 Mar 2024 14:24:06 +0800 (CST) Message-ID: <2ae82ded-4d8a-cc0d-65b0-9468eaede73c@huaweicloud.com> Date: Tue, 5 Mar 2024 14:24:04 +0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:104.0) Gecko/20100101 Thunderbird/104.0 Subject: Re: [PATCH] block: move capacity validation to blkpg_do_ioctl() To: Damien Le Moal , axboe@kernel.dk, min15.li@samsung.com Cc: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, hch@lst.de, yangerkun@huawei.com, yukuai1@huaweicloud.com, houtao1@huawei.com, yi.zhang@huawei.com References: <20240305032132.548958-1-lilingfeng@huaweicloud.com> <474e78e7-9056-4b74-9ce2-592b8ee9411c@kernel.org> From: Li Lingfeng In-Reply-To: <474e78e7-9056-4b74-9ce2-592b8ee9411c@kernel.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-CM-TRANSID:cCh0CgBXKBGEuuZlVtP5Fw--.21762S3 X-Coremail-Antispam: 1UD129KBjvJXoWxAF45ZFyrGFW7Kr17Kw1DGFg_yoW5Wr4Up3 yUJFW3Aw4rKF17uw12qan3Aw1Fgwnrtr4xAw4kG3WFy3srAws3KFyYkw4Yva40vr93KFWI vF4jvF929F17CFJanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUvIb4IE77IF4wAFF20E14v26r4j6ryUM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28lY4IEw2IIxxk0rwA2F7IY1VAKz4 vEj48ve4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Ar0_tr1l84ACjcxK6xIIjxv20xvEc7Cj xVAFwI0_Gr1j6F4UJwA2z4x0Y4vEx4A2jsIE14v26rxl6s0DM28EF7xvwVC2z280aVCY1x 0267AKxVW0oVCq3wAS0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzVAqx4xG 6I80ewAv7VC0I7IYx2IY67AKxVWUJVWUGwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6xCaFV Cjc4AY6r1j6r4UM4x0Y48IcVAKI48JM4IIrI8v6xkF7I0E8cxan2IY04v7Mxk0xIA0c2IE e2xFo4CEbIxvr21l42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxV Aqx4xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r1q 6r43MIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0I7IYx2IY6x kF7I0E14v26r1j6r4UMIIF0xvE42xK8VAvwI8IcIk0rVWrZr1j6s0DMIIF0xvEx4A2jsIE 14v26r1j6r4UMIIF0xvEx4A2jsIEc7CjxVAFwI0_Gr0_Gr1UYxBIdaVFxhVjvjDU0xZFpf 9x07UWE__UUUUU= X-CM-SenderInfo: polox0xjih0w46kxt4xhlfz01xgou0bp/ 在 2024/3/5 12:30, Damien Le Moal 写道: > On 3/5/24 12:21, Li Lingfeng wrote: >> From: Li Lingfeng >> >> Commit 6d4e80db4ebe ("block: add capacity validation in >> bdev_add_partition()") add check of partition's start and end sectors to >> prevent exceeding the size of the disk when adding partitions. However, >> there is still no check for resizing partitions now. >> Move the check to blkpg_do_ioctl() to cover resizing partitions. >> >> Signed-off-by: Li Lingfeng >> --- >> block/ioctl.c | 9 ++++++++- >> block/partitions/core.c | 11 ----------- >> 2 files changed, 8 insertions(+), 12 deletions(-) >> >> diff --git a/block/ioctl.c b/block/ioctl.c >> index 438f79c564cf..de0cc0d215c6 100644 >> --- a/block/ioctl.c >> +++ b/block/ioctl.c >> @@ -18,7 +18,7 @@ static int blkpg_do_ioctl(struct block_device *bdev, >> { >> struct gendisk *disk = bdev->bd_disk; >> struct blkpg_partition p; >> - sector_t start, length; >> + sector_t start, length, capacity, end; >> >> if (!capable(CAP_SYS_ADMIN)) >> return -EACCES; >> @@ -41,6 +41,13 @@ static int blkpg_do_ioctl(struct block_device *bdev, >> >> start = p.start >> SECTOR_SHIFT; >> length = p.length >> SECTOR_SHIFT; >> + capacity = get_capacity(disk); >> + >> + if (check_add_overflow(start, length, &end)) >> + return -EINVAL; >> + >> + if (start >= capacity || end > capacity) >> + return -EINVAL; >> >> switch (op) { >> case BLKPG_ADD_PARTITION: >> diff --git a/block/partitions/core.c b/block/partitions/core.c >> index 5f5ed5c75f04..b11e88c82c8c 100644 >> --- a/block/partitions/core.c >> +++ b/block/partitions/core.c >> @@ -419,21 +419,10 @@ static bool partition_overlaps(struct gendisk *disk, sector_t start, >> int bdev_add_partition(struct gendisk *disk, int partno, sector_t start, >> sector_t length) >> { >> - sector_t capacity = get_capacity(disk), end; >> struct block_device *part; >> int ret; >> >> mutex_lock(&disk->open_mutex); >> - if (check_add_overflow(start, length, &end)) { >> - ret = -EINVAL; >> - goto out; >> - } >> - >> - if (start >= capacity || end > capacity) { >> - ret = -EINVAL; >> - goto out; >> - } >> - > Why do you remove this ? The check will not be done when *existing* partitions I'm sorry, I didn't quite understand your point. The function bdev_add_partition() is only called within blkpg_do_ioctl(). I simply moved the check to a different location, but did not remove it entirely. > are added. To do the check when *creating* a partition, make this code a helper > and call that helper function here and from blkpg_do_ioctl() as well. Do you mean call the helper function in blk_add_partition() and blkpg_do_ioctl()? > >> if (!disk_live(disk)) { >> ret = -ENXIO; >> goto out;