Skip to content

Commit c7235ee

Browse files
brglgregkh
authored andcommitted
nvmem: remove the global cell list
Nvmem subsystem keeps a global list of cells that, for non-DT systems, can only be referenced by cell name, which makes it impossible to have more than one nvmem device with cells named the same. This patch makes every nvmem device the owner of the list of its cells. This effectively removes the support for non-DT systems, but it will be reintroduced following a different approach in subsequent patches. This isn't a problem as support for board files in nvmem is currently broken anyway: any user that would try to get an nvmem cell from the global cell list would remove the cell after the calling nvmem_cell_put(). This can cause anything from a subsequent user not being able to get the cell to double free errors if more users hold reference to the same cell at the same time. Fortunately there are no such users which allows us to rework this part. Signed-off-by: Bartosz Golaszewski <[email protected]> Signed-off-by: Srinivas Kandagatla <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent bf58e88 commit c7235ee

File tree

1 file changed

+15
-49
lines changed

1 file changed

+15
-49
lines changed

drivers/nvmem/core.c

Lines changed: 15 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ struct nvmem_device {
3838
int flags;
3939
struct bin_attribute eeprom;
4040
struct device *base_dev;
41+
struct list_head cells;
4142
nvmem_reg_read_t reg_read;
4243
nvmem_reg_write_t reg_write;
4344
void *priv;
@@ -58,9 +59,6 @@ struct nvmem_cell {
5859
static DEFINE_MUTEX(nvmem_mutex);
5960
static DEFINE_IDA(nvmem_ida);
6061

61-
static LIST_HEAD(nvmem_cells);
62-
static DEFINE_MUTEX(nvmem_cells_mutex);
63-
6462
#ifdef CONFIG_DEBUG_LOCK_ALLOC
6563
static struct lock_class_key eeprom_lock_key;
6664
#endif
@@ -282,45 +280,27 @@ static struct nvmem_device *of_nvmem_find(struct device_node *nvmem_np)
282280
return to_nvmem_device(d);
283281
}
284282

285-
static struct nvmem_cell *nvmem_find_cell(const char *cell_id)
286-
{
287-
struct nvmem_cell *p;
288-
289-
mutex_lock(&nvmem_cells_mutex);
290-
291-
list_for_each_entry(p, &nvmem_cells, node)
292-
if (!strcmp(p->name, cell_id)) {
293-
mutex_unlock(&nvmem_cells_mutex);
294-
return p;
295-
}
296-
297-
mutex_unlock(&nvmem_cells_mutex);
298-
299-
return NULL;
300-
}
301-
302283
static void nvmem_cell_drop(struct nvmem_cell *cell)
303284
{
304-
mutex_lock(&nvmem_cells_mutex);
285+
mutex_lock(&nvmem_mutex);
305286
list_del(&cell->node);
306-
mutex_unlock(&nvmem_cells_mutex);
287+
mutex_unlock(&nvmem_mutex);
307288
kfree(cell);
308289
}
309290

310291
static void nvmem_device_remove_all_cells(const struct nvmem_device *nvmem)
311292
{
312293
struct nvmem_cell *cell, *p;
313294

314-
list_for_each_entry_safe(cell, p, &nvmem_cells, node)
315-
if (cell->nvmem == nvmem)
316-
nvmem_cell_drop(cell);
295+
list_for_each_entry_safe(cell, p, &nvmem->cells, node)
296+
nvmem_cell_drop(cell);
317297
}
318298

319299
static void nvmem_cell_add(struct nvmem_cell *cell)
320300
{
321-
mutex_lock(&nvmem_cells_mutex);
322-
list_add_tail(&cell->node, &nvmem_cells);
323-
mutex_unlock(&nvmem_cells_mutex);
301+
mutex_lock(&nvmem_mutex);
302+
list_add_tail(&cell->node, &cell->nvmem->cells);
303+
mutex_unlock(&nvmem_mutex);
324304
}
325305

326306
static int nvmem_cell_info_to_nvmem_cell(struct nvmem_device *nvmem,
@@ -465,6 +445,7 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
465445
}
466446

467447
kref_init(&nvmem->refcnt);
448+
INIT_LIST_HEAD(&nvmem->cells);
468449

469450
nvmem->id = rval;
470451
nvmem->owner = config->owner;
@@ -626,29 +607,14 @@ static struct nvmem_device *__nvmem_device_get(struct device_node *np,
626607
{
627608
struct nvmem_device *nvmem = NULL;
628609

629-
mutex_lock(&nvmem_mutex);
630-
631-
if (np) {
632-
nvmem = of_nvmem_find(np);
633-
if (!nvmem) {
634-
mutex_unlock(&nvmem_mutex);
635-
return ERR_PTR(-EPROBE_DEFER);
636-
}
637-
} else {
638-
struct nvmem_cell *cell = nvmem_find_cell(cell_id);
639-
640-
if (cell) {
641-
nvmem = cell->nvmem;
642-
*cellp = cell;
643-
}
644-
645-
if (!nvmem) {
646-
mutex_unlock(&nvmem_mutex);
647-
return ERR_PTR(-ENOENT);
648-
}
649-
}
610+
if (!np)
611+
return ERR_PTR(-ENOENT);
650612

613+
mutex_lock(&nvmem_mutex);
614+
nvmem = of_nvmem_find(np);
651615
mutex_unlock(&nvmem_mutex);
616+
if (!nvmem)
617+
return ERR_PTR(-EPROBE_DEFER);
652618

653619
if (!try_module_get(nvmem->owner)) {
654620
dev_err(&nvmem->dev,

0 commit comments

Comments
 (0)