From patchwork Fri Jan 17 01:30:55 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SeongJae Park X-Patchwork-Id: 13942777 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 DF545C02188 for ; Fri, 17 Jan 2025 01:31:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 61BB56B0092; Thu, 16 Jan 2025 20:31:10 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5CA68280001; Thu, 16 Jan 2025 20:31:10 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4457A6B0095; Thu, 16 Jan 2025 20:31:10 -0500 (EST) 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 149036B0092 for ; Thu, 16 Jan 2025 20:31:10 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id BE03A1C81BB for ; Fri, 17 Jan 2025 01:31:09 +0000 (UTC) X-FDA: 83015215458.24.3AE6328 Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf30.hostedemail.com (Postfix) with ESMTP id 3455C80009 for ; Fri, 17 Jan 2025 01:31:08 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=TVs9VIbj; spf=pass (imf30.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=1737077468; 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=4k0WBxVBuA1c/p1ZWaaEJLPqwXoGR82S74kYvaz+E50=; b=Ch1XFfp/zSRjg9h+leOGNieNrPc4PangGBkQCBOphImTWUYGl8bSlidS7mRuaoDcpVVFQp VCPgHMecEw290vjcRA/M+nGSlQ6xs/96n51I191vQwAee1RaHX3zWu5Vyqw+8btc9ioHT7 rw7corIMCBu7KhJ4xGcfSLoCNzY3CCg= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=TVs9VIbj; spf=pass (imf30.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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737077468; a=rsa-sha256; cv=none; b=atVk2bjAJmA8iX6pdPmLfDrFklNCljFnGvro8A0uIK5RuVqk9Afr7L7lV8FW+SHsjbb+5S trHbak3VK5IHq7i3Hp6mf28sdkWk89FnSD2L3bg45M49UJHDpEyYAJMYC00g3btHNAuxYc kqiAAh1rtbtpPMKZ95ZHb6LBBnzNtuQ= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 8D1E2A41749; Fri, 17 Jan 2025 01:29:19 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0AEE0C4CEDF; Fri, 17 Jan 2025 01:31:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1737077467; bh=Gf6phLdwpHMJaDpy+vZUWVIeNkMlWtEQYgiasVzanO4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=TVs9VIbjk5xpy7aaORGBQQYXuvE+DvECCkKs4rZOuHtgv4IBA6xZy70nkNmI43FW4 r/RrhOJds7U9o5P9/gCQCcMB6QYjlkgoTlkW29bdNvDUxnzseHx6dFGF/jv/Rkhzyy LdfgS8HO2gZgLF8JjxFXalOkAWcqKy9avq4mBYHndUdNdpx4FBoDsjugxdGx8DALFf 7nU5lMD7+4yNbD2ogWdDTzups6cVsp3qxDEdDSFrc4pwFIrizADabFWhDAvPzCBW9r kwoDGvMiUWwxfdKwLxS2YRzTynpgL5ALRTvV4c6wY2uiYB87nR6MTC0n9v128ZQ09P QvM/OxLks/rvg== 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 1/4] mm/madvise: split out mmap locking operations for madvise() Date: Thu, 16 Jan 2025 17:30:55 -0800 Message-Id: <20250117013058.1843-2-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-Rspamd-Queue-Id: 3455C80009 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: mhkbcmxh5hbhiri8ijoc5uapqahrymyo X-HE-Tag: 1737077468-155355 X-HE-Meta: U2FsdGVkX1/wGOJa61NpVFGC0S/yGS8nlVXbCcAbWYoaZ1eJBtsgVPnuMiqGOX+MoXF++pRw2rni63cn9qceCEMMOHDfd3/gj7rfK6QfyqiDmRCMhsbKDgO50HWiAgguG2g1072DDA9yCD39cBaOOhPvHAxsWuEamIO90EmybSzcBCpwEpq2gXDYICrJQp91CI+h+ZYmxvolhCXMP1EC+YnUdD2uKNK4UdLaGImk6uftlzUI4lGrrrzzg7yf1PHksv6sTbVT67kRKU/cxNyAjnrdw+QU7YP3uYFj7Xn21WQnsVDoGix7moy8UhBSc/mK7EnKLhCFEG226+hrfUyVbV71vw5tzCad8REN2KPu7WFhzyI6ur1dEuwE0TGcaoZwmwA2qIpF/Iqp1FSH6yPJTKBwFzJ1wW7OLFrCjU35f8jvg3zVriDNdXnJjBg+mylrmuR1Yt+7o6XeCWWQO0ASwuypInh36GArUx8aQJtYf92695XJC0louRHzM9tohWP80a+XthUyzEJ5I+gNzTwzaa8eZr4xxxZeB1pRxf7mttmmnmchnu7Ul2UFgSh6+qQSucBT3Uj6hOiDCv5IaceFi2Ef6OIJkAQNQ41bAccps6flRplmdGj+45vCequ1utz/aD1daERDpMH+1kWxaXElUe2g2YheL3grT1Ym3nQarXo22HwcwA74E/d/6RcoxaBr5eHGj2H8VNDv3z9TWsR9diNK3LNVu6fPpaJRPzY04k7JayhVVLv4TMulT6kV2Zwv8Uhfr2omD/S96lXn6rH2M0s5Qp/0akqBRWQlP81oSHyibmv9qlZWoyEdqU8vFzYL3Fgdv6zOBO4gzmMcHQ3wiP3xSu6b4Xi3mek9Lx8LsyfxmfIUn55JuvxsPfzRORS47NWd2AXiNqZW2riXFUnALGn93a4PuUkGyAQRX9MtUYSuG0PeALeWIj85su9rLCkjRSaae/4oMLDq4waIWwk NsvNPe9+ XZlPVjFJWoBEVJavjfvdKL5MyvgiZAtrvRRIxJ/p4unFeRR03zX74uE6lvtkxsrpqynY+dxC3WlWYkfwWqds9eHwsUAHR4+KxlP7PIrTfJbI4AwykBy83j823v2iCpO5UyN9Wh1LRuh4vp5BUH0/kux4bDnLOVlJ56DkYKd/D5GpTch1VTqoHDCDUwbyfwKoQwmiw3Ok2oUW2jm/+w466z/uLh6aOeBE2IX2/AVr9kyoiyuU= 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 behavior-dependent mmap_lock operations from do_madvise(), for easier reuse of the logic in an upcoming change. Signed-off-by: SeongJae Park --- mm/madvise.c | 45 ++++++++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/mm/madvise.c b/mm/madvise.c index 49f3a75046f6..ae0964bc4d88 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -1565,6 +1565,33 @@ int madvise_set_anon_name(struct mm_struct *mm, unsigned long start, madvise_vma_anon_name); } #endif /* CONFIG_ANON_VMA_NAME */ + +static int madvise_lock(struct mm_struct *mm, int behavior) +{ + +#ifdef CONFIG_MEMORY_FAILURE + if (behavior == MADV_HWPOISON || behavior == MADV_SOFT_OFFLINE) + return 0; +#endif + + if (madvise_need_mmap_write(behavior)) { + if (mmap_write_lock_killable(mm)) + return -EINTR; + } else { + mmap_read_lock(mm); + } + return 0; + +} + +static void madvise_unlock(struct mm_struct *mm, int behavior) +{ + if (madvise_need_mmap_write(behavior)) + mmap_write_unlock(mm); + else + mmap_read_unlock(mm); +} + /* * The madvise(2) system call. * @@ -1641,7 +1668,6 @@ int do_madvise(struct mm_struct *mm, unsigned long start, size_t len_in, int beh { unsigned long end; int error; - int write; size_t len; struct blk_plug plug; @@ -1663,19 +1689,15 @@ int do_madvise(struct mm_struct *mm, unsigned long start, size_t len_in, int beh if (end == start) return 0; + error = madvise_lock(mm, behavior); + if (error) + return error; + #ifdef CONFIG_MEMORY_FAILURE if (behavior == MADV_HWPOISON || behavior == MADV_SOFT_OFFLINE) return madvise_inject_error(behavior, start, start + len_in); #endif - write = madvise_need_mmap_write(behavior); - if (write) { - if (mmap_write_lock_killable(mm)) - return -EINTR; - } else { - mmap_read_lock(mm); - } - start = untagged_addr_remote(mm, start); end = start + len; @@ -1692,10 +1714,7 @@ int do_madvise(struct mm_struct *mm, unsigned long start, size_t len_in, int beh } blk_finish_plug(&plug); - if (write) - mmap_write_unlock(mm); - else - mmap_read_unlock(mm); + madvise_unlock(mm, behavior); return error; } 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; From patchwork Fri Jan 17 01:30:57 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SeongJae Park X-Patchwork-Id: 13942779 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 43250C02188 for ; Fri, 17 Jan 2025 01:31:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DBBE06B0093; Thu, 16 Jan 2025 20:31:12 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D7D066B0098; Thu, 16 Jan 2025 20:31:12 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B4CE6280002; Thu, 16 Jan 2025 20:31:12 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 939CB6B0093 for ; Thu, 16 Jan 2025 20:31:12 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 2676FA1411 for ; Fri, 17 Jan 2025 01:31:12 +0000 (UTC) X-FDA: 83015215584.28.A70BCC0 Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf30.hostedemail.com (Postfix) with ESMTP id 913498000E for ; Fri, 17 Jan 2025 01:31:10 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=LaQoBjWA; spf=pass (imf30.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=1737077470; 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=Sl/n7tMIYUVbrjQmIpH54kIXy3INbLjVgCRDXXa2xCI=; b=YNLfAnYtizazPLPeiJ/MWXUs2YsGRZseoF2nb1FBgLvAiX+4LbFyYiNvFlKc86OqOVEbkR FC08fdl3pfJdIk6XNCe7oieWeTIq06ols4bEEP5lsIvqGGp91y0N6dpnFXzNY2clme2oAI zENwNVmSEBc6w75H3/nZ6i6c80OrFIg= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737077470; a=rsa-sha256; cv=none; b=EBYGdY8z6kycB9FUK4JS7dnT1ZIF3iIundTAJFjS4xBbjrsXgnYdX3g0xz1usiL0UdDOrF 4Lu7jYxZBGQeB2xzqprza7MUvqT7fFx/i1OZLlGMqhuUW5ruub9P4Ens301xUZR8A/t/YP l8P+pb4t9i3LMAtuJ61mb4AAuxXoOXo= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=LaQoBjWA; spf=pass (imf30.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 EBBD3A4175B; Fri, 17 Jan 2025 01:29:21 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 488AEC4CEDF; Fri, 17 Jan 2025 01:31:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1737077469; bh=kwZzbvixIZEJHWiCRUl/KCUq+QQFe5aGtgrwZ2gLoJg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LaQoBjWAhd/EZqsQlNeeWk8Iq3YfhRWsz6t9O3+vGwhYiSbDIUeBcwX5LHqcLSsjv t8ol4WiiNn4qz92p4G27mXp6HGL1MaI/dWr9q6MAvm1laS5nkwsEN+Cm2QCjEd6z/L 6A6p4QuxQ81N8R9kVLQ5RWpZNXbYUBFXqknxScIda+VnhJlOuC3KzFOeDNDNwh+cjk c2a031uobFUPFKbiuy28X0pyr4Mjb8HlZa8Ky6DRCGmGYEnaYfUqs80Nox2PQCPPnK /pUZTG4iY5Y+8Gm3OPFYblxubRvjqZNiUKAgJHnVcQ5piFykpazRKWKEZWy2gFWgVh ZmjwsOn/eXpBQ== 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 3/4] mm/madvise: split out madvise() behavior execution Date: Thu, 16 Jan 2025 17:30:57 -0800 Message-Id: <20250117013058.1843-4-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-Rspamd-Queue-Id: 913498000E X-Stat-Signature: tax1fqh4nsy9fhgqsqfrcen8g6eoipwg X-Rspam-User: X-Rspamd-Server: rspam12 X-HE-Tag: 1737077470-491547 X-HE-Meta: U2FsdGVkX1+ADZjcF+4g1B0aSDGQ5iSFwiakSWUK16UmkgYEM2exlnZUuKrXTi6e40D1QvKAJcj1AsKP6O/WYAcGSd4dIu2y/CLb4JpydqDiut5xtBDH1V/cGNi35d9vNPeBs9AAxrh5G3h5FQVkOOhTuF3AvmWQSjTn4CBZ1bBDnTYWorqz0Cnv0i6zcaRN3EEpkfS47ughMT33cS1jewWXK7u8326RAcKxUftVV42wMHaJDRPtMJLe+FOQprYh5/DDPCBIvF6AaOJ90EZZMwaZ1ebeIeLZRJ1I8gPjupS3uYOTjQZ5PdrqKdxWkbALKwEnM3qdzfWhGcQUh2zX46LfzGyfEwrE0szRlSflcu2u5VHdCBEd61cwmeO/VLKg4LNDZJT9m0ZzkB1NZPBpwbDNLOXE9kMA3Vts+kVzUgR5YzyqzTgUGpD1rAppH7Fa/B7UioDi1gQ8YhKfFweiyRWY1oQmEEDMSHfNWjK4D0x+saolcE43ex35TzBQPR+hYZEN+KFXZZX20M+Wqn8wMDL/WPClGxZGeofgEKgzdH4nAAc+dM52hwey+RRuEPf16ae+i8GxDIZtmtbGu/92vG0YvO1Rf/mmd+me9WJ8oG5thZVbAMqK9lUOW8omDbYi4/3N3GKLA81YQ0Cc5/O2H9TPayEGBSADRiJFBvDaoOY3wR4KcWRVQTxNFwFoe5zlqaR+STguyj1no674qS8wRGHge8c8FsoATceyO9vMbCSAr6vlJPiYMj2e5D1nOrSygGw8sl1O5PrNLZ/8K+XyeRo9niX9IpCWYtosIrThNN81PKSC4N+SpGzB9h1GjbBI1bTY68ZwM6ySi+tC0qdYRAXnTVOk3FMrViYu7frJAmthibAR6DZYqUKNYG4TxwlDjkTP/Gt95GvgyOUJlKN4/jTjS2PsSh6Q7SIs+eDJWPAeX4lkP9hirlRLwfQYwx4m96bSuks4037WBR52Byw gSop448Z RTTA3IBzM3fICEDFOU957NjiZMbFXv/i5Uh5KUE9diMwKNC3pVWgl6c81jEI1uUIz4H6N/DGhvtNKybWHVE8mobuBfES+hjMECwQi2E2knpYPUkvNuWIXxFHMdfeyHaH+eNTYyR2IzdYprM9qco+IpvM9Om5dKWrnFjTzK4WPzCSVOqtDFvJX/wb2kTDvNhKY8/aFBYDdJZcfrYBB/jOWr2yH3Z88oEJcBH+xjPJawZKYJbA= 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 behavior applying logic from do_madvise() to make it easier to reuse from the following change. Signed-off-by: SeongJae Park --- mm/madvise.c | 53 +++++++++++++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/mm/madvise.c b/mm/madvise.c index 9cc31efe875a..913936a5c353 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -1613,6 +1613,35 @@ static bool is_valid_madvise(unsigned long start, size_t len_in, int behavior) return true; } +static int madvise_do_behavior(struct mm_struct *mm, + unsigned long start, size_t len_in, size_t len, int behavior) +{ + struct blk_plug plug; + unsigned long end; + int error; + +#ifdef CONFIG_MEMORY_FAILURE + if (behavior == MADV_HWPOISON || behavior == MADV_SOFT_OFFLINE) + return madvise_inject_error(behavior, start, start + len_in); +#endif + start = untagged_addr_remote(mm, start); + end = start + len; + + blk_start_plug(&plug); + switch (behavior) { + case MADV_POPULATE_READ: + case MADV_POPULATE_WRITE: + error = madvise_populate(mm, start, end, behavior); + break; + default: + error = madvise_walk_vmas(mm, start, end, behavior, + madvise_vma_behavior); + break; + } + blk_finish_plug(&plug); + return error; +} + /* * The madvise(2) system call. * @@ -1690,7 +1719,6 @@ int do_madvise(struct mm_struct *mm, unsigned long start, size_t len_in, int beh unsigned long end; int error; size_t len; - struct blk_plug plug; if (!is_valid_madvise(start, len_in, behavior)) return -EINVAL; @@ -1704,28 +1732,7 @@ int do_madvise(struct mm_struct *mm, unsigned long start, size_t len_in, int beh error = madvise_lock(mm, behavior); if (error) return error; - -#ifdef CONFIG_MEMORY_FAILURE - if (behavior == MADV_HWPOISON || behavior == MADV_SOFT_OFFLINE) - return madvise_inject_error(behavior, start, start + len_in); -#endif - - start = untagged_addr_remote(mm, start); - end = start + len; - - blk_start_plug(&plug); - switch (behavior) { - case MADV_POPULATE_READ: - case MADV_POPULATE_WRITE: - error = madvise_populate(mm, start, end, behavior); - break; - default: - error = madvise_walk_vmas(mm, start, end, behavior, - madvise_vma_behavior); - break; - } - blk_finish_plug(&plug); - + error = madvise_do_behavior(mm, start, len_in, len, behavior); madvise_unlock(mm, behavior); return error; From patchwork Fri Jan 17 01:30:58 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SeongJae Park X-Patchwork-Id: 13942780 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 95609C02183 for ; Fri, 17 Jan 2025 01:31:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 494CF6B0096; Thu, 16 Jan 2025 20:31:13 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3F63C280003; Thu, 16 Jan 2025 20:31:13 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 222C0280002; Thu, 16 Jan 2025 20:31:13 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id CF7DE6B0096 for ; Thu, 16 Jan 2025 20:31:12 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 891F4120A8F for ; Fri, 17 Jan 2025 01:31:12 +0000 (UTC) X-FDA: 83015215584.07.1E5BB5F Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf13.hostedemail.com (Postfix) with ESMTP id 010AB20006 for ; Fri, 17 Jan 2025 01:31:10 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=i1LgonO2; 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=1737077471; 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=fcAsXCh0cphX3zfC6MtqTcyDqWfkR33OaSXaWk5nt4A=; b=ZoH7p8VEwdYWVUgAqUIv7htw93wruwJNkraqOuNaMX0MztglCIUs4etyn0P3a7fCGAsXmp 31SzE/OOfUxb/hKp9h2HcotP/uIAjg0TqDnCZTAyuksceFFIdwIJM6FlRn+KW7t8PFjo/e jjlZQ/d10k0yUoR7qC/TJYZ3TLkxS/Y= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737077471; a=rsa-sha256; cv=none; b=sE55pftXwhrWSH8pLquz6vHvq9hxxJHDOev7/svUd8uIkbnoxnG1eQFIZ+jcovcYkAXy7L o0ytkmpN8uF8aKBvoJiQhgkYpOwlqOoCzuF+tWs9X3n37RkQjQmEIsbzwbDqUMxy8ngL4o WKajm06tjGRNZOgVjZHJdEH9oRUKYlk= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=i1LgonO2; 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 B8D5BA41898; Fri, 17 Jan 2025 01:29:22 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5EE7EC4CEE1; Fri, 17 Jan 2025 01:31:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1737077470; bh=E41yb3derXfi0u00Nrf+03FhBriw1gS2QNY0ZO6tnXY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=i1LgonO2IFisFxXpOtYWRGWHbaU6RQ+sSLEZ/49a3dliUM8D5Q9j3NgynM6OAWyDn I8NEz3h6ZPWoO7CYSdLy7t6seEbz1AY+8K7MrWWSMVjVkFxLaOyyeXWfy9pOwn7zdG HJtqneNclvmwIYiCDnagVmQQkzh2nqSkm3AmN5u3wIU4YCM3tmRG9OT7c2aCw0cB2h 4m/zPi+JDb7K28wxwE6XkVT1AawSjgG1awi2EkrnoJl2x86ddyAvQe5T1CgCUZCyr2 JzkfjJiGghIwGi8GQG3GTDN9EHMwR4YAFhP50jL3XMCHx+PEsljy4JxF4cZffknNT7 Ig1TJ+Y8Gv7fg== 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 4/4] mm/madvise: remove redundant mmap_lock operations from process_madvise() Date: Thu, 16 Jan 2025 17:30:58 -0800 Message-Id: <20250117013058.1843-5-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: ud661tbae4ma51iijubwgnx831pnctxa X-Rspam-User: X-Rspamd-Queue-Id: 010AB20006 X-Rspamd-Server: rspam03 X-HE-Tag: 1737077470-596606 X-HE-Meta: U2FsdGVkX18sHMiRScUXHjnSSkDawIc7CdGqIe27UK5BdWh/xifd6jX4jYA4KnYBSkQ7I9SKefQnu+jK8GW0Vi+9yZNSSqzrZ2583me4RrCEfcvMtbD+AFpzkzbXYkeK7RNR70MvDlaNAFBaY8IrLfbQZYodguuGI0Jggp1Cw8GLWT1ovMxmlobhyqahD0ll3DHOWd/D7QNj1+Z20XJGt+irHmjqBeuDr/uKOuwHlgAkZasA46r67bpnSwFxmjipPaLCqNPjh7iH6ApK1klNgG2tMrYVPlAh2xb2AqRheXVlFJJJuFCLwC2MrKrAvrdLc8hNkuU8Vah8l2bcQYej/cmOOdoEUFKMafaotfbiQpDgNStuhhwS1BKqWuJE7qz/c0zu88PJoS7KmRac8w5RRMssDlu9T1OF1FmhziWy2VUcF6qzT+adlmmhDb0w5rKjxB4mYd/5KScJ1CVXukdiJQONAkYHCACuU7fN6hvkFRyrt8pW9h/6Lx3XFCQWXTLFmTVa5RZo4R2N3wAwWcjwR2CgavpvQHnhPU7vO9TJVbDLPytRswEl0ynuScpB617a7BcF6GGT/PlqR7p0nRkGweVVdRbs0az9uZgQeuNwDwzawpr/gb2ic50Kp9wWRb0Cxb5NuU7ciyltzbD0A2deF6n/vjsvVBh4tzZMI2hbck2zB4PvRLK+/Hh7z65HGKX/MW+BFPElHA0h7pfC1igfIqtB9Z3fK9pHVITpAtJhF8ReQNrcVqbmRP+jDNCuZpuy0IzKtjW/F8AvrT0ZMB88d9C/mHu4EP8T6ibLISOuZV2D5Fds+/VyM9Sq+SPwZriS2baz+z8SOIEvOSvHt+txFHCYEJ/vdNwCMIhJQ6kNECw4typ46NIMvmgCDKac9tyoKdvAgji41nL2RQsC4Kk26rSTa8jV2KlgVPABy313HlpkkkXxWWrEi9mEenJW15+SkFBpVs5o/w+xRxeKM8Y ieIjGH0m HY04axtflx4kbjwN4ibEVAMGaDKY8Agq2yiYHT8YzBMElF1iAokkEc5EGR+EKoUj0VmA0jYYLXqbb5dZTeVJFfEDtAijX2AiL3JhOmpZg3G45ZmnLMxMOpH6we2i5fqQ+oXGuU1kb1A5LWe5p8pnNY5RUyEXS99QNwfTsOGJPu2MD6Q6t1TjO93e9vzsG9bTQljraUDRmwagJeyKcUmXaxlrNijW5dcgWIWP9gPC7XSNL63Y82x5t6Dfbwz/7fNpzk9c1mX3P3YuwkpMxFSxnW5bl7A== 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: Optimize redundant mmap lock operations from process_madvise() by directly doing the mmap locking first, and then the remaining works for all ranges in the loop. Signed-off-by: SeongJae Park --- mm/madvise.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/mm/madvise.c b/mm/madvise.c index 913936a5c353..1a796a03a4fb 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -1752,9 +1752,26 @@ static ssize_t vector_madvise(struct mm_struct *mm, struct iov_iter *iter, total_len = iov_iter_count(iter); + ret = madvise_lock(mm, behavior); + if (ret) + return ret; + while (iov_iter_count(iter)) { - ret = do_madvise(mm, (unsigned long)iter_iov_addr(iter), - iter_iov_len(iter), behavior); + unsigned long start = (unsigned long)iter_iov_addr(iter); + size_t len_in = iter_iov_len(iter); + size_t len; + + if (!is_valid_madvise(start, len_in, behavior)) { + ret = -EINVAL; + break; + } + + len = PAGE_ALIGN(len_in); + if (start + len == start) + ret = 0; + else + ret = madvise_do_behavior(mm, start, len_in, len, + behavior); /* * An madvise operation is attempting to restart the syscall, * but we cannot proceed as it would not be correct to repeat @@ -1776,6 +1793,7 @@ static ssize_t vector_madvise(struct mm_struct *mm, struct iov_iter *iter, break; iov_iter_advance(iter, iter_iov_len(iter)); } + madvise_unlock(mm, behavior); ret = (total_len - iov_iter_count(iter)) ? : ret;