From patchwork Wed Sep 4 14:18:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Begunkov X-Patchwork-Id: 13790934 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 B8822CA0ED3 for ; Wed, 4 Sep 2024 14:17:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E13908D025A; Wed, 4 Sep 2024 10:17:52 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D25658D0253; Wed, 4 Sep 2024 10:17:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BA19B8D025A; Wed, 4 Sep 2024 10:17:52 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 9A27F8D0253 for ; Wed, 4 Sep 2024 10:17:52 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 5CB93AADB0 for ; Wed, 4 Sep 2024 14:17:52 +0000 (UTC) X-FDA: 82527259584.29.9C19CD9 Received: from mail-ej1-f47.google.com (mail-ej1-f47.google.com [209.85.218.47]) by imf16.hostedemail.com (Postfix) with ESMTP id 6DDBB18000C for ; Wed, 4 Sep 2024 14:17:50 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=M7RqykQA; spf=pass (imf16.hostedemail.com: domain of asml.silence@gmail.com designates 209.85.218.47 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=1725459343; 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=S45T4hIiNpKxCQF0woi6DrrvEVaAJn9/tndym3cDOuiBHI49lC4sVpRxcdcE0WKktzP8aO rSJndxa+9EIx1TBVSEFKhFByNuyt19t2zIIzCRAIOXdPIl0GO2a7KBdBl1EA0tdXKW+ZTK G8BtyG/Nf+bilhjTeHVQ1by/THCqw9g= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=M7RqykQA; spf=pass (imf16.hostedemail.com: domain of asml.silence@gmail.com designates 209.85.218.47 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=1725459343; a=rsa-sha256; cv=none; b=wgC2TyhAiHrfW3ymIVguSAOm63XnzbTfFIMZgIwyqlLKbzt9zXkLhwhnAoef81uRpNZtDI 3uiUqPWpHs1aeAAFXp+tNJWpRrc7bXaPKJgVI89ZlhcT3281RdVV/H47AbRCbVCqQy460m hDRzxSEJxqEeqf72ROxCUq2l7+xphnM= Received: by mail-ej1-f47.google.com with SMTP id a640c23a62f3a-a868831216cso789456166b.3 for ; Wed, 04 Sep 2024 07:17:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725459469; x=1726064269; 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=M7RqykQAvpXMxiq4tyWPRGbmC5nZyFVzpWR/OzoMQ/ctNMMdaY1UL3JXZ6cC0nOIzC 9YpZIFozh6RVj/VhhJ+BNNpyLXq/r9MrlHdlctwyoFsJNfktLezJbeTX3Oh3a544ehKM AwOP/SEjcyugoJ/V+Wlry0cuZObfZbpE8i63t2BP9sGANePtdwURBMD4A12pweS9tR4t rFTZgDQLaIQ8QNSDsrFYaNFRxseS12pVYfR9Ix3p99CrbUDxpfSfUFsgsamhJjM3fFeV BzG51ZEIEGgQHJj19oWHcl4Bp+Z3kjPcB+mdqAHraVorBoEnIzayPdil4YitH7Y+RXpx an2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725459469; x=1726064269; 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=SguobQYf0wsANswuxZX076rb7hWKqpyBSkNYWdTAaSTAB1brkrd36vI8re4LQgztxt FcjwAmEI4MmglDk7E3g0w4PGF6AMstBrLxUFwB/fR3jYO5kXTsieF8LS0Nb2rhLPcOik YnYWvLiEzS4QA+GqP9geY3MGsPZ5J+7Fs7tRHvfVc78YoXk1Qqy91QOKs8tjYOao4343 YtoP3/K2/dNVSaHOyVa99ERYN7lVv4aSof23g6+8b81DAWYaYPCr0vTbU0yBeDFjxx+I H4+8DCfg97rmvWZOdG+TLrIANaioM48qDYnzf4zlLR9TQljz65RTHhm+0fXA8hntSFkb j3aw== X-Forwarded-Encrypted: i=1; AJvYcCXeAvJH/xfnoeeurGmo24bd84TzLF/CT0lVxn4vw+IJPkFEY858FuUx0fWnK6esNdjJLTBtlhBMug==@kvack.org X-Gm-Message-State: AOJu0Yxvg+C8yDB2Vndq7y/+FdLT6fyxeSJ+PAzRtxH+3hkjSOZl9cyK ES+FgAykAXn7Rsjas6qwLdVSB1edps6cAESCwtvc4rsqAgON8rhn X-Google-Smtp-Source: AGHT+IEHqBvvipKDt3pVc4/T+6L4m8BSlDWTikVIuwjQS1tXdrsvfLbUudOAA51rWESeq0to4VuuuA== X-Received: by 2002:a17:907:6d02:b0:a86:a866:9e25 with SMTP id a640c23a62f3a-a89b9727ea8mr1148556666b.56.1725459468821; Wed, 04 Sep 2024 07:17:48 -0700 (PDT) Received: from 127.0.0.1localhost ([163.114.131.193]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a8989196c88sm811160766b.102.2024.09.04.07.17.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Sep 2024 07:17:47 -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 v3 4/8] block: introduce blk_validate_byte_range() Date: Wed, 4 Sep 2024 15:18:03 +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: 6DDBB18000C X-Stat-Signature: nr5nuhzj99n46tibue8n9n83r54b3wdz X-Rspam-User: X-HE-Tag: 1725459470-501906 X-HE-Meta: U2FsdGVkX1+2V9Zm/mZRELjRRJ21ke9cdwvX5/GF2nlIOj0PqZeO5qQfOM+BbRqWlb15GCCUPaMhHZKLqbJPduCgTvDrZ69EALKxF65Xsa92ARYr+uWDXNCZ1aYwc/QG2n4APZZ239QqY4JgrcgJKOcyQDVrbB0dy1ntHiIdKtvP4h47IbzrMC+rMAUxtIqp+SteNY4uVmYb1szpLEHyEFKsRb3nIhJtFzSj4DeJGmxoPMenSuGYlC2Swl0jDbK7Hhv37t4PJ8XKwhCiu2jUc9S/3Cw181MvWwqR6XJdHAn0kKdXXYf9DkebxJUc79CQx03Hl9MQZxzEw7isAvv+hNXWfwml5F74vVvlduS9BWhVIvofOgbDvsdZXby7T8MzujVA9G7xwULXFBtALf3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Ikbae/zkZZy+63heGNRyNX+LTg= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000262, 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);