From patchwork Sun Jun 24 15:05:21 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Akinobu Mita X-Patchwork-Id: 10484229 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 56358601C3 for ; Sun, 24 Jun 2018 15:05:50 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 35902288E8 for ; Sun, 24 Jun 2018 15:05:50 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2853D28985; Sun, 24 Jun 2018 15:05:50 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 90CA5288E8 for ; Sun, 24 Jun 2018 15:05:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751834AbeFXPFr (ORCPT ); Sun, 24 Jun 2018 11:05:47 -0400 Received: from mail-pf0-f195.google.com ([209.85.192.195]:42339 "EHLO mail-pf0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751792AbeFXPFq (ORCPT ); Sun, 24 Jun 2018 11:05:46 -0400 Received: by mail-pf0-f195.google.com with SMTP id w7-v6so5292132pfn.9 for ; Sun, 24 Jun 2018 08:05:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=tRuwSImrstOGRBMqyg9t/9wfhL0cbqEx2nd6F9HoiaM=; b=gt6rGxOly8pvcWiaKmdD0Q0QXB3H9Q6CS65EDHMxputTKIkpCpkCXGtMUnyTMT00ML WUH/RdmmakP6rx+gEc2P1sNnFZ+iTDUzUCVD/uD61irVzgHT5LVHwQIwCpsAJxdPnR8a 7hrDDKYRso87+WW6DLivBS19ZuVxLmFAhbTdm3+xKA4BbLIm++XYpMe9HjlDzj28H9qx pXoiZefVHcw8wcN+5U7RrYAiwwO8kn5XAoAVxPbp/jNA33EzTZ5x/9+rCc7TjQjpp1hf lSGMXBpZtvUemSptucM2xTzJ6FhG5h29xEzHxmjxRIZovuYDkBMfNaS1gAgaqIx59Yw4 XPiA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=tRuwSImrstOGRBMqyg9t/9wfhL0cbqEx2nd6F9HoiaM=; b=opocoGiQhGoAfyd0BHVxafY5HdkPSVa1S8HtozKLoxjY5765myiGbNB/J3oZKb82z6 9IqDObaflMlnAnQUfXfYv6M9pRIdTVMcJ2ZqJYytZGpS6JioD3kGgJw/SeuG9YVeoj48 V1SqQ9h3XjhtsVrJ/YCM+0BW3+bUvOm80aqfjzprGgcp3IWlV62ITvjhwyqHP9ukWpdZ M951tM4Qr0Ot1WOUYu2mmJMDTj67oW3heF4CjwFMaIZksP4qyeuwcCpRxCmBroSAyCn8 zICW3A4OZdHFuKrjpMJOX8pM9QcaQ+hLVK/kOvEtEUZhSMpTj+gqactq0SZ5JBdLg4Pq CUDA== X-Gm-Message-State: APt69E3y9nwjkxpAKZQYpc9DWe4i3VzNtHdeCRMpneR/LWtzjJ67wBQv PxLhzNbcB9400jxe/tVlWdNHIg== X-Google-Smtp-Source: ADUXVKJjIW54uRJdDuCipc9DV1b0l4g4swNfygEQv83ueLJ3UepQ5oeWLJwUV50iOqR4YDCauEdkjw== X-Received: by 2002:a62:a309:: with SMTP id s9-v6mr1069338pfe.80.1529852745941; Sun, 24 Jun 2018 08:05:45 -0700 (PDT) Received: from localhost.localdomain ([240f:34:212d:1:cd2d:af1e:c9ba:2bdf]) by smtp.gmail.com with ESMTPSA id t5-v6sm18375498pfh.32.2018.06.24.08.05.43 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 24 Jun 2018 08:05:45 -0700 (PDT) From: Akinobu Mita To: linux-iio@vger.kernel.org Cc: Akinobu Mita , =?UTF-8?q?Stefan=20Br=C3=BCns?= , Jonathan Cameron Subject: [PATCH] iio: adc: ina2xx: avoid kthread_stop() with stale task_struct Date: Mon, 25 Jun 2018 00:05:21 +0900 Message-Id: <1529852721-17828-1-git-send-email-akinobu.mita@gmail.com> X-Mailer: git-send-email 2.7.4 MIME-Version: 1.0 Sender: linux-iio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP When the buffer is enabled for ina2xx driver, a dedicated kthread is invoked to capture mesurement data. When the buffer is disabled, the kthread is stopped. However if the kthread gets register access errors, it immediately exits and when the malfunctional buffer is disabled, the stale task_struct pointer is accessed as there is no kthread to be stopped. A similar issue in the usbip driver is prevented by kthread_get_run and kthread_stop_put helpers by increasing usage count of the task_struct. This change applies the same solution. Cc: Stefan BrĂ¼ns Cc: Jonathan Cameron Signed-off-by: Akinobu Mita --- drivers/iio/adc/ina2xx-adc.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/drivers/iio/adc/ina2xx-adc.c b/drivers/iio/adc/ina2xx-adc.c index 0635a79..d123962 100644 --- a/drivers/iio/adc/ina2xx-adc.c +++ b/drivers/iio/adc/ina2xx-adc.c @@ -30,6 +30,7 @@ #include #include #include +#include #include #include @@ -826,6 +827,7 @@ static int ina2xx_buffer_enable(struct iio_dev *indio_dev) { struct ina2xx_chip_info *chip = iio_priv(indio_dev); unsigned int sampling_us = SAMPLING_PERIOD(chip); + struct task_struct *task; dev_dbg(&indio_dev->dev, "Enabling buffer w/ scan_mask %02x, freq = %d, avg =%u\n", (unsigned int)(*indio_dev->active_scan_mask), @@ -835,11 +837,17 @@ static int ina2xx_buffer_enable(struct iio_dev *indio_dev) dev_dbg(&indio_dev->dev, "Async readout mode: %d\n", chip->allow_async_readout); - chip->task = kthread_run(ina2xx_capture_thread, (void *)indio_dev, - "%s:%d-%uus", indio_dev->name, indio_dev->id, - sampling_us); + task = kthread_create(ina2xx_capture_thread, (void *)indio_dev, + "%s:%d-%uus", indio_dev->name, indio_dev->id, + sampling_us); + if (IS_ERR(task)) + return PTR_ERR(task); + + get_task_struct(task); + wake_up_process(task); + chip->task = task; - return PTR_ERR_OR_ZERO(chip->task); + return 0; } static int ina2xx_buffer_disable(struct iio_dev *indio_dev) @@ -848,6 +856,7 @@ static int ina2xx_buffer_disable(struct iio_dev *indio_dev) if (chip->task) { kthread_stop(chip->task); + put_task_struct(chip->task); chip->task = NULL; }