From patchwork Fri Sep 6 22:57:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Begunkov X-Patchwork-Id: 13794834 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id C3173E6FE49 for ; Fri, 6 Sep 2024 22:57:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9AFFC6B0095; Fri, 6 Sep 2024 18:57:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 95EE36B0098; Fri, 6 Sep 2024 18:57:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 763CD6B0096; Fri, 6 Sep 2024 18:57:05 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 513E66B0093 for ; Fri, 6 Sep 2024 18:57:05 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 070A9A98D9 for ; Fri, 6 Sep 2024 22:57:05 +0000 (UTC) X-FDA: 82535825610.07.C155338 Received: from mail-ej1-f54.google.com (mail-ej1-f54.google.com [209.85.218.54]) by imf20.hostedemail.com (Postfix) with ESMTP id 340561C0002 for ; Fri, 6 Sep 2024 22:57:02 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Wpq5taj6; spf=pass (imf20.hostedemail.com: domain of asml.silence@gmail.com designates 209.85.218.54 as permitted sender) smtp.mailfrom=asml.silence@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1725663292; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=gCQ1sFR6q4N5GvvlFRQ23FRxGveAlTZ5FLsIiX1fOqY=; b=l/QExP9rtpAjraqmL9xmD+2XZMvf4DMx+NJNkM2aT6DuuWVedXiQvQLUjSf4b9ojdMBLRe mGt0Jz+CZyj0lAVTwDA+t+L2g9nSxY/3oDAZ6Xnm1YdY4c/5hveMsvz8QEzkMxG6PVmnmg p3BbG2d2T1EcQw7mNqP1/ck7cKh+eMU= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Wpq5taj6; spf=pass (imf20.hostedemail.com: domain of asml.silence@gmail.com designates 209.85.218.54 as permitted sender) smtp.mailfrom=asml.silence@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725663292; a=rsa-sha256; cv=none; b=zMBiUNY0WYSrQU3JyfhXQqhOIQOyYHNDgbX8VKp301s9OThB7pK/ZIzbihzoTkD7RrwhGp x9IbXIzMsvTsxQKxHKn2093Zo/khZEvBHwksGLeCqdLXef1uo/B9Dfg4qYU+ZHcf+DR+RO 1MRHbPy+Z5HEGVjiCjNPbJwBMYk+CqM= Received: by mail-ej1-f54.google.com with SMTP id a640c23a62f3a-a7aa086b077so261311866b.0 for ; Fri, 06 Sep 2024 15:57:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725663422; x=1726268222; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=gCQ1sFR6q4N5GvvlFRQ23FRxGveAlTZ5FLsIiX1fOqY=; b=Wpq5taj6vUEiIR2/NoZgHgS4VfiTorpxiGn7AkDD2QO/5I0wOtLflYXBDbzzzWkmYW G/jjfpalTUqmLVaqiKALE422ajRlgAvwvP/eJu4YN1PqCn2BTtyGS/HqTVF2Wds3DD6j xJuyuhxojc38FlnzH+Gd6I/R/QRYlU82wHtkW4DR5xWz5E0B5RHJkqnjWoMK5FlILCBk 111x5vUPxWoNjTjjbn/h28CvGz9RjQMl5K8zy4faTcBD1WkHqj0ybhbDi7McPokuGOWt /ma4lyD6VEOkE9xv/32Or4dEL+Vc3Ef0jEOQWYDyL+if2y4FZ4ZhZNON965mxouZNcO+ zK7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725663422; x=1726268222; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=gCQ1sFR6q4N5GvvlFRQ23FRxGveAlTZ5FLsIiX1fOqY=; b=wUbrfM+q2yfF8cRi3+e7SxJHaJDy2YuNXtuy/mbZK9oyTz0gUqVtHrLtxMVkDLhFsq 10XVs0q0laRNjSxehutf8cMziKxFUD1j0JT6fd4gW9QS7eGyIqxm+0Rc8euuQHKXn6ox INhhsfReLyB2b3P1vtt65tMnZ+OUwp53XGVSqfoTf+Ujo9IlroXsfynMCYvkEDI2WKxr V3zezGNdizqkbvgAW/FbzY4tY0SjEx995nqRXQP9IDCHp6qYG4Yf3j4XiC5yG1+fDDS1 cVpIUR2289KDdyTaJgdK8YZs8FMB3ztNPF4lz11T+eNgtRKxUekMongK9w4QuuaPquMG sR/g== X-Forwarded-Encrypted: i=1; AJvYcCULrNMRhivLnsHJ87rHqBWgCPrcSYlNP3oIX9wmw0GV6GERm7ND0Q9hMwEAPiO7O31u73VEUCJx9A==@kvack.org X-Gm-Message-State: AOJu0YwkxCP0v/vRBJJGGQ6bRuLXB95+VvbSrcoTvuYFAy1y6Q2R9qvs M0Cww8WNAcUpSKz04gqEGNY7DAiC5C3SMO2knHP5JQ9p2KHztF0W X-Google-Smtp-Source: AGHT+IGJ6gzQkF2S9wNkzcDzctZY5nEoDtVuCYWlHsBqBW5skC54eWB3Kt2NmCi50PZKCSauBvcXGg== X-Received: by 2002:a17:907:7b92:b0:a7a:9a78:4b5a with SMTP id a640c23a62f3a-a8a887fcdb3mr350202066b.52.1725663421631; Fri, 06 Sep 2024 15:57:01 -0700 (PDT) Received: from 127.0.0.1localhost ([148.252.146.236]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a8d25d54978sm2679566b.199.2024.09.06.15.56.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 06 Sep 2024 15:57:01 -0700 (PDT) From: Pavel Begunkov To: io-uring@vger.kernel.org Cc: Jens Axboe , asml.silence@gmail.com, Conrad Meyer , linux-block@vger.kernel.org, linux-mm@kvack.org, Christoph Hellwig Subject: [PATCH v4 4/8] block: introduce blk_validate_byte_range() Date: Fri, 6 Sep 2024 23:57:21 +0100 Message-ID: X-Mailer: git-send-email 2.45.2 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 340561C0002 X-Stat-Signature: cb8s3irzmzxcrr549exq9tsn89t4r18i X-Rspam-User: X-HE-Tag: 1725663422-604172 X-HE-Meta: U2FsdGVkX19kvbQLS6EwjnYYDf3fYVZ7NODPvtR6jCZie6MeXNBersy9wpbt8jFBb+PQknWxFTqnbdPccVGMgeoP51OMok4657fqEPhu5Q4vy/7wKNAoQyjwcue5skJgJ1JXC0DIMqK9kJA2Q3CUYeTwLzOj+X3SISBYyBvxCNgsM+1rs1AzeLczgrGnEJFnNRW5ywkE6hmIRgkBjB9Ldmj/gJTWTsP5HJxFAxE7fB4ralKy+L69F3WPgpsN/9ArkjKI6UBcm6FAUQh2TTwea0Qyyr2uM8Ok9QM3zoJeM8wGGY2e/mrQn+3SyOZl4+AH9Sf1SDb/08Ohx0tPQ/ZdSqa8x+EqBVxIXkivL3q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72ZLyWESi8oaCbGbqNexSEJQWTj0uznhur6i4hY2/n0xlGn6xwFOb1lmVbQ6CVG1+IfMev56ntaGI1HJ8o= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000006, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: In preparation to further changes extract a helper function out of blk_ioctl_discard() that validates if we can do IO against the given range of disk byte addresses. Signed-off-by: Pavel Begunkov --- block/ioctl.c | 44 ++++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/block/ioctl.c b/block/ioctl.c index e8e4a4190f18..a820f692dd1c 100644 --- a/block/ioctl.c +++ b/block/ioctl.c @@ -92,38 +92,46 @@ static int compat_blkpg_ioctl(struct block_device *bdev, } #endif +static int blk_validate_byte_range(struct block_device *bdev, + uint64_t start, uint64_t len) +{ + unsigned int bs_mask = bdev_logical_block_size(bdev) - 1; + uint64_t end; + + if ((start | len) & bs_mask) + return -EINVAL; + if (!len) + return -EINVAL; + if (check_add_overflow(start, len, &end) || end > bdev_nr_bytes(bdev)) + return -EINVAL; + + return 0; +} + static int blk_ioctl_discard(struct block_device *bdev, blk_mode_t mode, unsigned long arg) { - unsigned int bs_mask = bdev_logical_block_size(bdev) - 1; - uint64_t range[2], start, len, end; + uint64_t range[2], start, len; struct bio *prev = NULL, *bio; sector_t sector, nr_sects; struct blk_plug plug; int err; - if (!(mode & BLK_OPEN_WRITE)) - return -EBADF; - - if (!bdev_max_discard_sectors(bdev)) - return -EOPNOTSUPP; - if (bdev_read_only(bdev)) - return -EPERM; - if (copy_from_user(range, (void __user *)arg, sizeof(range))) return -EFAULT; - start = range[0]; len = range[1]; - if (!len) - return -EINVAL; - if ((start | len) & bs_mask) - return -EINVAL; + if (!bdev_max_discard_sectors(bdev)) + return -EOPNOTSUPP; - if (check_add_overflow(start, len, &end) || - end > bdev_nr_bytes(bdev)) - return -EINVAL; + if (!(mode & BLK_OPEN_WRITE)) + return -EBADF; + if (bdev_read_only(bdev)) + return -EPERM; + err = blk_validate_byte_range(bdev, start, len); + if (err) + return err; filemap_invalidate_lock(bdev->bd_mapping); err = truncate_bdev_range(bdev, mode, start, start + len - 1);