Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp2136696rwd; Thu, 15 Jun 2023 22:56:56 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4QMiYaZ8K92NUgNUQPbdIxcZeZ9zCCeWCzoT5FgzZv186Bt/VQX8yNlWMSLsF9yMhfPETq X-Received: by 2002:a17:903:41c6:b0:1b5:21b0:1490 with SMTP id u6-20020a17090341c600b001b521b01490mr1356787ple.29.1686895015857; Thu, 15 Jun 2023 22:56:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686895015; cv=none; d=google.com; s=arc-20160816; b=yi8i1EOi0nFhJgym9BM4jxiwnwMX0KhDFD7/Z+0HDpr0FsDpbBBoS1sSWTyWHCxKOc 9pjeejwOCXxttzvBnBAXPghVR3uwj8GQ11H6WFHTryYc1ofKwjMkFgMqCNM1TFcNr64S X+HUEMLutXMjxb68ex8y2Mvw9MXxIcVxraaOgKYsJbSTWoMj6lSR7qqx8dk0ltNmWfC4 vCl1rOULHXi3oiyAeLtduD0rM3Lh2FDmwQuiZy4nJLwPA4ZtKJO6mOJ76l8amoX5Vq1m PY+whVfF9vmMDGd801kOMQpYOtsnE9isxR2ekxwPxs9/9R1TzlFt9fvRGYKFVC6pa5wU v8Zw== 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=m16fwRA5LS14WbJbN8VvCiAHKgYphZ4NVrw2FOliAmM=; b=rw1geXI+opPlFnnVWKqXJqnoJVPuKdzYtmRShxduOJLZQRVO+Bi/cCtp+mRRDDPVQW 7mrvaR10Ew+AEfdJXZasSqYY5JpK1sv4Ihb0JsSplOCmV1TA2th1Rlv9c5Gz7bcAwslS a3SHL06WAbNnOOtQOeVLI0dgoL7OdCAiA9XYNtRmz7tMoBmjUzeuIzymsYPaaTP6ECkW +iIqdqASDMy/bj+rmHlrKJI/x5cTRwHkke2HrJkGxsdJmU8hx2QzyjC/EAtDl/EOS7K5 IICgON5r3HHkpjw5JiodzIEoPykCpva8EZCc1q8ziBn32dJemmKpfdRtLpHxBF4c/GGx PRDw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=nxp.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f17-20020a170902f39100b001b2439e8befsi10137383ple.472.2023.06.15.22.56.43; Thu, 15 Jun 2023 22:56:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=nxp.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241493AbjFPFxT (ORCPT + 99 others); Fri, 16 Jun 2023 01:53:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41404 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241250AbjFPFxG (ORCPT ); Fri, 16 Jun 2023 01:53:06 -0400 Received: from inva020.nxp.com (inva020.nxp.com [92.121.34.13]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 703E82D59 for ; Thu, 15 Jun 2023 22:53:03 -0700 (PDT) Received: from inva020.nxp.com (localhost [127.0.0.1]) by inva020.eu-rdc02.nxp.com (Postfix) with ESMTP id 8532E1A068F; Fri, 16 Jun 2023 07:43:09 +0200 (CEST) Received: from aprdc01srsp001v.ap-rdc01.nxp.com (aprdc01srsp001v.ap-rdc01.nxp.com [165.114.16.16]) by inva020.eu-rdc02.nxp.com (Postfix) with ESMTP id 234031A249B; Fri, 16 Jun 2023 07:43:09 +0200 (CEST) Received: from localhost.localdomain (mega.ap.freescale.net [10.192.208.232]) by aprdc01srsp001v.ap-rdc01.nxp.com (Postfix) with ESMTP id BC8CF181D0C0; Fri, 16 Jun 2023 13:43:07 +0800 (+08) From: Alison Wang To: gregkh@linuxfoundation.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org Cc: leoyang.li@nxp.com, xuelin.shi@nxp.com, xiaofeng.ren@nxp.com, feng.guo@nxp.com, Alison Wang Subject: [PATCH 8/8] ethosu: Add rwlock when alloc and remove msg id Date: Fri, 16 Jun 2023 13:59:13 +0800 Message-Id: <20230616055913.2360-9-alison.wang@nxp.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230616055913.2360-1-alison.wang@nxp.com> References: <20230616055913.2360-1-alison.wang@nxp.com> X-Virus-Scanned: ClamAV using ClamSMTP X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The msg id will be removed unpredictable when one thread calls ethosu_rpmsg_register and another thread calls ethosu_rpmsg_deregister at the same time. This patch adds write_lock in ethosu_rpmsg_register/ethosu_rpmsg_deregister and adds read_lcok in ethosu_rpmsg_find. Signed-off-by: Feng Guo Reviewed-by: Peng Fan --- drivers/firmware/ethosu/ethosu_driver.c | 2 +- drivers/firmware/ethosu/ethosu_rpmsg.c | 20 ++++++++++++++------ drivers/firmware/ethosu/ethosu_rpmsg.h | 1 + 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/drivers/firmware/ethosu/ethosu_driver.c b/drivers/firmware/ethosu/ethosu_driver.c index 59ae79256564..0742564cfaf2 100644 --- a/drivers/firmware/ethosu/ethosu_driver.c +++ b/drivers/firmware/ethosu/ethosu_driver.c @@ -155,7 +155,7 @@ static int __init ethosu_init(void) { int ret; - ethosu_class = class_create(THIS_MODULE, ETHOSU_DRIVER_NAME); + ethosu_class = class_create(ETHOSU_DRIVER_NAME); if (IS_ERR(ethosu_class)) { printk("Failed to create class '%s'.\n", ETHOSU_DRIVER_NAME); diff --git a/drivers/firmware/ethosu/ethosu_rpmsg.c b/drivers/firmware/ethosu/ethosu_rpmsg.c index 351a1046e65e..d3e597e82762 100644 --- a/drivers/firmware/ethosu/ethosu_rpmsg.c +++ b/drivers/firmware/ethosu/ethosu_rpmsg.c @@ -35,7 +35,9 @@ static void ethosu_core_set_capacity(struct ethosu_buffer *buf, int ethosu_rpmsg_register(struct ethosu_rpmsg *erp, struct ethosu_rpmsg_msg *msg) { + write_lock(&erp->lock); msg->id = idr_alloc_cyclic(&erp->msg_idr, msg, 0, INT_MAX, GFP_KERNEL); + write_unlock(&erp->lock); if (msg->id < 0) return msg->id; @@ -45,14 +47,19 @@ int ethosu_rpmsg_register(struct ethosu_rpmsg *erp, void ethosu_rpmsg_deregister(struct ethosu_rpmsg *erp, struct ethosu_rpmsg_msg *msg) { + write_lock(&erp->lock); idr_remove(&erp->msg_idr, msg->id); + write_unlock(&erp->lock); } struct ethosu_rpmsg_msg *ethosu_rpmsg_find(struct ethosu_rpmsg *erp, int msg_id) { - struct ethosu_rpmsg_msg *ptr = - (struct ethosu_rpmsg_msg *)idr_find(&erp->msg_idr, msg_id); + struct ethosu_rpmsg_msg *ptr; + + read_lock(&erp->lock); + ptr = (struct ethosu_rpmsg_msg *)idr_find(&erp->msg_idr, msg_id); + read_unlock(&erp->lock); if (!ptr) return ERR_PTR(-EINVAL); @@ -97,8 +104,8 @@ static int ethosu_rpmsg_send(struct ethosu_rpmsg *erp, uint32_t type) msg.type = type; msg.length = 0; - print_hex_dump(KERN_DEBUG, __func__, DUMP_PREFIX_NONE, 16, 1, - (void *)&msg, sizeof(msg), true); + print_hex_dump_debug(__func__, DUMP_PREFIX_NONE, 16, 1, (void *)&msg, + sizeof(msg), true); ret = rpmsg_send(rpdev->ept, (void *)&msg, sizeof(msg)); if (ret) { @@ -337,8 +344,8 @@ static int rpmsg_ethosu_cb(struct rpmsg_device *rpdev, dev_dbg(&rpdev->dev, "msg(<- src 0x%x) len %d\n", src, len); - print_hex_dump(KERN_DEBUG, __func__, DUMP_PREFIX_NONE, 16, 1, - data, len, true); + print_hex_dump_debug(__func__, DUMP_PREFIX_NONE, 16, 1, data, + len, true); rpmsg->callback(rpmsg->user_arg, data); @@ -391,6 +398,7 @@ int ethosu_rpmsg_init(struct ethosu_rpmsg *erp, erp->user_arg = user_arg; erp->ping_count = 0; idr_init(&erp->msg_idr); + rwlock_init(&erp->lock); return register_rpmsg_driver(ðosu_rpmsg_driver); } diff --git a/drivers/firmware/ethosu/ethosu_rpmsg.h b/drivers/firmware/ethosu/ethosu_rpmsg.h index 283d401a0dbf..a4b879e9ef6c 100644 --- a/drivers/firmware/ethosu/ethosu_rpmsg.h +++ b/drivers/firmware/ethosu/ethosu_rpmsg.h @@ -21,6 +21,7 @@ struct ethosu_core_msg; typedef void (*ethosu_rpmsg_cb)(void *user_arg, void *data); struct ethosu_rpmsg { + rwlock_t lock; struct rpmsg_device *rpdev; ethosu_rpmsg_cb callback; void *user_arg; -- 2.17.1