LCOV - code coverage report
Current view: top level - drivers/gpu/drm - drm_managed.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 56 83 67.5 %
Date: 2023-03-27 20:00:47 Functions: 7 11 63.6 %

          Line data    Source code
       1             : // SPDX-License-Identifier: GPL-2.0
       2             : /*
       3             :  * Copyright (C) 2020 Intel
       4             :  *
       5             :  * Based on drivers/base/devres.c
       6             :  */
       7             : 
       8             : #include <drm/drm_managed.h>
       9             : 
      10             : #include <linux/list.h>
      11             : #include <linux/mutex.h>
      12             : #include <linux/slab.h>
      13             : #include <linux/spinlock.h>
      14             : 
      15             : #include <drm/drm_device.h>
      16             : #include <drm/drm_print.h>
      17             : 
      18             : #include "drm_internal.h"
      19             : 
      20             : /**
      21             :  * DOC: managed resources
      22             :  *
      23             :  * Inspired by struct &device managed resources, but tied to the lifetime of
      24             :  * struct &drm_device, which can outlive the underlying physical device, usually
      25             :  * when userspace has some open files and other handles to resources still open.
      26             :  *
      27             :  * Release actions can be added with drmm_add_action(), memory allocations can
      28             :  * be done directly with drmm_kmalloc() and the related functions. Everything
      29             :  * will be released on the final drm_dev_put() in reverse order of how the
      30             :  * release actions have been added and memory has been allocated since driver
      31             :  * loading started with devm_drm_dev_alloc().
      32             :  *
      33             :  * Note that release actions and managed memory can also be added and removed
      34             :  * during the lifetime of the driver, all the functions are fully concurrent
      35             :  * safe. But it is recommended to use managed resources only for resources that
      36             :  * change rarely, if ever, during the lifetime of the &drm_device instance.
      37             :  */
      38             : 
      39             : struct drmres_node {
      40             :         struct list_head        entry;
      41             :         drmres_release_t        release;
      42             :         const char              *name;
      43             :         size_t                  size;
      44             : };
      45             : 
      46             : struct drmres {
      47             :         struct drmres_node              node;
      48             :         /*
      49             :          * Some archs want to perform DMA into kmalloc caches
      50             :          * and need a guaranteed alignment larger than
      51             :          * the alignment of a 64-bit integer.
      52             :          * Thus we use ARCH_KMALLOC_MINALIGN here and get exactly the same
      53             :          * buffer alignment as if it was allocated by plain kmalloc().
      54             :          */
      55             :         u8 __aligned(ARCH_KMALLOC_MINALIGN) data[];
      56             : };
      57             : 
      58             : static void free_dr(struct drmres *dr)
      59             : {
      60          98 :         kfree_const(dr->node.name);
      61          98 :         kfree(dr);
      62             : }
      63             : 
      64          17 : void drm_managed_release(struct drm_device *dev)
      65             : {
      66             :         struct drmres *dr, *tmp;
      67             : 
      68          17 :         drm_dbg_drmres(dev, "drmres release begin\n");
      69         115 :         list_for_each_entry_safe(dr, tmp, &dev->managed.resources, node.entry) {
      70          98 :                 drm_dbg_drmres(dev, "REL %p %s (%zu bytes)\n",
      71             :                                dr, dr->node.name, dr->node.size);
      72             : 
      73          98 :                 if (dr->node.release)
      74          64 :                         dr->node.release(dev, dr->node.size ? *(void **)&dr->data : NULL);
      75             : 
      76         196 :                 list_del(&dr->node.entry);
      77          98 :                 free_dr(dr);
      78             :         }
      79          17 :         drm_dbg_drmres(dev, "drmres release end\n");
      80          17 : }
      81             : 
      82             : /*
      83             :  * Always inline so that kmalloc_track_caller tracks the actual interesting
      84             :  * caller outside of drm_managed.c.
      85             :  */
      86             : static __always_inline struct drmres * alloc_dr(drmres_release_t release,
      87             :                                                 size_t size, gfp_t gfp, int nid)
      88             : {
      89             :         size_t tot_size;
      90             :         struct drmres *dr;
      91             : 
      92             :         /* We must catch any near-SIZE_MAX cases that could overflow. */
      93         196 :         if (unlikely(check_add_overflow(sizeof(*dr), size, &tot_size)))
      94             :                 return NULL;
      95             : 
      96          98 :         dr = kmalloc_node_track_caller(tot_size, gfp, nid);
      97          98 :         if (unlikely(!dr))
      98             :                 return NULL;
      99             : 
     100          98 :         memset(dr, 0, offsetof(struct drmres, data));
     101             : 
     102         196 :         INIT_LIST_HEAD(&dr->node.entry);
     103          98 :         dr->node.release = release;
     104          98 :         dr->node.size = size;
     105             : 
     106             :         return dr;
     107             : }
     108             : 
     109           0 : static void del_dr(struct drm_device *dev, struct drmres *dr)
     110             : {
     111           0 :         list_del_init(&dr->node.entry);
     112             : 
     113           0 :         drm_dbg_drmres(dev, "DEL %p %s (%lu bytes)\n",
     114             :                        dr, dr->node.name, (unsigned long) dr->node.size);
     115           0 : }
     116             : 
     117          98 : static void add_dr(struct drm_device *dev, struct drmres *dr)
     118             : {
     119             :         unsigned long flags;
     120             : 
     121          98 :         spin_lock_irqsave(&dev->managed.lock, flags);
     122         196 :         list_add(&dr->node.entry, &dev->managed.resources);
     123         196 :         spin_unlock_irqrestore(&dev->managed.lock, flags);
     124             : 
     125          98 :         drm_dbg_drmres(dev, "ADD %p %s (%lu bytes)\n",
     126             :                        dr, dr->node.name, (unsigned long) dr->node.size);
     127          98 : }
     128             : 
     129          17 : void drmm_add_final_kfree(struct drm_device *dev, void *container)
     130             : {
     131          17 :         WARN_ON(dev->managed.final_kfree);
     132          17 :         WARN_ON(dev < (struct drm_device *) container);
     133          17 :         WARN_ON(dev + 1 > (struct drm_device *) (container + ksize(container)));
     134          17 :         dev->managed.final_kfree = container;
     135          17 : }
     136             : 
     137          64 : int __drmm_add_action(struct drm_device *dev,
     138             :                       drmres_release_t action,
     139             :                       void *data, const char *name)
     140             : {
     141             :         struct drmres *dr;
     142             :         void **void_ptr;
     143             : 
     144         128 :         dr = alloc_dr(action, data ? sizeof(void*) : 0,
     145             :                       GFP_KERNEL | __GFP_ZERO,
     146             :                       dev_to_node(dev->dev));
     147          64 :         if (!dr) {
     148           0 :                 drm_dbg_drmres(dev, "failed to add action %s for %p\n",
     149             :                                name, data);
     150           0 :                 return -ENOMEM;
     151             :         }
     152             : 
     153          64 :         dr->node.name = kstrdup_const(name, GFP_KERNEL);
     154          64 :         if (data) {
     155          30 :                 void_ptr = (void **)&dr->data;
     156          30 :                 *void_ptr = data;
     157             :         }
     158             : 
     159          64 :         add_dr(dev, dr);
     160             : 
     161          64 :         return 0;
     162             : }
     163             : EXPORT_SYMBOL(__drmm_add_action);
     164             : 
     165          64 : int __drmm_add_action_or_reset(struct drm_device *dev,
     166             :                                drmres_release_t action,
     167             :                                void *data, const char *name)
     168             : {
     169             :         int ret;
     170             : 
     171          64 :         ret = __drmm_add_action(dev, action, data, name);
     172          64 :         if (ret)
     173           0 :                 action(dev, data);
     174             : 
     175          64 :         return ret;
     176             : }
     177             : EXPORT_SYMBOL(__drmm_add_action_or_reset);
     178             : 
     179             : /**
     180             :  * drmm_kmalloc - &drm_device managed kmalloc()
     181             :  * @dev: DRM device
     182             :  * @size: size of the memory allocation
     183             :  * @gfp: GFP allocation flags
     184             :  *
     185             :  * This is a &drm_device managed version of kmalloc(). The allocated memory is
     186             :  * automatically freed on the final drm_dev_put(). Memory can also be freed
     187             :  * before the final drm_dev_put() by calling drmm_kfree().
     188             :  */
     189          34 : void *drmm_kmalloc(struct drm_device *dev, size_t size, gfp_t gfp)
     190             : {
     191             :         struct drmres *dr;
     192             : 
     193          68 :         dr = alloc_dr(NULL, size, gfp, dev_to_node(dev->dev));
     194          34 :         if (!dr) {
     195           0 :                 drm_dbg_drmres(dev, "failed to allocate %zu bytes, %u flags\n",
     196             :                                size, gfp);
     197           0 :                 return NULL;
     198             :         }
     199          34 :         dr->node.name = kstrdup_const("kmalloc", GFP_KERNEL);
     200             : 
     201          34 :         add_dr(dev, dr);
     202             : 
     203          34 :         return dr->data;
     204             : }
     205             : EXPORT_SYMBOL(drmm_kmalloc);
     206             : 
     207             : /**
     208             :  * drmm_kstrdup - &drm_device managed kstrdup()
     209             :  * @dev: DRM device
     210             :  * @s: 0-terminated string to be duplicated
     211             :  * @gfp: GFP allocation flags
     212             :  *
     213             :  * This is a &drm_device managed version of kstrdup(). The allocated memory is
     214             :  * automatically freed on the final drm_dev_put() and works exactly like a
     215             :  * memory allocation obtained by drmm_kmalloc().
     216             :  */
     217          17 : char *drmm_kstrdup(struct drm_device *dev, const char *s, gfp_t gfp)
     218             : {
     219             :         size_t size;
     220             :         char *buf;
     221             : 
     222          17 :         if (!s)
     223             :                 return NULL;
     224             : 
     225          17 :         size = strlen(s) + 1;
     226          17 :         buf = drmm_kmalloc(dev, size, gfp);
     227          17 :         if (buf)
     228          17 :                 memcpy(buf, s, size);
     229             :         return buf;
     230             : }
     231             : EXPORT_SYMBOL_GPL(drmm_kstrdup);
     232             : 
     233             : /**
     234             :  * drmm_kfree - &drm_device managed kfree()
     235             :  * @dev: DRM device
     236             :  * @data: memory allocation to be freed
     237             :  *
     238             :  * This is a &drm_device managed version of kfree() which can be used to
     239             :  * release memory allocated through drmm_kmalloc() or any of its related
     240             :  * functions before the final drm_dev_put() of @dev.
     241             :  */
     242           0 : void drmm_kfree(struct drm_device *dev, void *data)
     243             : {
     244           0 :         struct drmres *dr_match = NULL, *dr;
     245             :         unsigned long flags;
     246             : 
     247           0 :         if (!data)
     248             :                 return;
     249             : 
     250           0 :         spin_lock_irqsave(&dev->managed.lock, flags);
     251           0 :         list_for_each_entry(dr, &dev->managed.resources, node.entry) {
     252           0 :                 if (dr->data == data) {
     253           0 :                         dr_match = dr;
     254           0 :                         del_dr(dev, dr_match);
     255           0 :                         break;
     256             :                 }
     257             :         }
     258           0 :         spin_unlock_irqrestore(&dev->managed.lock, flags);
     259             : 
     260           0 :         if (WARN_ON(!dr_match))
     261             :                 return;
     262             : 
     263             :         free_dr(dr_match);
     264             : }
     265             : EXPORT_SYMBOL(drmm_kfree);
     266             : 
     267           0 : static void drmm_mutex_release(struct drm_device *dev, void *res)
     268             : {
     269           0 :         struct mutex *lock = res;
     270             : 
     271           0 :         mutex_destroy(lock);
     272           0 : }
     273             : 
     274             : /**
     275             :  * drmm_mutex_init - &drm_device-managed mutex_init()
     276             :  * @dev: DRM device
     277             :  * @lock: lock to be initialized
     278             :  *
     279             :  * Returns:
     280             :  * 0 on success, or a negative errno code otherwise.
     281             :  *
     282             :  * This is a &drm_device-managed version of mutex_init(). The initialized
     283             :  * lock is automatically destroyed on the final drm_dev_put().
     284             :  */
     285           0 : int drmm_mutex_init(struct drm_device *dev, struct mutex *lock)
     286             : {
     287           0 :         mutex_init(lock);
     288             : 
     289           0 :         return drmm_add_action_or_reset(dev, drmm_mutex_release, lock);
     290             : }
     291             : EXPORT_SYMBOL(drmm_mutex_init);

Generated by: LCOV version 1.14