From patchwork Thu Aug 22 03:35:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Begunkov X-Patchwork-Id: 13772577 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 915D1C3DA4A for ; Thu, 22 Aug 2024 03:35:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 73A536B0147; Wed, 21 Aug 2024 23:35:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6EB4F6B0255; Wed, 21 Aug 2024 23:35:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 53BF56B0257; Wed, 21 Aug 2024 23:35:44 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 33F236B0147 for ; Wed, 21 Aug 2024 23:35:44 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id DE0EA160435 for ; Thu, 22 Aug 2024 03:35:43 +0000 (UTC) X-FDA: 82478466966.10.57D1BE2 Received: from mail-wr1-f42.google.com (mail-wr1-f42.google.com [209.85.221.42]) by imf09.hostedemail.com (Postfix) with ESMTP id 063B5140021 for ; Thu, 22 Aug 2024 03:35:41 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=azA4TXDm; spf=pass (imf09.hostedemail.com: domain of asml.silence@gmail.com designates 209.85.221.42 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=1724297635; 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=G7yUgVOy5MpRKfji6spWllEFui/I+HP/hnabDQ6U+Y0=; b=F2g1FWmlUSHdpjZ8bN5sxIfHPOc5cjW+y/D6biZVQdgRgNyEI31M8Vjr7QrnUpfk668bEd y3POZHYUGN6QcYpZQmsVctqjAg5U45GAAHuDwP/Q4LOTlnCY5aISwYVotRJrLFHmtEWnOK e8h4k98xsS89ozSQJk3ddpuUmDAGUsU= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=azA4TXDm; spf=pass (imf09.hostedemail.com: domain of asml.silence@gmail.com designates 209.85.221.42 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=1724297635; a=rsa-sha256; cv=none; b=ho1qANuFMfUSRBp6wpkFqa6UUe8mU+YFaP6tHKdPCfvD71u0m5yrviZCcq5UpJv/9jym/o Jvy3oIie6FoMyCiONIiey4DipxssBZ/WElOgp1WKjePETN2gYTFRsRlUGCyv/84qNyHLhl /p92pt3Ht/36ZY6rSw92Un7sa4JeX6o= Received: by mail-wr1-f42.google.com with SMTP id ffacd0b85a97d-371bb8322b2so137097f8f.0 for ; Wed, 21 Aug 2024 20:35:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724297740; x=1724902540; 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=G7yUgVOy5MpRKfji6spWllEFui/I+HP/hnabDQ6U+Y0=; b=azA4TXDmNUZk2Dikgs8i3q07xrrPWpsbTvfg70EkmccV7KBG+eihChSl3jGVyLMs+1 w5jFvNLhGzL8kpenzRnOcADF6Ez1yHBINunnYmkiaN4XbFc7vmeTXCHwHk8YUFRIc7mC mz5MhCbBy5fNtDSd951t50aaFzlFsX+twD8uEb18bf+u5WkpH0FKJakGCAyYzHNBME4u 2y1+e26XGMARJCjLbU9UONEqQDoZDByPVz3J9nCa0llQ7FC1afY4OcPX+XPqrPrMUXNO ofXjBzONemP3HWRyP9NESDZOHc/qFDuuKojR5Xg4S08leOBOSTwwH5rLhog/6JiYusA5 AmrQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724297740; x=1724902540; 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=G7yUgVOy5MpRKfji6spWllEFui/I+HP/hnabDQ6U+Y0=; b=V5HHolbyz4rQyeoTGIQGOcPtwIhJd+t/HUBlJwf5E5thQjyrKiAlXskrius6Lply4u xGvqDOXogcA33ltKpPivNvJmnlg16mHPwwMu5fLfxeJK7GYckmridIBuU0oHmGRxt4Dh v4aPm6Quao2tG0zDSwvggbVkIB0b93RXdGcNpdG5U6f3jY4pdtS0mJWAJqWvAGwEElT+ A93l6q7l+42dF9oJfzpcBcFX1Z7SXFj6SBHyrYq5Q1MO6IhDl4d+VV6Aks54K+ky5uGC 0FtY4NM10J1JY8qZm7wS97hnjewFlRFogquJOsulBK7i96rxg3NPqfQpzPmT1se+F58M mo+A== X-Forwarded-Encrypted: i=1; AJvYcCWjQalw4A7HISMSYJPPw6rUIgtebs5iw7Lgmii1kIu/gvtADa8SX3ucRe7FuXj+LNctzojKBx/vOA==@kvack.org X-Gm-Message-State: AOJu0YzgPM9x4krLrfCCjpFKQvGn8MN7sxOanNlXXc5Npziw/UFb9lDX EWBfBrthvUDbGfnll9E+ByneKFvpBmUx1LgeTq2Fisj35FqML7N1 X-Google-Smtp-Source: AGHT+IFHlvWFFCxl8YjFlJaGHJvsG1W8kc3fcAPScv4ZmRwi4I8WRIgqdUzhutwlfp/55qdQx9eetw== X-Received: by 2002:a5d:4487:0:b0:365:aec0:e191 with SMTP id ffacd0b85a97d-373052b4b25mr815314f8f.21.1724297740108; Wed, 21 Aug 2024 20:35:40 -0700 (PDT) Received: from 127.0.0.1localhost ([148.252.128.6]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-42abefc626fsm45491995e9.31.2024.08.21.20.35.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Aug 2024 20:35:39 -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 Subject: [PATCH v2 4/7] block: introduce blk_validate_write() Date: Thu, 22 Aug 2024 04:35:54 +0100 Message-ID: <2ef85c782997ad40e923e7640039e0c7795e19da.1724297388.git.asml.silence@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 063B5140021 X-Stat-Signature: za15dizf9fc5tashiaa5m6k8a7rpexsr X-Rspam-User: X-HE-Tag: 1724297741-27151 X-HE-Meta: U2FsdGVkX1/rN3MF1cjCLIUxjADcKg7vDRzu26bUNSx642ntKe4iot0dOdw2sZzAVd9XHia7cOWfe9rYqMiMltCysNXQNSjBDms/+unQGGqF7mty216mC0Q+5StYv2dAygKZJoigA95nL9ySr115GjV5wqpWSjzS1mCPI+KUAUWo/EsvVp8lXSm41nehubM3clyWA3u3PIJicYzoG5VdClU4+cvKMCiZmE9YxDP/KeOmz9SgBHLstGLZFuXHmZAmxqPq1B4cpl6n8EGd9HZaTGk4yvH9/jN3vs98aGDtDCh9z9V2dS6L+xO3m7VbnPA80J/TTJRRFOpVxY1FxVEil7HC3pm45UrxXBnkkVwQWUGP0tVxtRIQoyF3xT/95GCV0qWMbTiEhg/yQCGS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+qSogq086ajJ+TL+Hw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, 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 it's allowed to do a write-like operation for the given range. Signed-off-by: Pavel Begunkov --- block/ioctl.c | 47 +++++++++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/block/ioctl.c b/block/ioctl.c index e8e4a4190f18..8df0bc8002f5 100644 --- a/block/ioctl.c +++ b/block/ioctl.c @@ -92,38 +92,49 @@ static int compat_blkpg_ioctl(struct block_device *bdev, } #endif +static int blk_validate_write(struct block_device *bdev, blk_mode_t mode, + uint64_t start, uint64_t len) +{ + unsigned int bs_mask; + uint64_t end; + + if (!(mode & BLK_OPEN_WRITE)) + return -EBADF; + if (bdev_read_only(bdev)) + return -EPERM; + + bs_mask = bdev_logical_block_size(bdev) - 1; + 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; + err = blk_validate_write(bdev, mode, start, len); + if (err) + return err; filemap_invalidate_lock(bdev->bd_mapping); err = truncate_bdev_range(bdev, mode, start, start + len - 1);