Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp1605268imm; Fri, 7 Sep 2018 03:12:11 -0700 (PDT) X-Google-Smtp-Source: ANB0VdbR4cm/YUt5LA5vj5pTlDkYWDk2kJO/VKiMflXx6ZeSk7Nq7RwavSDRKxZShsM+IEDDEseR X-Received: by 2002:a62:9bc9:: with SMTP id e70-v6mr7517083pfk.95.1536315131348; Fri, 07 Sep 2018 03:12:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536315131; cv=none; d=google.com; s=arc-20160816; b=Mlfm8gVGisZYUcpvdMBJh6zSdNv+GDdIyXyVG7KX0Z9I1UaZiXD8JNmqn+1zOdIJP9 MLTdgBxPMhbq9JmaSa8KVxPTo0YjbazdC8tFyTFRK+080AGWsTron8ZhKaKjlfSqoebP OZNKaOl2WbVB1Ceww4ySsL65taVzPfuXzqqtaX2D0Y4gxnRAf/WM/HJr8/wOhq9/cr63 ifE3haJf78y0mmfLRdsGQDNQdVsLms6LZYiP37NVRZle2DJRmQx2NrlTd7ZkfZhOitTA 4F9II/nlcWKRjQN+TSAvMZ7WdzJGlZHABES9mWIhWhVUKA5Lm9Y1A5CNT5gupq/CBYPu hkFQ== 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=AjjZb5tmBW6Cts7RgRu5LrEB05zfmvQVFT1H4KbDXG3SIRoyii7M8mN9puP2Y0TrvH tHEsgRcR+woEIaZ2Tuxf7PM0/26V615rHoiu05yv1ini084VBk1uauh9COayuJUBvgXx G8+F054AKDXancZ0gMH1/9tGT8T7OyD9aS5V69UVjRcTOfIygY6ZZFvAg+Vh+8bo3Frv LRhco7l/geYYBRg7+966Sus5EOEum0uqftoK76ZBYcTFGL+uO100jMoIl1wTsid8SLZC TNwddtMk7Hr8SV1435mZ4ZozIvwPznpf1zbKxYAO+3brCQ8y4Q0EDnObRAPgC5H6JpGU l/XQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bgdev-pl.20150623.gappssmtp.com header.s=20150623 header.b="q3di/OyP"; 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 e14-v6si7469878pfi.184.2018.09.07.03.11.56; Fri, 07 Sep 2018 03:12:11 -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="q3di/OyP"; 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 S1728646AbeIGOuA (ORCPT + 99 others); Fri, 7 Sep 2018 10:50:00 -0400 Received: from mail-wm0-f67.google.com ([74.125.82.67]:54128 "EHLO mail-wm0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728374AbeIGOsg (ORCPT ); Fri, 7 Sep 2018 10:48:36 -0400 Received: by mail-wm0-f67.google.com with SMTP id b19-v6so14192932wme.3 for ; Fri, 07 Sep 2018 03:08:23 -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=q3di/OyP3e0iEgHzrcPelNzofxqAHF64UVZUOTbHtf66zcgxeHt6qPKw4TvZgpHKaX B9FmyOPR9YAYzWENUgVX7InfVV/gf2FIjlfT/Z/M5knDhjCNkpv5yDcQLTVJ5VwyUc4Y SFxyKi5Yvfisw/v0eAvMbNM5tuMinzWKON8sFYENlC6M8U+4LM8nJ6oLfllVZ1Ea0mVX u5X4wg6rwGpXFKOT+f76+GNeFePJKB6tKzOJ8vPOPlzFkNAO4Ncq8AskHwuhlQ4DlAPW DGIaIUP+TOhBdRzSe0aKCRrNggqdQPT9H73vw/LeenBiQ0SpP9ajsoCwpcoa2I0BEsX1 Moqg== 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=hUKaR+Xbr4JPJdPtTIKf/HSx8aB+cKIX0OO69RJMp2No8V2J0NaXkSwKh3aYtV04tB HprPtHJJLEiSyuNCEy3qET5BtMwIVgb/rfAge3joT/1XXETY0CxR4wgUMzyDUNUPBrgj pvFoSQo5yDF0xUIMp+C1cMAzwPcL7skwSkaAouwpIjsPmkzpgS0FWh/qw8wsm9wRSjg5 Uvem71qFAdk0P4DYUkGr0w36g4Km3Eq0e85DWyXJWH3LVQA5W5xYARFuGWo76P0+k8si H/p9CEblJnc4XN1hvzD7KpP6hHtcQSNEPxXxxU/WDrPChhozAP+vFdkED+VxExo3oUux +61A== X-Gm-Message-State: APzg51C/j05anjXiDRQZAYMvIbVEKsGretm0RLMxjudUxiLhEaW6wavV p8cSs5+MogNkPGlpsPMfY3hYUg== X-Received: by 2002:a1c:9f41:: with SMTP id i62-v6mr4368466wme.87.1536314902557; Fri, 07 Sep 2018 03:08:22 -0700 (PDT) Received: from brgl-bgdev.baylibre.local (AStLambert-681-1-83-232.w90-86.abo.wanadoo.fr. [90.86.25.232]) by smtp.gmail.com with ESMTPSA id 72-v6sm6631619wrb.48.2018.09.07.03.08.21 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 07 Sep 2018 03:08:21 -0700 (PDT) From: Bartosz Golaszewski To: Srinivas Kandagatla , "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 , Chen-Yu Tsai Cc: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Bartosz Golaszewski Subject: [PATCH v2 03/16] nvmem: use kref Date: Fri, 7 Sep 2018 12:07:37 +0200 Message-Id: <20180907100750.14564-4-brgl@bgdev.pl> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180907100750.14564-1-brgl@bgdev.pl> References: <20180907100750.14564-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