From nobody Mon Feb 9 15:11:20 2026 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 0EAF515CD78 for ; Mon, 27 Jan 2025 15:51:22 +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=1737993085; cv=fail; b=mL6AuPghVCM/YUMN5rQ0XoVscgNBccO7uA0mADdkOnHUD675QjLuLlrn69E/HhtQISu3zZgWj4x+hTGT1EsOGy26rJYnv7r3Z+SlL9/TPo+Xia1xVSPXRuy8a7b+bfqA9TcqaJNAo0XAqlxf0vlM2pPwY9tjk8MVrNOpr+d7s04= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737993085; c=relaxed/simple; bh=uN4NeUMEyTbcLVYrPeaYsHuoVJgTmaZ+i74xAiLzp40=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=Wwvx8gyLTpZlPWFhhL3VK9jb4d/lHVKR9raLopY8XON2xXLxPKSCyGsIjLuhzrgq1eUpOWWosWiJBsvpJntHwySc25GU/b2iltvLIAcT8aWIujsx64Xy6WHJlu7DmD+jo5oyZf3d854YsSKSOhe/YF7hOhadu2Zl7lmp3uqwb4I= 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=HXjdw/Vv; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b=Dk4m1gV5; 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="HXjdw/Vv"; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b="Dk4m1gV5" Received: from pps.filterd (m0333521.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 50RFjSSP015964; Mon, 27 Jan 2025 15:51:18 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=7uviu8hul45H4tZf5iQNLGCckTDPU96fpah2kR9UPc4=; b= HXjdw/Vv82E3xdVn+aPaSaAcZPYIfgi0mm74RUpaS0b2XuCHjjK3UTAwuhN0dKdy H1IauILSh/gpGUsszxNDnNk9ua4+o3j5BHGuNMYp4aeXNszC9jTHkB8hSp9mqP/V +vP2DtD7LdbWASeh2L+PbX0UzHwcj0S/iJhxmz4AohiH7wkwVnEghMl2c/Uxxniw 1IZEsggpLWD3oMWi7kx7H1SZm/BkpwH0MPevlSykOH16DGORhEDb2P+46GhlRFrM dpXAhT2XYMciv+zfLPqo2QrzlI89GfBAdEKRuKg2fHnmvHZwCTtA6gp4bP8GrZN4 IYwUPsr5HN0YiovQGiGNBw== Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.appoci.oracle.com [138.1.114.2]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 44ed2br0e6-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 27 Jan 2025 15:51:18 +0000 (GMT) Received: from pps.filterd (phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 50RFes8v036983; Mon, 27 Jan 2025 15:51:17 GMT Received: from nam02-sn1-obe.outbound.protection.outlook.com (mail-sn1nam02lp2049.outbound.protection.outlook.com [104.47.57.49]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 44cpd73xue-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 27 Jan 2025 15:51:17 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=sdHyYnQef37/GoB4HWgBhX9nVh1QUEZtB6PGTK+B76U5/HWWC+P6sCUXCN56iG5I2t7VS1/leGXlyP+/tTUB8KdPLuDpRlNMqoHxLzZBXOAvUPa7+y2561Tn0bJReLRhA0hsa4hmNm/gOJND/5vmfsM2O0FGTJuConDcebsJ2k7N1YWpQFEVtmEMaX+DXacMXbbyoBknfWEhsBwm9IBrFKY2iG2uLk6yFgWTpIkf4YTWhERtc70AL5Yol3cAwlLaIC0KF3OBdRddkqBqTb6z9UxdwAsVBGc1qzKwOWKDIgHYOPhEi3qxhmrwQ+s8p4tHGNqrqb6fEvWKER93CdY3Zg== 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=7uviu8hul45H4tZf5iQNLGCckTDPU96fpah2kR9UPc4=; b=b7oQxpZEPCPvz+/A8aDtFUGIGAw6ACMAumuFJCvIvtMlR9FGQ4ZcInMZGoy6qnwXj1Js41C/6Yl0Ul8GuDmagD2nEng3p/EbwSMpC4ko+OXiyey5IGdrmBvg4XIIKNUCcj3gmmrDhxbWBF+3yLyK7mEWSwZSKwATDw61NjHb6a7o62qRK5yOSIld/WUVPl4p3kHa6zo96cfJCh7zPtuB33BlK6x86P1sycWrR3kdLOUUb5mMIHTlyjb0AyjAhsYJBgb+6yBUIExOShWDyC21Zak90C2fxY6eS3mTgFntBe8IYOeTCHcf2LEkndANLqdZbjp9jV567gs/zT9fz9HTyg== 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=7uviu8hul45H4tZf5iQNLGCckTDPU96fpah2kR9UPc4=; b=Dk4m1gV5U+cAT0fVSiahNux/WEn1JXzrX0bPXsCJd6edH8MObdZnoumI5ZvvsqRn/IuUmKItgcqr2agIiKzSvPWFFL7QU8YR/SbGByAZ55CIU/stpuogdLdcoONb1P9g7xaHiCDr22Hnfh7bk57GRJeIHDhZxBREfP6QBUuGAQk= Received: from MN2PR10MB3374.namprd10.prod.outlook.com (2603:10b6:208:12b::29) by PH8PR10MB6339.namprd10.prod.outlook.com (2603:10b6:510:1ce::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8377.19; Mon, 27 Jan 2025 15:51:15 +0000 Received: from MN2PR10MB3374.namprd10.prod.outlook.com ([fe80::eab5:3c8c:1b35:4348]) by MN2PR10MB3374.namprd10.prod.outlook.com ([fe80::eab5:3c8c:1b35:4348%3]) with mapi id 15.20.8377.021; Mon, 27 Jan 2025 15:51:15 +0000 From: Lorenzo Stoakes To: Andrew Morton Cc: "Liam R . Howlett" , Vlastimil Babka , Jann Horn , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 5/5] mm: completely abstract unnecessary adj_start calculation Date: Mon, 27 Jan 2025 15:50:44 +0000 Message-ID: X-Mailer: git-send-email 2.48.0 In-Reply-To: References: Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: LO4P123CA0018.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:150::23) To MN2PR10MB3374.namprd10.prod.outlook.com (2603:10b6:208:12b::29) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: MN2PR10MB3374:EE_|PH8PR10MB6339:EE_ X-MS-Office365-Filtering-Correlation-Id: 59023535-4251-4f81-c22a-08dd3eea6e49 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|366016|376014|1800799024; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?v9rebQ7yHUAom+SK5hzBjs+92UVw5Aczvf2cuSbmS49TXhoMD8SJjcSGDTTD?= =?us-ascii?Q?3Z6kqepxa1tx+mGpODOiEze9Qe/B0/m2F02k8/6f1Tktkt94DkOjyZnkFCdu?= =?us-ascii?Q?eHdWzbRQ+6P69zPehl0iHCFNqXElLEUFcdTsG5wyWAFHOp1c/oEx5TffLt2l?= =?us-ascii?Q?koMxaVeQT2qYrb08XewJi5LMSNJis+zzdTxRrm5LizSIBj+yqxnwhoIuOf/9?= =?us-ascii?Q?UEpzV2nwLLUqbSu2dbeVRg1nsMG5Gn7P/DhupPqrEnSi4QkKMym4GlL2XVGc?= =?us-ascii?Q?b5sN4Mj9A3a6o26J5qe9SrNusYkKOwzLqxIdMVbo2nJTzGoZZr0J0hdcKK3Z?= =?us-ascii?Q?9PwVm23UnRgvHLUXiByftLttAvi8di/Sakc6m072UoizXpS8gSEvMgRuiJbS?= =?us-ascii?Q?brGglv8LsV7ffq1c+GM/J77dq5g35Z+Ox+kDzsVLKBy6ustkZhNIKL2wbyHL?= =?us-ascii?Q?CfTjg1jyonqIX0f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5b0?= =?us-ascii?Q?f2oF2UhNWP16x3cM758K4dtsV9TI?= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:MN2PR10MB3374.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(366016)(376014)(1800799024);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?V7YJd+xvw7hLLqci3rpT8BROOcwENJuqRV2fHKFhqV5aBuddr3l0h6JZmtRe?= =?us-ascii?Q?8JX3PYiJZgKwJCWV6zN5/sLEcuyReIJ2KQnFe6vc8O4TIXu/9VQtmsScUWTY?= =?us-ascii?Q?SPA8ffXzs5SZ4J1KPS5aQe0yRloU1ZFKHtcnUxzvG8yFaCM7fvezpXovD2UE?= =?us-ascii?Q?CdEloYu005Kiz/IYrpOODBWIvzmfW/uh0knF8JWroK2ifrLiNMSLVgdoVMPD?= =?us-ascii?Q?wo7yvg7NUYH0z5SunsKrrJXc621752mWP+czx1UUIwL8jpRB+bRzkLBeKJtp?= =?us-ascii?Q?SaZTurkKkCCjnKw3ktLRO8hroL7XW3uYAt/BYNp8qKqdDk9XHqPIR7RlDNRx?= =?us-ascii?Q?qcav1V0vFJ195U/LvIu82Y3JeEnVOcJ22qz+cNvQ+Fo2dmyKpOqHH0t0/OWw?= =?us-ascii?Q?PyTQailBnW1syF6TTB6BogEDMy4ulpc2p0gdtF9gkrfUOn3ES7qs4QTyGaFq?= =?us-ascii?Q?4BOO6DZ8diQ3AkyGBYVvnyMLsi4mGI3lyr/VMq8WUEgnQpH5IyKZwsS3GNaO?= =?us-ascii?Q?qGQ5/fI6jhhmoEpE9bMI59IvFsl8POFEC6XN/nH+ye5dRUIFaYdEC0Iq44d3?= =?us-ascii?Q?P+IS+hmfqzMdgsHJVzb/PG05xMMYR8FH+doLHkhj8oJlseMRUmwnW14t0lM2?= =?us-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082KNHZnA2S?= =?us-ascii?Q?8XrGD4bfxwS4hP/nu2ZBN+urYPtDagbLcH3qsW3QnZPRmZV1YcMygm6X060L?= =?us-ascii?Q?Yw=3D=3D?= X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: O0wKc1aDonh8cZMFLu5EsTkv9MobcBCu+/GSbPcR3/8sNXCGgFGE3bDOfctoZCshGeqzDhRnLs0JP3ox3Sd/11xfGT1g2MwWoemK7Iz/mm/S+stklheMKfKVyCQC6lTCsg4II7a2ms2d1UY4QCLT4L8rCz+2/zfQTwsDPGlNGGzwThWgTzpE91XEdpM6S3U8k9mdQV3ObYuoDA+WzNvAJaNxHWyekvEoK6obVJrsKg65czy5H8We+vz8zypzZss/NFvIjQHz78FcPqgAzFtNRe8p5DCrdzEiZGbq5llBjdtCnd+tWEBykEG45if3MmXn/ivWRSCVkbzsKmxyQAPqTWv3pT9a3mF9V3YAblCm4+I26iJWY3o04IQcCdx3JEz7AWPn10Ytn5OqHToeHZEnT0MpJN3UyUOMGbQv9nlpmpY0eSGG/PYa2fcwiSuX/Bum/ntqDVQPHnfZTCVY5AGYKiVGyQY8yd8BI/EKKax2UvjNy3MIEIuC2di2LlLzRf9AyJDquT7zIwA0wUsrVjWzERbOjvi3dHBasLx7ULifZi9sk36M+/M1uwiQxfu/6Obx3C7X1zdDokIGGnzrclLs24VOCCoDaedy5CAtuQSUqZ0= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 59023535-4251-4f81-c22a-08dd3eea6e49 X-MS-Exchange-CrossTenant-AuthSource: MN2PR10MB3374.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Jan 2025 15:51:15.2099 (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: JQ/6fRQufDeIVpUJrNqvxpcduGbS259S1KVtVKxZIG9jDA1kM+OJRkukYuXcmY2RMUVdTHt/wSgsnjeU0wQi9B8BHMzToaoBpXQu36K7BZY= X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH8PR10MB6339 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-27_07,2025-01-27_01,2024-11-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 phishscore=0 bulkscore=0 malwarescore=0 mlxlogscore=999 suspectscore=0 spamscore=0 mlxscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2411120000 definitions=main-2501270126 X-Proofpoint-ORIG-GUID: eyna2I2dl_N7AeoS0NgDRV8Ci7ddphod X-Proofpoint-GUID: eyna2I2dl_N7AeoS0NgDRV8Ci7ddphod Content-Type: text/plain; charset="utf-8" The adj_start calculation has been a constant source of confusion in the VMA merge code. There are two cases to consider, one where we adjust the start of the vmg->middle VMA (i.e. the __VMG_FLAG_ADJUST_MIDDLE_START merge flag is set), in which case adj_start is calculated as: (1) adj_start =3D vmg->end - vmg->middle->vm_start And the case where we adjust the start of the vmg->next VMA (i.e.t he __VMG_FLAG_ADJUST_NEXT_START merge flag is set), in which case adj_start is calculated as: (2) adj_start =3D -(vmg->middle->vm_end - vmg->end) We apply (1) thusly: vmg->middle->vm_start =3D vmg->middle->vm_start + vmg->end - vmg->middle->vm_start Which simplifies to: vmg->middle->vm_start =3D vmg->end Similarly, we apply (2) as: vmg->next->vm_start =3D vmg->next->vm_start + -(vmg->middle->vm_end - vmg->end) Noting that for these VMAs to be mergeable vmg->middle->vm_end =3D=3D vmg->next->vm_start and so this simplifies to: vmg->next->vm_start =3D vmg->next->vm_start + -(vmg->next->vm_start - vmg->end) Which simplifies to: vmg->next->vm_start =3D vmg->end Therefore in each case, we simply need to adjust the start of the VMA to vmg->end (!) and can do away with this adj_start calculation. The only caveat is that we must ensure we update the vm_pgoff field correctly. We therefore abstract this entire calculation to a new function vmg_adjust_set_range() which performs this calculation and sets the adjusted VMA's new range using the general vma_set_range() function. We also must update vma_adjust_trans_huge() which expects the now-abstracted adj_start parameter. It turns out this is wholly unnecessary. In vma_adjust_trans_huge() the relevant code is: if (adjust_next > 0) { struct vm_area_struct *next =3D find_vma(vma->vm_mm, vma->vm_end); unsigned long nstart =3D next->vm_start; nstart +=3D adjust_next; split_huge_pmd_if_needed(next, nstart); } The only case where this is relevant is when __VMG_FLAG_ADJUST_MIDDLE_START is specified (in which case adj_next would have been positive), i.e. the one in which the vma specified is vmg->prev and this the sought 'next' VMA would be vmg->middle. We can therefore eliminate the find_vma() invocation altogether and simply provide the vmg->middle VMA in this instance, or NULL otherwise. Again we have an adj_next offset calculation: next->vm_start + vmg->end - vmg->middle->vm_start Where next =3D=3D vmg->middle this simplifies to vmg->end as previously demonstrated. Therefore nstart is equal to vmg->end, which is already passed to vma_adjust_trans_huge() via the 'end' parameter and so this code (rather delightfully) simplifies to: if (next) split_huge_pmd_if_needed(next, end); With these changes in place, it becomes silly for commit_merge() to return vmg->target, as it is always the same and threaded through vmg, so we finally change commit_merge() to return an error value once again. This patch has no change in functional behaviour. Signed-off-by: Lorenzo Stoakes Reviewed-by: Vlastimil Babka --- include/linux/huge_mm.h | 2 +- mm/huge_memory.c | 19 ++---- mm/vma.c | 102 +++++++++++++++---------------- tools/testing/vma/vma_internal.h | 4 +- 4 files changed, 58 insertions(+), 69 deletions(-) diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h index 93e509b6c00e..43f76b54b522 100644 --- a/include/linux/huge_mm.h +++ b/include/linux/huge_mm.h @@ -404,7 +404,7 @@ int madvise_collapse(struct vm_area_struct *vma, struct vm_area_struct **prev, unsigned long start, unsigned long end); void vma_adjust_trans_huge(struct vm_area_struct *vma, unsigned long start, - unsigned long end, long adjust_next); + unsigned long end, struct vm_area_struct *next); spinlock_t *__pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct *vma); spinlock_t *__pud_trans_huge_lock(pud_t *pud, struct vm_area_struct *vma); =20 diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 3d3ebdc002d5..c44599f9ad09 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -3017,9 +3017,9 @@ static inline void split_huge_pmd_if_needed(struct vm= _area_struct *vma, unsigned } =20 void vma_adjust_trans_huge(struct vm_area_struct *vma, - unsigned long start, - unsigned long end, - long adjust_next) + unsigned long start, + unsigned long end, + struct vm_area_struct *next) { /* Check if we need to split start first. */ split_huge_pmd_if_needed(vma, start); @@ -3027,16 +3027,9 @@ void vma_adjust_trans_huge(struct vm_area_struct *vm= a, /* Check if we need to split end next. */ split_huge_pmd_if_needed(vma, end); =20 - /* - * If we're also updating the next vma vm_start, - * check if we need to split it. - */ - if (adjust_next > 0) { - struct vm_area_struct *next =3D find_vma(vma->vm_mm, vma->vm_end); - unsigned long nstart =3D next->vm_start; - nstart +=3D adjust_next; - split_huge_pmd_if_needed(next, nstart); - } + /* If we're incrementing next->vm_start, we might need to split it. */ + if (next) + split_huge_pmd_if_needed(next, end); } =20 static void unmap_folio(struct folio *folio) diff --git a/mm/vma.c b/mm/vma.c index cfcadca7b116..48a7acc83a82 100644 --- a/mm/vma.c +++ b/mm/vma.c @@ -515,7 +515,7 @@ __split_vma(struct vma_iterator *vmi, struct vm_area_st= ruct *vma, init_vma_prep(&vp, vma); vp.insert =3D new; vma_prepare(&vp); - vma_adjust_trans_huge(vma, vma->vm_start, addr, 0); + vma_adjust_trans_huge(vma, vma->vm_start, addr, NULL); =20 if (new_below) { vma->vm_start =3D addr; @@ -646,47 +646,46 @@ void validate_mm(struct mm_struct *mm) #endif /* CONFIG_DEBUG_VM_MAPLE_TREE */ =20 /* - * Actually perform the VMA merge operation. - * - * On success, returns the merged VMA. Otherwise returns NULL. + * Based on the vmg flag indicating whether we need to adjust the vm_start= field + * for the middle or next VMA, we calculate what the range of the newly ad= justed + * VMA ought to be, and set the VMA's range accordingly. */ -static struct vm_area_struct *commit_merge(struct vma_merge_struct *vmg) +static void vmg_adjust_set_range(struct vma_merge_struct *vmg) { - struct vm_area_struct *vma; - struct vma_prepare vp; - struct vm_area_struct *adjust; - long adj_start; unsigned long flags =3D vmg->merge_flags; + struct vm_area_struct *adjust; + pgoff_t pgoff; =20 - /* - * If modifying an existing VMA and we don't remove vmg->middle, then we - * shrink the adjacent VMA. - */ if (flags & __VMG_FLAG_ADJUST_MIDDLE_START) { - vma =3D vmg->target; adjust =3D vmg->middle; - /* The POSITIVE value by which we offset vmg->middle->vm_start. */ - adj_start =3D vmg->end - vmg->middle->vm_start; - - /* Note: vma iterator must be pointing to 'start'. */ - vma_iter_config(vmg->vmi, vmg->start, vmg->end); + pgoff =3D adjust->vm_pgoff + PHYS_PFN(vmg->end - adjust->vm_start); } else if (flags & __VMG_FLAG_ADJUST_NEXT_START) { - /* - * In this case alone, the VMA we manipulate is vmg->middle, but - * we ultimately return vmg->next. - */ - vma =3D vmg->middle; adjust =3D vmg->next; - /* The NEGATIVE value by which we offset vmg->next->vm_start. */ - adj_start =3D -(vmg->middle->vm_end - vmg->end); + pgoff =3D adjust->vm_pgoff - PHYS_PFN(adjust->vm_start - vmg->end); + } else { + return; + } + + vma_set_range(adjust, vmg->end, adjust->vm_end, pgoff); +} + +/* + * Actually perform the VMA merge operation. + * + * On success, returns the merged VMA. Otherwise returns NULL. + */ +static int commit_merge(struct vma_merge_struct *vmg) +{ + struct vm_area_struct *vma; + struct vma_prepare vp; + bool adj_middle =3D vmg->merge_flags & __VMG_FLAG_ADJUST_MIDDLE_START; =20 - vma_iter_config(vmg->vmi, vmg->next->vm_start + adj_start, - vmg->next->vm_end); + if (vmg->merge_flags & __VMG_FLAG_ADJUST_NEXT_START) { + /* In this case we manipulate middle and return next. */ + vma =3D vmg->middle; + vma_iter_config(vmg->vmi, vmg->end, vmg->next->vm_end); } else { vma =3D vmg->target; - adjust =3D NULL; - adj_start =3D 0; - /* Note: vma iterator must be pointing to 'start'. */ vma_iter_config(vmg->vmi, vmg->start, vmg->end); } @@ -694,22 +693,22 @@ static struct vm_area_struct *commit_merge(struct vma= _merge_struct *vmg) init_multi_vma_prep(&vp, vma, vmg); =20 if (vma_iter_prealloc(vmg->vmi, vma)) - return NULL; + return -ENOMEM; =20 vma_prepare(&vp); - vma_adjust_trans_huge(vma, vmg->start, vmg->end, adj_start); + /* + * THP pages may need to do additional splits if we increase + * middle->vm_start. + */ + vma_adjust_trans_huge(vma, vmg->start, vmg->end, + adj_middle ? vmg->middle : NULL); vma_set_range(vma, vmg->start, vmg->end, vmg->pgoff); - - if (adj_start) { - adjust->vm_start +=3D adj_start; - adjust->vm_pgoff +=3D PHYS_PFN(adj_start); - } - + vmg_adjust_set_range(vmg); vma_iter_store(vmg->vmi, vmg->target); =20 vma_complete(&vp, vmg->vmi, vma->vm_mm); =20 - return vmg->target; + return 0; } =20 /* We can only remove VMAs when merging if they do not have a close hook. = */ @@ -752,7 +751,7 @@ static __must_check struct vm_area_struct *vma_merge_ex= isting_range( { struct vm_area_struct *middle =3D vmg->middle; struct vm_area_struct *prev =3D vmg->prev; - struct vm_area_struct *next, *res; + struct vm_area_struct *next; struct vm_area_struct *anon_dup =3D NULL; unsigned long start =3D vmg->start; unsigned long end =3D vmg->end; @@ -904,12 +903,7 @@ static __must_check struct vm_area_struct *vma_merge_e= xisting_range( vmg->end =3D next->vm_end; vmg->pgoff =3D next->vm_pgoff - pglen; } else { - /* - * We shrink middle and expand next. - * - * IMPORTANT: This is the ONLY case where the final - * merged VMA is NOT vmg->target, but rather vmg->next. - */ + /* We shrink middle and expand next. */ vmg->merge_flags |=3D __VMG_FLAG_ADJUST_NEXT_START; vmg->start =3D middle->vm_start; vmg->end =3D start; @@ -927,8 +921,10 @@ static __must_check struct vm_area_struct *vma_merge_e= xisting_range( if (merge_will_delete_next) vmg->merge_flags |=3D __VMG_FLAG_REMOVE_NEXT; =20 - res =3D commit_merge(vmg); - if (!res) { + err =3D commit_merge(vmg); + if (err) { + VM_WARN_ON(err !=3D -ENOMEM); + if (anon_dup) unlink_anon_vmas(anon_dup); =20 @@ -936,9 +932,9 @@ static __must_check struct vm_area_struct *vma_merge_ex= isting_range( return NULL; } =20 - khugepaged_enter_vma(res, vmg->flags); + khugepaged_enter_vma(vmg->target, vmg->flags); vmg->state =3D VMA_MERGE_SUCCESS; - return res; + return vmg->target; =20 abort: vma_iter_set(vmg->vmi, start); @@ -1102,7 +1098,7 @@ int vma_expand(struct vma_merge_struct *vmg) if (remove_next) vmg->merge_flags |=3D __VMG_FLAG_REMOVE_NEXT; =20 - if (!commit_merge(vmg)) + if (commit_merge(vmg)) goto nomem; =20 return 0; @@ -1142,7 +1138,7 @@ int vma_shrink(struct vma_iterator *vmi, struct vm_ar= ea_struct *vma, =20 init_vma_prep(&vp, vma); vma_prepare(&vp); - vma_adjust_trans_huge(vma, start, end, 0); + vma_adjust_trans_huge(vma, start, end, NULL); =20 vma_iter_clear(vmi); vma_set_range(vma, start, end, pgoff); diff --git a/tools/testing/vma/vma_internal.h b/tools/testing/vma/vma_inter= nal.h index 1eae23039854..bb273927af0f 100644 --- a/tools/testing/vma/vma_internal.h +++ b/tools/testing/vma/vma_internal.h @@ -796,12 +796,12 @@ static inline void vma_start_write(struct vm_area_str= uct *vma) static inline void vma_adjust_trans_huge(struct vm_area_struct *vma, unsigned long start, unsigned long end, - long adjust_next) + struct vm_area_struct *next) { (void)vma; (void)start; (void)end; - (void)adjust_next; + (void)next; } =20 static inline void vma_iter_free(struct vma_iterator *vmi) --=20 2.48.0