Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp964500ybt; Wed, 24 Jun 2020 16:05:04 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzPShyTHaShAw0cgQldUOGP1gCtxvyDcM6ZsYI5NlrsZsrfVqVTyqEIXeYWuzdvKPV/2sMX X-Received: by 2002:a17:906:5203:: with SMTP id g3mr26287658ejm.58.1593039903949; Wed, 24 Jun 2020 16:05:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1593039903; cv=none; d=google.com; s=arc-20160816; b=p0kigzYbAxVxMczY2jVZ812BSfraxZ6i/pZ3SuDRul1KFc/0iHyRF0g8xAE3A0Mbcs Ebjo3PBI5yfay2HqCD9a9pDmgOQ4fqI30k3yTNagfmhFmstko0ra5eavMSNs+F68X/nm ybD1vE5ppp6Q2redZnSSXKhKKH8QP/Qs7lMrFkIQq8y7+D+mfGxm5Lz4ar6v20lfuord YkuKNRWVxNtbbNJyQow4kf+V2jq9H991qfVO7BVNyyk0tmrizD44R1ctJnqNvbjtMGpP opWhbM5lflk9UONle2Yw8VrVQ5BFkXxVkYRFgtLrvS3Px0naGUrVcXNoL5oQkKU5YRVX 7ixQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=5fFSv63vrx2iH/K6v2qc1f863y3U8N/wNCQngVFz1pY=; b=tEmniO8fiOBU5mB/m96f9DHjAKVldo1AVj4AxvPHnQgPfVqj5bDf36NEMYqbPisKnN 7sbbiTm/HTRJUx/bISCm644yT+G9qsXj8v98kxDGOG6GVidvFAmYypDm1GChOL9grb+7 eqiCjs12/NMznGpo9PWBjjYMDQIvlsSDRSt03II9TcFgGyuVAFzX8ou2c+JRh0U6Uosh 8mXdzImTilKpTJzZX4SxypPbcSb0oL669jaZsi8O9+kv3y3RZjhnQcB29/idL5neBrlE VcDlyl5AwfsSU9CPcuD3l6Y086xwisuGBw8S94cGVtu+297wvsnS784WheRA4ZH07pjr eVtQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="bCP/dCOi"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id gf10si15534ejb.29.2020.06.24.16.04.40; Wed, 24 Jun 2020 16:05:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="bCP/dCOi"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387709AbgFXW6M (ORCPT + 99 others); Wed, 24 Jun 2020 18:58:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44868 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728383AbgFXW6M (ORCPT ); Wed, 24 Jun 2020 18:58:12 -0400 Received: from mail-pl1-x641.google.com (mail-pl1-x641.google.com [IPv6:2607:f8b0:4864:20::641]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DE9AAC061573; Wed, 24 Jun 2020 15:58:10 -0700 (PDT) Received: by mail-pl1-x641.google.com with SMTP id d10so1804515pls.5; Wed, 24 Jun 2020 15:58:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=5fFSv63vrx2iH/K6v2qc1f863y3U8N/wNCQngVFz1pY=; b=bCP/dCOilIiQ3qzUm/oFbudTkYh4ecnU3wkfGZB1rrcrBxtofienEF5mEHGj+F+97e GDGVKNTn+KFlQxZI0CFZP3QYOLA4V1vbUIgdPol+1ZI+ZX+Yo5WHsWOE2tw0pphCWTke aCnkEkV+vm1SiJUpMwUbtDmjQcKY9CLh5o6Wtx2emU456VNaIiq5wwnVfPppHQpQSnG7 DjjYUHV7SMS51gFMr5hWe5O5GECrs0Teljd8zQzrnF/iWt2R7/CgnjqVoJSEjIWstg+k XN8wdNHKHy1ksCycnrbdNDhHlIGc5Sk6jKiP81GFplZi1vRRrqOpTvdzm9yv6YaTzzrO 6KhA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=5fFSv63vrx2iH/K6v2qc1f863y3U8N/wNCQngVFz1pY=; b=ft/Ef5jeCxlU/nRONL9xX52y5XFGYb/uHdl7gMsb39A91MYKAQJ19n1ujt/3nGDQEW FWMf1H9+A1hTalu1lkaNRkMgxNIT4u4hAOtTbNKrimREWNUvGoicLU1p63DL0KZR7+a7 1PT3vZxrEGKgbIRKjHwfw/AnAjep8fbGOzkHd/DsHW3T0Np/FTcqOD6tkZtKrJpkGGuK qhr/tZAATYvH9qpe9YoVO6G1jgheWkxdeNzfFViLpqFTWaxUdREe9yYQou0e3WvCE84i fuCHRh6TwvQGoFRpJUlr3hn0lLa4D4KIE2DGQyCIL2NorohRf9zw89rb579onh6T0u1p WcWQ== X-Gm-Message-State: AOAM532u2uSt/UljQDvJhyG0OQA64gIn378dQFmUU4L8ZROLlRGrIqGi 33JO31R4rr2BUgbuiCnXxlo= X-Received: by 2002:a17:90a:668f:: with SMTP id m15mr102540pjj.32.1593039490207; Wed, 24 Jun 2020 15:58:10 -0700 (PDT) Received: from sol (220-235-99-174.dyn.iinet.net.au. [220.235.99.174]) by smtp.gmail.com with ESMTPSA id k28sm1856498pfp.82.2020.06.24.15.58.06 (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Wed, 24 Jun 2020 15:58:09 -0700 (PDT) Date: Thu, 25 Jun 2020 06:58:03 +0800 From: Kent Gibson To: Andy Shevchenko Cc: Linux Kernel Mailing List , "open list:GPIO SUBSYSTEM" , Bartosz Golaszewski , Linus Walleij Subject: Re: [PATCH 10/22] gpiolib: cdev: fix minor race in GET_LINEINFO_WATCH Message-ID: <20200624225803.GA3600@sol> References: <20200623040107.22270-1-warthog618@gmail.com> <20200623040107.22270-11-warthog618@gmail.com> <20200624155714.GB8622@sol> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200624155714.GB8622@sol> User-Agent: Mutt/1.9.4 (2018-02-28) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Jun 24, 2020 at 11:57:14PM +0800, Kent Gibson wrote: > On Wed, Jun 24, 2020 at 05:46:33PM +0300, Andy Shevchenko wrote: > > On Tue, Jun 23, 2020 at 7:03 AM Kent Gibson wrote: > > > > > > Merge separate usage of test_bit/set_bit into test_and_set_bit to remove > > > the possibility of a race between the test and set. > > > > > > Similarly test_bit and clear_bit. > > > > > > In the existing code it is possible for two threads to race past the > > > test_bit and then set or clear the watch bit, and neither return EBUSY. > > > > I stumbled over this myself, but... > > > > > - if (test_bit(hwgpio, gcdev->watched_lines)) > > > + if (test_and_set_bit(hwgpio, gcdev->watched_lines)) > > > return -EBUSY; > > > > > > gpio_desc_to_lineinfo(desc, &lineinfo); > > > @@ -897,7 +897,6 @@ static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg) > > > if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) > > > return -EFAULT; > > > > > > - set_bit(hwgpio, gcdev->watched_lines); > > > return 0; > > > > ...I think it's not an equivalent despite races involved. If you set > > bit and return error code, you will have the wrong state. > > > > Not quite sure what you mean. There is only an error if the bit is > already set, so you've changed nothing. > > And the watched state is not part of the lineinfo, so the state returned is > the same either way. > Perhaps you are referring to the case where the copy_to_user fails? To be honest I considered that to be so unlikely that I ignored it. Is there a relevant failure mode that I'm missing? Cheers, Kent.