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; }