Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp1306016ybt; Thu, 25 Jun 2020 02:55:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzjzqSG106tJ0kkFgwTTYo9cne2KhD5kn1tV/Q8QwJwQg3Du/CdeTo7OreTNqcUqzNj14T8 X-Received: by 2002:a05:6402:158d:: with SMTP id c13mr26621346edv.103.1593078907476; Thu, 25 Jun 2020 02:55:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1593078907; cv=none; d=google.com; s=arc-20160816; b=hTDvEnyHpjlp5ohZegRa9AJXAmXzO5KIDXm15F4/UHrDTf83JLc22PzNlf/XYPqH4m Z+DiSimhXOqzJIOYu1zOx3MNdoPpXkTwVvk7ghb90K1RiK3QyERdL/4BeSxktyiHjqaO F8K3JsSoBiytpXZYxFrkgsPrg1fu0ydf3ElrlmQRGoIEjnfbNqXp5105CTYbBwsOkkyO Ue5VpbBRtK6696NH295g+kULLEy2FgIhKo3c3zFc8tn2XyCuyV5gL1cva40a83f8TjnT FWTASzKGEJnZ5i6/sXdRCs0t4XCgWW0yNNUVQ3v6OcL62CZarZ7JBvqP7A07KgGYFKZd G9qA== 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=qA/ch1XijEj8vqDlFnL4wr2CZ9xVQ3j9phqNCaqDivM=; b=uBJ76DMY4maIkIQmktGSUH61SH5zHs80Yjis6v/7mpahqnvs67MPGTn4js6P7ZpceE 9sElI5hTNEpwa1wUPR90BYetroLvcSGu/QVOirxwK7Rxgis3Hj01+m9VrOaWkXFfEkqM vBzR42ClH0rowsvHePSZqGxDi7RsGJ5vQrwr2RgpPBbr4qrXs/15Om0151lph+73qPpz cKsHJZGgIaJzduOCmJxsMon3MReuGuODKQgN2VLthT2MQCcw0KWj9WES3kxysbP/LmgA 2RDcThUBUTIScE4TC442KW8VkFk/C9N9BRlQ9dlJY6g/esTZACiAvWRELbveh65WowBn 28fQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=nAwT0XDx; 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 h23si14656786ejx.452.2020.06.25.02.54.43; Thu, 25 Jun 2020 02:55:07 -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=nAwT0XDx; 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 S2389963AbgFYJNQ (ORCPT + 99 others); Thu, 25 Jun 2020 05:13:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54352 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727878AbgFYJNP (ORCPT ); Thu, 25 Jun 2020 05:13:15 -0400 Received: from mail-pj1-x1041.google.com (mail-pj1-x1041.google.com [IPv6:2607:f8b0:4864:20::1041]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4D8F7C061573; Thu, 25 Jun 2020 02:13:14 -0700 (PDT) Received: by mail-pj1-x1041.google.com with SMTP id q90so2255998pjh.3; Thu, 25 Jun 2020 02:13:14 -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=qA/ch1XijEj8vqDlFnL4wr2CZ9xVQ3j9phqNCaqDivM=; b=nAwT0XDxsxVITDzd469wsRcqEyID6/+jgdHzjNylZGa6CrWyxnLCQ+5Ubwd9XUFbLg lx8KyVQApRyNeYAe1tYx1GBCaejMxsIOpdIvpH5csMIeYTLN9HAnUQuuRnCt3c7OtA0g hy2DDF5SwwE/elaRmP3kZa66pn4g1lqINm7LnJSdqp0AaQXQRWpiywNWBSjosp9x8DWF KRdRHItJkU2khmKfeG+2UolVy9aWHEKMOjBoP7cnLU/yvpXfFzCLlH6DLyKEoo7EFOCf XRJBG4d8mEW22zRirjbFJQ6gDDsZVesZG3fhfAKY2eKb7Cy3C3qw/X1g+3WUOYtuYv7b TcrQ== 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=qA/ch1XijEj8vqDlFnL4wr2CZ9xVQ3j9phqNCaqDivM=; b=Mquhnxlb2DXi8NJPrE5QNn0jpeCESx/wylIbtwwP2ty4niP68wedP/gRvxzbijzpxP 6NCgIpISzWtbYZ19MA4rCfS/+daWZwGFGsq3hotHXdp+9/pCMJc/lfcSWwXKJ0FHMfjs Hixv5U3ehz8IUVdkbv3N4jkkroI7p2dkg2AvCIca6av6kL4cP44b1ZObraKibiSLjGBr 8pf+F3qwMQK2mE9gDLdY4jmQY7LSTsHIlOD+ie4RYUTz2hPFt3Q28r/Klk8xf3dtkqJH dCErYZaslbIvz4MWPcHXqfmD+1tbMdfJIrh00TDza01xPK22KDYDV25Kv0Io67qWyhXh 1/bw== X-Gm-Message-State: AOAM532t9CrHhgFS5xEWREkNs+MrKyrfkLqwsP7adISSFMSEmoyoNGuE Jv/P7wgDg0rcOoojMfZm+dmOdJc1Bfg= X-Received: by 2002:a17:90b:143:: with SMTP id em3mr2290937pjb.92.1593076393738; Thu, 25 Jun 2020 02:13:13 -0700 (PDT) Received: from sol (220-235-108-194.dyn.iinet.net.au. [220.235.108.194]) by smtp.gmail.com with ESMTPSA id gb4sm7559454pjb.6.2020.06.25.02.13.10 (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 25 Jun 2020 02:13:12 -0700 (PDT) Date: Thu, 25 Jun 2020 17:13:07 +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: <20200625091307.GA16386@sol> References: <20200623040107.22270-1-warthog618@gmail.com> <20200623040107.22270-11-warthog618@gmail.com> <20200624155714.GB8622@sol> <20200624225803.GA3600@sol> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: 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 Thu, Jun 25, 2020 at 11:44:21AM +0300, Andy Shevchenko wrote: > On Thu, Jun 25, 2020 at 1:58 AM Kent Gibson wrote: > > 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: > > ... > > > > > 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. > > > Perhaps you are referring to the case where the copy_to_user fails? > > Yes. > > > To be honest I considered that to be so unlikely that I ignored it. > > Is there a relevant failure mode that I'm missing? > > The traditional question for such cases is "what can possibly go wrong?" > I wouldn't underestimate the probability of failure. > The worst case is the watch is enabled and the userspace gets an EFAULT so it thinks it failed. If userspace retries then they get EBUSY, so userspace accounting gets muddled. We can clear the watch bit if the copy_to_user fails - before returning the EFAULT. Would that be satisfactory? Back to the failure, is it possible for the copy_to_user fail here, given that the corresponding copy_from_user has succeeded? If so, can that be manually triggered for test purposes? Cheers, Kent.