Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp3604475imm; Wed, 5 Sep 2018 02:59:50 -0700 (PDT) X-Google-Smtp-Source: ANB0VdZXTAj+QepcOZE+HkiG30nuUhnq8NNdzfNAysD24bGMiVGBtWpPv7/n/iDBqsJKNqKTA8Wx X-Received: by 2002:a17:902:1a9:: with SMTP id b38-v6mr37953932plb.89.1536141590509; Wed, 05 Sep 2018 02:59:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536141590; cv=none; d=google.com; s=arc-20160816; b=Jbu1ljg6YYS73OLiqPXK08CuZOgmWpHPyWhipAOxrACqjzIRWLdEoXHIiqzCY8V3VH PbnmcShFEydi65bW5AJCKJB70xZJqRGNbYU9fptWdbUAGYNO/mp0u7KHun2J20hhoQGv 73JJg3XoUP0cayCl0YXVaffVghBV+azFmBHLxGbQTR7C5rxmhgHnTw0QIrAoU+SCKk/h zRHrdaEhBDLvPmXjuKSha8/qsDDOMAJbvz04zcLE8EUtTOD0HAvZ8O2v9O/uYkXPgB64 lO8oLGiJaRrL3wL0k0TRS+3dvfi0tsSMJTKs6sW9AWJkkdLqhtj1x4b6u7ruq5SRWIAC qmeg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature; bh=QNr1Tad048IXHrlgjJJINGlU6RT88CBalusVA7tzIww=; b=v1+72tPCC9bPB0lwP7rIHtGKBKnRXv5WrgaUy3ZZB5E9q+ATwg70qIenoXhzt+t+XL A01J0ITPXUQGbBE8Bmtidr73L6heQ3cPy7zlM0XUr/tK9Mt/VyBzcYjnJikK7KInwYEH 2p1m+9BMkDb1N5o/cCDMBKI1H7cU1gS1fzZHvTtYcTGC/u9iRi3tQFogzSWvQ1qDKN07 SLAfyukW4XiDXgkRjmtY/tIZZPi+YxahcxPud39I48CROdo0O4wp6T4w/rVg3czyfHqs L2pXbHyKdNYpfj/9IE1wnMJkOUPylU1MEfiDrVahvMbA3X1ltvTjhC1fedWVaPg23FK4 ozuA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bgdev-pl.20150623.gappssmtp.com header.s=20150623 header.b=aAb9ZMjP; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k193-v6si1430504pge.4.2018.09.05.02.59.35; Wed, 05 Sep 2018 02:59:50 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@bgdev-pl.20150623.gappssmtp.com header.s=20150623 header.b=aAb9ZMjP; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728295AbeIEO1h (ORCPT + 99 others); Wed, 5 Sep 2018 10:27:37 -0400 Received: from mail-wr1-f68.google.com ([209.85.221.68]:33979 "EHLO mail-wr1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728140AbeIEO1g (ORCPT ); Wed, 5 Sep 2018 10:27:36 -0400 Received: by mail-wr1-f68.google.com with SMTP id g33-v6so6998758wrd.1 for ; Wed, 05 Sep 2018 02:58:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=QNr1Tad048IXHrlgjJJINGlU6RT88CBalusVA7tzIww=; b=aAb9ZMjPXwNsXi4wamX4XdPDOIusj3ue91kpAs/Yvl7vMFMc/DpREsi9h+SJMrsd3V YfmvOo3WTIjwBFYBFCcfJ6U+8NgWBm2cvk3e+YsUlwzmqUk9JgpjhUpXOo2oXh5ouWZj TDyjJg0+TDLknPFxGWSMQwYc5wASRqXn3CetBI7W3pFwjqMeMc5D6pSXlcvpNj0+yJjY 2b8dGZP4c7XON/87F1DrpPtUCrCVR0p0uXbCgoPd7Lcutj3Q5dFJ+zq4QKsEAOhlXOyW WBVsYi6YHpMvxWFeoogoqjQwi7vJAxrHZyWpbEur/l7PqXc4HySpetwdA6VPUyDG/Va3 dhvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=QNr1Tad048IXHrlgjJJINGlU6RT88CBalusVA7tzIww=; b=rR0vQs+Xz/b3cEm2Kwg+DI8vUHysONmzbTc52Bp7SiXrX+MnhbMEhgQAWamMgK6Shm 12oAPpKI0T8JEWTkh/MOBJ9t/yy2OWXkNVgPf8ppHWytQgVQLbd1k2mw6cl5DzeiPrhp v7ilWKohKCJhI/nIhWhuZAHrc/5oNjIYdaa2onvdBufHjUkfWX1MOCYZoKK6rNO/LfJp x8j0HvgIuUQem3DyRtAKBNlX2nGpgVvLoJyW5jUhd/mmUlvyZgRQfR76CFTN98hU2RNY KLccy9HmYaocV8XZqC9UwH+3pYL6j/v5umuRO/pMOdnzd5IKm0r1bEwoMyYu0tdzltSe YFgA== X-Gm-Message-State: APzg51Af0mnJ0gVN5KEya4P4hioWw0fX6ymLHv8mbLhu53RA+zPL2lfo f4PAhWPPcTO3Rx9xyp64W2RGkQ== X-Received: by 2002:a5d:404a:: with SMTP id w10-v6mr25212549wrp.147.1536141487773; Wed, 05 Sep 2018 02:58:07 -0700 (PDT) Received: from brgl-bgdev.baylibre.local (AStLambert-681-1-33-235.w90-86.abo.wanadoo.fr. [90.86.152.235]) by smtp.gmail.com with ESMTPSA id l12-v6sm2127815wrv.29.2018.09.05.02.58.06 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 05 Sep 2018 02:58:07 -0700 (PDT) From: Bartosz Golaszewski To: Srinivas Kandagatla , Joachim Eastwood , "David S . Miller" , Mauro Carvalho Chehab , Greg Kroah-Hartman , Andrew Morton , Arnd Bergmann , Jonathan Corbet , Sekhar Nori , Kevin Hilman , David Lechner , Boris Brezillon , Andrew Lunn , Alban Bedel , Maxime Ripard Cc: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Bartosz Golaszewski Subject: [PATCH 03/13] nvmem: use kref Date: Wed, 5 Sep 2018 11:57:28 +0200 Message-Id: <20180905095738.26406-4-brgl@bgdev.pl> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180905095738.26406-1-brgl@bgdev.pl> References: <20180905095738.26406-1-brgl@bgdev.pl> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Bartosz Golaszewski Use kref for reference counting. Use an approach similar to the one seen in the common clock subsystem: don't actually destroy the nvmem device until the last user puts it. This way we can get rid of the users check from nvmem_unregister(). Signed-off-by: Bartosz Golaszewski --- drivers/nvmem/core.c | 45 +++++++++++++++++++++----------------------- 1 file changed, 21 insertions(+), 24 deletions(-) diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c index 86af62e2df47..2335b72d4b3e 100644 --- a/drivers/nvmem/core.c +++ b/drivers/nvmem/core.c @@ -32,7 +32,7 @@ struct nvmem_device { int stride; int word_size; int id; - int users; + struct kref refcnt; size_t size; bool read_only; int flags; @@ -368,6 +368,8 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config) return ERR_PTR(rval); } + kref_init(&nvmem->refcnt); + nvmem->id = rval; nvmem->owner = config->owner; if (!nvmem->owner && config->dev->driver) @@ -430,6 +432,20 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config) } EXPORT_SYMBOL_GPL(nvmem_register); +static void nvmem_device_release(struct kref *kref) +{ + struct nvmem_device *nvmem; + + nvmem = container_of(kref, struct nvmem_device, refcnt); + + if (nvmem->flags & FLAG_COMPAT) + device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom); + + nvmem_device_remove_all_cells(nvmem); + device_del(&nvmem->dev); + put_device(&nvmem->dev); +} + /** * nvmem_unregister() - Unregister previously registered nvmem device * @@ -439,19 +455,7 @@ EXPORT_SYMBOL_GPL(nvmem_register); */ int nvmem_unregister(struct nvmem_device *nvmem) { - mutex_lock(&nvmem_mutex); - if (nvmem->users) { - mutex_unlock(&nvmem_mutex); - return -EBUSY; - } - mutex_unlock(&nvmem_mutex); - - if (nvmem->flags & FLAG_COMPAT) - device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom); - - nvmem_device_remove_all_cells(nvmem); - device_del(&nvmem->dev); - put_device(&nvmem->dev); + kref_put(&nvmem->refcnt, nvmem_device_release); return 0; } @@ -517,7 +521,6 @@ int devm_nvmem_unregister(struct device *dev, struct nvmem_device *nvmem) } EXPORT_SYMBOL(devm_nvmem_unregister); - static struct nvmem_device *__nvmem_device_get(struct device_node *np) { struct nvmem_device *nvmem = NULL; @@ -533,30 +536,24 @@ static struct nvmem_device *__nvmem_device_get(struct device_node *np) return ERR_PTR(-EPROBE_DEFER); } - nvmem->users++; mutex_unlock(&nvmem_mutex); if (!try_module_get(nvmem->owner)) { dev_err(&nvmem->dev, "could not increase module refcount for cell %s\n", nvmem->name); - - mutex_lock(&nvmem_mutex); - nvmem->users--; - mutex_unlock(&nvmem_mutex); - return ERR_PTR(-EINVAL); } + kref_get(&nvmem->refcnt); + return nvmem; } static void __nvmem_device_put(struct nvmem_device *nvmem) { module_put(nvmem->owner); - mutex_lock(&nvmem_mutex); - nvmem->users--; - mutex_unlock(&nvmem_mutex); + kref_put(&nvmem->refcnt, nvmem_device_release); } static struct nvmem_device *nvmem_find(const char *name) -- 2.18.0