From patchwork Sat Dec 24 08:17:12 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: 36396 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp64711wrt; Sat, 24 Dec 2022 00:21:09 -0800 (PST) X-Google-Smtp-Source: AMrXdXvJvXsnFa9L4k9SPPag6OgNDW6Fv0d0P4w1/4yAgLDQT6QNiSM0s5WU/fUHjbcRgLfwlNsu X-Received: by 2002:a05:6402:380b:b0:480:f01b:a385 with SMTP id es11-20020a056402380b00b00480f01ba385mr5714484edb.4.1671870069157; Sat, 24 Dec 2022 00:21:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671870069; cv=none; d=google.com; s=arc-20160816; b=JOMBw6L+cbKpumwBpVYZFxH0kQwd7KUqDip9tKUcYAT7vZ6vxtgmgXU4FXq7gbGm0s 0EVIL8T4eOc+dkU29PCFR33iBZ3Yzkhd9TW6BbO1WINvCfbG3dpifu/QgX/VTq+qFygn qkjhYWP4SISpMa7vjjm++v8e5qr7n2bHDK6yYY9mWYE4tUtlmiogGZki3qp2XX7Gmsk2 l1v+qxQaTI5/7ceWR8PI6H3AhfV9m91K4BsONolps/lIlqqf9HPvGJi1B7fzjZktqwZ1 mvO+9f4yrFErVdqYBNlZG1v2pzIJcXx6cdgSYECzsmBgWdrPBcOMl8hvcfZJf+DcRStX x/FA== 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=vIEhrassCsVPoOEjasBdGMLNWL6p2I+XfVzurbSk3jM=; b=N7RGkGyTUCg15P+OF3by7nD1LIoR1bwNfQ4zwHeKAMEDiyZ/6cYv2ZrWtWtfs17pPc rcr/wKvRzi3CYUvebO2ySD6jxvH2jgaO/cgcAeo5mqgzPViB+vl0LpbFT9+65ej3Qj7/ /KU2tnVMI6RhGcS1vmimaxthIjDQEooEc3c9Db4vf+RdoKsYqBheo8gVxxgeKTQtSlOk 6yYVvXWEEqOsX+K5hp2ZJgA32ouC/urxJZljSmAMKpwIX+RiChWs/7wfCA74xo4vGtjN tOMJALhodhNnSy3miA5zUUgmzl41ZJfbijq4GsCCwPPS9ztt6XaGfjFCbXW0bZooYVRv P07w== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@mailo.com header.s=mailo header.b="DP/vkiJc"; 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 dd16-20020a1709069b9000b008366ae33ec2si4514659ejc.595.2022.12.24.00.20.45; Sat, 24 Dec 2022 00:21:09 -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="DP/vkiJc"; 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 S230175AbiLXIRb (ORCPT + 99 others); Sat, 24 Dec 2022 03:17:31 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57640 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229650AbiLXIR3 (ORCPT ); Sat, 24 Dec 2022 03:17:29 -0500 Received: from msg-1.mailo.com (msg-1.mailo.com [213.182.54.11]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 99A48DFA2; Sat, 24 Dec 2022 00:17:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=mailo.com; s=mailo; t=1671869837; bh=zGGbbuCFDi0fJZzEl65qs35uUNliyt5H98E+AQuRzag=; h=X-EA-Auth:Date:From:To:Cc:Subject:Message-ID:MIME-Version: Content-Type; b=DP/vkiJcSd3PEEg4xqskXiTahRViQ5P31ekegAAxCjs8dmmtAFWZcgKt/Wglp7Zlp QeS+N88LUL1FHh9X3D7sGKeoGWTstOR5P1yCvZxtyotnkEX9eA3C3kCx7kTnrKOekI lOGgWSwuAjkmkMIzZysPmMhON7k56VI0wbg2i8No= Received: by b-1.in.mailobj.net [192.168.90.11] with ESMTP via ip-206.mailobj.net [213.182.55.206] Sat, 24 Dec 2022 09:17:17 +0100 (CET) X-EA-Auth: AEXDKs6VnpWAu5Q/NjyGfE/fHcrNKz18IDs33rsfJowVHSNknlD1UBEkvSnQchuJuGLbhSZu2gpwXZOXrGnOapzGsmtiYOie Date: Sat, 24 Dec 2022 13:47:12 +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: dz: 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?1753082829668664839?= X-GMAIL-MSGID: =?utf-8?q?1753082829668664839?= 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/dz.c | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) -- 2.34.1 diff --git a/drivers/tty/serial/dz.c b/drivers/tty/serial/dz.c index b70edc248f8b..0aa59a9beeb7 100644 --- a/drivers/tty/serial/dz.c +++ b/drivers/tty/serial/dz.c @@ -46,7 +46,6 @@ #include #include -#include #include #include #include @@ -77,7 +76,7 @@ struct dz_port { struct dz_mux { struct dz_port dport[DZ_NB_PORT]; refcount_t map_guard; - atomic_t irq_guard; + refcount_t irq_guard; int initialised; }; @@ -400,18 +399,16 @@ static int dz_startup(struct uart_port *uport) struct dz_port *dport = to_dport(uport); struct dz_mux *mux = dport->mux; unsigned long flags; - int irq_guard; int ret; u16 tmp; - irq_guard = atomic_add_return(1, &mux->irq_guard); - if (irq_guard != 1) + refcount_inc(&mux->irq_guard); + if (refcount_read(&mux->irq_guard) != 1) return 0; - ret = request_irq(dport->port.irq, dz_interrupt, - IRQF_SHARED, "dz", mux); + ret = request_irq(dport->port.irq, dz_interrupt, IRQF_SHARED, "dz", mux); if (ret) { - atomic_add(-1, &mux->irq_guard); + refcount_dec(&mux->irq_guard); printk(KERN_ERR "dz: Cannot get IRQ %d!\n", dport->port.irq); return ret; } @@ -441,15 +438,13 @@ static void dz_shutdown(struct uart_port *uport) struct dz_port *dport = to_dport(uport); struct dz_mux *mux = dport->mux; unsigned long flags; - int irq_guard; u16 tmp; spin_lock_irqsave(&dport->port.lock, flags); dz_stop_tx(&dport->port); spin_unlock_irqrestore(&dport->port.lock, flags); - irq_guard = atomic_add_return(-1, &mux->irq_guard); - if (!irq_guard) { + if (refcount_dec_and_test(&mux->irq_guard)) { /* Disable interrupts. */ tmp = dz_in(dport, DZ_CSR); tmp &= ~(DZ_RIE | DZ_TIE);