Received: by 2002:a05:6358:16cc:b0:ea:6187:17c9 with SMTP id r12csp7420218rwl; Tue, 10 Jan 2023 00:05:57 -0800 (PST) X-Google-Smtp-Source: AMrXdXscMbfC7qQAnRDi9sz54RAIlZKWC6cZRUnlmo1fifLC1oqX9KGWgABQjd6TtP90BmC+R8Bs X-Received: by 2002:a17:90b:48c4:b0:226:44e1:7868 with SMTP id li4-20020a17090b48c400b0022644e17868mr35428849pjb.37.1673337957708; Tue, 10 Jan 2023 00:05:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673337957; cv=none; d=google.com; s=arc-20160816; b=dtKgkXd9mNdlc9OTNbDMse1+IhRh0CcPM606TyFCxMaNoL+cl3m5H92dNEeI+Ymc2u JuL7Rg6/0OMSVxOsBn9Kf+RdWVwHgWIPVXKGOtaP+u7btjdZs/d768wfdK8DXcsCmkUX nIuSVw8g1NFOHUv7VD3r7E3vHlUAPYL8pjTTT7A7YtIdrjIud0utR2Z4uUD7BlX1juB/ QeeqKG/jY9B5hbh11Z3E1G5MmB9vZL9WvVQbWZXkrQXgOgDYPB4qzUnFhP8ku3086m4Z 6iKnWv5d1x5VPP4+kZfEUdEE/FFL8qW6t3wueAVAFXrAuNMTG2GSC2ywKA7D6kz12rDY jXcw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=pOEPkR8c+zmnaw4pPJpBw7VmWwSVV60tnRh9RnU70dI=; b=M+tO4L72VqLrjS8wDb6z6kEpO4/oppJm0NvZx2uHaZWk+qzf5SKbjoifPqvFHb2Fx4 XM/CFq6fu0R/qg76mK2uFXO8pyHUmB1Tgob5rydpyTHumYZYR8pPiqJU+HWN5o7x4FV5 ceAsnN0zR2+9bIKIVa/PPy6ZopU9sE4vGXGo9VUZIX+XFhQf2pwfq+bZEuSI6Sprl9b7 x9ppisxp6BGnRjaG/1uioz4v8WkP5qjN/lTfVHqRKpoiTP/VcJJFzLM866bQk8M+/6QB zcsKK+dU8XOROZbQt/y4U2j+L3EPgcxlh68LIZvEsfwme31Cbxpe+e90rujEvyu1QLzn 1f6A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=ws8isfKL; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s17-20020a63e811000000b004780e6f20adsi11261523pgh.522.2023.01.10.00.05.49; Tue, 10 Jan 2023 00:05:57 -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=pass header.i=@linuxfoundation.org header.s=korg header.b=ws8isfKL; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238076AbjAJH7N (ORCPT + 55 others); Tue, 10 Jan 2023 02:59:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60694 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237597AbjAJH6n (ORCPT ); Tue, 10 Jan 2023 02:58:43 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0094A1AA1C; Mon, 9 Jan 2023 23:57:57 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 6390BB810D2; Tue, 10 Jan 2023 07:57:56 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9A450C433D2; Tue, 10 Jan 2023 07:57:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1673337475; bh=xE48saHTLnYwBKfywpNkPCy00BCpa0LnBT94bNOVB50=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=ws8isfKLXhbiiTiqjxK0Pkte1GA4y9XipdWi6EfYG5c12sUI45Cpou3/S8MD4C8tt qtd0QYeZY09yCBIaYwxRtLhGjNTbxnrumKMwXfiU4n+M/sNkqLlEVG2wFj81pxS3hb DXuwPhrNYBf8bnhVOu5GEQ8ZfD/7anUnj+cKXrF0= Date: Tue, 10 Jan 2023 08:57:52 +0100 From: Greg Kroah-Hartman To: Deepak R Varma Cc: "Reshetova, Elena" , Jiri Slaby , "Maciej W. Rozycki" , "linux-serial@vger.kernel.org" , "linux-kernel@vger.kernel.org" , Saurabh Singh Sengar , Praveen Kumar , "ishkamiel@gmail.com" , "keescook@chromium.org" , "dwindsor@gmail.com" Subject: Re: [PATCH v4 1/2] tty: serial: dz: convert atomic_* to refcount_* APIs for map_guard Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, 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 On Tue, Jan 10, 2023 at 01:17:54PM +0530, Deepak R Varma wrote: > On Tue, Jan 10, 2023 at 07:27:44AM +0000, Reshetova, Elena wrote: > > > > > On Tue, Jan 03, 2023 at 09:59:52AM +0100, Jiri Slaby wrote: > > > > On 26. 12. 22, 7:21, Deepak R Varma wrote: > > > > > 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. > > > > > > > > Really? (see below) > > > > > > > > > --- a/drivers/tty/serial/dz.c > > > > > +++ b/drivers/tty/serial/dz.c > > > > ... > > > > > @@ -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) { > > > > > > > > This is now racy, right? > > > > > > Hello Jiri, > > > I found this [1] commit which introduced similar transformation in a > > > neighbouring driver. Can you please comment how is this different from the > > > current patch proposal? > > > > > > [1] commit ID: 22a33651a56f ("convert sbd_duart.map_guard from atomic_t to > > > refcount_t") > > > > > > On a side note, I have not been able to find an exact 1:1 map to the > > > atomic_add_result API. I am wondering should we have one? > > > > Hello Elena, > > > In past we have decided not to provide this API for refcount_t > > because for truly correctly behaving reference counters it should not be needed > > (vs atomics that cover a broader range of use cases). > > So, there is no FAA refcount wrapper? I think this is a pretty common need. > Please correct me if I am wrong. > > > Can you use !refcount_inc_not_zero in the above case? > > I actually did try that but was not sure if truly addresses the objection. > Please attached and let me know if you have a feedback on the alternate > approach. > > Thank you, > ./drv > > > > > > Best Regards, > > Elena. > ############## ORIGINAL CODE ################################## > - 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"); > return -EBUSY; > } > } > > ############## INITIAL APPROACH ################################## > + 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; > } > } > > ############## ALTERNATE APPROACH ################################## > > + if (!refcount_inc_not_zero(&mux->map_guard)) { > + refcount_inc(&mux->map_guard); > + 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; > } > } > This feels odd to me, why not just use a normal lock instead? thanks, greg k-h