From patchwork Fri Sep 21 07:29:05 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 10609199 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 6FB6814BD for ; Fri, 21 Sep 2018 07:30:04 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 515842DE4A for ; Fri, 21 Sep 2018 07:30:04 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 449E62DE50; Fri, 21 Sep 2018 07:30:04 +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 8E1BB2DE4A for ; Fri, 21 Sep 2018 07:30:02 +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=KAhHyESB9K3TZXTYLGXnDWjk21hvaM/s6dCyHhdbD9s=; b=sIRot4yf6FkP46F5xxvezkn9/U Hi/2Ba+HyTc/eTnpi5EszHgHTr7D81aT9eP6Exu6Yr1U+2Hqub3swjp5lreQuCEkDcfGLSo1UWOuY hF0qZRl1e89JnT0LUp3OPpnvhNkq9HNf/ofJRlTMJiX1rUUeQAl3cKrN98ZGZ4K11WHUTyF72ALM/ u65mNzXdL9EKhodL7/qzA5lVEalqkz3aNQr7/aEIpBeMNqTqiK5/plm1Ak+I4F4SFsaCITXQOMuf1 3SQc7s7G84q/B3YQOPpXUbapxsca6vJ5BQU1NgxsnOKKPEOE11hTn7woaBkPx8JX1NKhbqWKzMoF0 gfC07kAA==; 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 1g3Fsp-0006Cd-6l; Fri, 21 Sep 2018 07:29:47 +0000 Received: from mail-wm1-x344.google.com ([2a00:1450:4864:20::344]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1g3Fsh-00067Y-BX for linux-arm-kernel@lists.infradead.org; Fri, 21 Sep 2018 07:29:41 +0000 Received: by mail-wm1-x344.google.com with SMTP id 207-v6so2019447wme.5 for ; Fri, 21 Sep 2018 00:29:35 -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=qilqxEjxudzJVMWNjYeBFxyC20AgStI3tn0cY3uUaXY=; b=QXLJacYotp0D8kOLUA+bKuhvbsyrcLzt06gEKl1jD+g/f1Wku5M/X+HrlSDc5meezh io6rBgQZy7ljVZdCzk9MPu+93g20LBgJfF1P8idJFILBp96pmVPZ2f3X9fCUR/jaXoK4 tp6pJlfsSN7zV6RH3IP1uT2IsSY4qcOaSDpKrxVfDXk/x7MRussjNgVGnCkyarSb44f2 bXnHszeqAoYQY9XiXym4aRoQn6fRVzXQwYeGjvOnuRWyHRHBz5tJutx40ztUG1gH7z7A FKUHLYyP+r+5xaS/ILV5tJE4tPUxzlxAs/bcV6ayelAOGSTQjDTJpqDoPLjTDLI7Z0PE q6RA== 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=qilqxEjxudzJVMWNjYeBFxyC20AgStI3tn0cY3uUaXY=; b=gwTXWlz0NcMpD20AWAm4+YcuqgCdfzmd1ag+zfl2XJDrWFfAgxhsHL2wxKL4rTRWKV 3SDT/luM5M6VIGAdj0aaVru4NJK/fRBM/Jut8Tg8NVAUt+e5aJpeXOWG+XbSpyIS8MCR TkJHYVX/Ql5puOQEkyUy/yd3qvcnkMqaZ2DaA09zyeOMoav5Wosv7w4rkMcl1PpQapiP 0DXNcQlwELspXTuL0/WnjtsBknLgMcWXMA6fYo0uXPWSmhcnDPLPeKeqrgrjzs65cxgc 6yYlDYxupgw3ZajGzr6FiFRGF7ef0T8m5QKau1eO9GCMS1y8Pu2YTiFxYeak2pEFpEM1 6xIA== X-Gm-Message-State: APzg51Craztl1B9bjGKuTpoTShNU9fYKm2RwB+Iy8YDmG0rrev6eFVyv ruv7HhDlKB4FgjvI2btUWTUluw== X-Google-Smtp-Source: ANB0VdaTn/DLgZNz0Zxqs7v0Tm2x/PV3TIyKd8uAO0OV0kbEkFA9v26GB6gwPeIGE8RTjSPISkb59w== X-Received: by 2002:a1c:610b:: with SMTP id v11-v6mr6186846wmb.20.1537514974589; Fri, 21 Sep 2018 00:29:34 -0700 (PDT) Received: from brgl-bgdev.home ([2a01:cb1d:af:5b00:e837:b8d5:48c1:571b]) by smtp.gmail.com with ESMTPSA id d18-v6sm2841597wmb.33.2018.09.21.00.29.33 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 21 Sep 2018 00:29:33 -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 v5 06/22] nvmem: use kref Date: Fri, 21 Sep 2018 09:29:05 +0200 Message-Id: <20180921072921.19387-7-brgl@bgdev.pl> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180921072921.19387-1-brgl@bgdev.pl> References: <20180921072921.19387-1-brgl@bgdev.pl> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20180921_002939_406869_D6776042 X-CRM114-Status: GOOD ( 17.21 ) 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 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)