Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp145020pxk; Thu, 24 Sep 2020 01:42:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwou46//8kpvt+IUS5j8Zuf2kFMR+7ktmmPxmsY0m38E4EjyUOdLISnb6ctz6WMf74qao1E X-Received: by 2002:a17:907:213b:: with SMTP id qo27mr3396791ejb.441.1600936942045; Thu, 24 Sep 2020 01:42:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600936942; cv=none; d=google.com; s=arc-20160816; b=P336oHv5m5wXp4QgwloIZukb+RhLU0eL81VnJWsQwgIWf0ldMnQ5dyG/Kc/4BP0lDP RV1sxnoCN49N4qsQ1pbQuEU2Y92kBjswlkILqBf7FbbukcecX8kJ51aFtKlRyPlPMvj5 hN/ALn5dxevaldp5GbIKwztdepT38Sj4mUEKgjTYijqlJY0g2Vyl+ZaHS7DFKPL33aYr Hovl55yh921FeLLUAwPLIdiXKKVUhEbbN+5pn3Z8P2yAJdxzog76FbXa0Pr/uwaJ35Qe 3vG9fcN3xOTwvNj6kHO+l6sjp0eOb09dAJzHu1dx76juW8dQbJC8TUI1vlrH28RFgOrw wM5w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=jVyFBXyqmDS4jB1M2eyvEy1BeQ0zCzl71j0vO3zYwBw=; b=C9Ns53WKBD5e0DMyTONF+By0J5jLYmXUjMm109KDPgLd5yy6TaHwV8dNBtQCdnyxfk hF4/U+2m76FBBplCZLR8umUqrUGrO1v07fm3QdRY08g5MRz3yl2l44QLQ0ZnFyxm9gDW dp+IvCIiFOT47w2RocxrlOGqOzgGmtPW4r/UeM290Qzymoj5Xlin+/ZBp+VCkbIOhmts gTSAh5YQRl9TqvMwigdOA4kgPr2z+PB9daU2tPWZnJv+ABxpDX9aLASoJRH2rBg1MAe2 qt18Trey99EITi5kKWT6AqNdEVGFx4zaoh36qOa4ag991pDHfvDPlV18NN64kb7mqtjk r4cQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=QVtEZrwk; 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 t23si841460ejs.725.2020.09.24.01.41.58; Thu, 24 Sep 2020 01:42:22 -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=QVtEZrwk; 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 S1727286AbgIXIjU (ORCPT + 99 others); Thu, 24 Sep 2020 04:39:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60108 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727211AbgIXIjU (ORCPT ); Thu, 24 Sep 2020 04:39:20 -0400 Received: from mail-pl1-x644.google.com (mail-pl1-x644.google.com [IPv6:2607:f8b0:4864:20::644]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1B2D8C0613CE; Thu, 24 Sep 2020 01:39:20 -0700 (PDT) Received: by mail-pl1-x644.google.com with SMTP id d19so1327844pld.0; Thu, 24 Sep 2020 01:39:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=jVyFBXyqmDS4jB1M2eyvEy1BeQ0zCzl71j0vO3zYwBw=; b=QVtEZrwkNtcAmHs0xtRsmirz8bEp8ZPqq6D9HGs+VvSMMYMrB9tSbOfA+zdrOEvf6b GozITFVBL9ZLQRvTMEKGwFQvGz5Ng6Ax8YrzcDmKe4Khrg6sygvbUQ3AeXT4bBhRrBIz acthhHowDXBtrwfLrltzZ6fHmDF5lJ1kRbj/5l9TXDwR/8k7P+u2WHiUaJgp2nMRO00F IHU28vtI/mICjjsjZ79VEy5xJuYZJj4htLvjZgelDJp0GoWFSq8yUqz9bM48RBMwXJYp qHMwSYEZZMw5zjijr2kR8kM+P/3iVA+YvTvrnlw0s7sgdPWRMriN9NSRoLmIN/1autdc e16w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=jVyFBXyqmDS4jB1M2eyvEy1BeQ0zCzl71j0vO3zYwBw=; b=dxtZ8SwXoMliR1DGxJT/TGm+r1/j3aKaIO6ipgfTvAJDmeCzM7UNqK0Afbc+O2P6zw 3XDAf0cPD4se5MBwFzWhi8gTGWpFRS2Z6ho6ES2TedoFj3PIUbAFMigr2lH6EwXuuONx MA4mdfxK/Z1K5nAStcPL2SbsDjfbBpJBy4aIbqwyanAAwHVmZrqJlokVEjaAQp1z1x8A FoFUmpyRCyDMP25/1M5VlM2qwMX+uhFap+TKVC3ali15O0QlNytjka8i1hNw8syzfai8 sTb5CQCTLnqxprob5ZyyiYit1ceTdoTwSoGysuCtspg10CseCwqhRSlrpNxeQTYD7pHd JCgA== X-Gm-Message-State: AOAM532BdtomIhEXOWejZqbdFfIih6XNDLQqKzvu0uNUCQyQ2NNBszvi gAkHGBPX+KPR1SbH/UTLKOYK+TNYaTkzQfJEfUe5cJ7CQ/3Zyw== X-Received: by 2002:a17:902:c14c:b029:d2:4345:5a9 with SMTP id 12-20020a170902c14cb02900d2434505a9mr3615948plj.0.1600936759578; Thu, 24 Sep 2020 01:39:19 -0700 (PDT) MIME-Version: 1.0 References: <20200922023151.387447-1-warthog618@gmail.com> <20200922023151.387447-9-warthog618@gmail.com> <20200924023914.GA11575@sol> In-Reply-To: <20200924023914.GA11575@sol> From: Andy Shevchenko Date: Thu, 24 Sep 2020 11:39:03 +0300 Message-ID: Subject: Re: [PATCH v9 08/20] gpiolib: cdev: support GPIO_V2_GET_LINEINFO_IOCTL and GPIO_V2_GET_LINEINFO_WATCH_IOCTL To: Kent Gibson Cc: Linux Kernel Mailing List , "open list:GPIO SUBSYSTEM" , Bartosz Golaszewski , Linus Walleij , Arnd Bergmann Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Sep 24, 2020 at 5:39 AM Kent Gibson wrote: > On Wed, Sep 23, 2020 at 06:41:45PM +0300, Andy Shevchenko wrote: > > On Tue, Sep 22, 2020 at 5:35 AM Kent Gibson wrote: ... > > > + memcpy(info_v1->consumer, info_v2->consumer, > > > + sizeof(info_v1->consumer)); > > > > One line? > > > > It can be now the line length limit has been raised - it just breaks the > old 80 character limit. I really wouldn't care about this if it's only for a couple of characters. ... > > > +static int lineinfo_ensure_abi_version(struct gpio_chardev_data *cdata, > > > + unsigned int version) > > > +{ > > > > > + int abiv = atomic_read(&cdata->watch_abi_version); > > > + > > > + if (abiv == 0) { > > > > > + atomic_cmpxchg(&cdata->watch_abi_version, 0, version); > > > + abiv = atomic_read(&cdata->watch_abi_version); > > > > atomic_cmpxchng() returns a value. > > Yep, it returns the old value - which we don't care about - see below. Then what's the point to read back?.. > > Also there are no barriers here... > > > > No barriers required - the atomic_cmpxchg() is sufficient. > > > > + } > > > + if (abiv != version) > > > + return -EPERM; > > > > I'm not sure I understand why this is atomic. > > > > The algorithm requires some level of protection and atomic is > sufficient. > > > Also this seems to be racy if cdata changed in background. > > > > Can you provide a case? CPU0: CPU1: xchg() ... ... xchg() ... read() -> OK read() ->NOK > The atomic_cmpxchg() ensures cdata->watch_abi_version is only set > once - first in wins. The atomic_read() is so we can check that > the set version matches what the caller wants. > Note that multiple callers may request the same version - and all > should succeed. So, that's basically what you need when using _old_ value. 0 means you were first, right? Anything else you simply compare and bail out if it's not the same as what has been asked. > > > Shouldn't be rather > > > > if (atomic_cmpxchg() == 0) { > > if (atomic_read() != version) > > return ...; > > } > > > > My algorithm allows for multiple callers requesting the same version > to all succeed. Yours would fail the first conditional for all but > the first, and you haven't provided an else for that case... > > ... but it would probably look the same so the conditional is pointless, > or it would reject the request - which would be wrong. > > > But here is still the question: why do you expect the version to be > > changed on background? And what about barriers? > > > > While it is unlikely that userspace will be attempting to use both ABI > versions simultaneously on the same chip request, it is a possiblity and > so needs to be protected against. And better to have the watch request > fail than the read fail or worse - return the wrong struct version. > > The atomic_cmpxchg() is sufficient for this algorithm - no barriers > required. It could also be written with a spinlock but I was trying to > avoid locks unless they were absolutely necessary. A spinlock version > may arguably be more readable, but it would certainly be more verbose, > larger and slower. > > I'm happy to add some documentation to the function if that would help. Yes, I guess documentation is what is eagerly needed here. > > > + return 0; > > > +} > > > +#endif > > > + > > > +static int lineinfo_get(struct gpio_chardev_data *cdev, void __user *ip, > > > + bool watch) > > > +{ > > > + struct gpio_desc *desc; > > > + struct gpio_v2_line_info lineinfo; > > > + > > > + if (copy_from_user(&lineinfo, ip, sizeof(lineinfo))) > > > + return -EFAULT; > > > + > > > + if (memchr_inv(lineinfo.padding, 0, sizeof(lineinfo.padding))) > > > + return -EINVAL; > > > + > > > + desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.offset); > > > + if (IS_ERR(desc)) > > > + return PTR_ERR(desc); > > > + > > > + if (watch) { > > > +#ifdef CONFIG_GPIO_CDEV_V1 > > > > > + if (lineinfo_ensure_abi_version(cdev, 2)) > > > + return -EPERM; > > > > Can't you propagate error code from the function? > > > > You mean: > + ret = lineinfo_ensure_abi_version(cdev, 2) > + if (ret) > + return ret; > > That seems more verbose and less clear. And I'd need to conditionally > declare a ret - as this test is compiled out if CDEV_V1 is not defined. > > I did flip-flop on what lineinfo_ensure_abi_version() should return - > either a bool or an error code. > > If a bool then the code would include the dreaded negative conditional > ;-(: > > + if (!lineinfo_is_abi_version(cdev, 2)) > + return -EPERM; > > so I eventually settled for the error code. But I'm on the fence on > this one and happy to change it if you think the bool form is clearer. > > Cheers, > Kent. -- With Best Regards, Andy Shevchenko