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: 36393 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp56619wrt; Fri, 23 Dec 2022 23:51:21 -0800 (PST) X-Google-Smtp-Source: AMrXdXvYU5cZsXsAqa4/VOrOw6YDxBbzoDi2peVsCTPe9SSOrA/w3ejtNhI7wG2KZ8Sw9Nn00o19 X-Received: by 2002:a05:6a20:988a:b0:ad:79bb:7485 with SMTP id jk10-20020a056a20988a00b000ad79bb7485mr12372578pzb.11.1671868281440; Fri, 23 Dec 2022 23:51:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671868281; cv=none; d=google.com; s=arc-20160816; b=Y4aJEjZuUbYT5yHIeZARxKkryTPCS8NzjrD/ISwjvmZbODH16AQs173C0aohEnKQO0 ybwdSg84upc6fA8DFWlzlFs1o7C2d34yzhMBOnlz1kNIdPV57gv6VoDJ+LlQ5pyLyfyG 6I+QmLLIU7KraKpFF96bE3MKMqbGUC5JNgCcZ3bYD8GNQxQsnhyxg1PZyT/AY8zLcHaS wIzw2wT5qf4vif0W01fd0erjvVkR/wZzXab0xy3+nb/6LzzTBxd1HyszohlCjOMleWyO 6oHrPw1w+bldVPrttW7hRVyM+Z2ndUBaFULGc+/U6lOTU24he5LLXMme9rICqw67pWGj g3+g== 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id r10-20020a63ce4a000000b004768f1dd47csi5555196pgi.791.2022.12.23.23.51.08; Fri, 23 Dec 2022 23:51:21 -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 + 99 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-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?1753080955267952256?= X-GMAIL-MSGID: =?utf-8?q?1753080955267952256?= 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; }