// SPDX-License-Identifier: GPL-2.01/*2* drivers/base/devres.c - device resource management3*4* Copyright (c) 2006 SUSE Linux Products GmbH5* Copyright (c) 2006 Tejun Heo <[email protected]>6*/78#include <linux/device.h>9#include <linux/module.h>10#include <linux/slab.h>11#include <linux/percpu.h>1213#include <asm/sections.h>1415#include "base.h"16#include "trace.h"1718struct devres_node {19struct list_head entry;20dr_release_t release;21const char *name;22size_t size;23};2425struct devres {26struct devres_node node;27/*28* Some archs want to perform DMA into kmalloc caches29* and need a guaranteed alignment larger than30* the alignment of a 64-bit integer.31* Thus we use ARCH_DMA_MINALIGN for data[] which will force the same32* alignment for struct devres when allocated by kmalloc().33*/34u8 __aligned(ARCH_DMA_MINALIGN) data[];35};3637struct devres_group {38struct devres_node node[2];39void *id;40int color;41/* -- 8 pointers */42};4344static void set_node_dbginfo(struct devres_node *node, const char *name,45size_t size)46{47node->name = name;48node->size = size;49}5051#ifdef CONFIG_DEBUG_DEVRES52static int log_devres = 0;53module_param_named(log, log_devres, int, S_IRUGO | S_IWUSR);5455static void devres_dbg(struct device *dev, struct devres_node *node,56const char *op)57{58if (unlikely(log_devres))59dev_err(dev, "DEVRES %3s %p %s (%zu bytes)\n",60op, node, node->name, node->size);61}62#else /* CONFIG_DEBUG_DEVRES */63#define devres_dbg(dev, node, op) do {} while (0)64#endif /* CONFIG_DEBUG_DEVRES */6566static void devres_log(struct device *dev, struct devres_node *node,67const char *op)68{69trace_devres_log(dev, op, node, node->name, node->size);70devres_dbg(dev, node, op);71}7273/*74* Release functions for devres group. These callbacks are used only75* for identification.76*/77static void group_open_release(struct device *dev, void *res)78{79/* noop */80}8182static void group_close_release(struct device *dev, void *res)83{84/* noop */85}8687static struct devres_group *node_to_group(struct devres_node *node)88{89if (node->release == &group_open_release)90return container_of(node, struct devres_group, node[0]);91if (node->release == &group_close_release)92return container_of(node, struct devres_group, node[1]);93return NULL;94}9596static bool check_dr_size(size_t size, size_t *tot_size)97{98/* We must catch any near-SIZE_MAX cases that could overflow. */99if (unlikely(check_add_overflow(sizeof(struct devres),100size, tot_size)))101return false;102103/* Actually allocate the full kmalloc bucket size. */104*tot_size = kmalloc_size_roundup(*tot_size);105106return true;107}108109static __always_inline struct devres *alloc_dr(dr_release_t release,110size_t size, gfp_t gfp, int nid)111{112size_t tot_size;113struct devres *dr;114115if (!check_dr_size(size, &tot_size))116return NULL;117118dr = kmalloc_node_track_caller(tot_size, gfp, nid);119if (unlikely(!dr))120return NULL;121122/* No need to clear memory twice */123if (!(gfp & __GFP_ZERO))124memset(dr, 0, offsetof(struct devres, data));125126INIT_LIST_HEAD(&dr->node.entry);127dr->node.release = release;128return dr;129}130131static void add_dr(struct device *dev, struct devres_node *node)132{133devres_log(dev, node, "ADD");134BUG_ON(!list_empty(&node->entry));135list_add_tail(&node->entry, &dev->devres_head);136}137138static void replace_dr(struct device *dev,139struct devres_node *old, struct devres_node *new)140{141devres_log(dev, old, "REPLACE");142BUG_ON(!list_empty(&new->entry));143list_replace(&old->entry, &new->entry);144}145146/**147* __devres_alloc_node - Allocate device resource data148* @release: Release function devres will be associated with149* @size: Allocation size150* @gfp: Allocation flags151* @nid: NUMA node152* @name: Name of the resource153*154* Allocate devres of @size bytes. The allocated area is zeroed, then155* associated with @release. The returned pointer can be passed to156* other devres_*() functions.157*158* RETURNS:159* Pointer to allocated devres on success, NULL on failure.160*/161void *__devres_alloc_node(dr_release_t release, size_t size, gfp_t gfp, int nid,162const char *name)163{164struct devres *dr;165166dr = alloc_dr(release, size, gfp | __GFP_ZERO, nid);167if (unlikely(!dr))168return NULL;169set_node_dbginfo(&dr->node, name, size);170return dr->data;171}172EXPORT_SYMBOL_GPL(__devres_alloc_node);173174/**175* devres_for_each_res - Resource iterator176* @dev: Device to iterate resource from177* @release: Look for resources associated with this release function178* @match: Match function (optional)179* @match_data: Data for the match function180* @fn: Function to be called for each matched resource.181* @data: Data for @fn, the 3rd parameter of @fn182*183* Call @fn for each devres of @dev which is associated with @release184* and for which @match returns 1.185*186* RETURNS:187* void188*/189void devres_for_each_res(struct device *dev, dr_release_t release,190dr_match_t match, void *match_data,191void (*fn)(struct device *, void *, void *),192void *data)193{194struct devres_node *node;195struct devres_node *tmp;196unsigned long flags;197198if (!fn)199return;200201spin_lock_irqsave(&dev->devres_lock, flags);202list_for_each_entry_safe_reverse(node, tmp,203&dev->devres_head, entry) {204struct devres *dr = container_of(node, struct devres, node);205206if (node->release != release)207continue;208if (match && !match(dev, dr->data, match_data))209continue;210fn(dev, dr->data, data);211}212spin_unlock_irqrestore(&dev->devres_lock, flags);213}214EXPORT_SYMBOL_GPL(devres_for_each_res);215216/**217* devres_free - Free device resource data218* @res: Pointer to devres data to free219*220* Free devres created with devres_alloc().221*/222void devres_free(void *res)223{224if (res) {225struct devres *dr = container_of(res, struct devres, data);226227BUG_ON(!list_empty(&dr->node.entry));228kfree(dr);229}230}231EXPORT_SYMBOL_GPL(devres_free);232233/**234* devres_add - Register device resource235* @dev: Device to add resource to236* @res: Resource to register237*238* Register devres @res to @dev. @res should have been allocated239* using devres_alloc(). On driver detach, the associated release240* function will be invoked and devres will be freed automatically.241*/242void devres_add(struct device *dev, void *res)243{244struct devres *dr = container_of(res, struct devres, data);245unsigned long flags;246247spin_lock_irqsave(&dev->devres_lock, flags);248add_dr(dev, &dr->node);249spin_unlock_irqrestore(&dev->devres_lock, flags);250}251EXPORT_SYMBOL_GPL(devres_add);252253static struct devres *find_dr(struct device *dev, dr_release_t release,254dr_match_t match, void *match_data)255{256struct devres_node *node;257258list_for_each_entry_reverse(node, &dev->devres_head, entry) {259struct devres *dr = container_of(node, struct devres, node);260261if (node->release != release)262continue;263if (match && !match(dev, dr->data, match_data))264continue;265return dr;266}267268return NULL;269}270271/**272* devres_find - Find device resource273* @dev: Device to lookup resource from274* @release: Look for resources associated with this release function275* @match: Match function (optional)276* @match_data: Data for the match function277*278* Find the latest devres of @dev which is associated with @release279* and for which @match returns 1. If @match is NULL, it's considered280* to match all.281*282* RETURNS:283* Pointer to found devres, NULL if not found.284*/285void *devres_find(struct device *dev, dr_release_t release,286dr_match_t match, void *match_data)287{288struct devres *dr;289unsigned long flags;290291spin_lock_irqsave(&dev->devres_lock, flags);292dr = find_dr(dev, release, match, match_data);293spin_unlock_irqrestore(&dev->devres_lock, flags);294295if (dr)296return dr->data;297return NULL;298}299EXPORT_SYMBOL_GPL(devres_find);300301/**302* devres_get - Find devres, if non-existent, add one atomically303* @dev: Device to lookup or add devres for304* @new_res: Pointer to new initialized devres to add if not found305* @match: Match function (optional)306* @match_data: Data for the match function307*308* Find the latest devres of @dev which has the same release function309* as @new_res and for which @match return 1. If found, @new_res is310* freed; otherwise, @new_res is added atomically.311*312* RETURNS:313* Pointer to found or added devres.314*/315void *devres_get(struct device *dev, void *new_res,316dr_match_t match, void *match_data)317{318struct devres *new_dr = container_of(new_res, struct devres, data);319struct devres *dr;320unsigned long flags;321322spin_lock_irqsave(&dev->devres_lock, flags);323dr = find_dr(dev, new_dr->node.release, match, match_data);324if (!dr) {325add_dr(dev, &new_dr->node);326dr = new_dr;327new_res = NULL;328}329spin_unlock_irqrestore(&dev->devres_lock, flags);330devres_free(new_res);331332return dr->data;333}334EXPORT_SYMBOL_GPL(devres_get);335336/**337* devres_remove - Find a device resource and remove it338* @dev: Device to find resource from339* @release: Look for resources associated with this release function340* @match: Match function (optional)341* @match_data: Data for the match function342*343* Find the latest devres of @dev associated with @release and for344* which @match returns 1. If @match is NULL, it's considered to345* match all. If found, the resource is removed atomically and346* returned.347*348* RETURNS:349* Pointer to removed devres on success, NULL if not found.350*/351void *devres_remove(struct device *dev, dr_release_t release,352dr_match_t match, void *match_data)353{354struct devres *dr;355unsigned long flags;356357spin_lock_irqsave(&dev->devres_lock, flags);358dr = find_dr(dev, release, match, match_data);359if (dr) {360list_del_init(&dr->node.entry);361devres_log(dev, &dr->node, "REM");362}363spin_unlock_irqrestore(&dev->devres_lock, flags);364365if (dr)366return dr->data;367return NULL;368}369EXPORT_SYMBOL_GPL(devres_remove);370371/**372* devres_destroy - Find a device resource and destroy it373* @dev: Device to find resource from374* @release: Look for resources associated with this release function375* @match: Match function (optional)376* @match_data: Data for the match function377*378* Find the latest devres of @dev associated with @release and for379* which @match returns 1. If @match is NULL, it's considered to380* match all. If found, the resource is removed atomically and freed.381*382* Note that the release function for the resource will not be called,383* only the devres-allocated data will be freed. The caller becomes384* responsible for freeing any other data.385*386* RETURNS:387* 0 if devres is found and freed, -ENOENT if not found.388*/389int devres_destroy(struct device *dev, dr_release_t release,390dr_match_t match, void *match_data)391{392void *res;393394res = devres_remove(dev, release, match, match_data);395if (unlikely(!res))396return -ENOENT;397398devres_free(res);399return 0;400}401EXPORT_SYMBOL_GPL(devres_destroy);402403404/**405* devres_release - Find a device resource and destroy it, calling release406* @dev: Device to find resource from407* @release: Look for resources associated with this release function408* @match: Match function (optional)409* @match_data: Data for the match function410*411* Find the latest devres of @dev associated with @release and for412* which @match returns 1. If @match is NULL, it's considered to413* match all. If found, the resource is removed atomically, the414* release function called and the resource freed.415*416* RETURNS:417* 0 if devres is found and freed, -ENOENT if not found.418*/419int devres_release(struct device *dev, dr_release_t release,420dr_match_t match, void *match_data)421{422void *res;423424res = devres_remove(dev, release, match, match_data);425if (unlikely(!res))426return -ENOENT;427428(*release)(dev, res);429devres_free(res);430return 0;431}432EXPORT_SYMBOL_GPL(devres_release);433434static int remove_nodes(struct device *dev,435struct list_head *first, struct list_head *end,436struct list_head *todo)437{438struct devres_node *node, *n;439int cnt = 0, nr_groups = 0;440441/* First pass - move normal devres entries to @todo and clear442* devres_group colors.443*/444node = list_entry(first, struct devres_node, entry);445list_for_each_entry_safe_from(node, n, end, entry) {446struct devres_group *grp;447448grp = node_to_group(node);449if (grp) {450/* clear color of group markers in the first pass */451grp->color = 0;452nr_groups++;453} else {454/* regular devres entry */455if (&node->entry == first)456first = first->next;457list_move_tail(&node->entry, todo);458cnt++;459}460}461462if (!nr_groups)463return cnt;464465/* Second pass - Scan groups and color them. A group gets466* color value of two iff the group is wholly contained in467* [current node, end). That is, for a closed group, both opening468* and closing markers should be in the range, while just the469* opening marker is enough for an open group.470*/471node = list_entry(first, struct devres_node, entry);472list_for_each_entry_safe_from(node, n, end, entry) {473struct devres_group *grp;474475grp = node_to_group(node);476BUG_ON(!grp || list_empty(&grp->node[0].entry));477478grp->color++;479if (list_empty(&grp->node[1].entry))480grp->color++;481482BUG_ON(grp->color <= 0 || grp->color > 2);483if (grp->color == 2) {484/* No need to update current node or end. The removed485* nodes are always before both.486*/487list_move_tail(&grp->node[0].entry, todo);488list_del_init(&grp->node[1].entry);489}490}491492return cnt;493}494495static void release_nodes(struct device *dev, struct list_head *todo)496{497struct devres *dr, *tmp;498499/* Release. Note that both devres and devres_group are500* handled as devres in the following loop. This is safe.501*/502list_for_each_entry_safe_reverse(dr, tmp, todo, node.entry) {503devres_log(dev, &dr->node, "REL");504dr->node.release(dev, dr->data);505kfree(dr);506}507}508509/**510* devres_release_all - Release all managed resources511* @dev: Device to release resources for512*513* Release all resources associated with @dev. This function is514* called on driver detach.515*/516int devres_release_all(struct device *dev)517{518unsigned long flags;519LIST_HEAD(todo);520int cnt;521522/* Looks like an uninitialized device structure */523if (WARN_ON(dev->devres_head.next == NULL))524return -ENODEV;525526/* Nothing to release if list is empty */527if (list_empty(&dev->devres_head))528return 0;529530spin_lock_irqsave(&dev->devres_lock, flags);531cnt = remove_nodes(dev, dev->devres_head.next, &dev->devres_head, &todo);532spin_unlock_irqrestore(&dev->devres_lock, flags);533534release_nodes(dev, &todo);535return cnt;536}537538/**539* devres_open_group - Open a new devres group540* @dev: Device to open devres group for541* @id: Separator ID542* @gfp: Allocation flags543*544* Open a new devres group for @dev with @id. For @id, using a545* pointer to an object which won't be used for another group is546* recommended. If @id is NULL, address-wise unique ID is created.547*548* RETURNS:549* ID of the new group, NULL on failure.550*/551void *devres_open_group(struct device *dev, void *id, gfp_t gfp)552{553struct devres_group *grp;554unsigned long flags;555556grp = kmalloc(sizeof(*grp), gfp);557if (unlikely(!grp))558return NULL;559560grp->node[0].release = &group_open_release;561grp->node[1].release = &group_close_release;562INIT_LIST_HEAD(&grp->node[0].entry);563INIT_LIST_HEAD(&grp->node[1].entry);564set_node_dbginfo(&grp->node[0], "grp<", 0);565set_node_dbginfo(&grp->node[1], "grp>", 0);566grp->id = grp;567if (id)568grp->id = id;569grp->color = 0;570571spin_lock_irqsave(&dev->devres_lock, flags);572add_dr(dev, &grp->node[0]);573spin_unlock_irqrestore(&dev->devres_lock, flags);574return grp->id;575}576EXPORT_SYMBOL_GPL(devres_open_group);577578/*579* Find devres group with ID @id. If @id is NULL, look for the latest open580* group.581*/582static struct devres_group *find_group(struct device *dev, void *id)583{584struct devres_node *node;585586list_for_each_entry_reverse(node, &dev->devres_head, entry) {587struct devres_group *grp;588589if (node->release != &group_open_release)590continue;591592grp = container_of(node, struct devres_group, node[0]);593594if (id) {595if (grp->id == id)596return grp;597} else if (list_empty(&grp->node[1].entry))598return grp;599}600601return NULL;602}603604/**605* devres_close_group - Close a devres group606* @dev: Device to close devres group for607* @id: ID of target group, can be NULL608*609* Close the group identified by @id. If @id is NULL, the latest open610* group is selected.611*/612void devres_close_group(struct device *dev, void *id)613{614struct devres_group *grp;615unsigned long flags;616617spin_lock_irqsave(&dev->devres_lock, flags);618619grp = find_group(dev, id);620if (grp)621add_dr(dev, &grp->node[1]);622else623WARN_ON(1);624625spin_unlock_irqrestore(&dev->devres_lock, flags);626}627EXPORT_SYMBOL_GPL(devres_close_group);628629/**630* devres_remove_group - Remove a devres group631* @dev: Device to remove group for632* @id: ID of target group, can be NULL633*634* Remove the group identified by @id. If @id is NULL, the latest635* open group is selected. Note that removing a group doesn't affect636* any other resources.637*/638void devres_remove_group(struct device *dev, void *id)639{640struct devres_group *grp;641unsigned long flags;642643spin_lock_irqsave(&dev->devres_lock, flags);644645grp = find_group(dev, id);646if (grp) {647list_del_init(&grp->node[0].entry);648list_del_init(&grp->node[1].entry);649devres_log(dev, &grp->node[0], "REM");650} else651WARN_ON(1);652653spin_unlock_irqrestore(&dev->devres_lock, flags);654655kfree(grp);656}657EXPORT_SYMBOL_GPL(devres_remove_group);658659/**660* devres_release_group - Release resources in a devres group661* @dev: Device to release group for662* @id: ID of target group, can be NULL663*664* Release all resources in the group identified by @id. If @id is665* NULL, the latest open group is selected. The selected group and666* groups properly nested inside the selected group are removed.667*668* RETURNS:669* The number of released non-group resources.670*/671int devres_release_group(struct device *dev, void *id)672{673struct devres_group *grp;674unsigned long flags;675LIST_HEAD(todo);676int cnt = 0;677678spin_lock_irqsave(&dev->devres_lock, flags);679680grp = find_group(dev, id);681if (grp) {682struct list_head *first = &grp->node[0].entry;683struct list_head *end = &dev->devres_head;684685if (!list_empty(&grp->node[1].entry))686end = grp->node[1].entry.next;687688cnt = remove_nodes(dev, first, end, &todo);689spin_unlock_irqrestore(&dev->devres_lock, flags);690691release_nodes(dev, &todo);692} else if (list_empty(&dev->devres_head)) {693/*694* dev is probably dying via devres_release_all(): groups695* have already been removed and are on the process of696* being released - don't touch and don't warn.697*/698spin_unlock_irqrestore(&dev->devres_lock, flags);699} else {700WARN_ON(1);701spin_unlock_irqrestore(&dev->devres_lock, flags);702}703704return cnt;705}706EXPORT_SYMBOL_GPL(devres_release_group);707708/*709* Custom devres actions allow inserting a simple function call710* into the teardown sequence.711*/712713struct action_devres {714void *data;715void (*action)(void *);716};717718static int devm_action_match(struct device *dev, void *res, void *p)719{720struct action_devres *devres = res;721struct action_devres *target = p;722723return devres->action == target->action &&724devres->data == target->data;725}726727static void devm_action_release(struct device *dev, void *res)728{729struct action_devres *devres = res;730731devres->action(devres->data);732}733734/**735* __devm_add_action() - add a custom action to list of managed resources736* @dev: Device that owns the action737* @action: Function that should be called738* @data: Pointer to data passed to @action implementation739* @name: Name of the resource (for debugging purposes)740*741* This adds a custom action to the list of managed resources so that742* it gets executed as part of standard resource unwinding.743*/744int __devm_add_action(struct device *dev, void (*action)(void *), void *data, const char *name)745{746struct action_devres *devres;747748devres = __devres_alloc_node(devm_action_release, sizeof(struct action_devres),749GFP_KERNEL, NUMA_NO_NODE, name);750if (!devres)751return -ENOMEM;752753devres->data = data;754devres->action = action;755756devres_add(dev, devres);757return 0;758}759EXPORT_SYMBOL_GPL(__devm_add_action);760761bool devm_is_action_added(struct device *dev, void (*action)(void *), void *data)762{763struct action_devres devres = {764.data = data,765.action = action,766};767768return devres_find(dev, devm_action_release, devm_action_match, &devres);769}770EXPORT_SYMBOL_GPL(devm_is_action_added);771772/**773* devm_remove_action_nowarn() - removes previously added custom action774* @dev: Device that owns the action775* @action: Function implementing the action776* @data: Pointer to data passed to @action implementation777*778* Removes instance of @action previously added by devm_add_action().779* Both action and data should match one of the existing entries.780*781* In contrast to devm_remove_action(), this function does not WARN() if no782* entry could have been found.783*784* This should only be used if the action is contained in an object with785* independent lifetime management, e.g. the Devres rust abstraction.786*787* Causing the warning from regular driver code most likely indicates an abuse788* of the devres API.789*790* Returns: 0 on success, -ENOENT if no entry could have been found.791*/792int devm_remove_action_nowarn(struct device *dev,793void (*action)(void *),794void *data)795{796struct action_devres devres = {797.data = data,798.action = action,799};800801return devres_destroy(dev, devm_action_release, devm_action_match,802&devres);803}804EXPORT_SYMBOL_GPL(devm_remove_action_nowarn);805806/**807* devm_release_action() - release previously added custom action808* @dev: Device that owns the action809* @action: Function implementing the action810* @data: Pointer to data passed to @action implementation811*812* Releases and removes instance of @action previously added by813* devm_add_action(). Both action and data should match one of the814* existing entries.815*/816void devm_release_action(struct device *dev, void (*action)(void *), void *data)817{818struct action_devres devres = {819.data = data,820.action = action,821};822823WARN_ON(devres_release(dev, devm_action_release, devm_action_match,824&devres));825826}827EXPORT_SYMBOL_GPL(devm_release_action);828829/*830* Managed kmalloc/kfree831*/832static void devm_kmalloc_release(struct device *dev, void *res)833{834/* noop */835}836837static int devm_kmalloc_match(struct device *dev, void *res, void *data)838{839return res == data;840}841842/**843* devm_kmalloc - Resource-managed kmalloc844* @dev: Device to allocate memory for845* @size: Allocation size846* @gfp: Allocation gfp flags847*848* Managed kmalloc. Memory allocated with this function is849* automatically freed on driver detach. Like all other devres850* resources, guaranteed alignment is unsigned long long.851*852* RETURNS:853* Pointer to allocated memory on success, NULL on failure.854*/855void *devm_kmalloc(struct device *dev, size_t size, gfp_t gfp)856{857struct devres *dr;858859if (unlikely(!size))860return ZERO_SIZE_PTR;861862/* use raw alloc_dr for kmalloc caller tracing */863dr = alloc_dr(devm_kmalloc_release, size, gfp, dev_to_node(dev));864if (unlikely(!dr))865return NULL;866867/*868* This is named devm_kzalloc_release for historical reasons869* The initial implementation did not support kmalloc, only kzalloc870*/871set_node_dbginfo(&dr->node, "devm_kzalloc_release", size);872devres_add(dev, dr->data);873return dr->data;874}875EXPORT_SYMBOL_GPL(devm_kmalloc);876877/**878* devm_krealloc - Resource-managed krealloc()879* @dev: Device to re-allocate memory for880* @ptr: Pointer to the memory chunk to re-allocate881* @new_size: New allocation size882* @gfp: Allocation gfp flags883*884* Managed krealloc(). Resizes the memory chunk allocated with devm_kmalloc().885* Behaves similarly to regular krealloc(): if @ptr is NULL or ZERO_SIZE_PTR,886* it's the equivalent of devm_kmalloc(). If new_size is zero, it frees the887* previously allocated memory and returns ZERO_SIZE_PTR. This function doesn't888* change the order in which the release callback for the re-alloc'ed devres889* will be called (except when falling back to devm_kmalloc() or when freeing890* resources when new_size is zero). The contents of the memory are preserved891* up to the lesser of new and old sizes.892*/893void *devm_krealloc(struct device *dev, void *ptr, size_t new_size, gfp_t gfp)894{895size_t total_new_size, total_old_size;896struct devres *old_dr, *new_dr;897unsigned long flags;898899if (unlikely(!new_size)) {900devm_kfree(dev, ptr);901return ZERO_SIZE_PTR;902}903904if (unlikely(ZERO_OR_NULL_PTR(ptr)))905return devm_kmalloc(dev, new_size, gfp);906907if (WARN_ON(is_kernel_rodata((unsigned long)ptr)))908/*909* We cannot reliably realloc a const string returned by910* devm_kstrdup_const().911*/912return NULL;913914if (!check_dr_size(new_size, &total_new_size))915return NULL;916917total_old_size = ksize(container_of(ptr, struct devres, data));918if (total_old_size == 0) {919WARN(1, "Pointer doesn't point to dynamically allocated memory.");920return NULL;921}922923/*924* If new size is smaller or equal to the actual number of bytes925* allocated previously - just return the same pointer.926*/927if (total_new_size <= total_old_size)928return ptr;929930/*931* Otherwise: allocate new, larger chunk. We need to allocate before932* taking the lock as most probably the caller uses GFP_KERNEL.933* alloc_dr() will call check_dr_size() to reserve extra memory934* for struct devres automatically, so size @new_size user request935* is delivered to it directly as devm_kmalloc() does.936*/937new_dr = alloc_dr(devm_kmalloc_release,938new_size, gfp, dev_to_node(dev));939if (!new_dr)940return NULL;941942/*943* The spinlock protects the linked list against concurrent944* modifications but not the resource itself.945*/946spin_lock_irqsave(&dev->devres_lock, flags);947948old_dr = find_dr(dev, devm_kmalloc_release, devm_kmalloc_match, ptr);949if (!old_dr) {950spin_unlock_irqrestore(&dev->devres_lock, flags);951kfree(new_dr);952WARN(1, "Memory chunk not managed or managed by a different device.");953return NULL;954}955956replace_dr(dev, &old_dr->node, &new_dr->node);957958spin_unlock_irqrestore(&dev->devres_lock, flags);959960/*961* We can copy the memory contents after releasing the lock as we're962* no longer modifying the list links.963*/964memcpy(new_dr->data, old_dr->data,965total_old_size - offsetof(struct devres, data));966/*967* Same for releasing the old devres - it's now been removed from the968* list. This is also the reason why we must not use devm_kfree() - the969* links are no longer valid.970*/971kfree(old_dr);972973return new_dr->data;974}975EXPORT_SYMBOL_GPL(devm_krealloc);976977/**978* devm_kstrdup - Allocate resource managed space and979* copy an existing string into that.980* @dev: Device to allocate memory for981* @s: the string to duplicate982* @gfp: the GFP mask used in the devm_kmalloc() call when983* allocating memory984* RETURNS:985* Pointer to allocated string on success, NULL on failure.986*/987char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp)988{989if (!s)990return NULL;991992return devm_kmemdup(dev, s, strlen(s) + 1, gfp);993}994EXPORT_SYMBOL_GPL(devm_kstrdup);995996/**997* devm_kstrdup_const - resource managed conditional string duplication998* @dev: device for which to duplicate the string999* @s: the string to duplicate1000* @gfp: the GFP mask used in the kmalloc() call when allocating memory1001*1002* Strings allocated by devm_kstrdup_const will be automatically freed when1003* the associated device is detached.1004*1005* RETURNS:1006* Source string if it is in .rodata section otherwise it falls back to1007* devm_kstrdup.1008*/1009const char *devm_kstrdup_const(struct device *dev, const char *s, gfp_t gfp)1010{1011if (is_kernel_rodata((unsigned long)s))1012return s;10131014return devm_kstrdup(dev, s, gfp);1015}1016EXPORT_SYMBOL_GPL(devm_kstrdup_const);10171018/**1019* devm_kvasprintf - Allocate resource managed space and format a string1020* into that.1021* @dev: Device to allocate memory for1022* @gfp: the GFP mask used in the devm_kmalloc() call when1023* allocating memory1024* @fmt: The printf()-style format string1025* @ap: Arguments for the format string1026* RETURNS:1027* Pointer to allocated string on success, NULL on failure.1028*/1029char *devm_kvasprintf(struct device *dev, gfp_t gfp, const char *fmt,1030va_list ap)1031{1032unsigned int len;1033char *p;1034va_list aq;10351036va_copy(aq, ap);1037len = vsnprintf(NULL, 0, fmt, aq);1038va_end(aq);10391040p = devm_kmalloc(dev, len+1, gfp);1041if (!p)1042return NULL;10431044vsnprintf(p, len+1, fmt, ap);10451046return p;1047}1048EXPORT_SYMBOL(devm_kvasprintf);10491050/**1051* devm_kasprintf - Allocate resource managed space and format a string1052* into that.1053* @dev: Device to allocate memory for1054* @gfp: the GFP mask used in the devm_kmalloc() call when1055* allocating memory1056* @fmt: The printf()-style format string1057* @...: Arguments for the format string1058* RETURNS:1059* Pointer to allocated string on success, NULL on failure.1060*/1061char *devm_kasprintf(struct device *dev, gfp_t gfp, const char *fmt, ...)1062{1063va_list ap;1064char *p;10651066va_start(ap, fmt);1067p = devm_kvasprintf(dev, gfp, fmt, ap);1068va_end(ap);10691070return p;1071}1072EXPORT_SYMBOL_GPL(devm_kasprintf);10731074/**1075* devm_kfree - Resource-managed kfree1076* @dev: Device this memory belongs to1077* @p: Memory to free1078*1079* Free memory allocated with devm_kmalloc().1080*/1081void devm_kfree(struct device *dev, const void *p)1082{1083int rc;10841085/*1086* Special cases: pointer to a string in .rodata returned by1087* devm_kstrdup_const() or NULL/ZERO ptr.1088*/1089if (unlikely(is_kernel_rodata((unsigned long)p) || ZERO_OR_NULL_PTR(p)))1090return;10911092rc = devres_destroy(dev, devm_kmalloc_release,1093devm_kmalloc_match, (void *)p);1094WARN_ON(rc);1095}1096EXPORT_SYMBOL_GPL(devm_kfree);10971098/**1099* devm_kmemdup - Resource-managed kmemdup1100* @dev: Device this memory belongs to1101* @src: Memory region to duplicate1102* @len: Memory region length1103* @gfp: GFP mask to use1104*1105* Duplicate region of a memory using resource managed kmalloc1106*/1107void *devm_kmemdup(struct device *dev, const void *src, size_t len, gfp_t gfp)1108{1109void *p;11101111p = devm_kmalloc(dev, len, gfp);1112if (p)1113memcpy(p, src, len);11141115return p;1116}1117EXPORT_SYMBOL_GPL(devm_kmemdup);11181119struct pages_devres {1120unsigned long addr;1121unsigned int order;1122};11231124static int devm_pages_match(struct device *dev, void *res, void *p)1125{1126struct pages_devres *devres = res;1127struct pages_devres *target = p;11281129return devres->addr == target->addr;1130}11311132static void devm_pages_release(struct device *dev, void *res)1133{1134struct pages_devres *devres = res;11351136free_pages(devres->addr, devres->order);1137}11381139/**1140* devm_get_free_pages - Resource-managed __get_free_pages1141* @dev: Device to allocate memory for1142* @gfp_mask: Allocation gfp flags1143* @order: Allocation size is (1 << order) pages1144*1145* Managed get_free_pages. Memory allocated with this function is1146* automatically freed on driver detach.1147*1148* RETURNS:1149* Address of allocated memory on success, 0 on failure.1150*/11511152unsigned long devm_get_free_pages(struct device *dev,1153gfp_t gfp_mask, unsigned int order)1154{1155struct pages_devres *devres;1156unsigned long addr;11571158addr = __get_free_pages(gfp_mask, order);11591160if (unlikely(!addr))1161return 0;11621163devres = devres_alloc(devm_pages_release,1164sizeof(struct pages_devres), GFP_KERNEL);1165if (unlikely(!devres)) {1166free_pages(addr, order);1167return 0;1168}11691170devres->addr = addr;1171devres->order = order;11721173devres_add(dev, devres);1174return addr;1175}1176EXPORT_SYMBOL_GPL(devm_get_free_pages);11771178/**1179* devm_free_pages - Resource-managed free_pages1180* @dev: Device this memory belongs to1181* @addr: Memory to free1182*1183* Free memory allocated with devm_get_free_pages(). Unlike free_pages,1184* there is no need to supply the @order.1185*/1186void devm_free_pages(struct device *dev, unsigned long addr)1187{1188struct pages_devres devres = { .addr = addr };11891190WARN_ON(devres_release(dev, devm_pages_release, devm_pages_match,1191&devres));1192}1193EXPORT_SYMBOL_GPL(devm_free_pages);11941195static void devm_percpu_release(struct device *dev, void *pdata)1196{1197void __percpu *p;11981199p = *(void __percpu **)pdata;1200free_percpu(p);1201}12021203static int devm_percpu_match(struct device *dev, void *data, void *p)1204{1205struct devres *devr = container_of(data, struct devres, data);12061207return *(void **)devr->data == p;1208}12091210/**1211* __devm_alloc_percpu - Resource-managed alloc_percpu1212* @dev: Device to allocate per-cpu memory for1213* @size: Size of per-cpu memory to allocate1214* @align: Alignment of per-cpu memory to allocate1215*1216* Managed alloc_percpu. Per-cpu memory allocated with this function is1217* automatically freed on driver detach.1218*1219* RETURNS:1220* Pointer to allocated memory on success, NULL on failure.1221*/1222void __percpu *__devm_alloc_percpu(struct device *dev, size_t size,1223size_t align)1224{1225void *p;1226void __percpu *pcpu;12271228pcpu = __alloc_percpu(size, align);1229if (!pcpu)1230return NULL;12311232p = devres_alloc(devm_percpu_release, sizeof(void *), GFP_KERNEL);1233if (!p) {1234free_percpu(pcpu);1235return NULL;1236}12371238*(void __percpu **)p = pcpu;12391240devres_add(dev, p);12411242return pcpu;1243}1244EXPORT_SYMBOL_GPL(__devm_alloc_percpu);12451246/**1247* devm_free_percpu - Resource-managed free_percpu1248* @dev: Device this memory belongs to1249* @pdata: Per-cpu memory to free1250*1251* Free memory allocated with devm_alloc_percpu().1252*/1253void devm_free_percpu(struct device *dev, void __percpu *pdata)1254{1255/*1256* Use devres_release() to prevent memory leakage as1257* devm_free_pages() does.1258*/1259WARN_ON(devres_release(dev, devm_percpu_release, devm_percpu_match,1260(void *)(__force unsigned long)pdata));1261}1262EXPORT_SYMBOL_GPL(devm_free_percpu);126312641265