From patchwork Fri Sep 14 14:39:55 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 10600859 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 01A4814BD for ; Fri, 14 Sep 2018 14:43:40 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D52742B826 for ; Fri, 14 Sep 2018 14:43:39 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C69F92B890; Fri, 14 Sep 2018 14:43:39 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 494612B826 for ; Fri, 14 Sep 2018 14:43:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=JWo4b7LYDzLmm1Qo5AeqgCBh/roaIOS0AzFW2ljPXz4=; b=fMb+Cj3yIM1kJ93dE0fyUD5eZz k5Yp1A304S44BhrZ1+g+APgF96GUp+baR1bJOMzAEN1o5ZGiy402OnOmK/yZTGmVr+UF1fJL10U7V ZjcHGXZhujZb+KCf1vC1evBJmgoy876WG4ZlMAOR9hhiypv8ZDGDOQUCeKsI+PymcsDD2bVqc23ze IYRCt0EEbeoy0exjk9JFDQoGA0bQkbN7T4J4cpuTug4mBQvNRUKq1J8T9aQ1FD5fEjMJ7Zx3+wdi1 o5ldc1PlL6qPhe/kxnSBUCDzrb5/DI3mvNxlRoOBQrviS0wm/YDd5GXp9XEY7chaSv+wB2qOG034j 1udcjY8w==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1g0pJb-00021H-2e; Fri, 14 Sep 2018 14:43:23 +0000 Received: from mail-wr1-x442.google.com ([2a00:1450:4864:20::442]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1g0pGw-0000qx-50 for linux-arm-kernel@lists.infradead.org; Fri, 14 Sep 2018 14:40:40 +0000 Received: by mail-wr1-x442.google.com with SMTP id z96-v6so10882290wrb.8 for ; Fri, 14 Sep 2018 07:40:32 -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=ItU0oQ7oWgVAhdtVOsH1jZ2YSN2mvMhQOChLXBHLTVc=; b=x6jtNJmKVqd0wEpnwBYoZV8FBftk3EGFrPNFcDlF1XqWp/Ki77ks9xkLw4wPj1RBY8 0oPEswhGGAPv5BSYYJ67w8jl1f+eSQ/Nke/3cP2cLq2RUYUo7gKz3X26EB8OXUL1l39R R95dn0z/Zq7K8cWMU1h/IDWRl0i2k9F0da4mRjJx8Yp0tjflrAYjRZ27TOw6LIYSacZP nYh31cL/uy88pPKQB/pdkRx/OJPOKrPnUeGGxv0CZmZNc1MQE79X2e10WgfLGlL4lgGF RlDaxATYb8ETHkRvmQ9j7AxMos0OgG70z9SznH3cZsveMO7Mr+YeKSa4Z//jEZt9aOMn 4v9Q== 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=ItU0oQ7oWgVAhdtVOsH1jZ2YSN2mvMhQOChLXBHLTVc=; b=GeRVfy1NYOTEN3afdqLniwSlK2ZENesRi5YHoVCc6Nn/UvsjtMDuOvtgHwCtkuu9hM r8he8ZpFnBtZQHoZj1gFmVK+nzWmpB1lnA+ga6Y+jws19DL7pfiWfIkUHgZVox2Rbu7E huxU3D2hpbiZw6O/h0mBXEjmxdJ6PN14cMywNvE1uUAEWxc1nJlHKCl6AclVqf3x4ZXH xQhQUJlTFHTKp9I/ZSYy9VswDTJ+istl1zP5KoW4QbStCAbQ8Nyu28d6OT+3vn0s/NfQ 9vueSG/CdKnAXL41hnaRl53qW18Uleil6jSp3IJUxDe5hUUpa+1kgK6IuNgPtlQbUwy0 Bh2A== X-Gm-Message-State: APzg51DgMvhbQpg7W9o7D3lpQosQaya80zOnwdpJm81vYC+E2EpcZWNw xACoAurt1hrzYeUBvqstunaQlQ== X-Google-Smtp-Source: ANB0VdblATkKapARBX1WtsqhnW6GY0ysrGw8SesGgUpQeDdRDQTQMV4ViGuxsSz/4WGZQVyR03xqeA== X-Received: by 2002:adf:fc83:: with SMTP id g3-v6mr9627462wrr.216.1536936031633; Fri, 14 Sep 2018 07:40:31 -0700 (PDT) Received: from brgl-bgdev.home ([2a01:cb1d:af:5b00:e837:b8d5:48c1:571b]) by smtp.gmail.com with ESMTPSA id v46-v6sm10313526wrc.63.2018.09.14.07.40.30 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 14 Sep 2018 07:40:31 -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 Subject: [PATCH v4 06/22] nvmem: use kref Date: Fri, 14 Sep 2018 16:39:55 +0200 Message-Id: <20180914144011.27614-7-brgl@bgdev.pl> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180914144011.27614-1-brgl@bgdev.pl> References: <20180914144011.27614-1-brgl@bgdev.pl> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20180914_074038_203181_61CBFB80 X-CRM114-Status: GOOD ( 17.22 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Bartosz Golaszewski , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-doc@vger.kernel.org MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP 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 | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c index ffed8fa16936..552ffedce38e 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)