Received: by 2002:a05:6358:f14:b0:e5:3b68:ec04 with SMTP id b20csp1624406rwj; Sat, 24 Dec 2022 00:15:47 -0800 (PST) X-Google-Smtp-Source: AMrXdXsAgfv4Vf91N7Sq8FxEqKP/Y0/mmsweHhtQfygjwNn8uB4jS+yrTjd0DahiE+SWG06B3eJ3 X-Received: by 2002:a17:902:ab93:b0:191:317f:472 with SMTP id f19-20020a170902ab9300b00191317f0472mr13069196plr.64.1671869747323; Sat, 24 Dec 2022 00:15:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671869747; cv=none; d=google.com; s=arc-20160816; b=nhSnrBz/UOZ5apmv8D2Gn9WvQWvPbnb/ebfMb//WkIp4vfs9TKQtwfMEC2CVEzBOvE cntfifIGx8T/hb1p+yuIl5MohWcmOYk9QaNLscGutLz8WN1BEHCFHq6Q6Qugib7nh5vO MbDEXl5yjAjbwCwXC41lEpwGPowQXNyuo1W801HrqJK1pVtE/rTGtctsy+ugyBNvBfcA gjnpYSBInDx0b/iM6bxUhUYJ3ZG06LCLvKE18LFtaCo+o8JwJVFVqA2ZRgiXt+dkg3pU JcBVhE3dQElWg38dNyES8EQakSuAmTZPWQLgLubp8nJFmxCVORFcXbS7JFloCYTpmdY4 0I1A== 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=onrgR77/a+d9pAP/erxQEXJ2oIqzYg0z5Mn9XRCQ5KQ=; b=PXDteoftrEWoUy0dN8bzV/5YCRnGeF6upjIEBzd3EJcN1PDlg7iOlCT52FmuW1bfvr RgRMj3qvOTSAHaf4sM8iXXvmqDQPr03oea8+W6s0gm4Gm33w3xUeqd47nwPFqOeVxKf+ GkBQ7rwV/roYLDp9Bc4uLJuTlX0cqTZxppZ3/WykXtVOOeHs4nWQaiXTwjVhbnZyssGF MUaMg79sAsJ1Yo8n90vfpKBWiDUE1UrAXS9kxhc4/G8K3Z2pCTV9rlKozqrO0jJ8KvgT ftn7sRD7mz7Na+YqWO7zKQGdUp/owlszGa5VrcsgKxsh055eeE3DvYgEisYG+2ApM25h PDyA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@mailo.com header.s=mailo header.b=Puz3PNdx; 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 iw22-20020a170903045600b001890e4e7915si5075079plb.207.2022.12.24.00.15.38; Sat, 24 Dec 2022 00:15:47 -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=Puz3PNdx; 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 S230160AbiLXHo6 (ORCPT + 64 others); 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-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 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(-) 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; } -- 2.34.1