Received: by 2002:a05:6358:16cc:b0:ea:6187:17c9 with SMTP id r12csp7416363rwl; Tue, 10 Jan 2023 00:02:30 -0800 (PST) X-Google-Smtp-Source: AMrXdXvz41iX/6EuDQsBobDB5og56tcdfeahqzpdL1RwOMCrM1j86IfewaJWNGOu/Bd1LtKdFdoG X-Received: by 2002:a05:6a20:d695:b0:a4:255b:f3b3 with SMTP id it21-20020a056a20d69500b000a4255bf3b3mr83664334pzb.33.1673337750438; Tue, 10 Jan 2023 00:02:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673337750; cv=none; d=google.com; s=arc-20160816; b=A37E3JLXuY2x9TA3CILAV1hlB1/g1HiDHjciypdZnO+9p4wHhaHHqfdT+4zkIhtoPm QzRhPGA9D5aDzlDMjK9m7RRTsWvMsk65+RRnGkMxn+llM3A16+fS3zKQ6GcHdidOaVi8 zyiWxBu2005zy9MTnDYJUV+6btZ/Z78fMlqhVjuJZO3iDczhpc8/Yw9ftJaR6t3kwZCb wG5QOBW0OMli6SS2VJJSK7GEy4B59FbvlBz/Z2yd9Dl/+GeSIWo4k2XSC5N91kvNkE5k +tt89OQhfQbJQs3NHeihzBTWWJKYf4Kmfa8hhqgu9R4vAePQReP6RdpdJAA/8RmIKNGk xUfA== 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=X8GBALG6ZJHqOeNckcaBhafNDAhg8k4+rmUg5jb2U9I=; b=PH2O7zBOxoJp7iQRe0t1yDkRM1FSKbHDlsKG8+3xwZm25TK3Ck28JH89wNO0uIqVKg ciElzvIV3zShHR/9JWn+uqO2c0bZ6fW7XLufrdoJ/IB7Uj5GRGnlspyIJZEWTtYWgIHR hS9zLDuB/RkNigbNwcVn1nLOHrNdeUB45L0Q4q/LKQ+yO/5FgTLrv3tJ95HzE352huae /OxpIWDTj2AxoIFUv15GYQHGCDHYgDZlshWGgMIoBlFolDB+Y4pz+ett6MqQk9yBTBhz j1OhYTjm+uo3K9naCtjUKp0pmHDH0bW4+zbfD915g77Ykvh4FXcMTACMuSmCDsWl3gf7 Yh6A== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@mailo.com header.s=mailo header.b=IZKYGpjv; 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 h63-20020a638342000000b004b3bad0c227si4502619pge.195.2023.01.10.00.02.21; Tue, 10 Jan 2023 00:02:30 -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=IZKYGpjv; 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 S235197AbjAJHsX (ORCPT + 55 others); Tue, 10 Jan 2023 02:48:23 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57444 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231215AbjAJHsV (ORCPT ); Tue, 10 Jan 2023 02:48:21 -0500 Received: from msg-4.mailo.com (msg-4.mailo.com [213.182.54.15]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1E71F178B2; Mon, 9 Jan 2023 23:48:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=mailo.com; s=mailo; t=1673336879; bh=EiXkQ2JEZGnLUb6tfZO7vd8G6NiaZjjsaBJ1fvdxXPM=; h=X-EA-Auth:Date:From:To:Cc:Subject:Message-ID:References: MIME-Version:Content-Type:In-Reply-To; b=IZKYGpjv9ejzRTD7LR95ZrMWEH5H+A/L1ASzRI6+VMXqN1R51fca9X5f1pmzMVZaa iFr9Xrqmsb3B9UeMqvRtUfqxwc4AmgOa5s4wTeQ62iZ7FTJVFcriM9Vu63oTnKGI6z JRz3x6V9QArXtwOjpjl7qh5b6QeHnU4tq6G7VSC0= Received: by b-2.in.mailobj.net [192.168.90.12] with ESMTP via ip-206.mailobj.net [213.182.55.206] Tue, 10 Jan 2023 08:47:59 +0100 (CET) X-EA-Auth: PprNwOeWQeT6qxQf+3gYvLF4aPgOloBh7REyAC3mB29FCIBf1kwszEl+TiorbYIjmmwWduBdrFTsD74Hg5HAMFy19f1+7J9e Date: Tue, 10 Jan 2023 13:17:54 +0530 From: Deepak R Varma To: "Reshetova, Elena" Cc: Jiri Slaby , "Maciej W. Rozycki" , Greg Kroah-Hartman , "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: multipart/mixed; boundary="jSLdvTZAgZry9A9c" Content-Disposition: inline In-Reply-To: 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 --jSLdvTZAgZry9A9c Content-Type: text/plain; charset=us-ascii Content-Disposition: inline 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. --jSLdvTZAgZry9A9c Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=code_diff ############## 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; } } --jSLdvTZAgZry9A9c--