Received: by 2002:a05:6602:18e:0:0:0:0 with SMTP id m14csp1671782ioo; Sun, 22 May 2022 23:50:47 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwMIU8oPgju5g5b7qwyd4P69Ny+Fw4OVsKWIB0441qtu2LNgnKdNCvBDGDmvZVfhalfwrZl X-Received: by 2002:a17:90b:4c0a:b0:1dc:e81a:f0c with SMTP id na10-20020a17090b4c0a00b001dce81a0f0cmr25281097pjb.2.1653288647267; Sun, 22 May 2022 23:50:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1653288647; cv=none; d=google.com; s=arc-20160816; b=prxN9JzOuZlYY+/lrOiIwk3662BG77zyFRR0VAy+gBDZNfn/XggGsLdXAqb393BrCa DO9kypPmzN3YhS63M+1bPiVFKKWoW3q3P3rKR3yOZbgoAvG9OGgfyjNRN3O3v5Wh0j/F 9O7KQZh3+Z0TQXInDJwtjJHrwFqvwgCuDjwka6POmEQR8A2wavmVfD9Up/73cYkHkiDS U0cd8bqDzZJ8ZEcT65gj/Hxsckpy26rzoSH8lDbNjnQxTBm9KOdsM8KZygG9jZ2ByG7v 8UFaIrA56juEs29wZiite7jNjmly7eakqIX24LZcbD0s7hIfF+t9rU5UMozJ+hK/1VeY prJg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=msRtFnx10fRMLFk/oc8m4ErUAwQbBTDjYj4MsWPCcmw=; b=Yo75PaXEUOeYtzpp9CRMRVmxRarTRJY6u+4e7QK1fkKAG6AmihqGVrRSVrIViKTI8k vH+d7Huu0Ku/66Gsajv7YcyQw7OQK17qTBH9LkxVablwIwkP0re4KZsHEbe/jfMIrn/M jNMWB3yJeXn5QhJBkXXYkZCUaK5o8vR1RPK3V1METRIsDaGvspg1f3k6rPCyAZXb4/Wd hRStx6cDWSrX5XZzBEfFO/+LLZGuxQimhaRDtNDlOe24rtJ7sZg5YfUfn0PdTXK+2P+9 gyo3n+uyVkF+f+fBDgz//Iry1WFbflwlzMm3/GUpZ+uxRUrocd4SC/F6u//MXdNNyhmT iZSQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=IDtRu+QK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id h67-20020a62de46000000b0050dcf85b9e1si11750790pfg.141.2022.05.22.23.50.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 22 May 2022 23:50:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=IDtRu+QK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 9E40353E0B; Sun, 22 May 2022 23:19:31 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239086AbiEVK2S (ORCPT + 99 others); Sun, 22 May 2022 06:28:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46274 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230336AbiEVK2P (ORCPT ); Sun, 22 May 2022 06:28:15 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D59D039838; Sun, 22 May 2022 03:28:13 -0700 (PDT) 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 ams.source.kernel.org (Postfix) with ESMTPS id 82CCFB80B00; Sun, 22 May 2022 10:28:12 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4A85AC385AA; Sun, 22 May 2022 10:28:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1653215291; bh=78L9xq8+jayZIp2ngKH0d9f+WN+dtJ0EX80ikGjIYVk=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=IDtRu+QK7A9Ryg6Ai2i06P8ZcyfR2npgP2nPRrw1Cq3y27U85Q3KnNf9SJPPPRPGm QvDb/Vc7KPqjvJi5Bxc+7CccDClcHq+lnS21WsyoH8I3IRfNaxR2vwHibteAFfuXwJ EflVofSoRmKoE2d77snCCHsIk+mgTXXfTte8NjgqlC9k2PtbAKX1yy5k0AB8fOAJvv ecIXxLLabXgZOWuAca0BRbGuOEK+ktHmrWOiaNtlwXqrrw0sMAu+9KI8Uq4wpCIBT2 p5sjEqwBxzOh0nJftNtD7NMlX37Dx5DAqZXWoSOYb2+32SSVR/yxxrfpMTkXd8vfjS kJheQoEl9e15w== Date: Sun, 22 May 2022 11:36:54 +0100 From: Jonathan Cameron To: Dmitry Rokosov Cc: Jonathan Cameron , "robh+dt@kernel.org" , "stano.jakubek@gmail.com" , "shawnguo@kernel.org" , "lars@metafoo.de" , "andy.shevchenko@gmail.com" , "stephan@gerhold.net" , "linux-iio@vger.kernel.org" , "devicetree@vger.kernel.org" , kernel , "linux-kernel@vger.kernel.org" Subject: Re: [PATCH v1 2/3] iio: add MEMSensing MSA311 3-axis accelerometer driver Message-ID: <20220522113654.0e3c0023@jic23-huawei> In-Reply-To: <20220518122515.aby5lbb4xusr6pdt@CAB-WSD-L081021.sigma.sbrf.ru> References: <20220419154555.24191-1-ddrokosov@sberdevices.ru> <20220419154555.24191-3-ddrokosov@sberdevices.ru> <20220420115023.00006a25@Huawei.com> <20220426172406.s4h6g7nrpytaq263@CAB-WSD-L081021.sigma.sbrf.ru> <20220518122515.aby5lbb4xusr6pdt@CAB-WSD-L081021.sigma.sbrf.ru> X-Mailer: Claws Mail 4.1.0 (GTK 3.24.33; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-2.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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 Wed, 18 May 2022 12:25:59 +0000 Dmitry Rokosov wrote: > Hi Jonathan, > > I have two items to be discussed about iio_trigger_get(). > Please see my questions below and correct me if I'm wrong. > > On Tue, Apr 26, 2022 at 08:24:10PM +0300, Dmitry Rokosov wrote: > > > > + "%s-new-data", > > > > + indio_dev->name); > > > > + if (!msa311->new_data_trig) { > > > > + dev_err(&i2c->dev, "cannot allocate new data trig\n"); > > > > + err = -ENOMEM; > > > > + goto err_lock_destroy; > > > > + } > > > > + > > > > + msa311->new_data_trig->dev.parent = &i2c->dev; > > > > + msa311->new_data_trig->ops = &msa311_new_data_trig_ops; > > > > + iio_trigger_set_drvdata(msa311->new_data_trig, indio_dev); > > > > + indio_dev->trig = msa311->new_data_trig; > > > > > > This will create a double free if you were to change the trigger. > > > indio_dev->trig = iio_trigger_get(trig); > > > > > > > I didn't take into account other trigger usage. > > I'll rework this place for the v2. > > > > The first one problem is module_get() calling for trigger get() > semantic. > I've applied iio_trigger_get() function to acquire module refcnt, > but I've faced with rmmod busy problem. IIO driver module doesn't want to > stop and unload due to not having zero module refcnt. One option is to remove the trigger from sysfs - write an empty string current_trigger, but you are right this is a bit of a mess. Probably the best option is just don't assign the trigger automatically at all. This was what we almost always went with in the past. If a driver supports multiple triggers (and if it doesn't why expose the trigger at allm there is no obligation to do so?) then it's a policy decision to associate a trigger in the first place so shouldn't really happen in kernel. There is a corner case for drivers which can only use a particular trigger, but that trigger can be more generally used (validate_trigger provided, but not validate_device). Another corner case is drivers that didn't expose a trigger, but later gain support for other triggers then we need to set the default value. > Syscall delete_module() tries to stop module first and after calls > driver exit() function (which executes devm_* handlers inside, including IIO > trigger unregister). It means we have the chicken or the egg dilemma here. > Module can't be unloaded until module refcnt is not zero and we can't > execute IIO trigger unregister (decrease module refcnt) only when module > refcnt is zero. > I suppose the possible solution to such a problem is a different semantic > for internal triggers (inside driver itself) and external drivers (like > hwtimer trigger). What do you think? Potentially though it's going to be tricky as a driver doesn't generally have any way to know they are internal and we need to be careful not to underflow the reference counts. We could hid a flag somewhere and add an iio_trigger_get_same_owner() or something that sets that flag allowing us to decide not to drop the reference count it if is automatically unassociated. In the path where you get: 1) iio_trigger_get_same_owner() on probe 2) sysfs write changes to another trigger. 3) sysfs write back to original trigger it is reasonable to assume the need to clear the trigger before driver removal is possible, whereas clearing the trigger association if only step 1 happened is no intuitive. > > The second one issue is located in the different IIO drivers. Some modules > call iio_trigger_get() before iio_trigger_register(), trig->owner is not > initialized to the right value (THIS_MODULE) and we don't acquire refcnt > for proper driver object. Ah. Good point. I guess we missed that when we were moving over to automated setting of the module. > I'm going to send patchset to problem driver set, but I can test only > buildable status for such modules, are you okay with that? That should be fine. I can't immediately think of a case where it would be a problem as the iio_device_register() should be later and until that happens nothing can turn on the trigger - so there shouldn't be any other races. Jonathan >