From patchwork Fri Jan 17 01:30:56 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SeongJae Park X-Patchwork-Id: 13942778 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 C9205C02183 for ; Fri, 17 Jan 2025 01:31:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 705BB280001; Thu, 16 Jan 2025 20:31:11 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6B5C66B0096; Thu, 16 Jan 2025 20:31:11 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 509EF280001; Thu, 16 Jan 2025 20:31:11 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 2C6A86B0093 for ; Thu, 16 Jan 2025 20:31:11 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id D204CB0AD5 for ; Fri, 17 Jan 2025 01:31:10 +0000 (UTC) X-FDA: 83015215500.15.1F01B89 Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf13.hostedemail.com (Postfix) with ESMTP id 4683F20006 for ; Fri, 17 Jan 2025 01:31:09 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=qN8HbqYN; spf=pass (imf13.hostedemail.com: domain of sj@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=sj@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1737077469; 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=JnSAqigFswLAVo7cbt4ibPpt9LZu3mXTrSPqkVEoGHU=; b=UD9CPTtkaHhn37ey+7Jxmy8limiWZHkeT4uH9V4fcwmgs4tHq/2fhwUYuuiR2bVng4nzCz t7lWDsFzYuFRkWAXZa7BNma/SulIvnTL4oGNsRennDIRwgM8hgeMT3OoKhpaT78Clc6dEy aXoHjifXLS2SToTddijSDGsdxoweDI8= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737077469; a=rsa-sha256; cv=none; b=RWbLV1P5ybkhv8cZnYh3dctP5ZBgCg2mfyaePCYR2dnjX76xxTbzoSI6glNSXE7qKKcFkA mX4x0ozoLW84EFtLuIDcjyRZR0Wf7G0XrirdqZWRKsdO1mQtIppwkcsYnNIFHqH0pXhWQp 990pFAFXAJkpvq2ILTK+xeUbEsq4BDQ= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=qN8HbqYN; spf=pass (imf13.hostedemail.com: domain of sj@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=sj@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id A8207A4070F; Fri, 17 Jan 2025 01:29:20 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 287E3C4CEE1; Fri, 17 Jan 2025 01:31:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1737077468; bh=DjxKujI4JGXMt2ixtDL0O5/UiHpIa+TfTVLg8eL2qoo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qN8HbqYNv+0BPFVp7MR/Cln6d72dSLPk4HTE+KFIH1uUdIH632Yk5mBAOWx6ZC3o2 hBlyt4o+gP3uJHBnhLeCvC+GxEOgdlwWaB8a44jYRBCgaMgT3l3ElZ1iszVtpY00gz jrwBxDxUNo9160DThKvjn3nrldDSGMf5Bzpw/N2cciwv7ohZCdCvV9Vn8V6TYPQOpv FsEkq4FDBdLzpJfq40a1+h0KJ4qoXzxXZBFDBG1vnhKtKPKfIM/tW+rxgMwNl0SuZj 7Qac9aAKUD/p3mV6mOu2HJNOezPm7HSBIr3f1HQmoDUuhIkQgswLsS8PNeXiwgErF6 VT1m8GqIhSSyQ== From: SeongJae Park To: Cc: SeongJae Park , "Liam R. Howlett" , Andrew Morton , David Hildenbrand , Lorenzo Stoakes , Shakeel Butt , Vlastimil Babka , linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [RFC PATCH v2 2/4] mm/madvise: split out madvise input validity check Date: Thu, 16 Jan 2025 17:30:56 -0800 Message-Id: <20250117013058.1843-3-sj@kernel.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250117013058.1843-1-sj@kernel.org> References: <20250117013058.1843-1-sj@kernel.org> MIME-Version: 1.0 X-Stat-Signature: tfphtmo33mtyn3zqrwe95sqisadpg6qr X-Rspam-User: X-Rspamd-Queue-Id: 4683F20006 X-Rspamd-Server: rspam03 X-HE-Tag: 1737077469-350585 X-HE-Meta: U2FsdGVkX1+oGxHq3pVYBvESTyjyedoeC43nGN/Sfsci0NxKmKhOG0EGhT9A9GSzYq9R6D/sh7Ul0k6MBC8Ox6ndXr3yIPVpCwAK45r5IChXGMg5/11h1vjAiEa2LbWh6q+2vLcpNYQ+jlgtvjHg5Lw0tHZYPgOubMP58NATqXrb8Du8Y1aIE6x4D0gY6E8Im7uBAW7XY3g/xTNyyYYy9cK0kZLU9k7OA6ltxvUb+MBe2fU++uGrEyf1DjB3Jt9k4xyVLnJr23R9SeUvz4nF38VTx6+4JBNUglDcMiY3pA8wq7QxPhFoofK8rJtVPOdQKZ65gHCJ9B7f7G1+Nd/IslKCCNk4miNHIpDgdFmGbDgDfJwi7OXURjFlnpYTDP0xER7USOQQJYA5FlKr+y7FNDZdqnUJE6khFBiYhsUIZNYBO3bc/ZDyaJpSQmVxSxC0rwDdCmqwbtBdRnmnNzqPKwWrdhEHXOxEgPZMAERTyKljBkYs5VmWYCeguH4qdRMiSQt1HboVCl7U8sSLuR7YxUl1mBUg9KbSfkoR1AjQ4JOyFvlPybXK1Rkh2EjEvdQTTc8+ADnF36WzT9AJfeumDNHjL6w68aurtvTJXm5EZDfssXd3f+iMCpI/X43/1uoQyUe+2PTM4PfjyTS3p+9GcQJ3k1y0VZgi824+3OBp2XrHlmcjWJpMU/UalPSgwMfjcJH+4M8j+JCdRacv2o+fza5HWlYRtXEtFavK7UbYbT+ZmM7m0Sz0Zf9SefYEVTtcF4jXKgwRwA16vSicUCzxL9ML539avDie0xOtakp/YpMJpZj9HXeF5loj6M41TCHNqs62PeOmxgM0HD4H+HalS/0x2QPMauPzYm7RhOIFc9cZvuDXRrCY6Iuj+gk/i2CqEvLYPueHI4PXa4fCMWuOycRWJVOZhFAR7PPYsen8mIq3RvzncVK9BTwT1DUUvQFsSUSsgaC6SgjKn6VtcD8 1Un0aZaF 7HS10QlzWXFxjhTWsPImZvNK1gPeXI0kSM4SEWNJWbEzW3IdTFkTcdLUVQ/FZERg1vtZ0r3oOS9TGZ7lOQpv0PW1wSBPwOHP2FrylXAY3CHYECMsClMxA1mx7CIYZhV5eHovfZjwOPqxbt3o+ip61b0JH7TDwmxJvs1OE/jTmJpaktlXhPI0NP0xGJUjExMeny549nEkjoC1uku+Ivz25XJgtH93pe1ug650YDX2lqWkcSP8= 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: Split out the madvise parameters validation logic from do_madvise(), for easy reuse of the logic from a future change. Signed-off-by: SeongJae Park --- mm/madvise.c | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/mm/madvise.c b/mm/madvise.c index ae0964bc4d88..9cc31efe875a 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -1592,6 +1592,27 @@ static void madvise_unlock(struct mm_struct *mm, int behavior) mmap_read_unlock(mm); } +static bool is_valid_madvise(unsigned long start, size_t len_in, int behavior) +{ + size_t len; + + if (!madvise_behavior_valid(behavior)) + return false; + + if (!PAGE_ALIGNED(start)) + return false; + len = PAGE_ALIGN(len_in); + + /* Check to see whether len was rounded up from small -ve to zero */ + if (len_in && !len) + return false; + + if (start + len < start) + return false; + + return true; +} + /* * The madvise(2) system call. * @@ -1671,20 +1692,11 @@ int do_madvise(struct mm_struct *mm, unsigned long start, size_t len_in, int beh size_t len; struct blk_plug plug; - if (!madvise_behavior_valid(behavior)) + if (!is_valid_madvise(start, len_in, behavior)) return -EINVAL; - if (!PAGE_ALIGNED(start)) - return -EINVAL; len = PAGE_ALIGN(len_in); - - /* Check to see whether len was rounded up from small -ve to zero */ - if (len_in && !len) - return -EINVAL; - end = start + len; - if (end < start) - return -EINVAL; if (end == start) return 0;