Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp1757311ybb; Thu, 2 Apr 2020 06:52:11 -0700 (PDT) X-Google-Smtp-Source: APiQypKHPBOy0tn1npoxFZgvWpj0MDvT48mLWpfmvoxuO7tK08xxUTdtV1JYNcvKEGi5CPg+XaSI X-Received: by 2002:a9d:a51:: with SMTP id 75mr2368012otg.112.1585835531132; Thu, 02 Apr 2020 06:52:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1585835531; cv=none; d=google.com; s=arc-20160816; b=NnCt+Bkcs0ngX4VGGq5VCAp9l+3Lvm/ZlAw47QW33qs+mWJvg19D9hSCu45JCzCYqK mqOS5rNKIQWHjeqWH+vxD0mdAQeJmGy94CH8XwLCcziTv673jYOwT+XxBl7R/z5qpLph a6Uj+bsrl5+PVxZQthnI/7PoyHv3o9RPWflLC8AfcR46iaGwsimMdHQwB48tFvzmFqCi FYLvPwAbs/zZ3YkR0eCUp5HgtYOX1RHpIcsm/bDjqkpfT5LFB0Crh2TW43YTmP21o9Jq nzxOHCpop4zvbufRBNOC/xQpHnBrSe3d3YAwPNFc4pUYiQD9S3A60LtkN2d7SCVEOUM6 u/dA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:references:cc:to:from:subject:dkim-signature; bh=9cGC5mQvHMqoehJiKLkoHPLp5uGKyoKYNEExUqJ4aqQ=; b=feCd/jugxAI2mANoftrQzgGbgfAnhWkg8b+di1Y+cq4/M5fPn0/QPuJhS0OLOxH001 9NN07NzxfZAEt0dlDkuTAAIovF7i5vPVL0Sc3SJIezdv1FUiGIyKN4ChIIrO2tnagB/7 /Qjh3FOfbg/38kxG5p7XL06NRdKinl2sRGAlVjEfSexoizEkb17xrIhAYKrTJNV9uhLa yyo08Pt4gxMUasS/gwv8sl6fTH5+k1o2AOJxgGPy9k/h2/XARtHrv9OvsIDSI/G3jWzh 0xwzKM40TUoGuwerB6t/XYXd0ADyYTNFH2aRJmerVqLVvYR4GBr5LxCHi9SJDRmP7vK8 LTSg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=LWLC1DlN; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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. [209.132.180.67]) by mx.google.com with ESMTP id t7si2344173otj.7.2020.04.02.06.51.57; Thu, 02 Apr 2020 06:52:11 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=LWLC1DlN; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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 S1729123AbgDBNpC (ORCPT + 99 others); Thu, 2 Apr 2020 09:45:02 -0400 Received: from mail-lf1-f66.google.com ([209.85.167.66]:38443 "EHLO mail-lf1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726927AbgDBNpC (ORCPT ); Thu, 2 Apr 2020 09:45:02 -0400 Received: by mail-lf1-f66.google.com with SMTP id c5so2774274lfp.5; Thu, 02 Apr 2020 06:44:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:from:to:cc:references:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=9cGC5mQvHMqoehJiKLkoHPLp5uGKyoKYNEExUqJ4aqQ=; b=LWLC1DlNtFSk4Ta3m/l+LNcExM+8Wwvu/aVszUr1Hfa3cckku5xGjXesROqZcA/qkj BNyFQ9rZSsg9WQKEF/XOM3AEazD1CpkjriKuy/GEts8T63aJrkOnsUIfQonqZTcFn+dn iv3er0uURmatHGm/i0/Luv43RYp7XKc8QTeG3Tv6N1bNNQehfqWaYFGejJiDMFeyxBUU 3hs8Sd4ZGYv2fdrBo3TvRAtaMNcHKQy5sCOKTYf5cc/rJglqt72xvov29ZXfnJ0cimXw tNqobFlLZMg/6breqVtsymevsKdJOuM4Px0EUKzZN0c35kX6fuoj27EMi2eF5/i4hf0j sjHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:from:to:cc:references:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=9cGC5mQvHMqoehJiKLkoHPLp5uGKyoKYNEExUqJ4aqQ=; b=a1oAEfgkEM14Q7n+q0yXcfKlBLpP7M6RDrzp2zla67C8Vhm5MPYHQSK+M9FfTPj+EJ wlK+2oFa7jb//q7FKK/i5IQpyP9dvCBg7VH/hpbZz3US5XoeeztIbiaDa4JqqDSzvwD0 rWQfigsiV4uxUxL4bdyJOyltcLgG5ycV2yidBIkKS6Agkbq7avr0ZsuA7jGbk0VqAFt5 znE/cCq6kO5N+jI0NfqOlOjjrXQvzWD40sJAcbPfhqbJe3EGLmP64phSl6baB3FCE4Vo xOOYZgmFKKUmaqE/tfBRTkLg81fdqUFnTXSO9/VyrMWDG6qjzaSLaW6FH9QKV7rwk5j0 zWPA== X-Gm-Message-State: AGi0PuZkMXlaCZclPzkLLgQmqV4fkzOw43qltNM6VgHZOFfYbJr2KWuo 4eEl/uggGHlNTXNdl2XlEgc= X-Received: by 2002:a05:6512:3189:: with SMTP id i9mr2194428lfe.178.1585835097501; Thu, 02 Apr 2020 06:44:57 -0700 (PDT) Received: from [192.168.2.145] (ppp91-78-208-152.pppoe.mtu-net.ru. [91.78.208.152]) by smtp.googlemail.com with ESMTPSA id l20sm3328449lji.74.2020.04.02.06.44.56 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 02 Apr 2020 06:44:56 -0700 (PDT) Subject: Re: [PATCH v10 54/55] Input: atmel_mxt_ts: Implement synchronization during various operation From: Dmitry Osipenko To: "Wang, Jiada" , nick@shmanahar.org, dmitry.torokhov@gmail.com, jikos@kernel.org, benjamin.tissoires@redhat.com, bsz@semihalf.com Cc: linux-input@vger.kernel.org, linux-kernel@vger.kernel.org, erosca@de.adit-jv.com, Andrew_Gabbasov@mentor.com References: <20200331105051.58896-1-jiada_wang@mentor.com> <20200331105051.58896-55-jiada_wang@mentor.com> Message-ID: <12b913cd-cc0e-73b3-7ae5-8589ad5e968b@gmail.com> Date: Thu, 2 Apr 2020 16:44:55 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.5.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 02.04.2020 16:24, Dmitry Osipenko пишет: > 02.04.2020 14:50, Wang, Jiada пишет: >> Hi Dmitry >> >> On 2020/04/02 1:04, Dmitry Osipenko wrote: >>> 31.03.2020 13:50, Jiada Wang пишет: >>>> From: Sanjeev Chugh >>>> >>>> There could be scope of race conditions when sysfs is being handled >>>> and at the same time, device removal is occurring. For example, >>>> we don't want the device removal to begin if the Atmel device >>>> cfg update is going on or firmware update is going on. In such >>>> cases, wait for device update to be completed before the removal >>>> continues. >>>> >>>>      Thread                                          Thread 2: >>>> =========================                       >>>> ========================= >>>> mxt_update_fw_store()                           mxt_remove() >>>> mutex_lock(&data->lock)                         ... >>>> mxt_initialize()                                //Tries to acquire lock >>>>    request_firmware_nowait()                     mutex_lock(&data->lock) >>>> ...                                             ==>waits for lock() >>>> ...                                             . >>>> ...                                             . >>>> mutex_unlock(&data->lock)                       . >>>>                                                  //Gets lock and >>>> proceeds >>>>                                                  >>>> mxt_free_input_device(); >>>>                                                  ... >>>>                                                  >>>> mutex_unlock(&data->lock) >>>>                                                  //Frees atmel driver >>>> data >>>>                                                  kfree(data) >>>> >>>> If the request_firmware_nowait() completes after the driver removal, >>>> and callback is triggered. But kernel crashes since the module is >>>> already removed. >>>> >>>> This commit adds state machine to serialize such scenarios. >>> >>> Won't it be easier to bump driver's module use-count by __module_get() >>> while firmware is updating? Or remove sysfs during of mxt_remove()? > >> >> thanks for your inspiration, I will replace state machine with module >> use-count. > > I'm actually now thinking that the suggestion about the module-count > wasn't very correct because this won't really help in regards to > mxt_update_fw_store() / mxt_remove() racing. > > I see that mxt_remove() already invokes the mxt_sysfs_remove(), which > should block until mxt_update_fw_store() is completed, shouldn't it? > > I guess the kfree(data) isn't the real cause of the problem and > something like this should help: > > diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c > b/drivers/input/touchscreen/atmel_mxt_ts.c > index b2edf51e1595..4e66106feeb9 100644 > --- a/drivers/input/touchscreen/atmel_mxt_ts.c > +++ b/drivers/input/touchscreen/atmel_mxt_ts.c > @@ -4254,6 +4254,7 @@ static void mxt_sysfs_remove(struct mxt_data *data) > struct i2c_client *client = data->client; > > sysfs_remove_group(&client->dev.kobj, &mxt_attr_group); > + sysfs_remove_group(&client->dev.kobj, &mxt_fw_attr_group); > } > > static void mxt_reset_slots(struct mxt_data *data) > @@ -4649,31 +4650,19 @@ static int mxt_remove(struct i2c_client *client) > { > struct mxt_data *data = i2c_get_clientdata(client); > > - mutex_lock(&data->lock); > - if (data->e_state == MXT_STATE_UPDATING_CONFIG_ASYNC || > - data->e_state == MXT_STATE_UPDATING_CONFIG) { > - data->e_state = MXT_STATE_GOING_AWAY; > - mutex_unlock(&data->lock); > - mxt_wait_for_completion(data, &data->update_cfg_completion, > - MXT_CONFIG_TIMEOUT); > - } else { > - data->e_state = MXT_STATE_GOING_AWAY; > - mutex_unlock(&data->lock); > - } > + mxt_sysfs_remove(data); > > - disable_irq(data->irq); > - sysfs_remove_group(&client->dev.kobj, &mxt_fw_attr_group); > if (data->reset_gpio) { > sysfs_remove_link(&client->dev.kobj, "reset"); > gpiod_unexport(data->reset_gpio); > } > + > mxt_debug_msg_remove(data); > - mxt_sysfs_remove(data); > mxt_free_input_device(data); > mxt_free_object_table(data); > > if (debug_state) > cancel_delayed_work_sync(&data->watchdog_work); > + disable_irq(data->irq); > > return 0; > } > I'm also wondering why dev_attr_update_fw needs a separate attribute_group, couldn't it be moved into mxt_attrs[]?