From patchwork Sat Dec 24 16:55:58 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak R Varma X-Patchwork-Id: 36437 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp229587wrt; Sat, 24 Dec 2022 09:00:55 -0800 (PST) X-Google-Smtp-Source: AMrXdXsRY/NmOjvJQGez0Jiuvm7M/L6EQro4dF+c0TU9E3MhfIYRIecb5bK0Eb57NRTBSMbEubOZ X-Received: by 2002:aa7:94a1:0:b0:56c:962:fe7e with SMTP id a1-20020aa794a1000000b0056c0962fe7emr12543052pfl.32.1671901255033; Sat, 24 Dec 2022 09:00:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671901255; cv=none; d=google.com; s=arc-20160816; b=x0XWuK7IX1oweEaGQ0VnNwHcykABA7JCWHJquu5IfQdfyEHvca+E56M81CuboSWu6G hYr8F73z0Z1UFjaoMlAFEfGLXH7lXiI2iPkiIMsj7IkvB6ZG4iz1z91EP0umxGF937y2 2EYXHF/nAPB5yhfScJSKEbJfEnStdsrtY74l/5+HOQyz6axt+Mlay+jhhNrZBkiccrYH KClhTcRFVqHvXCOHB1usvIRKnpZR3QTEZTfyXc6wxx+wdRYQlHl6ti/1aUFvv8Sunefx dYBHt8DRIWoR4FDltxwXuOnZUO7rvknDfFQdpOs6OddcNRBgJ52HAtDaQowNkxfqKwp7 TTww== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-disposition:mime-version:message-id :subject:cc:to:from:date:dkim-signature; bh=yfTRUsk8SaArJjyc+SsfSrLb+xyrnS7c9LqKmKtEiqw=; b=sHjQcyxLMpd9e5Co7INopMehhc1ffGy60ekxTeG6evUPwPjbT6r+szwoF5wcfgs3bD TKH8wuKZsf/wQ+s+/R6X+Sz2FqOZGU2D+q9UrChUziIRSKa8xvODwILYS0G+ZqXBzNNv Lb16a6kfdTQa8PlAVEVBXY5/wl5vHfky3ltxn81+thNNl59cFLRWTIxxcaG5GLiGX4xH bwUjoKwWqRVK3xz6RpKXxjMC6d8ocSkl2EtEjEEdiUPohNkW0PbhZQPAnUK3Ta5uD5Pp 54P0GFTQUYRYqNINOYEcSauwMYSeLyN53X8QBO2koimsAu7orDHOhk+0Jc5uNef9NNl/ tknQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@mailo.com header.s=mailo header.b=ggGjINSn; 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=mailo.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q15-20020a056a00150f00b0054252c63d75si4744652pfu.98.2022.12.24.09.00.42; Sat, 24 Dec 2022 09:00:55 -0800 (PST) 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; dkim=fail header.i=@mailo.com header.s=mailo header.b=ggGjINSn; 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=mailo.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229598AbiLXQ4R (ORCPT + 99 others); Sat, 24 Dec 2022 11:56:17 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54058 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229688AbiLXQ4O (ORCPT ); Sat, 24 Dec 2022 11:56:14 -0500 Received: from msg-2.mailo.com (msg-2.mailo.com [213.182.54.12]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EBB162603; Sat, 24 Dec 2022 08:56:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=mailo.com; s=mailo; t=1671900963; bh=3C2JvUsacwZN7GumK7kKdnt5r4dLheWUSYF11PIXW9w=; h=X-EA-Auth:Date:From:To:Cc:Subject:Message-ID:MIME-Version: Content-Type; b=ggGjINSnf2Q/CriYnLlA/iPprvlC6WjH6Wt/FkK99/Y53G0FSxw1/ibA1aLHKTgsK UVPBKZBrXwXm/Nqau3eQH3CsD3Sn/QwvcsuAflG7swohkGXlsjCb5RPTb/iHqPyGiZ ZbhktDeh1d5ewav53hvbBeJKS+ZVgbvtxTpigp8A= Received: by b-2.in.mailobj.net [192.168.90.12] with ESMTP via ip-206.mailobj.net [213.182.55.206] Sat, 24 Dec 2022 17:56:03 +0100 (CET) X-EA-Auth: qanuojNqbxHKKlnmMrAkT89JArbMTly1Cg0TwjOlfKTX30ZqVjzUJ5YwEXDmBZyh+UamKiTd8vbOEEPYgb3JrzVBoS3zH88g Date: Sat, 24 Dec 2022 22:25:58 +0530 From: Deepak R Varma To: "Maciej W. Rozycki" , Greg Kroah-Hartman , Jiri Slaby , linux-serial@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Saurabh Singh Sengar , Praveen Kumar , Deepak R Varma Subject: [PATCH v2] tty: serial: zs: convert atomic_* to refcount_* APIs for irq_guard Message-ID: MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1753115530059534265?= X-GMAIL-MSGID: =?utf-8?q?1753115530059534265?= The refcount_* APIs are designed to address known issues with the atomic_t APIs for reference counting. They provide following distinct advantages: - protect the reference counters from overflow/underflow - avoid use-after-free errors - provide improved memory ordering guarantee schemes - neater and safer. Hence, replace the atomic_* APIs by their equivalent refcount_t API functions. This patch proposal address the following warnings generated by the atomic_as_refcounter.cocci coccinelle script atomic_add_return(-1, ...) Signed-off-by: Deepak R Varma --- Changes in v2: 1. Separate the combined change into one variable per patch as suggested by gregkh@linuxfoundation.org 2. There was additional feedback on validating the change as it appeared to modify the existing logic. However, I found that the logic does not change with the proposed refcount_* APIs used in this change. Hence that feedback is not applied in this version. Please Note: The patch is compile tested using dec_station.defconfig for MIPS architecture. drivers/tty/serial/zs.c | 14 +++++--------- drivers/tty/serial/zs.h | 2 +- 2 files changed, 6 insertions(+), 10 deletions(-) -- 2.34.1 diff --git a/drivers/tty/serial/zs.c b/drivers/tty/serial/zs.c index 730c648e32ff..6be9933eff5c 100644 --- a/drivers/tty/serial/zs.c +++ b/drivers/tty/serial/zs.c @@ -753,17 +753,15 @@ static int zs_startup(struct uart_port *uport) struct zs_port *zport = to_zport(uport); struct zs_scc *scc = zport->scc; unsigned long flags; - int irq_guard; int ret; - irq_guard = atomic_add_return(1, &scc->irq_guard); - if (irq_guard == 1) { + refcount_inc(&scc->irq_guard); + if (refcount_read(&scc->irq_guard) == 1) { ret = request_irq(zport->port.irq, zs_interrupt, IRQF_SHARED, "scc", scc); if (ret) { - atomic_add(-1, &scc->irq_guard); - printk(KERN_ERR "zs: can't get irq %d\n", - zport->port.irq); + refcount_dec(&scc->irq_guard); + printk(KERN_ERR "zs: can't get irq %d\n", zport->port.irq); return ret; } } @@ -806,7 +804,6 @@ static void zs_shutdown(struct uart_port *uport) struct zs_port *zport = to_zport(uport); struct zs_scc *scc = zport->scc; unsigned long flags; - int irq_guard; spin_lock_irqsave(&scc->zlock, flags); @@ -816,8 +813,7 @@ static void zs_shutdown(struct uart_port *uport) spin_unlock_irqrestore(&scc->zlock, flags); - irq_guard = atomic_add_return(-1, &scc->irq_guard); - if (!irq_guard) + if (refcount_dec_and_test(&scc->irq_guard)) free_irq(zport->port.irq, scc); } diff --git a/drivers/tty/serial/zs.h b/drivers/tty/serial/zs.h index 26ef8eafa1c1..bd97b73d7e16 100644 --- a/drivers/tty/serial/zs.h +++ b/drivers/tty/serial/zs.h @@ -40,7 +40,7 @@ struct zs_port { struct zs_scc { struct zs_port zport[2]; spinlock_t zlock; - atomic_t irq_guard; + refcount_t irq_guard; int initialised; };