From patchwork Wed Sep 11 16:34:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Begunkov X-Patchwork-Id: 13800880 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 4597EEE57C0 for ; Wed, 11 Sep 2024 16:34:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3946594004F; Wed, 11 Sep 2024 12:34:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1A855940067; Wed, 11 Sep 2024 12:34:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EE77194004F; Wed, 11 Sep 2024 12:34:29 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id AE245940066 for ; Wed, 11 Sep 2024 12:34:29 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 5CD01417C9 for ; Wed, 11 Sep 2024 16:34:29 +0000 (UTC) X-FDA: 82553005458.10.85EA9B6 Received: from mail-ej1-f48.google.com (mail-ej1-f48.google.com [209.85.218.48]) by imf11.hostedemail.com (Postfix) with ESMTP id 6942140006 for ; Wed, 11 Sep 2024 16:34:27 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=ZibtDetm; spf=pass (imf11.hostedemail.com: domain of asml.silence@gmail.com designates 209.85.218.48 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=1726072363; 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=j1ilgt3VEuuXKV+jcKbpBuwPBu7e3oK2wW8bPbjcXIU=; b=8r7AkqJBtcVhJK20F6NMMbJWFfvC+dX3DCiFE8BIKLTs9nub0u3DyX0Qey1AX9XHBXHEVB IVDB+371O71xsfb2IstIfvfxWrnSZAAX56YhUocc4JJtXsuWCf+ipe6Sl+0e24HpdsgZtx D89JRDJA2M/Lm8yedZJCalsSDuZh44Y= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726072363; a=rsa-sha256; cv=none; b=vDTZlrEfu91GaCJrXZ8cft2AC+2t+/Rpw2nW6rc9iwc6fi6bgBonTsXPtpDaRmSk1EPgeI 7BQWyjaYj6xGNyOlGTctKFVGzPpP4CWBxkBfZCgvJ9ZT5ZdSM4QMn4B4k2T2iIYBcs4W1r bhaqJd+gGyV+m+DwduvalsLfpAxUhwc= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=ZibtDetm; spf=pass (imf11.hostedemail.com: domain of asml.silence@gmail.com designates 209.85.218.48 as permitted sender) smtp.mailfrom=asml.silence@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-ej1-f48.google.com with SMTP id a640c23a62f3a-a90188ae58eso3536566b.1 for ; Wed, 11 Sep 2024 09:34:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1726072466; x=1726677266; 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=j1ilgt3VEuuXKV+jcKbpBuwPBu7e3oK2wW8bPbjcXIU=; b=ZibtDetmSeUS9bGIKlXFJ547JZcukEcFTVc7LcC1Lbm+eHihjVNAOAohzGW1vAs6QK +yHNuzlQgSag22I0TOOJSDcp/ReTu0qS02ksK5wlKfUuIw8HZZS99Tiv8zZbMv5778dS Wq0Lu2HgS1fQrhBKZEov3hdUXsMnUTj+zLxVqRVjpz5sz1sD07kHpptUYCCebgPP88Pu Ll7mGP4cWjiC7s2VTStwlWi1z+6BtXh4cVbHiF40vCFa1tfQihp9qxaUnnoMa7oQMl5O 363b8TpV/gwsjUSy+NZa1O5BwIxtjxWWWtIY3p/Ro1Uw3gPsEl9g3/5yKcnskeOudQ5g 0q4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726072466; x=1726677266; 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=j1ilgt3VEuuXKV+jcKbpBuwPBu7e3oK2wW8bPbjcXIU=; b=cJgNq7iIl6l973isgI13juqDCFGWnOpZSMu5uc/+j1+vCwRJbjZvgOK000MSeNW4e1 4rkLRUleNcRCmXvtXLA6aChWbvwoKA1bwLkV+pmbxe9rQl2PGtswpxN1iG/ZZZdOsVxW KOg1IeEuKz0d7L0fjC/b0V/9iOcbZEATunWnLVxOrBz2gAzv6AtW5HkPcbW3c9WUE7ul 1vZwPwNOnVHvMYGE/8EXaMjYEqZxkZ09hBnYS+DFCVFp99sTHlhiXLdk4QAswelQVO+n 0K9GWwdCK/W6BPNqK1NFY6aI2Z7TjsE2ODbyRgSb5wuVcS1IYTxkl2ewYs2VTJYOYwJF yx7A== X-Forwarded-Encrypted: i=1; AJvYcCUpYEfch3Ebtbohp9yXn3mJFjXS6E7s1MRbmy28/xd5TWhM/0uOULW1HufkBFxh6aTDmpYOxzuGEA==@kvack.org X-Gm-Message-State: AOJu0Yx9FHl1tFrv7u9cRyr6dqxBAJDid6JzRhpFz5JLlUsMaRiep+e9 3e7p87uHPC+JE7LetB++m0h5KpVRjE2FaR+R4nFK/9laKqXYGrsG X-Google-Smtp-Source: AGHT+IFotWTb4zxEeL7XJDWd5BKSyUFpJRfVXT5ql2T0pZNPygKiYk4YlGgrsyP58zfDitbg48lEeQ== X-Received: by 2002:a17:907:d3cf:b0:a8d:2d35:3dc6 with SMTP id a640c23a62f3a-a902945f284mr8613966b.26.1726072465802; Wed, 11 Sep 2024 09:34:25 -0700 (PDT) Received: from 127.0.0.1localhost ([163.114.131.193]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a8d25c72ed3sm631820866b.135.2024.09.11.09.34.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Sep 2024 09:34:25 -0700 (PDT) From: Pavel Begunkov To: io-uring@vger.kernel.org Cc: Jens Axboe , asml.silence@gmail.com, linux-block@vger.kernel.org, linux-mm@kvack.org, Christoph Hellwig Subject: [PATCH v5 4/8] block: introduce blk_validate_byte_range() Date: Wed, 11 Sep 2024 17:34:40 +0100 Message-ID: <19a7779323c71e742a2f511e4cf49efcfd68cfd4.1726072086.git.asml.silence@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: References: MIME-Version: 1.0 X-Stat-Signature: txc5sc7s859k9b16b9tkyfuoh6un4pbg X-Rspamd-Queue-Id: 6942140006 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1726072467-187844 X-HE-Meta: U2FsdGVkX18+xAnMVtVDWDOKowRzzKSIJHbtABeTN0dP3ri7q8xyiey1qhAxgUtpvPdBdEnD5IlxJBW282jFZ9rtJCvFpAzihxy1sV4QZ4QP2AgHeXB2GtGD3xOudz2h0aIO1hn8pWMYuaNi64VzSc83tGIim/Ru2wE4DW/+TQC0oH5ozZSUh+olbvVqLTOo/GCpfemLJlpWfLupBWTL449enLF8yeyH24Yb0A/WgDOSPv+XPSAPuBBATcJ9XiteJDW5+0/Gx05O6B0hRIrGquzISN6D8oS9STPEkcXIeVPUNHxsNHY8uixoF3BpmJkRf05l3KyqASmGB+n0oo5R+Jnu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ExQ0Rn50+hK74pW40ZduYTss6HkF01Wdc1Gj7K17cgOzpiBoezoshmzhcR9jHCeaKiPhAcnQciDFZXad5Y3D9BzDUOYc= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000001, 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 Reviewed-by: Christoph Hellwig --- block/ioctl.c | 49 +++++++++++++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/block/ioctl.c b/block/ioctl.c index e8e4a4190f18..6d663d6ae036 100644 --- a/block/ioctl.c +++ b/block/ioctl.c @@ -92,38 +92,51 @@ static int compat_blkpg_ioctl(struct block_device *bdev, } #endif +/* + * Check that [start, start + len) is a valid range from the block device's + * perspective, including verifying that it can be correctly translated into + * logical block addresses. + */ +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);