Received: by 2002:a05:6358:f14:b0:e5:3b68:ec04 with SMTP id b20csp6200384rwj; Wed, 21 Dec 2022 12:06:58 -0800 (PST) X-Google-Smtp-Source: AMrXdXvofa9P4Fj1dFPDdshdRkKC5O7zO7w2Nse7IB1yOQ0k8D3mjQOkjqh1MBDUKWxmwUlCs/gC X-Received: by 2002:a05:6402:174c:b0:467:8dd2:b42 with SMTP id v12-20020a056402174c00b004678dd20b42mr2363212edx.10.1671653218077; Wed, 21 Dec 2022 12:06:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671653218; cv=none; d=google.com; s=arc-20160816; b=wwlDV668dvMzPRGytMOqEJPQfw1lhj86RtggJaqZ41pOtXODyQpO68hJda4HJ4Y5Zb 7uAC509TwIDiVETnCXcVPP2G8f9YZs93h7X/45SRq+8OIm3KNCRdWY8RBerV4TkDeAqI vgGps4XPaZvVav3Cx6NA2JZjglvlEUzkXV+7tba6DaLLS56dWwj9EifeNOcaC5cW/OVb WAv7jkkCIg3vP3RIfGUjFOyYkKkiLIl6gglv/y16tsW2/P/PYdvq3LzJErnf9WJfpM3L spWKoweZh174RdSIQb++wQDUGB6gSf5vMdnHKkSX5nwxU0c33b+Mq/VuFJnSZylLopoW dFPg== 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=EQNUa9rpZslm5XXMKKVXrfeDzrg3eSUe4ThKw482uvk=; b=XGFeO20PeCNlBY697kj6EiEHQaLxV3BTE4cdhM8Ys/Y2p9OFWPolcf6bqvxbmutggO Xu/d+lJTrvLc9VNtiCfwY8deGSaLqaBojKto1KIsBkVL7aQvtOkJ9LCH/oWBKk1XQPwI wvluA+6xtLPBMkTuiP+cEuINPfHym3pgwUd9ViM1UyKScFQvWvCQ9NhTBMtefQyf9r9+ iOmOeeBXBt/43CVjIyLvTtIK6BZAr4hR2bGS59FPOP00WbbrvLBWK9b9Us7i/omgHshw QrtUHC1R8IyvQQcymCWAJDxkPWHNlUR1kIW8aXax4vTJbWRafYcH4DbCQ6YwGB/IY5/C ZBdQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@mailo.com header.s=mailo header.b=QBBgTrMG; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x11-20020a05640226cb00b00458a650f3fasi3519318edd.409.2022.12.21.12.06.42; Wed, 21 Dec 2022 12:06:58 -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=QBBgTrMG; 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 S234606AbiLUT6O (ORCPT + 67 others); Wed, 21 Dec 2022 14:58:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47804 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229791AbiLUT6K (ORCPT ); Wed, 21 Dec 2022 14:58:10 -0500 Received: from msg-1.mailo.com (msg-1.mailo.com [213.182.54.11]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1832DBE26 for ; Wed, 21 Dec 2022 11:58:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=mailo.com; s=mailo; t=1671652671; bh=nWkOMj2N6LbgMIWdXK2PUNqVACe/YQOGQRAijbpfG8U=; h=X-EA-Auth:Date:From:To:Cc:Subject:Message-ID:MIME-Version: Content-Type; b=QBBgTrMGUYzx8uaXK8tFNt6L5lDBJm2U1zfKs4oxvTop2Hn5fovA+qFDXHrg/lNRa 2AkmhKJzgrHV9p/bWJG7UVEZSmlG6QclvXY/eAR09SegukunBHQW4nLYbCA+X7OJTX bjPrVVZfhl/gIuUoPK4+K32TxC7bpaT1mpZQscwQ= Received: by b-3.in.mailobj.net [192.168.90.13] with ESMTP via ip-206.mailobj.net [213.182.55.206] Wed, 21 Dec 2022 20:57:51 +0100 (CET) X-EA-Auth: mNmn2jbSxUm86SXJ/w4EMIdyTLjohVzgPtkuIMBB19SVH0DA7nC/5Kz50Q3wR8loEedLMRaz60iyZoiefapIVjMUBPLfeEHr Date: Thu, 22 Dec 2022 01:27:40 +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 , drv@mailo.com Subject: [PATCH] tty: serial: convert atomic_* to refcount_* APIs Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii 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 The refcount_* APIs are designed to address known issues with the atomic_t APIs for reference counting. They protect the reference counters from overflow/underflow, use-after-free errors, provide improved memory ordering guarantee schemes, are 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 --- Note: The patch is compile tested using dec_station.defconfig for MIPS architecture. drivers/tty/serial/dz.c | 34 +++++++++++++--------------------- drivers/tty/serial/zs.c | 11 ++++------- drivers/tty/serial/zs.h | 2 +- 3 files changed, 18 insertions(+), 29 deletions(-) diff --git a/drivers/tty/serial/dz.c b/drivers/tty/serial/dz.c index 6b7ed7f2f3ca..c248201f9499 100644 --- a/drivers/tty/serial/dz.c +++ b/drivers/tty/serial/dz.c @@ -46,7 +46,7 @@ #include #include -#include +#include #include #include @@ -75,8 +75,8 @@ struct dz_port { struct dz_mux { struct dz_port dport[DZ_NB_PORT]; - atomic_t map_guard; - atomic_t irq_guard; + refcount_t map_guard; + refcount_t irq_guard; int initialised; }; @@ -399,18 +399,17 @@ 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); 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; } @@ -440,15 +439,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); @@ -662,13 +659,11 @@ static const char *dz_type(struct uart_port *uport) static void dz_release_port(struct uart_port *uport) { struct dz_mux *mux = to_dport(uport)->mux; - int map_guard; iounmap(uport->membase); uport->membase = NULL; - map_guard = atomic_add_return(-1, &mux->map_guard); - if (!map_guard) + if (refcount_dec_and_test(&mux->map_guard)) release_mem_region(uport->mapbase, dec_kn_slot_size); } @@ -687,14 +682,12 @@ static int dz_map_port(struct uart_port *uport) static int dz_request_port(struct uart_port *uport) { struct dz_mux *mux = to_dport(uport)->mux; - int map_guard; int ret; - map_guard = atomic_add_return(1, &mux->map_guard); - if (map_guard == 1) { - if (!request_mem_region(uport->mapbase, dec_kn_slot_size, - "dz")) { - atomic_add(-1, &mux->map_guard); + refcount_inc(&mux->map_guard); + if (refcount_read(&mux->map_guard) == 1) { + if (!request_mem_region(uport->mapbase, dec_kn_slot_size, "dz")) { + refcount_dec(&mux->map_guard); printk(KERN_ERR "dz: Unable to reserve MMIO resource\n"); return -EBUSY; @@ -702,8 +695,7 @@ static int dz_request_port(struct uart_port *uport) } ret = dz_map_port(uport); if (ret) { - map_guard = atomic_add_return(-1, &mux->map_guard); - if (!map_guard) + if (refcount_dec_and_test(&mux->map_guard)) release_mem_region(uport->mapbase, dec_kn_slot_size); return ret; } diff --git a/drivers/tty/serial/zs.c b/drivers/tty/serial/zs.c index 730c648e32ff..6fa79705a0c9 100644 --- a/drivers/tty/serial/zs.c +++ b/drivers/tty/serial/zs.c @@ -753,15 +753,14 @@ 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); + refcount_dec(&scc->irq_guard); printk(KERN_ERR "zs: can't get irq %d\n", zport->port.irq); return ret; @@ -806,7 +805,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 +814,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; }; -- 2.34.1