Received: by 2002:a05:6358:16cc:b0:ea:6187:17c9 with SMTP id r12csp12937217rwl; Wed, 4 Jan 2023 00:45:38 -0800 (PST) X-Google-Smtp-Source: AMrXdXv74mOB/6on4GBqBB5u2RqLBLXNO0n+RFC8qnKVVDyEvLn9qD6qgsvkz+4aWGE2+mb5UBVR X-Received: by 2002:a17:907:76c2:b0:829:59d5:e65a with SMTP id kf2-20020a17090776c200b0082959d5e65amr39872140ejc.77.1672821938665; Wed, 04 Jan 2023 00:45:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672821938; cv=none; d=google.com; s=arc-20160816; b=FHtQU5BCg1CMUuhriOyG2t9egpkVifVt6RIqRJokUoPAtTYhtOK14JCp1pv/zthRgf Gr5HN0BPmiDMee1AfC2jMiPx/Ka4C3ceQfjW+yMU/XVYklLP+L+TnLpPgqL6aeB++tbK 122I7BLNDQOV4k1uDLRRjSVeu21ZaqVZOAvpT5e2vgf4ZV5ydR9paSoGREj5aBkGzUjN 7260IJ2vwiQx+ORCjxcHNR4NvSuxIvtXmYOEGnJm+id9E711sF0iTrv60G6Yu8+TAgNr eqphK+9bvrkRrLfIEjy83Y+w8iuWx2E9x7vKMicFJuixzEdPEhq/mgKPO4G8u08bdcul Iolg== 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=tR5OsrXHfI5dStjuY2QlnwceINV8MwD8ZHikU3oPkYk=; b=oPpcf/qS8SDp/BVjwCKsWidd91IqpeSQn0gUSsQedWxY5JV7X/I4nLQKbWbqs9ubWj xBjphL9of5xf4qwYc3PDuZhRvuFOH42OLHH7WiePMyQx9s7+sPpwCfIEiTOFQfZ5u/ei PkKl7rHurmxcLQWnZI4hGW/SjWjXE3L+v5BGDPgHAmyYed3iF7tXL/rhM6wOP6v7LDg3 0TvuR6gArtlfOUQxHMWDVVvB/mxEg2QlAsC31vCg10ALtHsTVt8wLpJffAwWqESP4De1 gRlzRH+RVGNItu8h5fxzi2SIPNJdOeKHBEEowPrQOo/4ywImZCvx+2rhbWl8JuujuNsC cFBg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=jrihKAEd; 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 x4-20020a056402414400b004822681a660si26089703eda.517.2023.01.04.00.45.24; Wed, 04 Jan 2023 00:45:38 -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=jrihKAEd; 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 S233847AbjADI2Z (ORCPT + 59 others); Wed, 4 Jan 2023 03:28:25 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59002 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234032AbjADI2R (ORCPT ); Wed, 4 Jan 2023 03:28:17 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3967F186CC; Wed, 4 Jan 2023 00:28:17 -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 dfw.source.kernel.org (Postfix) with ESMTPS id CA0B7615C3; Wed, 4 Jan 2023 08:28:16 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B9058C433EF; Wed, 4 Jan 2023 08:28:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1672820896; bh=AJFMrKxnl4eUbyyWptRKXOFJIxojbdYw+u9qBEVdUYo=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=jrihKAEdmgJjdnSdToPubNpXoq+SgITHs9Q3VoE3EfHYEnOycNp5EQPTE2O0P4Qoo OQz8vGvvpExcVVakdOutHyJzDgL36UXQVCYhwJnsOtB+qbaz8Fa8EzK4QzFSCkQiag lw41t3MPXYkToOPPIZTK8o80QgiLq6ugst8uTAws= Date: Wed, 4 Jan 2023 09:28:13 +0100 From: Greg Kroah-Hartman To: Deepak R Varma Cc: Jiri Slaby , "Maciej W. Rozycki" , linux-serial@vger.kernel.org, linux-kernel@vger.kernel.org, Saurabh Singh Sengar , Praveen Kumar 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 03, 2023 at 03:35:15PM +0530, Deepak R Varma 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, > Thank you for the feedback. You are correct. I have split a single instruction > in two (or more?) instructions potentially resulting in race conditions. I > looked through the refcount_* APIs but did not find a direct match. > > > Can you please comment if the the following variation will avoid race condition? > > if (!refcount_add_not_zero(1, &mux->map_guard)) { > refcount_inc(&mux->map_guard); > ... > } What do you think? The onus is on you to prove the conversion is correct, otherwise, why do the conversion at all? Actually, why do this at all, what is the goal here? And how was this tested? thanks, greg k-h