From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.remote-ip=192.198.163.13 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=fail; t=1796419455; b=PeJ92hXyxr+/ZbxXkZ54OgQCl8Y6hE7JS9ruFlVbTW3leNGIWy2lQ9rjzRHOMown5nfl yhvM1QWVCjpImE2D2MO9FHsnk6B+4YH7y+rJVRpAhjRl5K+IpHI41PE0u6HNRYit3ZRZk Kdy5ObtukoazcK/16uXGYF0AFGGlZY+XQpZDk2s8VW+fLOc2KvmsA4bK200ogfrFHhiFA Zy+OVp4z4N8swnmAhG2mWWbYrdZ6cUppmJWepEYGZ+PRANGTM/PrdO4y5ScBvxmf3pm+2 CrV0DqfkwOfF012pVtYJG5tLJmJ++ot9Z+DI7YkRqJG6/cBya5NI9H97Zga7RA7KEyw== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796419455; h=DMARC-Filter:DKIM-Signature:X-CSE-ConnectionGUID:X-CSE-MsgGUID: X-IronPort-AV:X-IronPort-AV:Received:X-CSE-ConnectionGUID: X-CSE-MsgGUID:X-ExtLoop1:X-IronPort-AV:Received:Received:Received: Received:Received:Received:Date:From:To:CC:Subject:Message-ID: References:Content-Type:Content-Disposition:Content-Transfer-Encoding: In-Reply-To:X-ClientProxiedBy:MIME-Version:X-MS-PublicTrafficType: X-MS-TrafficTypeDiagnostic:X-MS-Office365-Filtering-Correlation-Id: X-LD-Processed:X-MS-Exchange-SenderADCheck: X-MS-Exchange-AntiSpam-Relay:X-Microsoft-Antispam: X-Microsoft-Antispam-Message-Info:X-Forefront-Antispam-Report: X-MS-Exchange-AntiSpam-MessageData-ChunkCount: X-MS-Exchange-AntiSpam-MessageData-0:X-Exchange-RoutingPolicyChecked: X-MS-Exchange-CrossTenant-Network-Message-Id: X-MS-Exchange-CrossTenant-AuthSource:X-MS-Exchange-CrossTenant-AuthAs: X-MS-Exchange-CrossTenant-OriginalArrivalTime: X-MS-Exchange-CrossTenant-FromEntityHeader: X-MS-Exchange-CrossTenant-Id:X-MS-Exchange-CrossTenant-MailboxType: X-MS-Exchange-CrossTenant-UserPrincipalName: X-MS-Exchange-Transport-CrossTenantHeadersStamped:X-OriginatorOrg; bh=b1+mZUxPfIxrfKLaipS7WcvhjTywmRCShxdqIdPy3fo=; b=LPBW2PqQ/8NPJf/2cGxVt7Yh5OFKw6lsuEMBac1yQ38xYNNkRqEQbJlh3yR9TQ2I1LIX jt/wHLjrYFQ1jaT6Sf2YubvBmOJynYCuPIZarkFtC9BOhbsmH6a6b/kDvIJsa8u4l9XdF ZTQsoPilq4gbusW/aB/4rWesDUKjD3d2fcvJLrnirREBylb5E71NYDVsOSuh9H4qHZFMK BUuJMRU6MD9nfPdLNn/vgPE7kJcA8RvWq68d6uZEMnfD3GStUAq+KpdHNFTYUFdI8gxqe Y9L2WQ33BlNji66plOpk6M/j/0tgtVGjzkg5Mdedoguy0z0CLmrXNaWINC80Utpy0EA== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=OAFENxKz; arc=fail smtp.remote-ip=192.198.163.13 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.13]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6BF70606A32C for ; Fri, 4 Dec 2026 21:24:14 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 6BF70606A32C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=OAFENxKz DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796419454; x=1827955454; h=date:from:to:cc:subject:message-id:references: content-transfer-encoding:in-reply-to:mime-version; bh=b1+mZUxPfIxrfKLaipS7WcvhjTywmRCShxdqIdPy3fo=; b=OAFENxKzxenSZwD6ARmVq9GpL60TqQ4KwDT57PqzybV+dYt0m0iVUO/w Tyl2xSZMV0FULxp1gxODowVw+zbHifsos1QhwTsuf6ps8pVd+zF2nFy28 LYI2ip5FZKRCiBpMDby/eNmogrrQrADpTINCCYWoyqrRIbzoqLA2xPyCE jJorh5o1nYdL8BzxHkqONBwAaOQ8M92zlm7TcpVdaOVPIfu+OD7jcBQAq uW5UUxNILkNvOe7hSQqBT/nlz/t6ObILMH/StDa8NxmcG+tlTZ9QsUGHf 9ebtsQ6DBF+ArM57CxIKKOIN4o/NIXsOvEMFqCC3bdRq7ANCiR5GwpU22 A==; X-CSE-ConnectionGUID: DaL6pzVrQtqGjWP0+s85cw== X-CSE-MsgGUID: SMzYIRHcR2KVqA8pgGFBfg== X-IronPort-AV: E=McAfee;i="6800,10657,11986"; a="5386789" X-IronPort-AV: E=Sophos;i="6.29,79,1794902400"; d="scan'208";a="5386789" Received: from fmviesa004.fm.intel.com ([10.60.135.144]) by fmvoesa107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Dec 2026 13:24:13 -0800 X-CSE-ConnectionGUID: JkCpr7VQQJSXUKp/OeM7xA== X-CSE-MsgGUID: 4eCbKDgxSVKqBm/iakadLQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.29,79,1794902400"; d="scan'208";a="14071598" Received: from fmsmsx901.amr.corp.intel.com ([10.18.126.90]) by fmviesa004.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Dec 2026 13:24:13 -0800 Received: from FMSMSX903.amr.corp.intel.com (10.18.126.92) by fmsmsx901.amr.corp.intel.com (10.18.126.90) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.57; Fri, 4 Dec 2026 13:24:13 -0800 Received: from fmsedg902.ED.cps.intel.com (10.1.192.144) by FMSMSX903.amr.corp.intel.com (10.18.126.92) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.57 via Frontend Transport; Fri, 4 Dec 2026 13:24:13 -0800 Received: from BN8PR05CU002.outbound.protection.outlook.com (52.101.57.16) by edgegateway.intel.com (192.55.55.82) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.57; Fri, 4 Dec 2026 13:24:12 -0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=w6WEjywwaEYIswdrIA8PTL8DDLHF9VaaWu4tDWgL1ZlLi73WBL4RNsAWtkGEV4Uq0asQ9FB8jFsUFIYSruyAfVP/gmvaKpHR7n4elxAyrT/BWE/P406fwO0w78wwUfmZbbFzCVXIZ1UePw12UGbmURTIjZ4amyF8WNpzC0xA8uIhUpNdXNRigm5ZOb+EK2c+ny6iIeADtZseI9GpsJCv71m3tjpALHla5cCSLW7tSNXXWffYK5SNhCDdqDfS2cbkgzNVp8z7Jk1JqojBD/Lu1bXOdb5UPgYmseTsP1JzQAAI5kXqlkBpiDecsJIujlkxr/XOeBMphL8+K+TgzXiAGA== 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=7QPKTYMh58HaNjbHjjV2Y9xaKsnHi6og4TuC6wZvTps=; b=f6poIiAyOWOrEqQgEXIOZ2gFoy/eMRe/34fGue9xcpApqn9P/Nvo2jg/D3Ucf2nakR4TfTubwHsyNQbeMDoecKnVXmw5Fzzjcj08fN5C21XqS/cbybHxj1ca4p2rYmkQBVVx7AuCBj23Ea+RPF+BxxaWOVD2sMQWr8PVOiQtRTNGzWra//NmJ6smr5l0S6knWJzFvV3AfNZLhiWXtAbh8kAvhoK2kv4l0xM9Xw/3D0unCET7/TZGJDHPVPSt1S2rST7qJ0TRZZrdvjE+58YBY1ZaLs0g9u456iXl39qOxiXlLwqeCM4QojqAuX9SlYi7kPxwL4SlJ7Hl/hZrK6lP0g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=intel.com; dmarc=pass action=none header.from=intel.com; dkim=pass header.d=intel.com; arc=none Authentication-Results: mx.microsoft.com 1; dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=intel.com; Received: from MW3PR11MB4684.namprd11.prod.outlook.com (2603:10b6:303:5d::14) by PH7PR11MB6556.namprd11.prod.outlook.com (2603:10b6:510:1aa::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.688.10; Fri, 4 Dec 2026 21:22:40 +0000 Received: from MW3PR11MB4684.namprd11.prod.outlook.com ([fe80::2f5a:96ce:f7e4:c315]) by MW3PR11MB4684.namprd11.prod.outlook.com ([fe80::2f5a:96ce:f7e4:c315%7]) with mapi id 15.21.0688.011; Fri, 4 Dec 2026 21:22:40 +0000 Date: Fri, 4 Dec 2026 13:22:37 -0800 From: Niranjana Vishwanathapura To: Matthew Brost CC: , , Carlos Santa , Ryan Neph , Natalie Vock , Arun Pravin , Matthew Auld , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter , , Thomas =?iso-8859-1?Q?Hellstr=F6m?= Subject: Re: [PATCH v6 18/35] drm/xe: Add a page defragmentation worker Message-ID: References: <20261203202352.4153159-1-matthew.brost@intel.com> <20261203202352.4153159-19-matthew.brost@intel.com> Content-Type: text/plain; charset=iso-8859-1; format=flowed Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20261203202352.4153159-19-matthew.brost@intel.com> X-ClientProxiedBy: SJ0PR13CA0162.namprd13.prod.outlook.com (2603:10b6:a03:2c7::17) To MW3PR11MB4684.namprd11.prod.outlook.com (2603:10b6:303:5d::14) X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: MW3PR11MB4684:EE_|PH7PR11MB6556:EE_ X-MS-Office365-Filtering-Correlation-Id: 8f810cf2-0009-4ba2-6942-08df524cba15 X-LD-Processed: 46c98d88-e344-4ed4-8496-4ed7712e255d,ExtAddr X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|366016|23010399003|1800799024|376014|7416014|6133799003|4143699003|18002099003|22082099003|5023799004|56012099006|261022105027099003|10067099003|261102123227099003|11063799006|261021123027099003|261026140227099003|261026121527099003|261009223027099003|261022075727099003|261009221227099003|261021121427099003|261009223327099003|261009220027099003|261009221727099003|261028144727099003; X-Microsoft-Antispam-Message-Info: tPBchTFogsrwOx/2qzZb6iZqHs8WeIPRtsyxj/1NfeXMx8S742uGch89MjfzFoTCriq58FPQbdd1OOpWgS5jP+U5bbkK9eReYJwpOvrjknVBatzGMnCs5dKul6w6aB5H90/Y75sJul9I8AGDetzAVx/kLC2HaJ4dulRvEoV1gV2Vosi4Pa1bcet4A7Zwj9ygLKjQj9DsFabBBtCpRU7s+wXFmcRVrXQuFVl59ijx6eo6kDqs20DorBHeSCLdetUikB5O6QVIHDR3phnXt/4bHAPAWmRPigzCvEcROwPZIoSlEeXzIcSX25bqxCXkLFJoqs+WlUO4ur0RNOwJni7twfTycgl0VZ0imOwA9sXQrdQ3GjX5EDbcg3IDwJemFrCZAtmEVsenaKBDggj/KTl6KKXYA4EpBWvKss6F1hW8PxgMf2NKYL9NfMUFTipa/i7FTLH57PH1woArGdrMK2qDLaJj2Cq+wAm/pwIzg/4EzgXxNoOM2qm2aIH7VNl0TvGnSuelQzSEbvoSu1tOXk0tFJLyuRjT9Bs/qu+DgrSHZVQyK11goGEf0N+RLlMmEtzA5pgYl0xRjRU/5sT7hFs6zD/ARFZxdtVux//3EEhyxOmjhSup9z9IRh+qC7STkwDZ3A4VHUOykjtZ79WcXnmw20Mxk6ddc+Tp1qtiTRwyaZ8= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:MW3PR11MB4684.namprd11.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(366016)(23010399003)(1800799024)(376014)(7416014)(6133799003)(4143699003)(18002099003)(22082099003)(5023799004)(56012099006)(261022105027099003)(10067099003)(261102123227099003)(11063799006)(261021123027099003)(261026140227099003)(261026121527099003)(261009223027099003)(261022075727099003)(261009221227099003)(261021121427099003)(261009223327099003)(261009220027099003)(261009221727099003)(261028144727099003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?iso-8859-1?Q?PyOinjxcj9JxqA+UIgsd3m2DvRB5bX4aDDLf85xrfwuzUrAXS2ecOb8pku?= =?iso-8859-1?Q?Z7d1Srmc9OA2DkMpcbGU/dufT7BpTEGaEnzfgcPphpfa/aP2bB6YoimNyK?= =?iso-8859-1?Q?sNZtmqln/dxCgDwo49LiIvi/PcGLuuhhl4yyJo/sarCkklMnpuVJwWjZNJ?= =?iso-8859-1?Q?sXUwslM7SDaCkUgZhfiuLpsgG8kaYnvRaqYPNNAZ3h7ykb5J+fEMqBOJhK?= =?iso-8859-1?Q?md2nlHKNE+r1HHGTzkZhTDJaONZyqEJmrMW5M/oR2uVNOVpP5o0yEj+SM4?= =?iso-8859-1?Q?Zl8Io4rxgKjkYL3UrmwKzjRYl82/fAGSsafu8XWzekHiGH+dW/OnOPpv5A?= =?iso-8859-1?Q?NtZ6C33Luu3APRcxF4SljWHcEWL9WqoGIwDJzeE7N8Lvp8vL9d/BRiyp5y?= =?iso-8859-1?Q?ceqkrzdEM0wVo3QEf33t8qJggpTcDRdr9k7K1+YStFyJmKhbR4iaDQUgRV?= =?iso-8859-1?Q?z+BkvFsqaz84uzV1zsDkHl5Rsow7BVZSca09hQrv9/b7JRFBViFDZ/T6C0?= =?iso-8859-1?Q?45XnuInAKuAroTTDzgEeoAuWpsxEjNRxT2dqhUYn+yckn4wAhxT1ksRF98?= =?iso-8859-1?Q?JkjtrS0QFEfadZtMe4O15o6L10hPSWYTHjUCjDHVVnJagqS3BlKhYL3JYK?= =?iso-8859-1?Q?MbDOzai1Fz0F9Go2XFsr8lLbTSZtx1hs/8luHP2D+P8U+IApI+JfLFn9Jb?= =?iso-8859-1?Q?FUvuX2GBekfrFglXM3V2x7Ek6/f0hjq8S3Un0Imo66IhjlpyRDgHzCnbfX?= =?iso-8859-1?Q?CMWvHjMm8YMLEMn4vwpYv4DZm/mUxEPKkKzA6ISTuBxgr48jrQJaj+MIO3?= =?iso-8859-1?Q?aWjLdRnjNMfextdjWRRuS5SDezb9yEkKrzrPupCdsBwi6AXefceP6jC7fu?= =?iso-8859-1?Q?znPlgN/nijOZXv7V4ZRNLiJb5S7lNiD/fcKlYBLS5Ut9CiE9A+1bppwjYz?= =?iso-8859-1?Q?n9eGoyDv1n/e88ifcJETwlzedKcBLUesL4VSeFVU0RuPsl2BpNt97llb/0?= =?iso-8859-1?Q?9GpgRDGhJ8haNG41+Z5OjZaptW22yw79XzmkMBPCERJ2ZvjkfAipTcRY1v?= =?iso-8859-1?Q?rPswzWAI+wqjEPT8av5W00qRp7d4PyCchHWeSsrgbUyabBORdpdAIMrsni?= =?iso-8859-1?Q?20bQ2QNV1AyeM7AaqT1qK9sljUjJoijp5a7U5QjiYbudogygAU13vArqzg?= =?iso-8859-1?Q?r1tQR6/KxXFglNRkEYJ/hebkYJMUpI7xWyovgRMxpVSghANV+LUMuL23CB?= =?iso-8859-1?Q?ZoVyONFws89CZ0qszgh3fQHmgL+wqKqNlFvjSwXLtHkXxTzkQpf79vw2Gj?= =?iso-8859-1?Q?mIy73/eNg7OroJMTNRXSe40xp1PMdyIaBY7lw+Xoc570klVdd0W8VsSFgq?= =?iso-8859-1?Q?qz1wYNO6aLYaM8XHoxlgC0piiowlPoiRuaB8Mtny/a8w8iuUViWSmXbORT?= =?iso-8859-1?Q?AxGqdD4PYvxmgOxUzzYrX1neubb5L4QIEXgAMXQzPpqW2BX/JSFeVWfNdf?= =?iso-8859-1?Q?qebOZfcunftu4rHFSRTIakMdtUtPglCxFklvUJtqIX+JDPUaoKJFAl0VDS?= =?iso-8859-1?Q?86lVXBXCDuvA12BHo5797dff0FoAudZQi2EqpKeGnROniKvhbYYSsGvip4?= =?iso-8859-1?Q?hKZUvlith6Td7yRyYPlXW5bf3Hzkqoq+nyYjSJVELXqsLEux/ph9p2tWCm?= =?iso-8859-1?Q?TltEJkQ4biSIWdVMxLWnIpXZorhgh6r/FPUczl8lqNQYyyGi2r0Qdy2PNR?= =?iso-8859-1?Q?M8xUJtqoFuT2rVgfzuacgmhr0KudDpF/V0OeLhQVCuROxPmCpzJt35jRou?= =?iso-8859-1?Q?noHXxk3/NMPbWqMdYq6alEJXZwh+AMJ53ppsmQDbU1r/0fs527Qi?= X-Exchange-RoutingPolicyChecked: lQWzeOuOlexRUw/m/WyOr+qwGrPQM46RD+Nl358i4UVyU/yEVnvropcdPy3T/nIIp5I0ITU0N6FGigXYD4KPTEq1mQjzVSPDzqTYtvCUKZaOjgmSGBSGQFH6/Mzvvjizawf+ln3rTas8hgiZTdKoe6ylVT81+z7sxU7Wa5NzBvxkKijohuVBpBSw59L7gZPHHuxJqd2STXrX2ALQqdc0pN+g1IYlkfNR6PKl+yPywAgEqPvmfa4dlvyaxlwqi7pfDuG1Tixl8V5rv+Q7RFDPXumMLT2T4h5RfgqdX2fk62Xe1ArUN6Krecv0QI5SBXjRa2uGn8VaMhgGujdpR7LLKg== X-MS-Exchange-CrossTenant-Network-Message-Id: 8f810cf2-0009-4ba2-6942-08df524cba15 X-MS-Exchange-CrossTenant-AuthSource: MW3PR11MB4684.namprd11.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 Dec 2026 21:22:40.4730 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 46c98d88-e344-4ed4-8496-4ed7712e255d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: D9FxsA1TtqgjC4hZ/h13OzpqirOKSkB3WlWunqpkfwEEkhgS4SJVGu3u1pKeC271OwevpX/o8EYLTVX5VFPWa9VcFAd6KKVhmQvhXx6tByOwF3ziQg9gEpZX3FGjBk6L X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR11MB6556 X-OriginatorOrg: intel.com On Thu, Dec 03, 2026 at 12:23:35PM -0800, Matthew Brost wrote: >BOs whose backing TT pages were allocated below the device's beneficial >order (tracked via the TTM_TT_FLAG_BENEFICIAL_ORDER_FAILED tt page flag) >are added to a per-device defrag list. Add a delayed worker that >periodically walks this list and attempts to re-back each BO with >beneficial-order pages, improving TLB efficiency for long-lived objects >that were initially allocated under memory pressure. > >The worker is kicked when the list first transitions to non-empty and >reschedules itself while work remains. On a failed pass it backs off >exponentially (XE_BO_DEFRAG_INTERVAL_MS up to >XE_BO_DEFRAG_INTERVAL_MAX_MS) and retries the whole list later; the >interval is reset to the default on the next fresh enqueue. The worker >stops scheduling once the list drains. > >A defrag move synchronously reallocates and re-copies a BO's backing >store, which is not free. To avoid stalling concurrent active work when >many BOs (or one very large BO) become eligible at once (e.g. after a >burst of memory pressure, which would otherwise manifest as a large FPS >drop while the worker churns through the whole list), cap the number of >newly (re)allocated bytes processed per run to XE_BO_DEFRAG_SIZE_LIMIT >and reschedule. Only the pages a move actually reallocates are charged - >pages harvested (borrowed) from the old backing are free and do not count >- so the budget tracks real defrag work and an object larger than the >budget is upgraded in budget-sized slices across runs. This spreads the >defrag effort over time, keeping it in the background and yielding to >userspace progress. > >Defragmenting a BO forces a TTM move (ctx.defrag) that reallocates the >backing at the beneficial order and relocates the contents on the GPU >(see xe_bo_move()). The per-BO move is bounded by the run's remaining >byte budget via ctx.defrag_bytes_remaining; the pool allocator reports >how much it spent so the worker can charge it accurately. When >beneficial-order pages cannot be obtained the new allocation is >discharged during populate and the BO keeps its original backing, staying >on the list for a later retry. > >v4: > - Only enter reclaim from defrag worker > - Disable worker with early cleanup handler too (Sashiko) >v5: > - Fix suspend / resume by pausing defrag worker (Sashiko) > - Clear TTM_PL_FLAG_TEMPORARY after defrag (Sashiko) > - Use xe_pm_runtime_get_if_active in defrag worker (Sashiko) >v6: > - Skip defrag when the device is wedged (Sashiko) > - Stop the defrag worker in xe_device_shutdown() (Sashiko) > >Cc: Carlos Santa >Cc: Ryan Neph >Cc: Natalie Vock >Cc: Arun Pravin >Cc: Matthew Auld >Cc: Maarten Lankhorst >Cc: Maxime Ripard >Cc: Thomas Zimmermann >Cc: David Airlie >Cc: Simona Vetter >Cc: dri-devel@lists.freedesktop.org >Cc: linux-kernel@vger.kernel.org >Cc: Thomas Hellström >Signed-off-by: Matthew Brost >--- > drivers/gpu/drm/xe/xe_bo.c | 412 ++++++++++++++++++++++++++- > drivers/gpu/drm/xe/xe_bo.h | 6 +- > drivers/gpu/drm/xe/xe_device.c | 25 +- > drivers/gpu/drm/xe/xe_device_types.h | 17 ++ > drivers/gpu/drm/xe/xe_pm.c | 12 + > 5 files changed, 463 insertions(+), 9 deletions(-) > >diff --git a/drivers/gpu/drm/xe/xe_bo.c b/drivers/gpu/drm/xe/xe_bo.c >index 01667dfba78f..d5153c40bc32 100644 >--- a/drivers/gpu/drm/xe/xe_bo.c >+++ b/drivers/gpu/drm/xe/xe_bo.c >@@ -34,6 +34,7 @@ > #include "xe_pat.h" > #include "xe_pm.h" > #include "xe_preempt_fence.h" >+#include "xe_printk.h" > #include "xe_pxp.h" > #include "xe_res_cursor.h" > #include "xe_shrinker.h" >@@ -45,6 +46,35 @@ > #include "xe_vram_types.h" > #include "xe_wedge.h" > >+/* >+ * Maximum number of bytes of newly (re)allocated backing the defrag worker will >+ * produce in a single run before yielding and rescheduling itself. >+ * >+ * A defrag move synchronously reallocates and re-copies a BO's backing store, >+ * which is not free. If a large number of BOs (or one very large BO) become >+ * eligible at once (e.g. after a burst of memory pressure), processing them all >+ * in one worker run would hold things up for a long, unbounded stretch and can >+ * visibly starve concurrent active work (e.g. a large FPS drop). Instead, cap >+ * the bytes actually upgraded per run and requeue, spreading the defrag effort >+ * out over time so it stays in the background and yields to userspace progress. >+ * Only pages a move truly reallocates are charged; pages harvested from the old >+ * backing are free, so an object larger than the budget is upgraded in >+ * budget-sized slices across runs. >+ */ >+#define XE_BO_DEFRAG_SIZE_LIMIT SZ_32M >+ >+/* Default delay before (re)running the defrag worker, in milliseconds. */ >+#define XE_BO_DEFRAG_INTERVAL_MS 25 >+ >+/* >+ * Upper bound for the (exponentially backed off) defrag worker interval, in >+ * milliseconds, so repeated failures don't push the retry arbitrarily far out. >+ */ >+#define XE_BO_DEFRAG_INTERVAL_MAX_MS 15000 /* 15 seconds */ >+ >+static void xe_bo_defrag_worker(struct work_struct *w); >+static void xe_place_from_ttm_type(u32 mem_type, struct ttm_place *place); >+ > const char *const xe_mem_type_to_name[TTM_NUM_MEM_TYPES] = { > [XE_PL_SYSTEM] = "system", > [XE_PL_TT] = "gtt", >@@ -727,9 +757,12 @@ static struct ttm_tt *xe_ttm_tt_create(struct ttm_buffer_object *ttm_bo, > return tt; > } > >+static bool xe_bo_defrag_supported(struct xe_device *xe); >+ > static int xe_ttm_tt_populate(struct ttm_device *ttm_dev, struct ttm_tt *tt, > struct ttm_operation_ctx *ctx) > { >+ struct xe_device *xe = ttm_to_xe_device(ttm_dev); > struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm); > int err; > >@@ -741,6 +774,10 @@ static int xe_ttm_tt_populate(struct ttm_device *ttm_dev, struct ttm_tt *tt, > !(tt->page_flags & TTM_TT_FLAG_EXTERNAL_MAPPABLE)) > return 0; > >+ /* Only defrag will enter reclaim at higher orders */ >+ ctx->beneficial_reclaim_backoff = !ctx->defrag && >+ xe_bo_defrag_supported(xe); >+ > if (ttm_tt_is_backed_up(tt) && !xe_tt->purgeable) { > err = ttm_tt_restore(ttm_dev, tt, ctx); > } else { >@@ -1183,17 +1220,140 @@ int xe_ttm_bo_purge(struct ttm_buffer_object *ttm_bo, struct ttm_operation_ctx * > return 0; > } > >+static void xe_bo_defrag_fini_early(void *arg) >+{ >+ struct xe_device *xe = arg; >+ >+ disable_delayed_work_sync(&xe->mem.defrag.worker); >+} >+ >+static void xe_bo_defrag_fini(void *arg) >+{ >+ struct xe_device *xe = arg; >+ >+ disable_delayed_work_sync(&xe->mem.defrag.worker); >+} >+ > /** > * xe_bo_defrag_init_early() - Initialize the device defrag BO tracking > * @xe: The xe device > * >- * Initialize the list and lock used to track BOs whose backing TT >- * pages were allocated at a sub-optimal order. >+ * Initialize the list, lock and delayed worker used to track and >+ * defragment BOs whose backing TT pages were allocated at a sub-optimal order. >+ * >+ * Return: 0 on success, negative error code on failure. > */ >-void xe_bo_defrag_init_early(struct xe_device *xe) >+int xe_bo_defrag_init_early(struct xe_device *xe) > { > spin_lock_init(&xe->mem.defrag.lock); > INIT_LIST_HEAD(&xe->mem.defrag.list); >+ xe->mem.defrag.interval_ms = XE_BO_DEFRAG_INTERVAL_MS; >+ INIT_DELAYED_WORK(&xe->mem.defrag.worker, xe_bo_defrag_worker); >+ >+ return devm_add_action_or_reset(xe->drm.dev, xe_bo_defrag_fini_early, xe); >+} >+ >+/** >+ * xe_bo_defrag_init() - Register defrag worker teardown >+ * @xe: The xe device >+ * >+ * Register the devm action that disables and drains the defrag worker on >+ * device teardown. This must be called after xe_migrate_init() so that, with >+ * devm's reverse-order cleanup, the worker is stopped before the migrate >+ * contexts it relies on (via xe_bo_defrag_one() -> ttm_bo_validate() -> >+ * xe_bo_move() -> xe_migrate_copy_defrag()) are torn down. >+ * >+ * Return: 0 on success, negative error code on failure. >+ */ >+int xe_bo_defrag_init(struct xe_device *xe) >+{ >+ return devm_add_action_or_reset(xe->drm.dev, xe_bo_defrag_fini, xe); >+} >+ >+/* >+ * Queue the defrag worker at the current interval. Called under >+ * @mem.defrag.lock so it cannot race xe_bo_defrag_suspend() setting >+ * @mem.defrag.suspended and cancelling the worker. A @kick (list became >+ * non-empty or the device resumed) uses mod_delayed_work() so that it pulls in >+ * a pending, backed-off timer rather than leaving it in place. >+ */ >+static void xe_bo_defrag_schedule_locked(struct xe_device *xe, bool kick) >+{ >+ unsigned long delay; >+ >+ lockdep_assert_held(&xe->mem.defrag.lock); >+ >+ if (xe->mem.defrag.suspended) >+ return; >+ >+ delay = msecs_to_jiffies(xe->mem.defrag.interval_ms); >+ if (kick) >+ mod_delayed_work(system_percpu_wq, &xe->mem.defrag.worker, >+ delay); >+ else >+ queue_delayed_work(system_percpu_wq, &xe->mem.defrag.worker, >+ delay); >+} >+ >+static void xe_bo_defrag_kick_locked(struct xe_device *xe) >+{ >+ lockdep_assert_held(&xe->mem.defrag.lock); >+ >+ if (list_empty(&xe->mem.defrag.list)) >+ return; >+ >+ xe->mem.defrag.interval_ms = XE_BO_DEFRAG_INTERVAL_MS; >+ xe_bo_defrag_schedule_locked(xe, true); >+} >+ >+/** >+ * xe_bo_defrag_suspend() - Stop the defragmentation worker for suspend / FLR >+ * @xe: xe device instance >+ * >+ * Block the defrag worker from being queued and wait for any running instance >+ * to finish, so no defrag move (and its GPU copy) can run while the device is >+ * being suspended or reset. Idempotent; undone by xe_bo_defrag_resume(). >+ */ >+void xe_bo_defrag_suspend(struct xe_device *xe) >+{ >+ scoped_guard(spinlock, &xe->mem.defrag.lock) >+ xe->mem.defrag.suspended = true; >+ >+ cancel_delayed_work_sync(&xe->mem.defrag.worker); >+} >+ >+/** >+ * xe_bo_defrag_resume() - Re-enable the defragmentation worker >+ * @xe: xe device instance >+ * >+ * Undo xe_bo_defrag_suspend() after system resume or FLR (or an aborted >+ * suspend) and re-arm the worker if BOs are still pending. Idempotent. >+ */ >+void xe_bo_defrag_resume(struct xe_device *xe) >+{ >+ guard(spinlock)(&xe->mem.defrag.lock); >+ >+ xe->mem.defrag.suspended = false; >+ xe_bo_defrag_kick_locked(xe); >+} >+ >+/** >+ * xe_bo_defrag_kick() - Re-arm the defragmentation worker >+ * @xe: xe device instance >+ * >+ * The defrag worker bails without rescheduling when it cannot take a runtime >+ * PM reference (the device is runtime suspended/suspending). Call this when the >+ * device becomes runtime active again (runtime resume, or a failed runtime >+ * suspend) so the worker is re-armed if BOs are still pending, rather than >+ * polling a wakeup timer while suspended. A no-op if the tracking list is empty >+ * (the next xe_bo_defrag_add() will kick the worker) or while >+ * xe_bo_defrag_suspend() is in effect. >+ */ >+void xe_bo_defrag_kick(struct xe_device *xe) >+{ >+ guard(spinlock)(&xe->mem.defrag.lock); >+ >+ xe_bo_defrag_kick_locked(xe); > } > > /* >@@ -1251,9 +1411,14 @@ static void xe_bo_defrag_add(struct xe_bo *bo) > if (!list_empty(&bo->defrag_link)) > return; > >- scoped_guard(spinlock, &xe->mem.defrag.lock) { >- if (list_empty(&bo->defrag_link)) >- list_add_tail(&bo->defrag_link, &xe->mem.defrag.list); >+ guard(spinlock)(&xe->mem.defrag.lock); >+ if (list_empty(&bo->defrag_link)) { >+ /* Kick the worker when the list transitions to non-empty. */ >+ bool kick = list_empty(&xe->mem.defrag.list); >+ >+ list_add_tail(&bo->defrag_link, &xe->mem.defrag.list); >+ if (kick) >+ xe_bo_defrag_kick_locked(xe); > } > } > >@@ -1300,6 +1465,235 @@ static void xe_bo_defrag_update(struct xe_bo *bo) > xe_bo_defrag_remove(bo); > } > >+/* >+ * Attempt to defragment a single BO by forcing a move that reallocates its >+ * backing at the device's beneficial order. @budget bounds how many bytes of >+ * new pages the move may (re)allocate; the remainder of the object is completed >+ * by harvesting the old backing, and the BO stays flagged for a later pass. On >+ * return *@consumed holds the bytes actually (re)allocated (0 on failure or >+ * when nothing needed reallocating). *@needs_more is set to true when the move >+ * succeeded but the BO still has sub-beneficial backing (partial upgrade) and >+ * should be revisited; it is computed under the BO lock so the caller does not >+ * need to re-inspect bo->ttm.ttm after the lock is dropped. Returns 0 if the BO >+ * no longer needs to be tracked (either defragmented or no longer eligible), or >+ * a negative error code if the attempt should be retried later. >+ */ >+static int xe_bo_defrag_one(struct xe_device *xe, struct xe_bo *bo, >+ u64 budget, u64 *consumed, bool *needs_more) >+{ >+ struct ttm_operation_ctx ctx = { >+ .gfp_retry_mayfail = true, >+ .defrag = true, >+ .defrag_bytes_remaining = budget, >+ }; >+ struct ttm_buffer_object *ttm_bo = &bo->ttm; >+ struct ttm_placement placement; >+ struct ttm_place place; >+ int ret = 0; >+ >+ *consumed = 0; >+ *needs_more = false; >+ >+ xe_bo_lock(bo, false); >+ >+ /* Re-check eligibility under the BO lock. */ >+ if (!xe_bo_needs_defrag(bo)) { >+ xe_bo_defrag_remove(bo); >+ goto unlock; >+ } >+ >+ xe_place_from_ttm_type(ttm_bo->resource->mem_type, &place); >+ place.flags |= TTM_PL_FLAG_TEMPORARY; >+ placement.num_placement = 1; >+ placement.placement = &place; >+ >+ /* >+ * On success the move reallocates the backing at beneficial order and >+ * drops the BO from the defrag list. On failure the BO keeps its >+ * original backing and stays on the list for a later retry. >+ */ >+ ret = ttm_bo_validate(ttm_bo, &placement, &ctx); >+ >+ /* >+ * TTM_PL_FLAG_TEMPORARY only lets the new XE_PL_TT resource bypass the >+ * sys manager's size check while the old and new resources are >+ * transiently both charged. ttm_resource_init() copied it into the new >+ * resource's placement, where it would make ttm_resource_compatible() >+ * reject the resource on the next validate and force a needless move. >+ */ >+ if (!ret) >+ ttm_bo->resource->placement &= ~TTM_PL_FLAG_TEMPORARY; >+ >+ /* >+ * The pool allocator decremented defrag_bytes_remaining as it >+ * (re)allocated pages, so the unused budget tells us how much was spent. >+ * Only meaningful when the move succeeded, in which case TTM has >+ * (re)allocated at least one chunk. >+ */ >+ if (!ret) { >+ s64 spent = (s64)budget - ctx.defrag_bytes_remaining; >+ >+ *consumed = spent > 0 ? spent : 0; >+ } >+ >+ xe_dbg(xe, "Defrag attempt on BO size=%zu: ret=%pe consumed=%llu\n", >+ xe_bo_size(bo), ERR_PTR(ret), *consumed); >+ >+ if (!ret && ttm_tt_is_beneficial_order_failed(bo->ttm.ttm)) >+ *needs_more = true; >+ >+unlock: >+ xe_bo_unlock(bo); >+ return ret; >+} >+ >+static void xe_bo_defrag_worker(struct work_struct *w) >+{ >+ struct delayed_work *dwork = to_delayed_work(w); >+ struct xe_device *xe = >+ container_of(dwork, struct xe_device, mem.defrag.worker); >+ u64 defrag_bytes = 0; >+ bool requeue = false; >+ int idx; >+ >+ if (!drm_dev_enter(&xe->drm, &idx)) >+ return; >+ >+ if (!xe_pm_runtime_get_if_active(xe)) { >+ /* >+ * The device is (runtime) suspending/suspended. Do not spin a >+ * wakeup timer here: the worker is re-armed via >+ * xe_bo_defrag_kick() once the device is runtime active again >+ * (runtime resume or a failed runtime suspend), so tracked BOs >+ * are picked up without polling while suspended. >+ */ >+ drm_dev_exit(idx); >+ return; >+ } >+ >+ /* >+ * A wedged device (declared wedged or prepared for a PCI reset) skips >+ * GPU submission, so a defrag move would commit uncopied backing and >+ * free the old pages. Leave the BOs alone; retry with backoff if the >+ * wedge may clear (PCI error recovery), stop if it is permanent. >+ */ >+ if (xe_wedged(xe)) { >+ if (!xe_wedged_permanent(xe)) { >+ guard(spinlock)(&xe->mem.defrag.lock); >+ >+ xe->mem.defrag.interval_ms = >+ min(xe->mem.defrag.interval_ms * 2, >+ (unsigned int)XE_BO_DEFRAG_INTERVAL_MAX_MS); >+ xe_bo_defrag_schedule_locked(xe, false); >+ } >+ goto out; >+ } >+ >+ /* >+ * Process at most XE_BO_DEFRAG_SIZE_LIMIT bytes of newly (re)allocated >+ * backing per run rather than draining the whole list in one go. Only >+ * the pages a move actually reallocates are charged - pages harvested >+ * (borrowed) from the old backing are free and do not count - so the >+ * budget tracks real defrag work. A single object larger than the >+ * budget is upgraded in slices across successive runs. Each defrag is >+ * synchronous and relatively expensive, so bounding the work per run >+ * keeps the worker from monopolising resources and lets concurrent >+ * active work make progress; any remaining work is handled by a >+ * follow-up run scheduled below. >+ */ >+ while (defrag_bytes < XE_BO_DEFRAG_SIZE_LIMIT) { >+ struct xe_bo *first, *bo; >+ u64 consumed = 0; >+ bool needs_more = false; >+ int ret; >+ >+ scoped_guard(spinlock, &xe->mem.defrag.lock) { >+ bool again; >+ >+ /* Progress, reset interval */ >+ if (defrag_bytes) >+ xe->mem.defrag.interval_ms = XE_BO_DEFRAG_INTERVAL_MS; >+ >+ do { >+ first = list_first_entry_or_null(&xe->mem.defrag.list, >+ struct xe_bo, >+ defrag_link); >+ bo = first ? xe_bo_get_unless_zero(first) : NULL; >+ again = first && !bo; >+ if (again) >+ list_del_init(&first->defrag_link); >+ } while (again); >+ } >+ >+ if (!bo) >+ break; >+ >+ /* >+ * Bound the per-BO move to the run's remaining byte budget. A >+ * large object is upgraded in budget-sized slices across runs; >+ * the slices already upgraded come back as free already- >+ * beneficial harvests, so each run only spends budget on new >+ * forward progress. >+ */ >+ ret = xe_bo_defrag_one(xe, bo, XE_BO_DEFRAG_SIZE_LIMIT - defrag_bytes, >+ &consumed, &needs_more); >+ defrag_bytes += consumed; >+ >+ if (ret || needs_more) { >+ scoped_guard(spinlock, &xe->mem.defrag.lock) { >+ if (ret) >+ /* >+ * Abort the pass and retry the whole >+ * list later, backing off exponentially >+ * on every failure. >+ */ >+ xe->mem.defrag.interval_ms = >+ min(xe->mem.defrag.interval_ms * 2, >+ (unsigned int)XE_BO_DEFRAG_INTERVAL_MAX_MS); Assisted-by: LLM NIT...instead of doing this exponantial backoff upon 'ret', LLM suggests doing this upon 'ret || (needs_more && consumed < budget)'. ie., even for the case where needs_more is true and defragmentation budget is not fully consumed (heavy fragmentation case), as jut resheduling as in the 'else' case here doesn't probably make any progress. In anycase, Reviewed-by: Niranjana Vishwanathapura >+ else >+ /* Progress, reset interval */ >+ xe->mem.defrag.interval_ms = XE_BO_DEFRAG_INTERVAL_MS; >+ } >+ >+ xe_bo_put(bo); >+ requeue = true; >+ break; >+ } >+ >+ xe_bo_put(bo); >+ } >+ >+ /* >+ * Decide whether to reschedule: >+ * >+ * - The loop hit its per-run limit (defrag_bytes >= >+ * XE_BO_DEFRAG_SIZE_LIMIT): we stopped early to spread the work out, >+ * so requeue at the default interval (reset here since this run made >+ * progress) whenever the list still has entries. >+ * >+ * - The loop aborted on a failed defrag (requeue): retry later at the >+ * backed-off interval computed above. >+ * >+ * - The loop drained the list (broke on the empty list): nothing left to >+ * do, so stop scheduling. A fresh enqueue will kick the worker again. >+ */ >+ if (defrag_bytes >= XE_BO_DEFRAG_SIZE_LIMIT) { >+ guard(spinlock)(&xe->mem.defrag.lock); >+ >+ xe->mem.defrag.interval_ms = XE_BO_DEFRAG_INTERVAL_MS; >+ if (!list_empty(&xe->mem.defrag.list)) >+ xe_bo_defrag_schedule_locked(xe, false); >+ } else if (requeue) { >+ guard(spinlock)(&xe->mem.defrag.lock); >+ >+ xe_bo_defrag_schedule_locked(xe, false); >+ } >+ >+out: >+ xe_pm_runtime_put(xe); >+ drm_dev_exit(idx); >+} >+ > static int xe_bo_move(struct ttm_buffer_object *ttm_bo, bool evict, > struct ttm_operation_ctx *ctx, > struct ttm_resource *new_mem, >@@ -1364,6 +1758,12 @@ static int xe_bo_move(struct ttm_buffer_object *ttm_bo, bool evict, > */ > if (has_defrag_old_tt) { > xe_assert(xe, bo->flags & XE_BO_FLAG_SYSTEM); >+ /* >+ * Wedged queues skip submission; fail so TTM restores the old >+ * tt instead of committing uncopied backing. >+ */ >+ if (xe_wedged(xe)) >+ return -ENODEV; > move_lacks_source = false; > } > >diff --git a/drivers/gpu/drm/xe/xe_bo.h b/drivers/gpu/drm/xe/xe_bo.h >index f6b8b71228c6..ff99bed7089b 100644 >--- a/drivers/gpu/drm/xe/xe_bo.h >+++ b/drivers/gpu/drm/xe/xe_bo.h >@@ -232,7 +232,11 @@ void xe_bo_unpin_external(struct xe_bo *bo); > void xe_bo_unpin(struct xe_bo *bo); > void xe_bo_pin_account(struct xe_bo *bo); > void xe_bo_unpin_account(struct xe_bo *bo); >-void xe_bo_defrag_init_early(struct xe_device *xe); >+int xe_bo_defrag_init_early(struct xe_device *xe); >+int xe_bo_defrag_init(struct xe_device *xe); >+void xe_bo_defrag_suspend(struct xe_device *xe); >+void xe_bo_defrag_resume(struct xe_device *xe); >+void xe_bo_defrag_kick(struct xe_device *xe); > int xe_bo_validate(struct xe_bo *bo, struct xe_vm *vm, bool allow_res_evict, > struct drm_exec *exec); > >diff --git a/drivers/gpu/drm/xe/xe_device.c b/drivers/gpu/drm/xe/xe_device.c >index 081a34425181..1dd9d39837da 100644 >--- a/drivers/gpu/drm/xe/xe_device.c >+++ b/drivers/gpu/drm/xe/xe_device.c >@@ -549,7 +549,9 @@ int xe_device_init_early(struct xe_device *xe) > if (err) > return err; > >- xe_bo_defrag_init_early(xe); >+ err = xe_bo_defrag_init_early(xe); >+ if (err) >+ return err; > > xe->preempt_fence_wq = alloc_ordered_workqueue("xe-preempt-fence-wq", > WQ_MEM_RECLAIM); >@@ -1071,6 +1073,15 @@ int xe_device_probe(struct xe_device *xe) > if (err) > return err; > >+ /* >+ * Register the defrag worker teardown now that the migrate contexts it >+ * depends on are initialized, so devm's reverse-order cleanup stops the >+ * worker before those contexts are torn down. >+ */ >+ err = xe_bo_defrag_init(xe); >+ if (err) >+ return err; >+ > err = xe_pagefault_init(xe); > if (err) > return err; >@@ -1191,6 +1202,9 @@ void xe_device_shutdown(struct xe_device *xe) > > xe_hwmon_unregister(xe); > >+ /* No defrag moves (GPU copies) across GT shutdown and FLR. */ >+ xe_bo_defrag_suspend(xe); >+ > xe_display_shutdown(xe); > > xe_irq_suspend(xe); >@@ -1458,9 +1472,12 @@ int xe_device_suspend(struct xe_device *xe, bool hibernate, bool flr) > u8 id; > int err; > >+ /* No defrag moves (GPU copies) while suspending or across FLR. */ >+ xe_bo_defrag_suspend(xe); >+ > err = xe_pxp_pm_suspend(xe->pxp); > if (err) >- return err; >+ goto err_defrag; > > xe_late_bind_pm_suspend(&xe->late_bind); > >@@ -1508,6 +1525,8 @@ int xe_device_suspend(struct xe_device *xe, bool hibernate, bool flr) > xe_display_pm_resume(xe); > xe_pxp_pm_resume(xe->pxp); > xe_hwmon_suspend_abort(xe); >+err_defrag: >+ xe_bo_defrag_resume(xe); > return err; > } > >@@ -1605,5 +1624,7 @@ int xe_device_resume(struct xe_device *xe, bool flr) > xe_late_bind_pm_resume(&xe->late_bind); > xe_hwmon_resume(xe, false); > >+ xe_bo_defrag_resume(xe); >+ > return 0; > } >diff --git a/drivers/gpu/drm/xe/xe_device_types.h b/drivers/gpu/drm/xe/xe_device_types.h >index 5273d5663570..1c3f4b5c9232 100644 >--- a/drivers/gpu/drm/xe/xe_device_types.h >+++ b/drivers/gpu/drm/xe/xe_device_types.h >@@ -339,6 +339,23 @@ struct xe_device { > spinlock_t lock; > /** @mem.defrag.list: List of struct xe_bo defrag_link. */ > struct list_head list; >+ /** >+ * @mem.defrag.worker: Delayed worker that walks >+ * @mem.defrag.list trying to reallocate BO backing >+ * store at the device's beneficial order. >+ */ >+ struct delayed_work worker; >+ /** >+ * @mem.defrag.interval_ms: Reschedule interval for >+ * @mem.defrag.worker, in milliseconds. >+ */ >+ unsigned int interval_ms; >+ /** >+ * @mem.defrag.suspended: The device is suspending, >+ * suspended or undergoing FLR; @mem.defrag.worker must >+ * not be queued. Protected by @mem.defrag.lock. >+ */ >+ bool suspended; > } defrag; > } mem; > >diff --git a/drivers/gpu/drm/xe/xe_pm.c b/drivers/gpu/drm/xe/xe_pm.c >index 51e020fbf9a6..0d1a118afa91 100644 >--- a/drivers/gpu/drm/xe/xe_pm.c >+++ b/drivers/gpu/drm/xe/xe_pm.c >@@ -389,6 +389,8 @@ static int xe_pm_notifier_callback(struct notifier_block *nb, > struct xe_validation_ctx ctx; > > reinit_completion(&xe->pm_block); >+ /* Don't defrag BOs we are about to evict or tear down under. */ >+ xe_bo_defrag_suspend(xe); > if (!xe_pm_needs_bo_evict(xe, action == PM_HIBERNATION_PREPARE)) > break; > >@@ -416,6 +418,8 @@ static int xe_pm_notifier_callback(struct notifier_block *nb, > case PM_POST_SUSPEND: > complete_all(&xe->pm_block); > xe_pm_wake_rebind_workers(xe); >+ /* Also covers a suspend aborted after PM_*_PREPARE. */ >+ xe_bo_defrag_resume(xe); > if (!xe_pm_needs_bo_evict(xe, action == PM_POST_HIBERNATION)) > break; > >@@ -614,6 +618,12 @@ int xe_pm_runtime_suspend(struct xe_device *xe) > xe_pxp_pm_resume(xe->pxp); > xe_hwmon_suspend_abort(xe); > out: >+ /* >+ * The runtime PM core puts the device back to active without calling >+ * xe_pm_runtime_resume(), so re-arm a defrag worker that bailed while >+ * we were suspending. >+ */ >+ xe_bo_defrag_kick(xe); > xe_rpm_lockmap_release(xe); > xe_pm_write_callback_task(xe, NULL); > return err; >@@ -702,6 +712,8 @@ int xe_pm_runtime_resume(struct xe_device *xe) > if (IS_VF_CCS_READY(xe)) > xe_sriov_vf_ccs_register_context(xe); > >+ xe_bo_defrag_kick(xe); >+ > out: > xe_rpm_lockmap_release(xe); > xe_pm_write_callback_task(xe, NULL); >-- >2.34.1 >