From patchwork Tue Nov 4 16:29:27 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thierry Reding X-Patchwork-Id: 5229451 Return-Path: X-Original-To: patchwork-dri-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id BF9C79F3EE for ; Tue, 4 Nov 2014 16:29:38 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id B390220172 for ; Tue, 4 Nov 2014 16:29:37 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) by mail.kernel.org (Postfix) with ESMTP id 976F320125 for ; Tue, 4 Nov 2014 16:29:36 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 499056E0A4; Tue, 4 Nov 2014 08:29:35 -0800 (PST) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-pa0-f47.google.com (mail-pa0-f47.google.com [209.85.220.47]) by gabe.freedesktop.org (Postfix) with ESMTP id 69B9D6E0A4 for ; Tue, 4 Nov 2014 08:29:34 -0800 (PST) Received: by mail-pa0-f47.google.com with SMTP id kx10so14621434pab.6 for ; Tue, 04 Nov 2014 08:29:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id; bh=KzSeT5lkqiwfFSokvgD5RgXi7t9MbV4SMeTjCQWMF8w=; b=Y8u3/REUIMTQjQUDP3BbRBG6hJHketU9qXZPHJVXD47VaDTsGawd6W8n2TI5sfgiWV iq+gOm9TKpdXZA0U4DfpHV2vJu5LmsQ7maOatoY4hNF77YBZuywxocn/vfkRGuuyruuv nbOLhecxMYKalJ2V/bJScyqZQr6XaUODIHzNBoefWkc/mZUkHgACu6bADgkE1aQyMF92 nrKkzQc1YM0T0E7tfrX/LaAU4AUD/ZSgpCVfD91+cU1EGmIFxDBuG/HgOFSDGSkDKtMZ kUNlo4lMTrkQab/KqfiMRdFaxEStPlVsyLoYJd+aiwBj2FoLcB8CHLgOmDJ/hF1Eqxxp uI6A== X-Received: by 10.68.136.195 with SMTP id qc3mr51286381pbb.40.1415118574284; Tue, 04 Nov 2014 08:29:34 -0800 (PST) Received: from localhost ([216.228.120.20]) by mx.google.com with ESMTPSA id uj7sm882793pac.4.2014.11.04.08.29.32 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 04 Nov 2014 08:29:33 -0800 (PST) From: Thierry Reding To: Greg Kroah-Hartman Subject: [RFC 1/2] core: Add generic object registry implementation Date: Tue, 4 Nov 2014 17:29:27 +0100 Message-Id: <1415118568-18771-1-git-send-email-thierry.reding@gmail.com> X-Mailer: git-send-email 2.1.3 Cc: Daniel Vetter , linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Spam-Status: No, score=-4.7 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_MED, RP_MATCHES_RCVD, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Thierry Reding Add a generic implementation of an object registry. This targets drivers and subsystems that provide auxiliary objects that other drivers need to look up. The goal is to put the difficult parts (keep object references, module usage count, ...) into core code so that individual subsystems do not have to deal with them. The intention is for subsystems to instantiate a struct registry and use a struct registry_record embedded into a subsystem-specific structure to provide a subsystem-specific API around that. Signed-off-by: Thierry Reding --- drivers/base/Makefile | 2 +- drivers/base/registry.c | 147 +++++++++++++++++++++++++++++++++++++++++++++++ include/linux/registry.h | 62 ++++++++++++++++++++ 3 files changed, 210 insertions(+), 1 deletion(-) create mode 100644 drivers/base/registry.c create mode 100644 include/linux/registry.h diff --git a/drivers/base/Makefile b/drivers/base/Makefile index 53c3fe1aeb29..250262d1af2c 100644 --- a/drivers/base/Makefile +++ b/drivers/base/Makefile @@ -4,7 +4,7 @@ obj-y := component.o core.o bus.o dd.o syscore.o \ driver.o class.o platform.o \ cpu.o firmware.o init.o map.o devres.o \ attribute_container.o transport_class.o \ - topology.o container.o property.o + topology.o container.o property.o registry.o obj-$(CONFIG_DEVTMPFS) += devtmpfs.o obj-$(CONFIG_DMA_CMA) += dma-contiguous.o obj-y += power/ diff --git a/drivers/base/registry.c b/drivers/base/registry.c new file mode 100644 index 000000000000..9f510f6237b7 --- /dev/null +++ b/drivers/base/registry.c @@ -0,0 +1,147 @@ +/* + * Copyright (C) 2014, NVIDIA Corporation. All rights reserved. + * + * This file is released under the GPL v2. + */ + +#include +#include +#include + +static inline struct registry_record *to_registry_record(struct kref *kref) +{ + return container_of(kref, struct registry_record, kref); +} + +static void registry_record_release(struct kref *kref) +{ + struct registry_record *record = to_registry_record(kref); + + record->release(record); +} + +/** + * registry_record_init - initialize a registry record + * @record: record to initialize + * + * Sets up internal fields of the registry record so that it can subsequently + * be added to a registry. + */ +void registry_record_init(struct registry_record *record) +{ + INIT_LIST_HEAD(&record->list); + kref_init(&record->kref); +} + +/** + * registry_record_ref - reference on the registry record + * @record: record to reference + * + * Increases the reference count on the record and returns a pointer to it. + * + * Return: A pointer to the record on success or NULL on failure. + */ +struct registry_record *registry_record_ref(struct registry_record *record) +{ + if (!record) + return NULL; + + /* + * Refuse to give out any more references if the module owning the + * record is being removed. + */ + if (!try_module_get(record->owner)) + return NULL; + + kref_get(&record->kref); + + return record; +} + +/** + * registry_record_unref - drop a reference to a registry record + * @record: record to unreference + * + * Decreases the reference count on the record. When the reference count + * reaches zero the record will be destroyed. + */ +void registry_record_unref(struct registry_record *record) +{ + if (record) { + /* + * Keep a copy of the module owner since the record may + * disappear during the kref_put(). + */ + struct module *owner = record->owner; + + kref_put(&record->kref, registry_record_release); + module_put(owner); + } +} + +/** + * registry_add - add a record to a registry + * @registry: registry to add the record to + * @record: record to add + * + * Tries to increase the reference count of the module owning the registry. If + * successful adds the new record to the registry. + * + * Return: 0 on success or a negative error code on failure. + */ +int registry_add(struct registry *registry, struct registry_record *record) +{ + if (!try_module_get(registry->owner)) + return -ENODEV; + + mutex_lock(®istry->lock); + list_add_tail(&record->list, ®istry->list); + mutex_unlock(®istry->lock); + + return 0; +} + +/** + * registry_remove - remove a record from a registry + * @registry: registry to remove the record from + * @record: record to remove + * + * Decreases the reference count on the module owning the registry. + */ +void registry_remove(struct registry *registry, + struct registry_record *record) +{ + mutex_lock(®istry->lock); + list_del_init(&record->list); + mutex_unlock(®istry->lock); + + module_put(registry->owner); +} + +#ifdef CONFIG_OF +/** + * registry_find_by_of_node - look up an object by device node in a registry + * @registry: registry to search + * @np: device node to match on + * + * Return: A pointer to the record matching @np or NULL if no such record was + * found. + */ +struct registry_record *registry_find_by_of_node(struct registry *registry, + struct device_node *np) +{ + struct registry_record *record; + + mutex_lock(®istry->lock); + + list_for_each_entry(record, ®istry->list, list) + if (record->dev->of_node == np) + goto out; + + record = NULL; + +out: + mutex_unlock(®istry->lock); + return record; +} +#endif diff --git a/include/linux/registry.h b/include/linux/registry.h new file mode 100644 index 000000000000..a807f4124736 --- /dev/null +++ b/include/linux/registry.h @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2014, NVIDIA Corporation. All rights reserved. + * + * This file is released under the GPL v2. + */ + +#ifndef __LINUX_REGISTRY_H +#define __LINUX_REGISTRY_H + +#include +#include +#include + +struct device; +struct device_node; +struct module; + +struct registry; + +/** + * struct registry_record - registry record object + * @list: entry in registry for this record + * @owner: owner module + * @kref: reference count + * @dev: parent device + * @release: callback to destroy a record when no reference are left + */ +struct registry_record { + struct list_head list; + struct module *owner; + struct kref kref; + struct device *dev; + + void (*release)(struct registry_record *record); +}; + +void registry_record_init(struct registry_record *record); +struct registry_record *registry_record_ref(struct registry_record *record); +void registry_record_unref(struct registry_record *record); + +/** + * struct registry - generic object registry + * @list: list head of objects + * @owner: owner module + * @lock: lock for object list + */ +struct registry { + struct list_head list; + struct module *owner; + struct mutex lock; +}; + +int registry_add(struct registry *registry, struct registry_record *record); +void registry_remove(struct registry *registry, + struct registry_record *record); + +#ifdef CONFIG_OF +struct registry_record *registry_find_by_of_node(struct registry *registry, + struct device_node *np); +#endif + +#endif