Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp2929640ybb; Sun, 5 Apr 2020 21:11:16 -0700 (PDT) X-Google-Smtp-Source: APiQypJw9wo/81gHyogoCKko7Qx1iSRdGjHfQmUO00xfcTXqGRMsSatX4B79cLczmDbm3G2oZ+E3 X-Received: by 2002:a05:6830:3150:: with SMTP id c16mr15175823ots.251.1586146276615; Sun, 05 Apr 2020 21:11:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1586146276; cv=none; d=google.com; s=arc-20160816; b=dABb6vM/d1VfQgblAdEWuWbpHJnd1C/TD9788SYZULG7Mdjgd6u3dSDK0m6mGuBeoX POBwVeY+70uYcMm279iRWNn498axVBRgDSquybn3D5eqP+hurztVb6rRC7b1RoiEYH1C ZeWV3EJ51t8a2AR9c/qOhwT2uol7bFp0u75XIJCooIKaFf896ySuphy4bOhuoQ2sS5bf Qw72P3ZZ8VlwN5BYOtesqj/siYayjR5dFPFljMeb2wW7tzDyNUBH4/IxSFSWeCtpVHyw t7YUwmEW5sAIIUEXFUDbEknp/7EgyBvffUY3Fqig/uCMVzAZVBPh45E/f8aPRqynnATx tA6g== 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:from:references:cc:to:subject:ironport-sdr:ironport-sdr; bh=7zIbfc+4QS2ZvxMFPJADmcLt8cLGQHRj5umU+SPkpKw=; b=WOecn/xdgqC5lgwDxtl+jA96YP/U5GSQ7SO46Kp95+eQZSP+dcWRk5vgmtPUQWHVsB zT6TVor2etqu3t+xUx2Tsmmaw0eJ3cDk4epTXChwEm+AHISes9L712d+vYTneVa/+oIc aG9LmHlGowQu/nywl6xr8rdwQKvGX1+ZfKWcIZM8yrrkU5x+fMUvH6EbquIK30qYRcsh 3H+Ty/5h8iHxj07ABBRFfl/ye3EIq3oiMGyes0HCn6KUwMeRygOiHYSi9hEmTpk2uFq6 r/Dc5FxTBbW5pm9WSvcNCeuEFoakep7CFhduMIk0pjlXwu6PMLxToBxu+FYUW+KlXSPs VA/Q== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id s2si1242035ooh.88.2020.04.05.21.10.53; Sun, 05 Apr 2020 21:11:16 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726050AbgDFEHk (ORCPT + 99 others); Mon, 6 Apr 2020 00:07:40 -0400 Received: from esa3.mentor.iphmx.com ([68.232.137.180]:63907 "EHLO esa3.mentor.iphmx.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725768AbgDFEHk (ORCPT ); Mon, 6 Apr 2020 00:07:40 -0400 IronPort-SDR: xU+VbEKldOlnKkVcx+JEOjZ8T1FbRj0/omO4mNtzlZtQp/YRaF/LcJ3tnnXk1KxJBJobvPnPlf XtIqI4w46vVt9D/aI4Mzs/gWDv06tnNeKGgnzXvEhhg8VHDAKJQQBF1C1F15W/p6hj2SNkLxFU s+EHl3OpFZk6JUhd4HEnj4Q7Oh32YvnwIvJTciD7nZccvzP/R+iK4rR/ULk4IcfpJS5qcCngtc GwFxFIR+X8aVDp8F3v3/T/z+j+KWJOcll7Oyk+MaGmsqf0pzn22zw3YDcYoDrco4JMPEL6q16s muM= X-IronPort-AV: E=Sophos;i="5.72,349,1580803200"; d="scan'208";a="47461306" Received: from orw-gwy-02-in.mentorg.com ([192.94.38.167]) by esa3.mentor.iphmx.com with ESMTP; 05 Apr 2020 20:07:39 -0800 IronPort-SDR: zofhbpvw1FMAY96LhnFDLug/QL2Adiu8gPR23GZo0AuxoVZyJ+N0MTl/cBxR+Uv1PCiC0/fyMr yntkhrPMBKU9RqrliWOP7r4cyicBeCFGnSLGaUMxmlr3tPEKPCgmoLSdffP1VY3Rs8EVY+MC8O +hfkpm5YVQWE+ZOt9psgiPYtQpF4BnVz1E9B3WIbB/3ohxR9m7yhaxs9baUFQl5aw9ei7470Ix l4rKoMfCM1BRm5CnTHb+UTuhAwLxYgQAFtvN+g9GBDCdD/1TOPl/3/rKX72C3tj0ktetdM1EGI 2x0= Subject: Re: [PATCH v10 54/55] Input: atmel_mxt_ts: Implement synchronization during various operation To: Dmitry Osipenko , , , , , CC: , , , References: <20200331105051.58896-1-jiada_wang@mentor.com> <20200331105051.58896-55-jiada_wang@mentor.com> <12b913cd-cc0e-73b3-7ae5-8589ad5e968b@gmail.com> From: "Wang, Jiada" Message-ID: <7f5435d1-27b0-c337-224c-c78acd989d9e@mentor.com> Date: Mon, 6 Apr 2020 13:07:33 +0900 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.6.0 MIME-Version: 1.0 In-Reply-To: <12b913cd-cc0e-73b3-7ae5-8589ad5e968b@gmail.com> Content-Type: text/plain; charset="utf-8"; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit X-ClientProxiedBy: SVR-ORW-MBX-07.mgc.mentorg.com (147.34.90.207) To svr-orw-mbx-01.mgc.mentorg.com (147.34.90.201) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Dmitrij On 2020/04/02 22:44, Dmitry Osipenko wrote: > 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[] Separate sysfs into different groups are done by commit "Input: atmel_mxt_ts - rework sysfs init/remove" I think the main purpose, is to remove other sysfs entries when firmware is being updated. Thanks, Jiada