From patchwork Wed Jan 8 16:18:40 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 855777 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 65225200127; Wed, 8 Jan 2025 16:19:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=205.220.177.32 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736353156; cv=fail; b=BYrvXb6FIFkfBGZLsXj66qiW7K2zRc754irPrTGfpSNYxNdXsJxbAuK838YkNDqpKXeeapzwGQDp4+0BHdZeyhdCZPIT7WLj9bahtBRV+WZJqzhiwMVCNNzRuPO3BRMpF16y0hDb+WYser7FNlP3rQ+Q7UUB1vbt/fAKnIqY178= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736353156; c=relaxed/simple; bh=5g6Mg7MkbpefLEWNeqlAH+svKfVdHQj6cih1SYEeJCM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=ogZll48W4A9isGEaDyTEVIT01ZqHt26mDgefJCV7+g99vU5I7Zj1iqI3ThNJTVFx9m7aWCD1NgSDpig3cnDDZvYThMEpiyXIm1J8V46Mk46HTBacXi3USK2UhDX/w2KWuVO2tK88+h9zfEi1iKmMqiyqHgPM4YM/rh3bHZXiJvM= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com; spf=pass smtp.mailfrom=oracle.com; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b=RWnyotQE; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b=Eq23ICEQ; arc=fail smtp.client-ip=205.220.177.32 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oracle.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="RWnyotQE"; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b="Eq23ICEQ" Received: from pps.filterd (m0246631.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 508FNI5V017695; Wed, 8 Jan 2025 16:19:02 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=cc :content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s= corp-2023-11-20; bh=TeW/+yJcrglGhUjMtcHmixJQKg4v6rstkAUVUeseIlM=; b= RWnyotQEMtOTg1BGQnAEk/X+3ocXuzxIT1Vys/Gx/Fn5HjbrxWHpx7k7LyieuhZC X2P/VQsxK7cYYdE4aGAPEmtnd6Bm7IXOLNINtfJO3syjUQZBlR4S30sAwyvrkwyW 4xmj6NyKJO4ldMUzrV/vHtt7dcEZMF0wWRglU9+ApceqRxqRqAgsIMgeggZrD00s UhfTGwgYN2hw3pAmOXSN5NZV91vCBgrqyY8IqLqSfzscVI9f5wM9kBP3JhKERiBp 7uEs1RV+cNnrprM8dDBPxqKGdhXNBKq1D1iyhVhloKbRJfwL5AfeRnxW//QMvb7s nrQDc3iisEmc/gbJ9SX2gA== Received: from iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta01.appoci.oracle.com [130.35.100.223]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 43xus2f7td-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 08 Jan 2025 16:19:02 +0000 (GMT) Received: from pps.filterd (iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 508F5tOj011115; Wed, 8 Jan 2025 16:19:01 GMT Received: from nam12-mw2-obe.outbound.protection.outlook.com (mail-mw2nam12lp2048.outbound.protection.outlook.com [104.47.66.48]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 43xue9s9ps-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 08 Jan 2025 16:19:01 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=W4gdT+7hwaGcSn/vCeI6geDWnpxrlmiQJeVaVUFccnEOtI86i2J82RY8lzCZZJtFhGZxLys09m5fLb+rBPgIIdjdDUSgVU21yIE5Sr0SWWuHIMHVJodUm1ivzcRvyCUvsWa5Osa8rVocffUfoSGmYzQbf/IcJp1LxQKjy7tGNKX0ZoeMHqFheFJjsMtu/qNe6HfLvLzSx/Xgy1/5BOqCtuBtcnSMRBnIq7k/ylMy4mnzMTG9BTCP7T1a6QUADvKPRHbFe9gS6DkMydxvyUqrntPDbAMiPiE2faixdlg002mVP/oOvxVWeFowJoV64hfPh2BFGVv12rUS8sgfLPeyrw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=TeW/+yJcrglGhUjMtcHmixJQKg4v6rstkAUVUeseIlM=; b=WOv/8cTdQTCMh9/qyQJcjNy2e5HqNBnIO3pHxq3qMDMhhuhR7d+PuzeS7S6PZD+VHfoqrNmVXtu8OFwryGwrNh2byOzu4IdVmmQFO+YNiEzklj+VCUr+4piWDVD2IKIJ8tqf2+WIM5nR5EO++2drNHtwXTlra47nXQviXYxk5rjjjg7bOB1PcrlErn10gT2HRkRDfS8GZrhIL854EO/g6IAPb7guGusE+x55cDnYFylJq0sUGwwF2SD8eju6wOxylf6oKCHGNzDeT84iEcBwpTrtxfA8L8+WvOLuLPSI7XNY5WGSorqK2BNU6NcD/s9zJn/3hJW7gSb5hZYOspcg8w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=TeW/+yJcrglGhUjMtcHmixJQKg4v6rstkAUVUeseIlM=; b=Eq23ICEQuhM7omv1xTKjHAdBW3ZSAKOti/bdCVNa2tZDXhi6agPlUOx61hxzJV/F3OqHXSxvd3mlAopf7Et0uA8qWudNU6Ce/V2oK3SrBWuGKRpwm9TTn7iQJuC6G6oT+x7KRO04CMuQJuUxKGaWvW2m+L9BgT6dcMKkFg7n3Ms= Received: from BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) by CY5PR10MB6046.namprd10.prod.outlook.com (2603:10b6:930:3d::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8335.11; Wed, 8 Jan 2025 16:18:59 +0000 Received: from BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9]) by BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9%4]) with mapi id 15.20.8335.010; Wed, 8 Jan 2025 16:18:59 +0000 From: Lorenzo Stoakes To: Andrew Morton Cc: Jaya Kumar , Simona Vetter , Helge Deller , linux-fbdev@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Matthew Wilcox , David Hildenbrand Subject: [RFC PATCH 1/3] mm: refactor rmap_walk_file() to separate out traversal logic Date: Wed, 8 Jan 2025 16:18:40 +0000 Message-ID: <0c53589f34a6195938eeb58c3a88594fa30cc90a.1736352361.git.lorenzo.stoakes@oracle.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: References: X-ClientProxiedBy: LO4P265CA0063.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:2af::16) To BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) Precedence: bulk X-Mailing-List: linux-fbdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR10MB3366:EE_|CY5PR10MB6046:EE_ X-MS-Office365-Filtering-Correlation-Id: cfc8154b-c2a1-4298-b3ec-08dd300027fe X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|366016|376014|7416014|1800799024; X-Microsoft-Antispam-Message-Info: 3ahoROuAXolnvzPpI35353fLbrlSN6EOvisdQ91yODr3/NpPllsrpogegcfzef5FbXYufbUGX56shMt5mumNgBkjCpo494eRyadm+0MY9tQC8Lr76Gscl/rpdpLjQviL8Lu+G3YpmO0Br5BCwW7eDQXEzotGKyW41wvQCUsBm91NOqQZuyr5TROGz5Lk5jG4x5u2WrJja+z3As9wtcGLlIGK9MR+PamHxVmzfZsR23yh6EN9TykFbHsEq4tWcfODRMj1fIONwWtuaP09gaoWG+eUgy4Gb1/UNgB1zQDMlP7JknbjiX9ILltnBxu1sUOjUgH1NMAUmUK2f4nD5u8PWsa/XFTeJy5WguAnhFB4fC5k7nQiPGs2qf3SyScozhkSPZr6PGHVjWLTEr6c3s1hmbE+B8qdLMgHimdi8IhqP6ACGPsgjgGt1Ykx4Skqk2hde5fiKs5mCB2E/1qiiy1KQ7HiPjrs8znROLsC3eqEHEbDQ3TnLyaY9lobN236JxRO0f1D+Jc97clN8KGyn71bhUUVC2Hmtn+8z/Y/4mfs+wflsbRe3cuOyHkWhqWO82voh1HjXTopPrt3xvNAtmOM+I6y4JMFW/RdBNQgQnhcBUvUOFJciMzLPbz8k8EWy1HwkmdtB2x2pmNMqV5JY6PLa609U2hFLt3w7S6lfYOfHlHjaQnmWKO7vwz9zypXr+lW9u0uAdT4MwwIn8/4KV8thNis5SmGNcp4ZtyOk1LpU7k7DCohdtOUJhEIMLtdRrpFaGoIaoouD5jJJWgf0U1JpwIEY/0lUK1gU78JGzzey8RrTcs4wFVl9iDbxMED3P2SX9eKhLUpVsDK61CHNu1wljDHtOL1nH+rsEbaoLwpJ1B9VcQVM/OKAUbv2nNk4dPafGNAVS9DJz4mjvBSfPqVLRxfe8GstszvUr/1bZ5KUSkfzJ1X2BrlChLupc2D79A7rTaH2+Lb4VkAR7mHjBtD69cyra2iF91RLWjtZ6rDsJPDhs8byrUTVsx/AHU5M2i+N++10miWic1w4zOCMOgC/X2hKbepmHkO5h9AFrGuScSMCmbzi2ybeKs3jZXPaJ94KLiP/PLDKDBOWMVprnd3tXNvf1wGgczJFSNpscP5SBSUiJH4utNXDzWf/VSPk1NxB7DDw4qCrO/UrCfTSpHJJCLrY7smVRob7Vefpi9+iOo7EnYXolVF+viA8i3fELr93pJhunzRmuQD9EzSIfDRfB0kJD39ViZESFylm1znsoo+/XmKUSZAqekZubFs8uX/dAzLcmMw63IyJu1NwAqpy+GIadD7da3rP96kqUa1metQ6DdgsGLNfA3BiNGdkTFP5OfsjEgfCVt2UkQZRxkryO0VIvpGLwKn8sEQVMZI2n645JSCgWWBFLwmbHfhDsQG X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:BYAPR10MB3366.namprd10.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230040)(366016)(376014)(7416014)(1800799024); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: ucPBUDtYS6gcp0bQdME2RKnSpyz5JPYE+2hdOcVtN9qLiOJjU2URUvPE/x4CNOlzsiezjdJ0Dgxc9W7UMzKtLUeAVlaZtx6FWWTGqYlOrTrWhyj/PpwL5JBFBosi1sbi5CKMSBg/MNDOxQvCv/za1pvovhwxHFU1XpADIkU2usSRGVu/cIT5QOCA4flE9Y7qxe0o1nETpie9hJkdoMhjSY9mk3n5St4VQrNDDtK/2ejG/8v10zfXmela7AFt4JUBnIbUXGcQNAPVXRqA2QZ6z2ZUT08akr+715SgKJEuAaQTjbpBf7OGPYSFYyODK/+Yt9+pKlLKxBcjrQ4lDTInd0YSxVNsyvw3hwls9GLbgmMGnahXZ1vqqZ4OOpVCgGghsfvKPPqGPu+QSnWkHXklzrWrYSXFZ72Y9WXbGbTwYRdX3kHIc2iTFq1AuiArW+dghD+u/lsifsHiDj5gY2rOhfB/jDj9g0brbAZvXzpJMGuecm6IWFdoABrJuLNw45gjm9VbT29gYxfUoEQZ3AFe6K36YRcALEwcDiylsRLF6VUjyPlu8cOvtWRCypq729rb+uyJY2PwwRqS/jqLy005jb+pPQGxWn/5RpzoCM6N7sjvtmdOwC1EvuJtu8QjAGZEKUtn0iZ3Wzvq2Q1SnC0E7fLM2K6dBvWKrexv6p5FflKCBAJMB1p7gRbCAh5/MsGVgWePQpMJ0ifEzRWcoD3QXsQZ4jN62eI+dZTLg6EsvjnpuzE/rW+8MnkWyBPRFhcUyEsk7NnvhYuvi381dXKZFelvG2V/1useT+IMfDY6Xucez8ETWbl3+6y51pNlsmZ9dghf/E6vipTuLY3n9AKiLyLyyoappLLKVKjNP02U7bvV26OhA1bvlqVVZcEOzQdc1LXvEWMf/k2rmhwKY7BO00ZLCguZDQhXh/P4kOGue3uVzkszJABRH4PthAp9xXmXOQY8kWQ2SSqX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ZoFV9gVGyyxdfEURdahebKnj6SPLCDv6+k3llsFyLPntsdvqmOMkZeeDTGVx4ZVZO5tuvn7Sla9yNohsRuKBDrqStywospuJFnOGX+521AtvRfll93GfjnRc68meyvDi6PYIx/kGKc8EkMYUGz+f3MRDfi9jthBVi9mc8L6KaQekyOTFh1erzVn4hYRIGdjEkcoSdS1a3Bq2Op9b2bwWuZ+KNVRIPp+HG1bHPzLe+mhUTta98gnhtl7K0gnN5Yr+hCbmr0= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: cfc8154b-c2a1-4298-b3ec-08dd300027fe X-MS-Exchange-CrossTenant-AuthSource: BYAPR10MB3366.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Jan 2025 16:18:59.1281 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: GP4RvtXxEuGeEXJhRK7KmiMRiYscA+4+/GLZv0JaHeKK9kgLVfmgimlQSqBG2/fJq21g59bVn40hTQIcEm6bJcE5OjJOJ+CTO6R0b4M6Cc8= X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY5PR10MB6046 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1057,Hydra:6.0.680,FMLib:17.12.68.34 definitions=2025-01-08_04,2025-01-08_01,2024-11-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 spamscore=0 adultscore=0 mlxscore=0 phishscore=0 suspectscore=0 mlxlogscore=999 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2411120000 definitions=main-2501080135 X-Proofpoint-ORIG-GUID: csesqimgMFh39tn_UcBcPEE2_sOHbexQ X-Proofpoint-GUID: csesqimgMFh39tn_UcBcPEE2_sOHbexQ In order to permit the traversal of the reverse mapping at a specified mapping and offset rather than those specified by an input folio, we need to separate out the portion of the rmap file logic which deals with this traversal from those parts of the logic which interact with the folio. This patch achieves this by adding a new static __rmap_walk_file() function which rmap_walk_file() invokes. This function permits the ability to pass NULL folio, on the assumption that the caller has provided for this correctly in the callbacks specified in the rmap_walk_control object. Though it provides for this, and adds debug asserts to ensure that, should a folio be specified, these are equal to the mapping and offset specified in the folio, there should be no functional change as a result of this patch. The reason for adding this is to enable for future changes to permit users to be able to traverse mappings of userland-mapped kernel memory, write-protecting those mappings to enable page_mkwrite() or pfn_mkwrite() fault handlers to be retriggered on subsequent dirty. Signed-off-by: Lorenzo Stoakes --- mm/rmap.c | 81 +++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 55 insertions(+), 26 deletions(-) diff --git a/mm/rmap.c b/mm/rmap.c index 227c60e38261..effafdb44365 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -2710,35 +2710,37 @@ static void rmap_walk_anon(struct folio *folio, anon_vma_unlock_read(anon_vma); } -/* - * rmap_walk_file - do something to file page using the object-based rmap method - * @folio: the folio to be handled - * @rwc: control variable according to each walk type - * @locked: caller holds relevant rmap lock +/** + * __rmap_walk_file() - Traverse the reverse mapping for a file-backed mapping + * of a page mapped within a specified page cache object at a specified offset. * - * Find all the mappings of a folio using the mapping pointer and the vma chains - * contained in the address_space struct it points to. + * @folio: Either the folio whose mappings to traverse, or if NULL, + * the callbacks specified in @rwc will be configured such + * as to be able to look up mappings correctly. + * @mapping: The page cache object whose mapping VMAs we intend to + * traverse. If @folio is non-NULL, this should be equal to + * folio_mapping(folio). + * @pgoff_start: The offset within @mapping of the page which we are + * looking up. If @folio is non-NULL, this should be equal + * to folio_pgoff(folio). + * @nr_pages: The number of pages mapped by the mapping. If @folio is + * non-NULL, this should be equal to folio_nr_pages(folio). + * @rwc: The reverse mapping walk control object describing how + * the traversal should proceed. + * @locked: Is the @mapping already locked? If not, we acquire the + * lock. */ -static void rmap_walk_file(struct folio *folio, - struct rmap_walk_control *rwc, bool locked) +static void __rmap_walk_file(struct folio *folio, struct address_space *mapping, + pgoff_t pgoff_start, unsigned long nr_pages, + struct rmap_walk_control *rwc, bool locked) { - struct address_space *mapping = folio_mapping(folio); - pgoff_t pgoff_start, pgoff_end; + pgoff_t pgoff_end = pgoff_start + nr_pages - 1; struct vm_area_struct *vma; - /* - * The page lock not only makes sure that page->mapping cannot - * suddenly be NULLified by truncation, it makes sure that the - * structure at mapping cannot be freed and reused yet, - * so we can safely take mapping->i_mmap_rwsem. - */ - VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio); + VM_WARN_ON_FOLIO(folio && mapping != folio_mapping(folio), folio); + VM_WARN_ON_FOLIO(folio && pgoff_start != folio_pgoff(folio), folio); + VM_WARN_ON_FOLIO(folio && nr_pages != folio_nr_pages(folio), folio); - if (!mapping) - return; - - pgoff_start = folio_pgoff(folio); - pgoff_end = pgoff_start + folio_nr_pages(folio) - 1; if (!locked) { if (i_mmap_trylock_read(mapping)) goto lookup; @@ -2753,8 +2755,7 @@ static void rmap_walk_file(struct folio *folio, lookup: vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff_start, pgoff_end) { - unsigned long address = vma_address(vma, pgoff_start, - folio_nr_pages(folio)); + unsigned long address = vma_address(vma, pgoff_start, nr_pages); VM_BUG_ON_VMA(address == -EFAULT, vma); cond_resched(); @@ -2767,12 +2768,40 @@ static void rmap_walk_file(struct folio *folio, if (rwc->done && rwc->done(folio)) goto done; } - done: if (!locked) i_mmap_unlock_read(mapping); } +/* + * rmap_walk_file - do something to file page using the object-based rmap method + * @folio: the folio to be handled + * @rwc: control variable according to each walk type + * @locked: caller holds relevant rmap lock + * + * Find all the mappings of a folio using the mapping pointer and the vma chains + * contained in the address_space struct it points to. + */ +static void rmap_walk_file(struct folio *folio, + struct rmap_walk_control *rwc, bool locked) +{ + struct address_space *mapping = folio_mapping(folio); + + /* + * The page lock not only makes sure that page->mapping cannot + * suddenly be NULLified by truncation, it makes sure that the + * structure at mapping cannot be freed and reused yet, + * so we can safely take mapping->i_mmap_rwsem. + */ + VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio); + + if (!mapping) + return; + + __rmap_walk_file(folio, mapping, folio_pgoff(folio), + folio_nr_pages(folio), rwc, locked); +} + void rmap_walk(struct folio *folio, struct rmap_walk_control *rwc) { if (unlikely(folio_test_ksm(folio))) From patchwork Wed Jan 8 16:18:41 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 855776 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5493220101E; Wed, 8 Jan 2025 16:19:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=205.220.165.32 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736353166; cv=fail; b=tjhD5TM0r4hT3R+UAFxrrZoBrVoWZ9X34eVrsfA+R/IsjASYiLSCe6qhUq3o4Yb5IeFLe1uC6IkbVxiWvpfIODu0UrRlOKedrJa6mXi6NrG/Px1Crewl0zIakyxGe2BstaeaPgI9I5/30EXUkopmHsxvY0Ei3FWEGNPHLq20HoQ= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736353166; c=relaxed/simple; bh=9CyR/7JA8KMrLzQ6MuPu4Pt4CVvYW5HdKqc7SxJI/ms=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=DGEQM2pbELhdgJjiIB7zNzj2belWKWu+KJIhnFUNZ0oWU2F1nKUXVKjNtUGD5pfU8gsdhiJgPWgH55LJUsE2jKs8mcc55/nejETNevukijTdaL/jjAjT/vEZ92ftOoWOSW/U0ewHab9n42YRcN+/6tmfrwUhvChY6o7quWy0TBU= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com; spf=pass smtp.mailfrom=oracle.com; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b=IsaNJz5Z; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b=MwWIGJx8; arc=fail smtp.client-ip=205.220.165.32 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oracle.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="IsaNJz5Z"; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b="MwWIGJx8" Received: from pps.filterd (m0246617.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 508FMioG028814; Wed, 8 Jan 2025 16:19:10 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=cc :content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s= corp-2023-11-20; bh=SClICP+IPp8rcmeGP9luWaP5TU7uyj37cIwbWaQCxoM=; b= IsaNJz5ZE+su+/USzj2szuSFJBpoicYYJUz9k3/AG8+InCXQ0fX8wlER/w1okVec lZV9yWGRGZobuZVHpUptjmB8ugKBAR5s2EtAoAFcCNnol9YcyMH/Upjd5YSiG3Od m7z7WjjpEcD/orzc/naZaJHaZ2EC/nvtvUD0gyIiu5TCixTQs4/P62CRlNxZAsBS DLVbc+LxYpi98gGWzN68Q4hDpGHXlvfnUbC+yjFzBf4cy6RRfrNuYuJi5ey5oji7 TYHBnvbR/rWsjb3YxiA0CtiZoe8/v4NZ1ywsQAcdoCODu1T+7FIpHSMQvmaZ1C0t tRm3trzWFkNql3Sk+uTVtQ== Received: from iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta03.appoci.oracle.com [130.35.103.27]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 43xwhspy3e-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 08 Jan 2025 16:19:10 +0000 (GMT) Received: from pps.filterd (iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 508G9Ogj010953; Wed, 8 Jan 2025 16:19:08 GMT Received: from nam12-mw2-obe.outbound.protection.outlook.com (mail-mw2nam12lp2046.outbound.protection.outlook.com [104.47.66.46]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 43xue9sh23-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 08 Jan 2025 16:19:08 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=ttmO+GLK4lQHcf0Nxbw2JqcIws8Mors+imC1uHhZl2qx/4nKfGWYMD4r4OWs18T1If/7uMRyzOcaRv7NzDxHVXFgfW6XMnhisftZJRH5iD0rrGSy3x23B+hZ7mgUFrr/JW6f2ASvOg6nIEa0BoHUCVGiOQvjjJ/ogFzf/s52GawRV3O3Py36/E4CGOYWRh78TEf+GsQVpjjDEUBVJyFhj9b1+oWJ9dh9zqKDCB9n6KpVnvBNWIIXrydFJTTSdCEhLrUZyL827l6SQjVeaQu6qwdpE6//hzvlpnX8Kf2Azyv5bfy+46dsM+zTdG6o+tSmpIL1idocLIL/OqIZhsiyQg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=SClICP+IPp8rcmeGP9luWaP5TU7uyj37cIwbWaQCxoM=; b=ybEmdCoEaBnMURJxKWviYkg+2vxxl2uGswSWxorGUSRhE8Qw9n/xgHlUFrsbpxXsVn1gUfMBskZC0lshX+f9EcWw54+tEeslZjnKtVxU8dZuSgjyL9Lqcrptmu/0SjqWRhRuCKdLd7l6QEbEeHZDyWzHSCyZigxmK0FJ7vreDtB7ADy5aOyFzmOuABycYVCK6+aV503ZHQTY8rJDzD0L1qGSlFUsl1zB+XtnCj9a7wRqjkncTy/JivcAh0zqT7kEs7vZXYbo4umeZ8WWNvUYTqP5eT9jBIikxFuTiE8t1U9cDhwbneonp5hYjfakBc5NXXxWChYevWG0kPxDNxTFTQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=SClICP+IPp8rcmeGP9luWaP5TU7uyj37cIwbWaQCxoM=; b=MwWIGJx8LjM9egpssUdwVIEuiYoSxNRrKTnLWpW4aBb+Fhi2pWDxhSnNIDquPITCPREuQ3blkN29JL5APMD+zd9OnU0/wnYnk49oPZMOgUPQG7t6Hzk4GbMFwTdPyYq6tirS1fywE0ldjUAspzjAMTEsrumrUTX8Njv336LEtdg= Received: from BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) by SA6PR10MB8134.namprd10.prod.outlook.com (2603:10b6:806:443::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8314.17; Wed, 8 Jan 2025 16:19:02 +0000 Received: from BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9]) by BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9%4]) with mapi id 15.20.8335.010; Wed, 8 Jan 2025 16:19:02 +0000 From: Lorenzo Stoakes To: Andrew Morton Cc: Jaya Kumar , Simona Vetter , Helge Deller , linux-fbdev@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Matthew Wilcox , David Hildenbrand Subject: [RFC PATCH 2/3] mm: provide rmap_wrprotect_file_page() function Date: Wed, 8 Jan 2025 16:18:41 +0000 Message-ID: <701a67692d5bf9c8424cdbda103c988bbb278e38.1736352361.git.lorenzo.stoakes@oracle.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: References: X-ClientProxiedBy: LO4P123CA0241.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:1a7::12) To BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) Precedence: bulk X-Mailing-List: linux-fbdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR10MB3366:EE_|SA6PR10MB8134:EE_ X-MS-Office365-Filtering-Correlation-Id: 516a9acd-fcf5-44b7-f98f-08dd30002a21 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|366016|376014|7416014; X-Microsoft-Antispam-Message-Info: NR7c02bK5HI2CTSD2rMxeCpwYCOZPtqg09IYGBBaC0KS/97ATZYKkfcBmBoGwYaNBCEc+b8y4DyQxklUCL/etziDer4xIofHPQPnw/v7u8+OQctJppnkmzx9An1OYZ7SZkv2B8yGcjGiHJ/fuR58KTjVx7iFX2ZHRaGv/b+7g5R4FBZ/zAl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AJcmbn7YQif0/h1H2zw8ej0b3esHmntBHLR73PycPLQEXJ6xGuKNt+OhzWp2WdwVHdvdrwkmrDDmE9yDcM3RSkv2rPrSvrmdHYAlMKlfXTXeawEuiDMl9sNGZXE4UvYNJetf7qVhqhpHLaMU X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:BYAPR10MB3366.namprd10.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230040)(1800799024)(366016)(376014)(7416014); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: pPJh7cbMkqmAVJlsuZwd6jkwgpBo3+IdRgXdLNwy65laFgucIwILa9FNyZIqMH4pp8ChfeQYV4uX9XSIgMhtHY0Cs5DLDXAJp9hZ1/g/WexN4DKQczhIKVkbKRq+5hu1wr05C0jeVczykfA8XPqx5oEdKmg8lTic1Zf+2MLscF4Qs92R0JPSTnfLI0XWqS43molxBOeJRD7/xW98lH2uVGB9hsgZDHnv0yT5F3s4oTJCpmMvLwRgxGjUsR3UE0G4HdVdZcGosLDzFUy4gnHyju5Rmjs2vewJZpyOptsIZFunr2HZgrCZZEbpYDz14mbSVaVkJS6eEnc5yBGMqWRC6ArN4YBTK+IVf+Qfl/decjrNjwkgyYeaTyAQW2JutiFGVTgohTvEAWqsQOJrBs+1cjS2WF1gJfe9bCN7aJ/S/rzQPXOf7gZtS1/BqBjm1N/p6iEuqZ53x8nWHw6TRCE+8+KEnQWmJ8lsj5/FmwUTG9Y9p3GPHEfs3V4KJ4erUkztFoYe/l5CP8XZs91RlI7lQV1okPHnTdIl//XvEvinut+hLEhdeaL86h8uLUDQyQ1r3Upf1mnU0SzLtttEHFjACc2O85TL09ll4oGFU7wClyp5PRcdKxdwa9ouMuLpxj1T9ERcXlFHPBdSnc4MKjcIw17MQWJfQmyeIryfqwEUDgVp1kliTKfXwz9RBBlM3wKwvpCvDWU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+O045Sw+ak5pRV+LLj8BAtyScMWdwygN5hiRlf9cZetB0M5ALBj/vSEi3ah+/CZbVROMYLgCHMt9/sChlDyqfswdY8s0KbMvSR9pnGuPb2TZlcVOPQSjVkLZaqdtPZdwCqe2cuBiavhR9ORR6Bz60ng6V8Bfz34IPHEa/2208P1u7GHnt+vCgXbtCt3K3nHxdMpfcgsCDhs6XrzH13m8BWQmMLfZgslUQWfcNqXChr0QNDZ2ZWE+G/ra6nvw6NVS94lOnIUlURxVFF4891XKmze4o961O0MJ26kMwW+l1KtS30H5f6o16h2Hauiyu2ljL5itM9H7mrCa1wHKSMJYNhMD2UqNTAtYKKLG54/HZkKvylPpPgmrwjtzdkCWozh4I1qO6SQpliYz4zsWhsC7WevNnnaaVJVPxJS1or9sndjmt8OIGMELkSsT4AK6+HfkNucMSREm1i6V1EqoIZM3+m32wOEwChX9MIlVYG5+o7HldNRPLoLJV/PMqUQDy1Ljim5v942glb7yD7I= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 516a9acd-fcf5-44b7-f98f-08dd30002a21 X-MS-Exchange-CrossTenant-AuthSource: BYAPR10MB3366.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Jan 2025 16:19:02.3962 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: wj9+j7AMv6hQo0yMNjQ3L4KarQR6DJ2tJfy/qSXzieEBSDvN2GJswL5qcHv1OnxMAowa0lrCxXwne07AonFMTpmkUGAS5PynYGNSd0w0rz8= X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA6PR10MB8134 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1057,Hydra:6.0.680,FMLib:17.12.68.34 definitions=2025-01-08_04,2025-01-08_01,2024-11-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 phishscore=0 malwarescore=0 adultscore=0 mlxlogscore=999 suspectscore=0 mlxscore=0 spamscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2411120000 definitions=main-2501080135 X-Proofpoint-GUID: 02cpufoxGwLD41JEmM6zpB5ynB3c1xOL X-Proofpoint-ORIG-GUID: 02cpufoxGwLD41JEmM6zpB5ynB3c1xOL in the fb_defio video driver, page dirty state is used to determine when frame buffer pages have been changed, allowing for batched, deferred I/O to be performed for efficiency. This implementation had only one means of doing so effectively - the use of the folio_mkclean() function. However, this use of the function is inappropriate, as the fb_defio implementation allocates kernel memory to back the framebuffer, and then is forced to specified page->index, mapping fields in order to permit the folio_mkclean() rmap traversal to proceed correctly. It is not correct to specify these fields on kernel-allocated memory, and moreover since these are not folios, page->index, mapping are deprecated fields, soon to be removed. We therefore need to provide a means by which we can correctly traverse the reverse mapping and write-protect mappings for a page backing an address_space page cache object at a given offset. This patch provides this - rmap_wrprotect_file_page() allows for this operation to be performed for a specified address_space, offset and PFN, without requiring a folio nor, of course, an inappropriate use of page->index, mapping. With this provided, we can subequently adjust the fb_defio implementation to make use of this function and avoid incorrect invocation of folio_mkclean() and more importantly, incorrect manipulation of page->index, mapping fields. Signed-off-by: Lorenzo Stoakes --- include/linux/rmap.h | 20 ++++++++++++++++ mm/rmap.c | 54 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/include/linux/rmap.h b/include/linux/rmap.h index 4509a43fe59f..9d80b09e58ae 100644 --- a/include/linux/rmap.h +++ b/include/linux/rmap.h @@ -754,6 +754,26 @@ unsigned long page_address_in_vma(const struct folio *folio, */ int folio_mkclean(struct folio *); +/** + * rmap_wrprotect_file_page() - Traverses the reverse mapping, finding all VMAs + * which contain a shared mapping of the single page at PFN @pfn in @mapping at + * offset @pgoff and write-protecting the mappings. + * + * The PFN mapped does not have to be a folio, but rather can be a kernel + * allocation that is mapped into userland. We therefore do not require that the + * PFN maps to a folio with a valid mapping or index field, rather these are + * specified in @mapping and @pgoff. + * + * @mapping: The mapping whose reverse mapping should be traversed. + * @pgoff: The page offset at which @pfn is mapped within @mapping. + * @nr_pages: The number of physically contiguous base pages spanned. + * @pfn: The PFN of the memory mapped in @mapping at @pgoff. + * + * Return the number of write-protected PTEs, or an error. + */ +int rmap_wrprotect_file_page(struct address_space *mapping, pgoff_t pgoff, + unsigned long nr_pages, unsigned long pfn); + int pfn_mkclean_range(unsigned long pfn, unsigned long nr_pages, pgoff_t pgoff, struct vm_area_struct *vma); diff --git a/mm/rmap.c b/mm/rmap.c index effafdb44365..46474343116c 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1127,6 +1127,60 @@ int folio_mkclean(struct folio *folio) } EXPORT_SYMBOL_GPL(folio_mkclean); +struct wrprotect_file_state { + int cleaned; + pgoff_t pgoff; + unsigned long pfn; + unsigned long nr_pages; +}; + +static bool rmap_wrprotect_file_one(struct folio *folio, struct vm_area_struct *vma, + unsigned long address, void *arg) +{ + struct wrprotect_file_state *state = (struct wrprotect_file_state *)arg; + struct page_vma_mapped_walk pvmw = { + .pfn = state->pfn, + .nr_pages = state->nr_pages, + .pgoff = state->pgoff, + .vma = vma, + .address = address, + .flags = PVMW_SYNC, + }; + + state->cleaned += page_vma_mkclean_one(&pvmw); + + return true; +} + +static void __rmap_walk_file(struct folio *folio, struct address_space *mapping, + pgoff_t pgoff_start, unsigned long nr_pages, + struct rmap_walk_control *rwc, bool locked); + +int rmap_wrprotect_file_page(struct address_space *mapping, pgoff_t pgoff, + unsigned long nr_pages, unsigned long pfn) +{ + struct wrprotect_file_state state = { + .cleaned = 0, + .pgoff = pgoff, + .pfn = pfn, + .nr_pages = nr_pages, + }; + struct rmap_walk_control rwc = { + .arg = (void *)&state, + .rmap_one = rmap_wrprotect_file_one, + .invalid_vma = invalid_mkclean_vma, + }; + + if (!mapping) + return 0; + + __rmap_walk_file(/* folio = */NULL, mapping, pgoff, nr_pages, &rwc, + /* locked = */false); + + return state.cleaned; +} +EXPORT_SYMBOL_GPL(rmap_wrprotect_file_page); + /** * pfn_mkclean_range - Cleans the PTEs (including PMDs) mapped with range of * [@pfn, @pfn + @nr_pages) at the specific offset (@pgoff)