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;