From patchwork Wed Aug 14 10:45:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Begunkov X-Patchwork-Id: 13763318 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 0B54CC52D7F for ; Wed, 14 Aug 2024 10:45:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A041E6B0092; Wed, 14 Aug 2024 06:45:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 987B26B0093; Wed, 14 Aug 2024 06:45:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7AE9C6B0095; Wed, 14 Aug 2024 06:45:30 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 5B94E6B0092 for ; Wed, 14 Aug 2024 06:45:30 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 0F2C980E0E for ; Wed, 14 Aug 2024 10:45:30 +0000 (UTC) X-FDA: 82450519620.22.29A05CF Received: from mail-ej1-f42.google.com (mail-ej1-f42.google.com [209.85.218.42]) by imf18.hostedemail.com (Postfix) with ESMTP id 2EF3E1C0008 for ; Wed, 14 Aug 2024 10:45:27 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=RgzEeoAa; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf18.hostedemail.com: domain of asml.silence@gmail.com designates 209.85.218.42 as permitted sender) smtp.mailfrom=asml.silence@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723632270; a=rsa-sha256; cv=none; b=AYrAv6GIcHCLUYOtjJUSkyUwbRFrjwVma4NrMFPJzM7apW1WpbwuQy811U55BOO8nemm0s yPiuL1kCVZlfZL3kap/gL984O4zchPptVozVbWn08Mivebj+dnCD9sv5eFHjV+l80At1W1 GHAMxqfYK+wtCJ0fCLOMOuVA4j1AMSo= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=RgzEeoAa; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf18.hostedemail.com: domain of asml.silence@gmail.com designates 209.85.218.42 as permitted sender) smtp.mailfrom=asml.silence@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1723632270; 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=fvJpr7p454Y/UfNPCoQA/axDAfLDDtHMeofcmIJVphY=; b=zFsQWpjBmYMHVDGXa/+GqUfi6pvbFTrulFphUnCY275KxMhJ2njI6gH4wfAyr4051JsJ5E CgF/aoPU4IR/1JsMG1aFuok1oANMSAK2TcMFX+6ZDw/qDoeqXdrlANckfnhRPAszHXgNSv K9+4c1cLtDI2veWYGHU7LXDJO5N3w5k= Received: by mail-ej1-f42.google.com with SMTP id a640c23a62f3a-a7aac70e30dso720819766b.1 for ; Wed, 14 Aug 2024 03:45:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723632327; x=1724237127; 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=fvJpr7p454Y/UfNPCoQA/axDAfLDDtHMeofcmIJVphY=; b=RgzEeoAaFHH35+m5+o+uK9rZlf3jJ/7OqRBss8eSdReNGq6xLsQZSVwDyR98p+rd1C ekicxv6rowXJYwNW8fE7Qe82ycSrYwDle6GFOrnYUFBRRuKe4FqzjzZZ1v1Z+Rfb2Oaz 5rQ2bjvX58Abh6yUWUPK+FubQrqNyLUadLshYVgv/rOCPkmcYzILR+f0zdDfdf57veTV 7Nxha5jCN6P+oqFiNHm+PBn0P/MrEZY/V3Q0vHH1o+7rR7uiGLWhAhJysJzZcI3PAZk7 mM7zuoqzamYdByxVYdGv7xyr9pIEOXMNhNNdmyHhKf488odIoDDvJr5zzFM6cECSnTsY LnQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723632327; x=1724237127; 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=fvJpr7p454Y/UfNPCoQA/axDAfLDDtHMeofcmIJVphY=; b=mjn30vFSdZjj8nZ5eKdyrGW+1u09ys1UmMWo0BYSR9n092eZsmdyKBr21S8SA77jn0 U62cPKCOGllYZhY6IK9X+Vig5n2UA7CPOjuyGxLM62C8hJcyIKD5V/k7EjP9iKK/L238 rUgs1KGNVipKNhHtZLdWu4pCwD9d/sDrRc49DpwBhVO1zHmbe44jg0g7w/KLr0+Esah9 nEA+1zqh9Kzb8hDoDnP19tSwe8YViLHYSY+McwCL+keuHATIm/le1RbmeJnyVzGYEusg I0d9LRnOTQ6S5xYb9U0w78b5DXXPwp7fwoeMsIpWiKhyKin6mLxLDPfYsN0hWWbT4JF+ hGfQ== X-Forwarded-Encrypted: i=1; AJvYcCWldDo6YNuu5JY+t1VLi9K+We5T9faW0CezVo2igSgQhEvQ5o5nx5UQAr1u9jEpfnXIEvY7zEKiqY8ft6SgEMlxHqc= X-Gm-Message-State: AOJu0YzPBLIHaGSJY8qS3TixcssKvqDSD/WGJZB5OpHR4vJxhIjR5f8f yZHE1LtcxYzWuHlMdv9/usEJrIrbd4Nfdc9ghrczTuF216CLGsju X-Google-Smtp-Source: AGHT+IF5x7vVq+APZiVW0Zf/kYmxy4TtcaaAPS1Fj1piCtyS5MQvRUzOqgbF+0Kt0wNs5ja3bp6E1A== X-Received: by 2002:a17:907:e601:b0:a7a:c197:8701 with SMTP id a640c23a62f3a-a8366d5b86cmr180254766b.31.1723632326621; Wed, 14 Aug 2024 03:45:26 -0700 (PDT) Received: from 127.0.0.1localhost ([148.252.132.251]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a80f418692asm157212766b.224.2024.08.14.03.45.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Aug 2024 03:45:25 -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: [RFC 4/5] block: introduce blk_validate_discard() Date: Wed, 14 Aug 2024 11:45:53 +0100 Message-ID: <41ac9ff000cbd47fd8e386ee70e8049c3ac80ead.1723601134.git.asml.silence@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 2EF3E1C0008 X-Stat-Signature: sfesmcycp66wjjrew87y4qxtja5j5kjh X-Rspam-User: X-HE-Tag: 1723632327-312273 X-HE-Meta: U2FsdGVkX1+KDTppNTGhlwa0C1HX3cp6HFaWHqB+vFfn4hFbS1jroySpEJeVu8Mnb4qvjQZL5JCT3mJ8BFCNZcc916vCAfXEzBLa/lLIjDrz4MlAu/h2dkA1/FYusM5XkMZOEc8Md4RuaCdpNWvBN/Kp5Wy/ouD9EMektx+9scONXHOXLcM5fl+Mi/jQ8Mlz+4/egmzDO7Ujxghc0I+gdCqP9H/NkGk1pIC+auns+EdXNWsW4MPuiUTO9UAk5E1MiU5yAfHVZa+7ARBqDnv6hhNI2RqeTYN2idwp07zeeUPdkuHCpXZvfH2syyCo4YqyFP8hKN7cULP3BgR0sTBrasnNk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sFFKT88idDm87R44KLKNW4sCPy0iuoaPOqLlHlFpb5OKNE1Q3AAeSuyVjn/strE/J0f0J47piUvA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000280, 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 discard arguments. Signed-off-by: Pavel Begunkov --- block/ioctl.c | 45 ++++++++++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/block/ioctl.c b/block/ioctl.c index e8e4a4190f18..c7a3e6c6f5fa 100644 --- a/block/ioctl.c +++ b/block/ioctl.c @@ -92,39 +92,50 @@ static int compat_blkpg_ioctl(struct block_device *bdev, } #endif -static int blk_ioctl_discard(struct block_device *bdev, blk_mode_t mode, - unsigned long arg) +static int blk_validate_discard(struct block_device *bdev, blk_mode_t mode, + uint64_t start, uint64_t len) { - unsigned int bs_mask = bdev_logical_block_size(bdev) - 1; - uint64_t range[2], start, len, end; - struct bio *prev = NULL, *bio; - sector_t sector, nr_sects; - struct blk_plug plug; - int err; + unsigned int bs_mask; + uint64_t end; 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; + 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) +{ + uint64_t range[2], start, len; + struct bio *prev = NULL, *bio; + sector_t sector, nr_sects; + struct blk_plug plug; + int err; + + if (copy_from_user(range, (void __user *)arg, sizeof(range))) + return -EFAULT; + start = range[0]; + len = range[1]; + + err = blk_validate_discard(bdev, mode, start, len); + if (err) + return err; + filemap_invalidate_lock(bdev->bd_mapping); err = truncate_bdev_range(bdev, mode, start, start + len - 1); if (err)