1c6603c74SDaniel Vetter // SPDX-License-Identifier: GPL-2.0 2c6603c74SDaniel Vetter /* 3c6603c74SDaniel Vetter * Copyright (C) 2020 Intel 4c6603c74SDaniel Vetter * 5c6603c74SDaniel Vetter * Based on drivers/base/devres.c 6c6603c74SDaniel Vetter */ 7c6603c74SDaniel Vetter 8c6603c74SDaniel Vetter #include <drm/drm_managed.h> 9c6603c74SDaniel Vetter 10c6603c74SDaniel Vetter #include <linux/list.h> 11c6603c74SDaniel Vetter #include <linux/slab.h> 12c6603c74SDaniel Vetter #include <linux/spinlock.h> 13c6603c74SDaniel Vetter 14c6603c74SDaniel Vetter #include <drm/drm_device.h> 15c6603c74SDaniel Vetter #include <drm/drm_print.h> 16c6603c74SDaniel Vetter 17*3df6fad4SChris Wilson #include "drm_internal.h" 18*3df6fad4SChris Wilson 19c6603c74SDaniel Vetter /** 20c6603c74SDaniel Vetter * DOC: managed resources 21c6603c74SDaniel Vetter * 22c6603c74SDaniel Vetter * Inspired by struct &device managed resources, but tied to the lifetime of 23c6603c74SDaniel Vetter * struct &drm_device, which can outlive the underlying physical device, usually 24c6603c74SDaniel Vetter * when userspace has some open files and other handles to resources still open. 259e1ed9fbSDaniel Vetter * 269e1ed9fbSDaniel Vetter * Release actions can be added with drmm_add_action(), memory allocations can 279e1ed9fbSDaniel Vetter * be done directly with drmm_kmalloc() and the related functions. Everything 289e1ed9fbSDaniel Vetter * will be released on the final drm_dev_put() in reverse order of how the 299e1ed9fbSDaniel Vetter * release actions have been added and memory has been allocated since driver 309e1ed9fbSDaniel Vetter * loading started with drm_dev_init(). 319e1ed9fbSDaniel Vetter * 329e1ed9fbSDaniel Vetter * Note that release actions and managed memory can also be added and removed 339e1ed9fbSDaniel Vetter * during the lifetime of the driver, all the functions are fully concurrent 349e1ed9fbSDaniel Vetter * safe. But it is recommended to use managed resources only for resources that 359e1ed9fbSDaniel Vetter * change rarely, if ever, during the lifetime of the &drm_device instance. 36c6603c74SDaniel Vetter */ 379e1ed9fbSDaniel Vetter 38c6603c74SDaniel Vetter struct drmres_node { 39c6603c74SDaniel Vetter struct list_head entry; 40c6603c74SDaniel Vetter drmres_release_t release; 41c6603c74SDaniel Vetter const char *name; 42c6603c74SDaniel Vetter size_t size; 43c6603c74SDaniel Vetter }; 44c6603c74SDaniel Vetter 45c6603c74SDaniel Vetter struct drmres { 46c6603c74SDaniel Vetter struct drmres_node node; 47c6603c74SDaniel Vetter /* 48c6603c74SDaniel Vetter * Some archs want to perform DMA into kmalloc caches 49c6603c74SDaniel Vetter * and need a guaranteed alignment larger than 50c6603c74SDaniel Vetter * the alignment of a 64-bit integer. 51c6603c74SDaniel Vetter * Thus we use ARCH_KMALLOC_MINALIGN here and get exactly the same 52c6603c74SDaniel Vetter * buffer alignment as if it was allocated by plain kmalloc(). 53c6603c74SDaniel Vetter */ 54c6603c74SDaniel Vetter u8 __aligned(ARCH_KMALLOC_MINALIGN) data[]; 55c6603c74SDaniel Vetter }; 56c6603c74SDaniel Vetter 57c6603c74SDaniel Vetter static void free_dr(struct drmres *dr) 58c6603c74SDaniel Vetter { 59c6603c74SDaniel Vetter kfree_const(dr->node.name); 60c6603c74SDaniel Vetter kfree(dr); 61c6603c74SDaniel Vetter } 62c6603c74SDaniel Vetter 63c6603c74SDaniel Vetter void drm_managed_release(struct drm_device *dev) 64c6603c74SDaniel Vetter { 65c6603c74SDaniel Vetter struct drmres *dr, *tmp; 66c6603c74SDaniel Vetter 67c6603c74SDaniel Vetter drm_dbg_drmres(dev, "drmres release begin\n"); 68c6603c74SDaniel Vetter list_for_each_entry_safe(dr, tmp, &dev->managed.resources, node.entry) { 69c6603c74SDaniel Vetter drm_dbg_drmres(dev, "REL %p %s (%zu bytes)\n", 70c6603c74SDaniel Vetter dr, dr->node.name, dr->node.size); 71c6603c74SDaniel Vetter 72c6603c74SDaniel Vetter if (dr->node.release) 73c6603c74SDaniel Vetter dr->node.release(dev, dr->node.size ? *(void **)&dr->data : NULL); 74c6603c74SDaniel Vetter 75c6603c74SDaniel Vetter list_del(&dr->node.entry); 76c6603c74SDaniel Vetter free_dr(dr); 77c6603c74SDaniel Vetter } 78c6603c74SDaniel Vetter drm_dbg_drmres(dev, "drmres release end\n"); 79c6603c74SDaniel Vetter } 80c6603c74SDaniel Vetter 81c6603c74SDaniel Vetter /* 82c6603c74SDaniel Vetter * Always inline so that kmalloc_track_caller tracks the actual interesting 83c6603c74SDaniel Vetter * caller outside of drm_managed.c. 84c6603c74SDaniel Vetter */ 85c6603c74SDaniel Vetter static __always_inline struct drmres * alloc_dr(drmres_release_t release, 86c6603c74SDaniel Vetter size_t size, gfp_t gfp, int nid) 87c6603c74SDaniel Vetter { 88c6603c74SDaniel Vetter size_t tot_size; 89c6603c74SDaniel Vetter struct drmres *dr; 90c6603c74SDaniel Vetter 91c6603c74SDaniel Vetter /* We must catch any near-SIZE_MAX cases that could overflow. */ 92c6603c74SDaniel Vetter if (unlikely(check_add_overflow(sizeof(*dr), size, &tot_size))) 93c6603c74SDaniel Vetter return NULL; 94c6603c74SDaniel Vetter 95c6603c74SDaniel Vetter dr = kmalloc_node_track_caller(tot_size, gfp, nid); 96c6603c74SDaniel Vetter if (unlikely(!dr)) 97c6603c74SDaniel Vetter return NULL; 98c6603c74SDaniel Vetter 99c6603c74SDaniel Vetter memset(dr, 0, offsetof(struct drmres, data)); 100c6603c74SDaniel Vetter 101c6603c74SDaniel Vetter INIT_LIST_HEAD(&dr->node.entry); 102c6603c74SDaniel Vetter dr->node.release = release; 103c6603c74SDaniel Vetter dr->node.size = size; 104c6603c74SDaniel Vetter 105c6603c74SDaniel Vetter return dr; 106c6603c74SDaniel Vetter } 107c6603c74SDaniel Vetter 108c6603c74SDaniel Vetter static void del_dr(struct drm_device *dev, struct drmres *dr) 109c6603c74SDaniel Vetter { 110c6603c74SDaniel Vetter list_del_init(&dr->node.entry); 111c6603c74SDaniel Vetter 112c6603c74SDaniel Vetter drm_dbg_drmres(dev, "DEL %p %s (%lu bytes)\n", 113c6603c74SDaniel Vetter dr, dr->node.name, (unsigned long) dr->node.size); 114c6603c74SDaniel Vetter } 115c6603c74SDaniel Vetter 116c6603c74SDaniel Vetter static void add_dr(struct drm_device *dev, struct drmres *dr) 117c6603c74SDaniel Vetter { 118c6603c74SDaniel Vetter unsigned long flags; 119c6603c74SDaniel Vetter 120c6603c74SDaniel Vetter spin_lock_irqsave(&dev->managed.lock, flags); 121c6603c74SDaniel Vetter list_add(&dr->node.entry, &dev->managed.resources); 122c6603c74SDaniel Vetter spin_unlock_irqrestore(&dev->managed.lock, flags); 123c6603c74SDaniel Vetter 124c6603c74SDaniel Vetter drm_dbg_drmres(dev, "ADD %p %s (%lu bytes)\n", 125c6603c74SDaniel Vetter dr, dr->node.name, (unsigned long) dr->node.size); 126c6603c74SDaniel Vetter } 127c6603c74SDaniel Vetter 1289e1ed9fbSDaniel Vetter /** 1299e1ed9fbSDaniel Vetter * drmm_add_final_kfree - add release action for the final kfree() 1309e1ed9fbSDaniel Vetter * @dev: DRM device 1319e1ed9fbSDaniel Vetter * @container: pointer to the kmalloc allocation containing @dev 1329e1ed9fbSDaniel Vetter * 1339e1ed9fbSDaniel Vetter * Since the allocation containing the struct &drm_device must be allocated 1349e1ed9fbSDaniel Vetter * before it can be initialized with drm_dev_init() there's no way to allocate 1359e1ed9fbSDaniel Vetter * that memory with drmm_kmalloc(). To side-step this chicken-egg problem the 1369e1ed9fbSDaniel Vetter * pointer for this final kfree() must be specified by calling this function. It 1379e1ed9fbSDaniel Vetter * will be released in the final drm_dev_put() for @dev, after all other release 1389e1ed9fbSDaniel Vetter * actions installed through drmm_add_action() have been processed. 1399e1ed9fbSDaniel Vetter */ 140c6603c74SDaniel Vetter void drmm_add_final_kfree(struct drm_device *dev, void *container) 141c6603c74SDaniel Vetter { 142c6603c74SDaniel Vetter WARN_ON(dev->managed.final_kfree); 143c6603c74SDaniel Vetter WARN_ON(dev < (struct drm_device *) container); 144c7da606eSDaniel Vetter WARN_ON(dev + 1 > (struct drm_device *) (container + ksize(container))); 145c6603c74SDaniel Vetter dev->managed.final_kfree = container; 146c6603c74SDaniel Vetter } 147c6603c74SDaniel Vetter EXPORT_SYMBOL(drmm_add_final_kfree); 148c6603c74SDaniel Vetter 149c6603c74SDaniel Vetter int __drmm_add_action(struct drm_device *dev, 150c6603c74SDaniel Vetter drmres_release_t action, 151c6603c74SDaniel Vetter void *data, const char *name) 152c6603c74SDaniel Vetter { 153c6603c74SDaniel Vetter struct drmres *dr; 154c6603c74SDaniel Vetter void **void_ptr; 155c6603c74SDaniel Vetter 156c6603c74SDaniel Vetter dr = alloc_dr(action, data ? sizeof(void*) : 0, 157c6603c74SDaniel Vetter GFP_KERNEL | __GFP_ZERO, 158c6603c74SDaniel Vetter dev_to_node(dev->dev)); 159c6603c74SDaniel Vetter if (!dr) { 160c6603c74SDaniel Vetter drm_dbg_drmres(dev, "failed to add action %s for %p\n", 161c6603c74SDaniel Vetter name, data); 162c6603c74SDaniel Vetter return -ENOMEM; 163c6603c74SDaniel Vetter } 164c6603c74SDaniel Vetter 165c6603c74SDaniel Vetter dr->node.name = kstrdup_const(name, GFP_KERNEL); 166c6603c74SDaniel Vetter if (data) { 167c6603c74SDaniel Vetter void_ptr = (void **)&dr->data; 168c6603c74SDaniel Vetter *void_ptr = data; 169c6603c74SDaniel Vetter } 170c6603c74SDaniel Vetter 171c6603c74SDaniel Vetter add_dr(dev, dr); 172c6603c74SDaniel Vetter 173c6603c74SDaniel Vetter return 0; 174c6603c74SDaniel Vetter } 175c6603c74SDaniel Vetter EXPORT_SYMBOL(__drmm_add_action); 176c6603c74SDaniel Vetter 177f96306f9SDaniel Vetter int __drmm_add_action_or_reset(struct drm_device *dev, 178f96306f9SDaniel Vetter drmres_release_t action, 179f96306f9SDaniel Vetter void *data, const char *name) 180f96306f9SDaniel Vetter { 181f96306f9SDaniel Vetter int ret; 182f96306f9SDaniel Vetter 183f96306f9SDaniel Vetter ret = __drmm_add_action(dev, action, data, name); 184f96306f9SDaniel Vetter if (ret) 185f96306f9SDaniel Vetter action(dev, data); 186f96306f9SDaniel Vetter 187f96306f9SDaniel Vetter return ret; 188f96306f9SDaniel Vetter } 189f96306f9SDaniel Vetter EXPORT_SYMBOL(__drmm_add_action_or_reset); 190f96306f9SDaniel Vetter 1919e1ed9fbSDaniel Vetter /** 1929e1ed9fbSDaniel Vetter * drmm_kmalloc - &drm_device managed kmalloc() 1939e1ed9fbSDaniel Vetter * @dev: DRM device 1949e1ed9fbSDaniel Vetter * @size: size of the memory allocation 1959e1ed9fbSDaniel Vetter * @gfp: GFP allocation flags 1969e1ed9fbSDaniel Vetter * 1979e1ed9fbSDaniel Vetter * This is a &drm_device managed version of kmalloc(). The allocated memory is 1989e1ed9fbSDaniel Vetter * automatically freed on the final drm_dev_put(). Memory can also be freed 1999e1ed9fbSDaniel Vetter * before the final drm_dev_put() by calling drmm_kfree(). 2009e1ed9fbSDaniel Vetter */ 201c6603c74SDaniel Vetter void *drmm_kmalloc(struct drm_device *dev, size_t size, gfp_t gfp) 202c6603c74SDaniel Vetter { 203c6603c74SDaniel Vetter struct drmres *dr; 204c6603c74SDaniel Vetter 205c6603c74SDaniel Vetter dr = alloc_dr(NULL, size, gfp, dev_to_node(dev->dev)); 206c6603c74SDaniel Vetter if (!dr) { 207c6603c74SDaniel Vetter drm_dbg_drmres(dev, "failed to allocate %zu bytes, %u flags\n", 208c6603c74SDaniel Vetter size, gfp); 209c6603c74SDaniel Vetter return NULL; 210c6603c74SDaniel Vetter } 211c6603c74SDaniel Vetter dr->node.name = kstrdup_const("kmalloc", GFP_KERNEL); 212c6603c74SDaniel Vetter 213c6603c74SDaniel Vetter add_dr(dev, dr); 214c6603c74SDaniel Vetter 215c6603c74SDaniel Vetter return dr->data; 216c6603c74SDaniel Vetter } 217c6603c74SDaniel Vetter EXPORT_SYMBOL(drmm_kmalloc); 218c6603c74SDaniel Vetter 2199e1ed9fbSDaniel Vetter /** 2209e1ed9fbSDaniel Vetter * drmm_kstrdup - &drm_device managed kstrdup() 2219e1ed9fbSDaniel Vetter * @dev: DRM device 2229e1ed9fbSDaniel Vetter * @s: 0-terminated string to be duplicated 2239e1ed9fbSDaniel Vetter * @gfp: GFP allocation flags 2249e1ed9fbSDaniel Vetter * 2259e1ed9fbSDaniel Vetter * This is a &drm_device managed version of kstrdup(). The allocated memory is 2269e1ed9fbSDaniel Vetter * automatically freed on the final drm_dev_put() and works exactly like a 2279e1ed9fbSDaniel Vetter * memory allocation obtained by drmm_kmalloc(). 2289e1ed9fbSDaniel Vetter */ 229a5c71fdbSDaniel Vetter char *drmm_kstrdup(struct drm_device *dev, const char *s, gfp_t gfp) 230a5c71fdbSDaniel Vetter { 231a5c71fdbSDaniel Vetter size_t size; 232a5c71fdbSDaniel Vetter char *buf; 233a5c71fdbSDaniel Vetter 234a5c71fdbSDaniel Vetter if (!s) 235a5c71fdbSDaniel Vetter return NULL; 236a5c71fdbSDaniel Vetter 237a5c71fdbSDaniel Vetter size = strlen(s) + 1; 238a5c71fdbSDaniel Vetter buf = drmm_kmalloc(dev, size, gfp); 239a5c71fdbSDaniel Vetter if (buf) 240a5c71fdbSDaniel Vetter memcpy(buf, s, size); 241a5c71fdbSDaniel Vetter return buf; 242a5c71fdbSDaniel Vetter } 243a5c71fdbSDaniel Vetter EXPORT_SYMBOL_GPL(drmm_kstrdup); 244a5c71fdbSDaniel Vetter 2459e1ed9fbSDaniel Vetter /** 2469e1ed9fbSDaniel Vetter * drmm_kfree - &drm_device managed kfree() 2479e1ed9fbSDaniel Vetter * @dev: DRM device 2489e1ed9fbSDaniel Vetter * @data: memory allocation to be freed 2499e1ed9fbSDaniel Vetter * 2509e1ed9fbSDaniel Vetter * This is a &drm_device managed version of kfree() which can be used to 2519e1ed9fbSDaniel Vetter * release memory allocated through drmm_kmalloc() or any of its related 2529e1ed9fbSDaniel Vetter * functions before the final drm_dev_put() of @dev. 2539e1ed9fbSDaniel Vetter */ 254c6603c74SDaniel Vetter void drmm_kfree(struct drm_device *dev, void *data) 255c6603c74SDaniel Vetter { 256c6603c74SDaniel Vetter struct drmres *dr_match = NULL, *dr; 257c6603c74SDaniel Vetter unsigned long flags; 258c6603c74SDaniel Vetter 259c6603c74SDaniel Vetter if (!data) 260c6603c74SDaniel Vetter return; 261c6603c74SDaniel Vetter 262c6603c74SDaniel Vetter spin_lock_irqsave(&dev->managed.lock, flags); 263c6603c74SDaniel Vetter list_for_each_entry(dr, &dev->managed.resources, node.entry) { 264c6603c74SDaniel Vetter if (dr->data == data) { 265c6603c74SDaniel Vetter dr_match = dr; 266c6603c74SDaniel Vetter del_dr(dev, dr_match); 267c6603c74SDaniel Vetter break; 268c6603c74SDaniel Vetter } 269c6603c74SDaniel Vetter } 270c6603c74SDaniel Vetter spin_unlock_irqrestore(&dev->managed.lock, flags); 271c6603c74SDaniel Vetter 272c6603c74SDaniel Vetter if (WARN_ON(!dr_match)) 273c6603c74SDaniel Vetter return; 274c6603c74SDaniel Vetter 275c6603c74SDaniel Vetter free_dr(dr_match); 276c6603c74SDaniel Vetter } 277c6603c74SDaniel Vetter EXPORT_SYMBOL(drmm_kfree); 278