Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp2933704ybb; Sun, 5 Apr 2020 21:19:05 -0700 (PDT) X-Google-Smtp-Source: APiQypJIMhDf1d4Knyvn3pS8031EJIqsUxodmMrmBRGbaTx1haYv4+P12inmKr2RPX5RpYOv2BlO X-Received: by 2002:aca:47c8:: with SMTP id u191mr11892145oia.170.1586146745421; Sun, 05 Apr 2020 21:19:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1586146745; cv=none; d=google.com; s=arc-20160816; b=xvAEBPzRW8pRJys8jkfy2nbmVrth1mMHpx3q2nYqqlPeEG5Ty6P8xttbTcrIq/U/OW K0jW3UUEaIq131NcqNRwxfPxg1N+BSKyvqRxbjBCGwMVX1T40QvUCmZqB+jXHfNFI5UQ 94lHYSgzXueghFH8y3Ww2qIHkj1IrYLMgv7N1UFAg/OyOo6p0c62K9vK4qNDBFBhfOFo 5rt55oTFqS/75avJ9Q1RuzdqQg3clxzfNRDH2SaJbOsy92wte112qjMDavgyg7sG+nR2 yQ8NePWjYlziEFxiU6AbPHHDMQdGwtl5sOCqbBEtt6hBd9vqcJbRfpSulgeJ87wWLaOj fcGg== 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=4kyy5QuLqadlwU13vnAmK3M+XjcrYui6tlBrLCLcHIY=; b=bVHxO9jK4RASudHqNdY4nb9EURYzuUN3jXprmRIKn1S9w/TRSR5huDMlm//eDgUgl9 nT0J4rmFYPWM4498+Jpxri4cvdsSmABpcU4I/wfcYxXQnWlmSUc1Upuq3GbxYKIDlkeT SKjYxOQnGKsxWy7UEnKjJF/trlKmPpxA63wP2NG0ZpAJX30FOHE0G7fslTvY49W12UXI kzg4bJhA2TCd6O8xG2n0TAkbDDn+206hGyLZ1Yjn64FCBHa+EAvnsfX5+Y2Ttmh8PAtk vrm014DaSEL693+PEzK2oOC0bt+9vysubvBjZ26+S4zYiocPyFGC4zJvOWMaIneY9F8K XKCQ== 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 f12si7829300otc.147.2020.04.05.21.18.51; Sun, 05 Apr 2020 21:19:05 -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 S1726408AbgDFES2 (ORCPT + 99 others); Mon, 6 Apr 2020 00:18:28 -0400 Received: from esa2.mentor.iphmx.com ([68.232.141.98]:28337 "EHLO esa2.mentor.iphmx.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725601AbgDFES1 (ORCPT ); Mon, 6 Apr 2020 00:18:27 -0400 IronPort-SDR: Ni8MtU0rJJpVR4l9FjeVfxTyuH1oSMqr3+kyLKYYjvYauQfwfkAFdVFfENMCnsjbElQnTBUVuj mbXDudO+ScuhjqW3GyPCONzkUZYDqitNilICWPZmA8g6zRlvCNmAS3F/sRghbhuql0/kJS50fC 9uHkOcjdYYS6WX0ZWjanBjOCglfrRRHg7N3HpSVbQ/M+LVvVOElxLORsSpKuwz4/kAkxUNHbW6 OlnR0UkINdFnkVF0iSG+rWod0UiugDz6O9W+w2RMQiuLFaD1WQQ+t/wjtDjql00ZgIo2KlxBeL LPY= X-IronPort-AV: E=Sophos;i="5.72,349,1580803200"; d="scan'208";a="47376664" Received: from orw-gwy-02-in.mentorg.com ([192.94.38.167]) by esa2.mentor.iphmx.com with ESMTP; 05 Apr 2020 20:18:26 -0800 IronPort-SDR: DBR+18PDclapNTKp4JqtQltGoJHolxARNGD5NY7LCmWz+dXxDjHa/TY6XMEBYCDWnuwCohZQAh cdLrXIWc0hdH+WHUcS6DFotq9di5G00ZLMt/lP8fo0x9Bl+PP8xEi2X3L+rTxMhQeSoWVvP7O2 0h/tzHcnpn/l5EZLps8bLc/UmUSePbtjIoZYXBvRPKtd0zruZ4JOeYkjHV3G2iWKkhb0gw8bt7 LgMsPBBmiVe7EO61VYQ7XNjpcT4+4cZ/jTOC9MF1u/eROgufi/dfD918f1/DC/zKgvShVdoT6d 85w= 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> From: "Wang, Jiada" Message-ID: <500c814a-b0f4-db9f-30f6-bc6ac985c5e2@mentor.com> Date: Mon, 6 Apr 2020 13:18:20 +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: Content-Type: text/plain; charset="utf-8"; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit X-ClientProxiedBy: svr-orw-mbx-02.mgc.mentorg.com (147.34.90.202) 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 Dmitry On 2020/04/02 23:00, 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 looking at this again and the original tear-down order of the > mxt_remove() looks okay, so no need to change it. > > Reading the commit message, it says that request_firmware_nowait() races > with kfree(data), but that can't happen because the data is > resource-managed and request_firmware_nowait() bumps device's use-count. > > https://elixir.bootlin.com/linux/v5.6.2/source/drivers/base/firmware_loader/main.c#L1043 > > I think this patch was ported from some very old kernel version and it's > simply not applicable to the upstream anymore. > I had some test, and confirmed you are right, this commit is no longer applicable to upstream, but as discussed in another patch, disable_irq() need to be moved after remove of mxt_fw_attr_group. I will add this change in a new commit. Thanks, Jiada