Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp1105539pxu; Wed, 6 Jan 2021 12:24:13 -0800 (PST) X-Google-Smtp-Source: ABdhPJxX9GI5DIQIJOHt8nru0JKBRDSFs72k2DVVdMH56C/DAOdCHLxUBFNkR4xrgu4ICZrmjiWD X-Received: by 2002:a17:906:c83b:: with SMTP id dd27mr4158791ejb.356.1609964653386; Wed, 06 Jan 2021 12:24:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1609964653; cv=none; d=google.com; s=arc-20160816; b=je479X6UrJbVG4UVFGHTUm2g6D0dEt7QMlseFvnTTnr5Bt3oSXBfO7l2JjMtkL6ERl LCSQ3/1rl9Dpz0EH/RKBzMEeYi6jETRzxLhHT3FDysZJdfcYPwcAB0NgzpGVZIPIOF3p 2XaLT3TbjSBe+seEq/Q7CBQ/1w2BmO2ZgSwt+qb/EahdXkbqNWDA8G8ZDN7ow/LS+/+A 1jKXYS0F9VCnoiBlvwfSy8YCIooe9HAX3d1A/v0euHkIPxydetOCqk1/t4KIUBYQ1mG1 60MT5wZXg0+2P4G07gUBljGKICdVsrVWpPp1bANiKOTgWN0lHn2avMER5MWWUOGHbAGA 0h7A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from; bh=MxUVrl1OpSPzXf98p4QIfrKq9OcwMkG/vklluTzz2bY=; b=RxKZvNnM4nki0WqMfSCUR9X3QVKngqPjp4hPpPOwya4javmjgDXlmctBlUVblLjytd MMo0lbeB4ov4s/Vg1jp+4QaZSogD+OqX3DuI64ybM/6/85JTAJujmfEyq5Z9lijP69eR lIWh6E+taSHxMwyzzWWpt2vq7E0Gb/8lvSVCGh+v4y2jChgR/Uc2BmNg3xlfxfrT9D+7 fSRbhzBpfpQv0jdkSufp/HLhh5z3eRfSLxaaNNv6QWW4JkdEaY0T6CHbh8cGSdANdHlD lbdQw+NrOyPy4h9b1+V2UpfQ7u8CgZGGfww5oTuXBd1J5IRh+IbYI0bB/U0HzOMHsE9i b19g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v25si1300036edt.63.2021.01.06.12.23.50; Wed, 06 Jan 2021 12:24:13 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727398AbhAFUSW (ORCPT + 99 others); Wed, 6 Jan 2021 15:18:22 -0500 Received: from foss.arm.com ([217.140.110.172]:46938 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727394AbhAFUSW (ORCPT ); Wed, 6 Jan 2021 15:18:22 -0500 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 5777C1396; Wed, 6 Jan 2021 12:17:03 -0800 (PST) Received: from e120937-lin.home (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 183D53F719; Wed, 6 Jan 2021 12:17:00 -0800 (PST) From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org Cc: sudeep.holla@arm.com, lukasz.luba@arm.com, james.quinlan@broadcom.com, Jonathan.Cameron@Huawei.com, f.fainelli@gmail.com, etienne.carriere@linaro.org, thara.gopinath@linaro.org, vincent.guittot@linaro.org, souvik.chakravarty@arm.com, cristian.marussi@arm.com Subject: [PATCH v4 05/37] firmware: arm_scmi: make notifications aware of protocols users Date: Wed, 6 Jan 2021 20:15:38 +0000 Message-Id: <20210106201610.26538-6-cristian.marussi@arm.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210106201610.26538-1-cristian.marussi@arm.com> References: <20210106201610.26538-1-cristian.marussi@arm.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Account for any active registered notifier against the proper related protocol; do not consider pending event handlers, only active handlers will concur to protocol usage accounting. Signed-off-by: Cristian Marussi --- drivers/firmware/arm_scmi/notify.c | 51 ++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 7 deletions(-) diff --git a/drivers/firmware/arm_scmi/notify.c b/drivers/firmware/arm_scmi/notify.c index 66196b293b6c..cdaf0f962d84 100644 --- a/drivers/firmware/arm_scmi/notify.c +++ b/drivers/firmware/arm_scmi/notify.c @@ -91,6 +91,7 @@ #include #include +#include "common.h" #include "notify.h" #define SCMI_MAX_PROTO 256 @@ -368,7 +369,7 @@ static struct scmi_event_handler * scmi_get_active_handler(struct scmi_notify_instance *ni, u32 evt_key); static void scmi_put_active_handler(struct scmi_notify_instance *ni, struct scmi_event_handler *hndl); -static void scmi_put_handler_unlocked(struct scmi_notify_instance *ni, +static bool scmi_put_handler_unlocked(struct scmi_notify_instance *ni, struct scmi_event_handler *hndl); /** @@ -900,9 +901,21 @@ static inline int scmi_bind_event_handler(struct scmi_notify_instance *ni, if (!r_evt) return -EINVAL; - /* Remove from pending and insert into registered */ + /* + * Remove from pending and insert into registered while getting hold + * of protocol instance. + */ hash_del(&hndl->hash); + /* + * Acquire protocols only for NON pending handlers, so as NOT to trigger + * protocol initialization when a notifier is registered against a still + * not registered protocol, since it would make little sense to force init + * protocols for which still no SCMI driver user exists: they wouldn't + * emit any event anyway till some SCMI driver starts using it. + */ + scmi_acquire_protocol(ni->handle, KEY_XTRACT_PROTO_ID(hndl->key)); hndl->r_evt = r_evt; + mutex_lock(&r_evt->proto->registered_mtx); hash_add(r_evt->proto->registered_events_handlers, &hndl->hash, hndl->key); @@ -1193,41 +1206,65 @@ static int scmi_disable_events(struct scmi_event_handler *hndl) * * unregister and free the handler itself * * Context: Assumes all the proper locking has been managed by the caller. + * + * Return: True if handler was freed (users dropped to zero) */ -static void scmi_put_handler_unlocked(struct scmi_notify_instance *ni, +static bool scmi_put_handler_unlocked(struct scmi_notify_instance *ni, struct scmi_event_handler *hndl) { + bool freed = false; + if (refcount_dec_and_test(&hndl->users)) { if (!IS_HNDL_PENDING(hndl)) scmi_disable_events(hndl); scmi_free_event_handler(hndl); + freed = true; } + + return freed; } static void scmi_put_handler(struct scmi_notify_instance *ni, struct scmi_event_handler *hndl) { + bool freed; + u8 protocol_id; struct scmi_registered_event *r_evt = hndl->r_evt; mutex_lock(&ni->pending_mtx); - if (r_evt) + if (r_evt) { + protocol_id = r_evt->proto->id; mutex_lock(&r_evt->proto->registered_mtx); + } - scmi_put_handler_unlocked(ni, hndl); + freed = scmi_put_handler_unlocked(ni, hndl); - if (r_evt) + if (r_evt) { mutex_unlock(&r_evt->proto->registered_mtx); + /* + * Only registered handler acquired protocol; must be here + * released only AFTER unlocking registered_mtx, since + * releasing a protocol can trigger its de-initialization + * (ie. including r_evt and registered_mtx) + */ + if (freed) + scmi_release_protocol(ni->handle, protocol_id); + } mutex_unlock(&ni->pending_mtx); } static void scmi_put_active_handler(struct scmi_notify_instance *ni, struct scmi_event_handler *hndl) { + bool freed; struct scmi_registered_event *r_evt = hndl->r_evt; + u8 protocol_id = r_evt->proto->id; mutex_lock(&r_evt->proto->registered_mtx); - scmi_put_handler_unlocked(ni, hndl); + freed = scmi_put_handler_unlocked(ni, hndl); mutex_unlock(&r_evt->proto->registered_mtx); + if (freed) + scmi_release_protocol(ni->handle, protocol_id); } /** -- 2.17.1