Received: by 2002:a05:6358:16cc:b0:ea:6187:17c9 with SMTP id r12csp11628234rwl; Tue, 3 Jan 2023 02:12:20 -0800 (PST) X-Google-Smtp-Source: AMrXdXtdQ8Z1FVhjNhQHEtnD/Hkg9J7o1a8qHiWJWqW1s+W9hFN9qcDlSnWG/iYruLsFHV0gqBXV X-Received: by 2002:a17:903:228c:b0:191:22fb:b16f with SMTP id b12-20020a170903228c00b0019122fbb16fmr61197796plh.54.1672740740700; Tue, 03 Jan 2023 02:12:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672740740; cv=none; d=google.com; s=arc-20160816; b=weMkEF07a/XsuOeRXeHqvrzWRGQZQzOrvzkTc4X56uv4XV+78c0bTV0LNjOg9td04c ypGBXIyT+IOXESEVyh3i4J9HCF0XKiyLvsY74c7icjXRPnldAi0FQawzwiWuPQ5euWpe +uBAbKU8C3CjWhFV/JIoG0SwKOJ2j36HRNKnAaCQlhTyAftiIPvBqsHIvT/Orp1NeJvP cd8zAOUDhzFEhCOABkrM/YRf45CozmiSYX2+1wryd0H585Z1zsiwh7tGddBhGbOAn8Pc NmdhLuNDUZMJCgYXwuBbiVew8x1fNGKhCQnsBfY1zIBfDfOUBbM88z6Ufl1KGU0moUoc V2wg== 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=cRcwB3Tr3mjxJTS2N+u3ao7QweLCw8t5sMgLyp3bxmk=; b=NYacepn8768oWhLp1swFF89OESRPvlH030bTGS0dA2pwLiKts1Ckjzh+8kb6oXzYDb 6WbMvZOajaoP8Jug6MIA59ego7ZrDutaFHTKmEGN5b+DA/H40HABibYCHimm8W6lcKX1 yWTfYXN1rm2F+2fTWmHQBEpLeGpjQ0RGHfC+Osg4sJULdlSygjNsPsDYsv8pTsMCdbXo 5fDbPMfJtaihNRqjeqBcLGgFyCxbA2oeWF7guGBAiDEfz9zpv+QphV4ms5xY3S6jA92d eHi7x3k8D/rKsLuXXcXBXYAyYqUfte5eFN0Jau1myaS6+vkWVPHEikBP9LjCIvyOEIwl XvJg== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@mailo.com header.s=mailo header.b=h5rr47Do; 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 m4-20020a170902e40400b00192a2e850eesi12924839ple.29.2023.01.03.02.12.11; Tue, 03 Jan 2023 02:12:20 -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=h5rr47Do; 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 S229584AbjACKFo (ORCPT + 61 others); Tue, 3 Jan 2023 05:05:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39738 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232179AbjACKFm (ORCPT ); Tue, 3 Jan 2023 05:05:42 -0500 Received: from msg-1.mailo.com (msg-1.mailo.com [213.182.54.11]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 37D962AC1; Tue, 3 Jan 2023 02:05:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=mailo.com; s=mailo; t=1672740326; bh=mbJPsA4UDzDHjFi3vHuznbJvnwuV6TpmkKB2pWQH7KI=; h=X-EA-Auth:Date:From:To:Cc:Subject:Message-ID:References: MIME-Version:Content-Type:In-Reply-To; b=h5rr47Do9/K9HgrgcJReCWc2VQnHaWLaGQUJzyhtPrh+j4PiKlaJPcSwLnZnwK/8W oDE+/Y+tiPkhYz3twYLaTCTXaPn1p/ChbIcyhE+butNq/q/tyO0MZR8ZFsCVlfR8va MgIkYuyE5dlBTBg33qiaJz2m5ckV43g9LaKTsY7U= Received: by b-3.in.mailobj.net [192.168.90.13] with ESMTP via ip-206.mailobj.net [213.182.55.206] Tue, 3 Jan 2023 11:05:26 +0100 (CET) X-EA-Auth: NWGOiTN8/a9U6L2da4zFkljN32D3PAKpjqUOxmBnNMRIyLGwe7HRfPX9VWV1/c/hVHp+27ccTDNeG4gONjBSjqot74kdHD3V Date: Tue, 3 Jan 2023 15:35:15 +0530 From: Deepak R Varma To: Jiri Slaby Cc: "Maciej W. Rozycki" , Greg Kroah-Hartman , linux-serial@vger.kernel.org, linux-kernel@vger.kernel.org, Saurabh Singh Sengar , Praveen Kumar , Deepak R Varma 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=-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 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); ... } Here, refcount_add_not_zero would return false if &mux->map_guard is already 0. Which means, incrementing it by 1 would have met the earlier if evaluation. Whereas, if &mux->map_guard is something other than 0, refcount_add_not_zero will increment it by 1 and return true, in which case the if condition will fail, similar to the previous if evaluation. Hope that helps clarify my revised thought. Can you please let me know if this revision looks safe? Thank you, ./drv > > thanks, > -- > js > suse labs >