From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1163A606A32C for ; Fri, 4 Dec 2026 19:18:07 +0000 (UTC) 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=krkxuZ0f Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id C597811315E; Fri, 4 Dec 2026 19:18:01 +0000 (UTC) Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.14]) by gabe.freedesktop.org (Postfix) with ESMTPS id D295C10EC64; Fri, 4 Dec 2026 19:17:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796411877; x=1827947877; h=date:from:to:cc:subject:message-id:references: content-transfer-encoding:in-reply-to:mime-version; bh=yGH1mzdlpBC6LYc08WOGP0oS07oUObr1sXygPrYhE84=; b=krkxuZ0fNaWMc0EzNCPDSSOjBF1UHc/4eK2u6KvD3zk5yfGIZe5y+7bD tQ4RfGrZdhNArWZKc3pNvFAO4G27WCTLX+wnFivg+T5RgqSHiZgPK/uos feBWAsMwjbvOCgwdRd28oUU9UnZpmkkvdE/50lMLNHTVzIlJCuF141BS2 lcHquu4SXa0thsOmh7OjdTOdNWXyAm4SepPua+L1Ed5vY28EQ8z45D5VJ wo3rsTj02e9Qj1bDF7lvf7u0jVxfhYobcJX/Kzy/Irq6wQWMEcClJ/zu5 BMww8ZfaTOkzEUjmR1G+YbezuQWOw2ZtZlPPKSqlEFZo70lrkN3qtAXqX Q==; X-CSE-ConnectionGUID: k8sIUPrPTrKPitBOKn0Rdg== X-CSE-MsgGUID: fdXPxXTCSwS30ZJ5AVMHmg== X-IronPort-AV: E=McAfee;i="6800,10657,11986"; a="5322277" X-IronPort-AV: E=Sophos;i="6.29,78,1794902400"; d="scan'208";a="5322277" Received: from orviesa010.jf.intel.com ([10.64.159.150]) by orvoesa106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Dec 2026 11:17:56 -0800 X-CSE-ConnectionGUID: VPEyYaiBTbmoAo43klFV6Q== X-CSE-MsgGUID: wkJBRdvpQZCNje53C24nyw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.29,78,1794902400"; d="scan'208";a="5629090" Received: from fmsmsx901.amr.corp.intel.com ([10.18.126.90]) by orviesa010.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Dec 2026 11:17:55 -0800 Received: from FMSMSX902.amr.corp.intel.com (10.18.126.91) 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 11:17:54 -0800 Received: from fmsedg901.ED.cps.intel.com (10.1.192.143) by FMSMSX902.amr.corp.intel.com (10.18.126.91) 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 11:17:54 -0800 Received: from BL2PR02CU003.outbound.protection.outlook.com (52.101.52.53) by edgegateway.intel.com (192.55.55.81) 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 11:17:53 -0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=Lm5+UpspFdaMiGhwKF6J40JQdGNbzcKgSxMAkac5FDzSsBC+MQEInKHh1REQfxrPue2/hP7B1DYn+LrcMN7cMQbFg+4qVZdVMckJ3jhwFe5b1Eb9lIESj31xJ930jte15olQl0e1UVLFkdJI6QUnvLxtVP66QqGEdOVLK/hpftGyfooAYDmk25d715o1Y4zLpiH519q78AFQBXv4CI5vuwLTlcL4gUjBgy04qGWxeTQsG+2gW+pNI3MGr42XKgVSAaBuVhWBuyi29XJBBQlh9Q6xTpo1XK6zTW3K9PLuMXig0SdsT5lvPiWQ2VgUkmvlB+XZsxt8/n1DV0+CpgujNQ== 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=3gcCBqrtSsR7fSr5b/r40rhUBz/A21eJlSML7VZ6dlw=; b=mUwqXuh4O2/PdzfwqJTKZR8X2viHMofj3GVX4NnqLoyu2RaNdXFybEFKWRphgp36Feh4zkPEXkNRKU1VjfPjVLa05ED1Z76wIDoQtKEYl6l/IZt+eetW4BUrfQQmUCkhOjbG9J+Pxiu6J5qX0jryuEA8jEY7OLEL4UgIGF0pboFB2qwFRUJGnE7LhVYSt8vrO4QFTksdMURL6kPqLe1SMbDcBYzF51d3a7439BmmqdYsrmBzDe+KQbaR0IbtVd9Q21V+QgExumbcaGYGU1jazEODPJIoUU3Fk3thpVP8IkC/i1lWwUneqNWZWVlsSH/rphksf5woP+gpPQWXxpGcdQ== 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 CO1PR11MB4787.namprd11.prod.outlook.com (2603:10b6:303:95::23) by SA3PR11MB892134.namprd11.prod.outlook.com (2603:10b6:806:52e::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.688.12; Fri, 4 Dec 2026 19:17:51 +0000 Received: from CO1PR11MB4787.namprd11.prod.outlook.com ([fe80::e7eb:a872:53d1:21fd]) by CO1PR11MB4787.namprd11.prod.outlook.com ([fe80::e7eb:a872:53d1:21fd%4]) with mapi id 15.21.0688.011; Fri, 4 Dec 2026 19:17:51 +0000 Date: Fri, 4 Dec 2026 11:17:48 -0800 From: Matthew Brost To: Natalie Vock CC: , , Carlos Santa , Ryan Neph , "Arun Pravin" , Matthew Auld , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter , , Thomas =?iso-8859-1?Q?Hellstr=F6m?= , "Niranjana Vishwanathapura" Subject: Re: [PATCH v6 05/35] drm/ttm: Support defragmentation moves Message-ID: References: <20261203202352.4153159-1-matthew.brost@intel.com> <20261203202352.4153159-6-matthew.brost@intel.com> Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-ClientProxiedBy: MW4PR03CA0248.namprd03.prod.outlook.com (2603:10b6:303:b4::13) To CO1PR11MB4787.namprd11.prod.outlook.com (2603:10b6:303:95::23) X-Archived-By: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1PR11MB4787:EE_|SA3PR11MB892134:EE_ X-MS-Office365-Filtering-Correlation-Id: e5287682-accc-492d-dbed-08df523b4a08 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|7416014|23010399003|366016|376014|1800799024|56012099006|10067099003|261009223327099003|261021135927099003|261021123027099003|261026121527099003|261022075727099003|261022105027099003|261026140227099003|261021121427099003|261021133027099003|261028144727099003|261022115727099003|261009223027099003|261022113327099003|261026135527099003|261009221227099003|261009220027099003|261102123227099003|261026130827099003|261009221727099003|261021130227099003|18002099003|22082099003|5023799004|11063799006|4143699003|6133799003|3023799007; X-Microsoft-Antispam-Message-Info: UpS+iuNK4x49tg1T0x9iEMaEG6SRSIa5I/asvSOYov06sb6ARZKoFXp7ND70Yr50oaf8BwNthABRfB/eJbhSBV5agcECUtE3xX1nXCkpgoTtx45BnusZlVNLSM1VbvwyYnvCl248yldcjA9ujjtShD3PC8oUU1VJFk1ylIlarUY67jD23rcxfD51W8tSoyWpq8O98uY/1hmHMCoUFbV8iuSK60RAb2WwmtTe1u6r582KX+L2CDQHmv6pWQSuO+DH3fMBw71S7K8oDXBTOYy9n8oVIFS5nptKXNd6Qi6G7KrL+6vEjIql5QLo2U7FEzxP+BmJvRMCHGQtsFKzoN6Ho6XtSgLtxHF8EzDzFa+H6r8hJJsDa+e8Tyryh/yygfZWUH4+V9MffBViB/4ReJ33OatI0Xrrop8fmmJusTSZduKKuzM2yiuuEjf5HacN84wa+EQyWPi7bqsUlorIqRk1TU5wyp87Cr31SOVLshp0rgqAy+x7aLr8rySOBi540eJA55Z7vlFG/Ur4Wy13jDLSEgn2LfTvA4rKh2/aUZelh82bhKXH1a8be/a3kNhAONIlOqC/8ttjFcLwudAlSaycOihhuGHlN5p93x/iA/aKrzYI+GaQOTE2FmpQOt5CWuS3bKwimnH2wMUWezjBZr1ZB+Ht7yg9kL8o3nNTFHMzJPw= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:CO1PR11MB4787.namprd11.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230040)(7416014)(23010399003)(366016)(376014)(1800799024)(56012099006)(10067099003)(261009223327099003)(261021135927099003)(261021123027099003)(261026121527099003)(261022075727099003)(261022105027099003)(261026140227099003)(261021121427099003)(261021133027099003)(261028144727099003)(261022115727099003)(261009223027099003)(261022113327099003)(261026135527099003)(261009221227099003)(261009220027099003)(261102123227099003)(261026130827099003)(261009221727099003)(261021130227099003)(18002099003)(22082099003)(5023799004)(11063799006)(4143699003)(6133799003)(3023799007); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?iso-8859-1?Q?5tWCYQ1Pcb2IDtkkKk11Rr1Uy17PRFiuUo+5ZMMFcnILbcGriYUNlBBXAo?= =?iso-8859-1?Q?qDdd55B/ljEi48VQ8BZUi9d7/NKwuN6VPY741XoVwekSfo9xGdm8GCkDsH?= =?iso-8859-1?Q?KUBNVSIQCePOu3vornLLDEkt7EndZMJjZmxf6s86EbRS0XNmttBhTV1G8Z?= =?iso-8859-1?Q?oUPbFJzge3eEkzDXpezfTOT2VDkfOMPrOnHTDV7XzBgS9hrSYM+MyP2LDZ?= =?iso-8859-1?Q?PuVLX7isQYojbvJ77id7Qu3rU+hCd8k4eorZ2xJIcJkmvVerFqIiMfHThu?= =?iso-8859-1?Q?eQfZtizkYM6bjV9zv6Qnt25ItbRCatZCTR8KGwnmDaLWPqB7uUHOsMd31c?= =?iso-8859-1?Q?2R3IY4YqAgnp5VFa3o9t57BCGuYy3ERmssrlf+JTyYLTi0siD7/bMgpbyO?= =?iso-8859-1?Q?yp0HXN3gjrWrlR2i9zc6XdcHYxhso2f/YT2/zKJySSYFsRev8A255OHUAc?= =?iso-8859-1?Q?8ArMhgRnHvXGstq56OJJZ/NiIBhB5sonOm3+C+jhDeEsru6P8y3oAbbGTH?= =?iso-8859-1?Q?lYVKqB6CHDQmWGEMD8LobUFCkJg6s2lBqJiOAZBM652juATjxhjfac5rTu?= =?iso-8859-1?Q?CHH902txhzOx192EBm0CMzgGu/OxQJip0gXFjWkFumfhmqINl7NIohKzR5?= =?iso-8859-1?Q?B0dls4/wlOc0DvmRkdh0TMyXlOeSrnilJqN/cILyPTN7cxEwuRTJX1hXfF?= =?iso-8859-1?Q?+QSFfngTWnWjrmN5uBMIKlPppFcihYleYGXeKqEedP6WRsprouePUuDGRq?= =?iso-8859-1?Q?SyHulL1tWdWAeflEdPdcv6pGhgGqqUG5FtW7O6ydd840fAXfFiR0piOCcF?= =?iso-8859-1?Q?X4WnlpHNogPpxhjBIq7jHSC9XXzBKj4g6ehQaEQG98tjEOp2mhPCEgb1BN?= =?iso-8859-1?Q?+nh5CMA1nx3skkiElgxDt+XVT/3O/tvKoiLZu9HwU0Ndvuj+2jRtGOEqV/?= =?iso-8859-1?Q?Rv54K556x8xhwxBZfn7BpaFok2MW+YklG3b4kdei1j4GYiwqer4FPRAO5L?= =?iso-8859-1?Q?VzoEX5cgp1KpZMKYucbLHJ3XzxjVpupiF5SxAiRZb+pgHCrUQAeOI+eFhk?= =?iso-8859-1?Q?K22SKBIezAb5lLJI7Izuqv8vl9plDD1cGykKPU3ln4RyR+E1LEACMLvIxN?= =?iso-8859-1?Q?x7PtzKcVJmymINAgWr6uHbXxXWtv8NhGFgzyrbPWsJTSrTru6Su20DvHoi?= =?iso-8859-1?Q?A0LxKxvLFVmR325AxBQn/+J1b7gwDpl5CBBo3sqYzJe27UeFAhRbOp1uQa?= =?iso-8859-1?Q?+P+XzS91OznreKKwGec4MS+KE+4n36HqFm0eXYqBdQWmm/vBOyBRi632Ro?= =?iso-8859-1?Q?cka+D55YJbGvq+z8l6uswdSj6w3sK69E58NPmf77tB60rYLuziir9D8qrn?= =?iso-8859-1?Q?WbX/Kn4Z78yvf7zwxf7TxYLipcbqbefUYjHuCr4YhsoDRJX3ZVBk0J0T46?= =?iso-8859-1?Q?L+zABsXWBJJpucZUPOyeRRWT6WjE4D0WLQEs20qzCcLoKlwul/3V1QyhEp?= =?iso-8859-1?Q?ThNXhhA8cPJOWXF9ti/pmlnqdDhzlmTov52QZNg7UnqzcNpW3a8jaOyqrd?= =?iso-8859-1?Q?LGssAji8fOwMiOp4phDGqp/Z5gOygtynhhMkV3OZxMHElQz/vPTPKK0Ul7?= =?iso-8859-1?Q?S4Lom//XkkBC1hmiNSTeeCxAF7JuzabN2KuoReCajDQsFzjeG22p7/1xnH?= =?iso-8859-1?Q?qWPqEdmCn6O4/27BNfM4qiHhuFLfJRkhrIQgeIHyl2Mj8/dlPaUmQ9d9qL?= =?iso-8859-1?Q?5dgxW6JR993k3W6IoaxXl9LlkD3KXsnZH4s3FPyq6S5YPUI4rG4bAv34pd?= =?iso-8859-1?Q?dePT/18prw=3D=3D?= X-Exchange-RoutingPolicyChecked: CD39j/jTVnh/f81jVhL7fvbzq09WZb7VgYC9sonaFH+Mio5qe5NMhpSmDWEQ5ewx4CVVnojyEs+fmSGPINOxHgpygEVj9c57+miD5VSZiRFQ9854msHu+DacgyHrCpEvv60l3qmXZyBziMPWYxS5ybj1OVrHtLdb4mvGP7e78+lWR0al4KXY/9uyonTEMjWCzHOTmwSx9Y+rog28OCBSFYDdwnA5sOlpS3D5jtSmRWdmoONBJWJs9yQk7ydI2Z6pKJCDhrtO2CDFTdKb/ouAUfFaYULFJonprxkdfR/7nkA/eI+waRZ+UqSQQRKJVGMrd/QiLqTFjgImg3B9T15Mgg== X-MS-Exchange-CrossTenant-Network-Message-Id: e5287682-accc-492d-dbed-08df523b4a08 X-MS-Exchange-CrossTenant-AuthSource: CO1PR11MB4787.namprd11.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 Dec 2026 19:17:51.0325 (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: J/0sI3fwaf+7u4bZgZxHgcGlAHc1Vg/6h5RDuUtI1L5hMT6uoeI8lDKgKtb0SaqBrssYRLfsfGohlqvGkB426A== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA3PR11MB892134 X-OriginatorOrg: intel.com X-BeenThere: intel-xe@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Intel Xe graphics driver List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "Intel-xe" X-Spam-Status: No, score=-16.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS autolearn=unavailable autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 On Fri, Dec 04, 2026 at 03:04:35PM +0100, Natalie Vock wrote: > On Thursday, 3 December 2026 21:23 Matthew Brost wrote: > > Add infrastructure to let a driver re-back an already-resident BO with > > fresh pages (e.g. at a more beneficial order) by reusing the normal move > > machinery. > > > > Add ctx->defrag, which makes ttm_bo_validate() skip the "current > > resource already compatible" short-circuit so a move is forced even when > > placement is unchanged. In ttm_bo_handle_move_mem(), when defragging a > > populated BO, stash the old tt, create and populate a fresh one > > (allocating new backing), and hand the old tt to the driver move > > callback via bo->defrag_old_tt for copying. On success the old tt is > > unpopulated and freed; if populate or the move fails, the freshly > > allocated backing is discharged and the BO is restored to its original > > tt and placement. > > > > The old tt is handed off through bo->defrag_old_tt (reservation-lock > > protected) rather than the operation context. The pointer describes BO > > state for the duration of the move, so keeping it on the buffer object > > is better layering and lets ttm_bo_move_accel_cleanup() pick it up. > > > > Because a defrag move commonly fails (beneficial-order pages are often > > unavailable) and then leaves the BO untouched on its original backing, > > defer ttm_bo_unmap_virtual() for the defrag case until the new backing > > has been populated and the move is about to commit. Ordinary moves still > > unmap up front. This avoids needlessly zapping and refaulting every > > userspace mapping on the common failure path where nothing moved. > > > > Teach ttm_bo_move_accel_cleanup()'s ghost path about defrag: instead of > > tearing the move's source tt down synchronously, hand bo->defrag_old_tt > > to the ghost object so it is kept populated until the copy fence signals > > and then unbound and destroyed asynchronously, while the BO keeps its > > new tt. This lets a driver pipeline defrag teardown behind the GPU copy > > rather than blocking on it. > > > > This is driver-opt-in and a no-op unless ctx->defrag is set. > > > > v4: > > - Single unmap (Niranjana) > > - Warn on defrag_old_tt in TTM memcpy (Niranjana) > > - comment around -ENOMEM in ttm_pool_iter_lower_order (Niranjana) > > - warn on defrag in ttm_bo_bounce_temp_buffer (Niranjana) > > > > 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 > > Reviewed-by: Niranjana Vishwanathapura > > --- > > drivers/gpu/drm/ttm/ttm_bo.c | 90 ++++++++++++++++++++++++++++--- > > drivers/gpu/drm/ttm/ttm_bo_util.c | 18 +++++-- > > drivers/gpu/drm/ttm/ttm_pool.c | 10 +++- > > include/drm/ttm/ttm_bo.h | 17 ++++++ > > 4 files changed, 123 insertions(+), 12 deletions(-) > > > > diff --git a/drivers/gpu/drm/ttm/ttm_bo.c b/drivers/gpu/drm/ttm/ttm_bo.c > > index 53c68e29ebc2..a32b78c80989 100644 > > --- a/drivers/gpu/drm/ttm/ttm_bo.c > > +++ b/drivers/gpu/drm/ttm/ttm_bo.c > > @@ -125,49 +125,119 @@ static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo, > > { > > struct ttm_device *bdev = bo->bdev; > > bool old_use_tt, new_use_tt; > > + bool defrag; > > int ret; > > > > old_use_tt = !bo->resource || ttm_manager_type(bdev, bo->resource->mem_type)->use_tt; > > new_use_tt = ttm_manager_type(bdev, mem->mem_type)->use_tt; > > > > - ttm_bo_unmap_virtual(bo); > > + /* > > + * Only the BO that is the actual defrag target (moved in place via > > + * ttm_bo_validate(), evict == false) is being defragmented. The same > > + * ctx->defrag is threaded into evictions triggered while allocating > > + * that target's new backing (ttm_bo_evict_alloc()), so guard against > > + * an unrelated victim BO (evict == true) being misclassified as a > > + * defrag move and needlessly reallocating its backing. > > + */ > > + defrag = ctx->defrag && !evict && new_use_tt && bo->ttm && > > + ttm_tt_is_populated(bo->ttm); > > If ctx->defrag && !evict, any other part of this condition failing is > invalid and means a driver bug, doesn't it? > > defrag = ctx->defrag && !evict; > > if (defrag && WARN_ON(!new_use_tt || !bo->ttm || !ttm_tt_is_populated(bo->ttm)) > return -EINVAL; > > (or WARN_ON_ONCE, see below) > Yes, this is corrcet. Let me adjust and s/WARN_ON/WARN_ON_ONCE. > > > > /* > > * Create and bind a ttm if required. > > */ > > > > if (new_use_tt) { > > + struct ttm_tt *defrag_old_tt = NULL; > > + > > + /* > > + * For a defrag move, stash the current populated tt and force a > > + * fresh one to be created and populated (at beneficial order). > > + * The old tt is handed to the driver move callback to copy the > > + * contents from, and freed once the move completes. > > + */ > > + if (defrag) { > > + defrag_old_tt = bo->ttm; > > + bo->ttm = NULL; > > + } > > + > > /* Zero init the new TTM structure if the old location should > > * have used one as well. > > */ > > - ret = ttm_tt_create(bo, old_use_tt); > > - if (ret) > > + ret = ttm_tt_create(bo, old_use_tt && !defrag_old_tt); > > + if (ret) { > > + if (defrag_old_tt) > > + bo->ttm = defrag_old_tt; > > goto out_err; > > + } > > > > - if (mem->mem_type != TTM_PL_SYSTEM) { > > + if (defrag_old_tt || mem->mem_type != TTM_PL_SYSTEM) { > > ret = ttm_bo_populate(bo, ctx); > > - if (ret) > > + if (ret) { > > + /* > > + * Discharge the freshly allocated backing and > > + * leave the BO on its original tt/placement. > > + */ > > + if (defrag_old_tt) { > > + ttm_tt_destroy(bo->bdev, bo->ttm); > > + bo->ttm = defrag_old_tt; > > + } > > goto out_err; > > + } > > } > > + > > + bo->defrag_old_tt = defrag_old_tt; > > } > > > > ret = dma_resv_reserve_fences(bo->base.resv, 1); > > if (ret) > > goto out_err; > > > > + /* > > + * Only tear down mappings after populate succeeds, as population can > > + * commonly fail during defragmentation moves. > > + */ > > + ttm_bo_unmap_virtual(bo); > > + > > ret = bdev->funcs->move(bo, evict, ctx, mem, hop); > > if (ret) { > > - if (ret == -EMULTIHOP) > > + if (ret == -EMULTIHOP) { > > + /* > > + * A defrag move targets the BO's current placement and > > + * must complete in a single hop. A driver requesting a > > + * bounce here is unsupported: restore the original > > + * backing and fail the defrag rather than retrying the > > + * multihop with ctx->defrag still set, which would > > + * misclassify the bounce as another defrag move and > > + * overwrite/leak bo->defrag_old_tt. > > + */ > > + if (unlikely(defrag)) { > > + WARN_ON_ONCE(1); > > Here you use WARN_ON_ONCE (which, btw, could probably be folded into the if like so:) > > if (WARN_ON_ONCE(defrag)) > > which should imply unlikely() Yes, this is better. Will change. > > > + ret = -EINVAL; > > + goto out_err; > > + } > > return ret; > > + } > > goto out_err; > > } > > > > + if (bo->defrag_old_tt) { > > + ttm_tt_unpopulate(bo->bdev, bo->defrag_old_tt); > > + ttm_tt_destroy(bo->bdev, bo->defrag_old_tt); > > + bo->defrag_old_tt = NULL; > > + } > > + > > ctx->bytes_moved += bo->base.size; > > return 0; > > > > out_err: > > - if (!old_use_tt) > > + if (bo->defrag_old_tt) { > > + /* Failed defrag move: restore the original backing. */ > > ttm_bo_tt_destroy(bo); > > + bo->ttm = bo->defrag_old_tt; > > + bo->defrag_old_tt = NULL; > > + } else if (!old_use_tt) { > > + ttm_bo_tt_destroy(bo); > > + } > > > > return ret; > > } > > @@ -339,6 +409,10 @@ static int ttm_bo_bounce_temp_buffer(struct ttm_buffer_object *bo, > > struct ttm_resource *hop_mem; > > int ret; > > > > + /* We shouldn't be bouncing temp buffers during a defrag */ > > + if (WARN_ON(ctx->defrag)) > > + return -EINVAL; > > but here you use WARN_ON without ONCE. Any reason for that mismatch? > I get not wanting to spam the journal on every move if something in the > driver is busted, but then we should be consistent. > Let's do a WARN_ON_ONCE here too. > > + > > hop_placement.num_placement = 1; > > hop_placement.placement = hop; > > > > @@ -1090,7 +1164,7 @@ int ttm_bo_validate(struct ttm_buffer_object *bo, > > force_space = false; > > do { > > /* Check whether we need to move buffer. */ > > - if (bo->resource && > > + if (bo->resource && !ctx->defrag && > > ttm_resource_compatible(bo->resource, placement, > > force_space)) > > return 0; > > diff --git a/drivers/gpu/drm/ttm/ttm_bo_util.c b/drivers/gpu/drm/ttm/ttm_bo_util.c > > index 8c5de3a0506f..21a25048d64d 100644 > > --- a/drivers/gpu/drm/ttm/ttm_bo_util.c > > +++ b/drivers/gpu/drm/ttm/ttm_bo_util.c > > @@ -161,7 +161,7 @@ int ttm_bo_move_memcpy(struct ttm_buffer_object *bo, > > bool clear; > > int ret = 0; > > > > - if (WARN_ON(!src_mem)) > > + if (WARN_ON(!src_mem) || WARN_ON(bo->defrag_old_tt)) > > return -EINVAL; > > > > src_man = ttm_manager_type(bdev, src_mem->mem_type); > > @@ -653,10 +653,22 @@ static int ttm_bo_move_to_ghost(struct ttm_buffer_object *bo, > > * bo to be unbound and destroyed. > > */ > > > > - if (dst_use_tt) > > + if (bo->defrag_old_tt) { > > + /* > > + * Defrag move: @bo already points at the freshly allocated, > > + * beneficial-order tt that the move copied into. Hand the old > > + * tt (the move's source) to the ghost so it is kept populated > > + * until @fence signals and then unbound and destroyed, instead > > + * of being torn down synchronously. @bo keeps its new tt. > > + */ > > + ghost_obj->ttm = bo->defrag_old_tt; > > + ghost_obj->defrag_old_tt = NULL; > > ghost_obj->defrag_old_tt needs to be in ttm_bo_transfer. > (I really need to get around to the per-member-copy TODO there, I don't > like this footgun at all) > See below, I can drop bo->defrag_old_tt. The fallout will be slightly different way to pipeline the destroy of the old TT - we will need 2nd ghost object to hold onto the old TT or a new helper. More below. > > + bo->defrag_old_tt = NULL; > > + } else if (dst_use_tt) { > > ghost_obj->ttm = NULL; > > - else > > + } else { > > bo->ttm = NULL; > > + } > > > > dma_resv_unlock(&ghost_obj->base._resv); > > ttm_bo_put(ghost_obj); > > diff --git a/drivers/gpu/drm/ttm/ttm_pool.c b/drivers/gpu/drm/ttm/ttm_pool.c > > index a053d78191f6..182e84efcd92 100644 > > --- a/drivers/gpu/drm/ttm/ttm_pool.c > > +++ b/drivers/gpu/drm/ttm/ttm_pool.c > > @@ -875,9 +875,17 @@ static int ttm_pool_iter_lower_order(struct ttm_pool_alloc_iter *it) > > * pages. Record it so the driver can later try to defragment the object > > * back to beneficial order. > > */ > > - if (it->beneficial_order && it->order == it->beneficial_order) > > + if (it->beneficial_order && it->order == it->beneficial_order) { > > it->tt->page_flags |= TTM_TT_FLAG_BENEFICIAL_ORDER_FAILED; > > > > + /* > > + * If we are defragmenting, we only want beneficial_order, so > > + * any failure is treated as -ENOMEM. > > + */ > > + if (it->ctx->defrag) > > + return -ENOMEM; > > + } > > + > > it->order--; > > it->page_caching = it->tt->caching; > > it->allow_pools = true; > > diff --git a/include/drm/ttm/ttm_bo.h b/include/drm/ttm/ttm_bo.h > > index cd3d3980dbce..c5205b8e8f10 100644 > > --- a/include/drm/ttm/ttm_bo.h > > +++ b/include/drm/ttm/ttm_bo.h > > @@ -135,6 +135,16 @@ struct ttm_buffer_object { > > * reservation lock. > > */ > > struct sg_table *sg; > > + > > + /** > > + * @defrag_old_tt: Used internally during a defrag move to hand the BO's > > + * previous, populated struct ttm_tt to the driver's move callback so > > + * that it can copy the contents into the freshly allocated > > + * beneficial-order backing. Set by TTM, consumed by the driver; the old > > + * tt is freed (or restored on failure) by TTM once the move completes. > > + * Protected by the reservation lock. > > + */ > > + struct ttm_tt *defrag_old_tt; > > FWIW, I don't exactly love stashing temporary state in BO members like > this. > Me either. I already have defrag_old_tt in the context, so it doesn't need to be stashed in the BO for the ->move() vfunc. It actually needed for ttm_bo_move_accel_cleanup moving the old TT to ghost. Any objections to converting the code to use ctx->defrag_old_tt everywhere and dropping bo->defrag_old_tt entirely. Then we have two options that I see: 1. A new varient of ttm_bo_move_accel_cleanup used when ctx->defrag_old_tt is set called driver side in ->move() 2. ttm_bo_handle_move_mem creates 2nd ghost object owning delayed destroy of defrag_old_tt after ->move() returns. I'd lean towards options #2. I have coded it doesn't look to bad. Matt > We already have the "evict" being passed around in a similar way as > defrag_old_tt needs to be passed around, i.e. given to the move_notify > callback and then passed to accel_cleanup. > > Maybe we could refactor the move_notify callback to take a some kind of > "notify data" struct that contains these kinds of parameters and then > add defrag_old_tt there? > > Natalie > > > }; > > > > #define TTM_BO_MAP_IOMEM_MASK 0x80 > > @@ -194,6 +204,13 @@ struct ttm_operation_ctx { > > * reclaim/compaction before falling back to a smaller order anyway. > > */ > > bool beneficial_reclaim_backoff; > > + /** > > + * @defrag: Defragmentation pass. Attempt a move even when the bo's > > + * current resource is already compatible with the requested placement, > > + * so that the backing store can be reallocated at the pool's beneficial > > + * order. > > + */ > > + bool defrag; > > /** > > * @resv: Reservation object to be used together with > > * @allow_res_evict. > > > > > >