From patchwork Fri Sep 21 13:40:08 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Srinivas Kandagatla X-Patchwork-Id: 147259 Delivered-To: patch@linaro.org Received: by 2002:a2e:1648:0:0:0:0:0 with SMTP id 8-v6csp837236ljw; Fri, 21 Sep 2018 06:40:47 -0700 (PDT) X-Google-Smtp-Source: ANB0VdZ+RExFkCN4UPj/jjY3YvIz5r4EN6SF+GGtfPdLNZVwrB7vJyOpLyvbJE2aTmEOiufpydas X-Received: by 2002:a62:23c2:: with SMTP id q63-v6mr46622708pfj.116.1537537247557; Fri, 21 Sep 2018 06:40:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537537247; cv=none; d=google.com; s=arc-20160816; b=HXlrCcfKqZUzJcCqnPrjBa6tlGgWG3m86ZdTojTJX7KfGr62bvyOmP4GgLkkS73syJ 9RUeVvmSgx3hEZA/890SZ3WMSnr2NQY8y8ZQw8QvKqbDFJx39a+MgQRSX7P1MRGAreN0 MGWNmEd3iA5eCzAzq0KW+uR2dMlJuQcHidAIXOojQVpfslT0xaMTREzZUEyCIBF6Zq+z ZELa5UBFkYiVRGCECB68PuUWgKIU3dIS7cw5jr2N7YgqJCsgJ3QAeobbNKOAWy2pKreH 7i4w4Jl6VJo5tnS2aTHzCnJCMQk8CUwzyzSxHRBpw4Yx0ONhZSY2i56HZAQA/qimcatW A38g== 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=uyo2v8Q81f5j5TT9ArxjR0qlqnA7vKY1hVWSAJE87jI=; b=LZ5xE1oOh4wk4H/8p3j9rIvbtJip9wcvV1OPsvMUIjpPDXzq+L3Objj76ZbAYlBcj9 OnkD8kvqAQYvy8veE8XWuisDlPEosYs6I/P3kqsYOTh1AOyQ5SQiyI628QD90Y/c77o3 9gxZSCHVGii6aHQQDlR/EtJSQUJcu6HmOtkpbmPABIB/x42GmRfhv/C8HeMY2JrTcMOC kJnNplP4sThQNvpGLiw7KQ9SBBgwTGkgkqWKzfLtC075p0qzIoGS6fku2TCg0Q/NAbuz nXcAfHdn1hXWVTbJz/TTfZ5zwrN0dhbnedtxBVaIoJLAZaREsVx3BVsGystxWYHZl6qy il9Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="D/33HqbK"; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b61-v6si29217419plc.499.2018.09.21.06.40.47; Fri, 21 Sep 2018 06:40:47 -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=@linaro.org header.s=google header.b="D/33HqbK"; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2390195AbeIUT3k (ORCPT + 32 others); Fri, 21 Sep 2018 15:29:40 -0400 Received: from mail-io1-f68.google.com ([209.85.166.68]:43764 "EHLO mail-io1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2390137AbeIUT3j (ORCPT ); Fri, 21 Sep 2018 15:29:39 -0400 Received: by mail-io1-f68.google.com with SMTP id y10-v6so12199787ioa.10 for ; Fri, 21 Sep 2018 06:40:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=uyo2v8Q81f5j5TT9ArxjR0qlqnA7vKY1hVWSAJE87jI=; b=D/33HqbKd5pZx/nzfIl24UnrbAa8X8/e+b/fwg+pmCIuMmmvEbwz01sdS14gk18Fxm 94mvUlkVuEPObGVtHy8PdS9+4rBDpY/H5QOuPm16bjYJCREmbaX0RIIWoAhg6IvXWQfj zB5R8Bw8Z13dz3FO3+/MjUksjjmmaatnDVRTU= 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=uyo2v8Q81f5j5TT9ArxjR0qlqnA7vKY1hVWSAJE87jI=; b=U+x2FNH8AUFS5KEvSpmLd2zbmoYUTgo6K5IZEkZP9+aaCw1baZB85v+O6u1K+WyLyu 4zzC8RFz0CM032q8UwAhEW0SlB2auTqfrWww7jjs11AgCOTCrWlg4gKpmLYBvKgpkpnG 2wNmOER/T7wVppBN6hTiMVsNxkfoLFR8s5FyVHTov851SV73rSiiApwoQDhGfDDD5mHi WzOOASNd7zpyNqH0xgF9aksQs0VpD5hdvsVMLNnpEqFLkY+S5zZ9yf5Dngw9CIof9UOB yvDj4tlpDO7b+LbKqbIb+kykatvIt719EhmsVf+34CIG685hBqcmoFuoIyt4Hxizon0D Bc3g== X-Gm-Message-State: APzg51BukIVFPA0rd4Y5ELvG6tBX9RCRAJoWMbvXGZPRRfeUv2Mo08QC 3Qj1jJwIr2x/MN+K1ZVYhVw40g== X-Received: by 2002:a02:c7d1:: with SMTP id s17-v6mr39843839jao.27.1537537243739; Fri, 21 Sep 2018 06:40:43 -0700 (PDT) Received: from localhost.localdomain ([209.82.80.116]) by smtp.gmail.com with ESMTPSA id c12-v6sm9896298ioq.81.2018.09.21.06.40.42 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 21 Sep 2018 06:40:43 -0700 (PDT) From: srinivas.kandagatla@linaro.org To: gregkh@linuxfoundation.org Cc: linux-kernel@vger.kernel.org, Bartosz Golaszewski , Srinivas Kandagatla Subject: [PATCH 08/26] nvmem: use kref Date: Fri, 21 Sep 2018 06:40:08 -0700 Message-Id: <20180921134026.4596-9-srinivas.kandagatla@linaro.org> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20180921134026.4596-1-srinivas.kandagatla@linaro.org> References: <20180921134026.4596-1-srinivas.kandagatla@linaro.org> 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 Signed-off-by: Srinivas Kandagatla --- drivers/nvmem/core.c | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) -- 2.19.0 diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c index 0ef84441ebe5..920b56e500cc 100644 --- a/drivers/nvmem/core.c +++ b/drivers/nvmem/core.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -31,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; @@ -463,6 +464,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) @@ -532,6 +535,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 * @@ -541,19 +558,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; } @@ -647,7 +652,6 @@ static struct nvmem_device *__nvmem_device_get(struct device_node *np, } } - nvmem->users++; mutex_unlock(&nvmem_mutex); if (!try_module_get(nvmem->owner)) { @@ -655,22 +659,18 @@ static struct nvmem_device *__nvmem_device_get(struct device_node *np, "could not increase module refcount for cell %s\n", nvmem_dev_name(nvmem)); - 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)