From patchwork Sat Dec 24 07:44:32 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: 636667 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BE6D2C4332F for ; Sat, 24 Dec 2022 07:44:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229457AbiLXHo6 (ORCPT ); Sat, 24 Dec 2022 02:44:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51058 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229445AbiLXHo5 (ORCPT ); Sat, 24 Dec 2022 02:44:57 -0500 Received: from msg-4.mailo.com (msg-4.mailo.com [213.182.54.15]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2648CC756; Fri, 23 Dec 2022 23:44:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=mailo.com; s=mailo; t=1671867879; bh=EPkYose1e3yrKl5ykxyOmyiDf9URlXGGrlEnGyZvwbI=; h=X-EA-Auth:Date:From:To:Cc:Subject:Message-ID:MIME-Version: Content-Type; b=Puz3PNdxyeZkpG+Qvhmha/U0cVogMsMqOUNdzVnd5AhWwo4kgUowfyrPO8FGJJlCd Rc3gCV7B16GYp8VeMtfrUdO53ANowjIa+Xww/+i3duYG4gg05sxv2aIBmv+/KRH5qO UYVNX2KVeV/FHWrJEmletzzFmD64uSDJTSylfZUs= Received: by b-6.in.mailobj.net [192.168.90.16] with ESMTP via ip-206.mailobj.net [213.182.55.206] Sat, 24 Dec 2022 08:44:39 +0100 (CET) X-EA-Auth: gJnmbxUVyJjJm0gxPqKRmKfx/xtAurON5XX5Cu+SW9Mkhn5NcZjfZnpRmtOvQMsB4fsWi30SoQudVSDFIctPWmBnsG8vec0w Date: Sat, 24 Dec 2022 13:14:32 +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 Message-ID: MIME-Version: 1.0 Content-Disposition: inline Precedence: bulk List-ID: X-Mailing-List: linux-serial@vger.kernel.org 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. drivers/tty/serial/dz.c | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) -- 2.34.1 diff --git a/drivers/tty/serial/dz.c b/drivers/tty/serial/dz.c index 6b7ed7f2f3ca..b70edc248f8b 100644 --- a/drivers/tty/serial/dz.c +++ b/drivers/tty/serial/dz.c @@ -47,6 +47,7 @@ #include #include +#include #include #include @@ -75,7 +76,7 @@ struct dz_port { struct dz_mux { struct dz_port dport[DZ_NB_PORT]; - atomic_t map_guard; + refcount_t map_guard; atomic_t irq_guard; int initialised; }; @@ -662,13 +663,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,23 +686,19 @@ 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); - printk(KERN_ERR - "dz: Unable to reserve MMIO resource\n"); + 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; } } 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; }