Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp1066163ybl; Wed, 14 Aug 2019 10:09:35 -0700 (PDT) X-Google-Smtp-Source: APXvYqwjZV4PkhYj7In0VoIQ+uKCAbK0nE1shdQnnGngX/EF0IninVbuv3cz5vN9CfhKDanWx2uO X-Received: by 2002:a63:5550:: with SMTP id f16mr197029pgm.426.1565802575596; Wed, 14 Aug 2019 10:09:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565802575; cv=none; d=google.com; s=arc-20160816; b=tB3QVpXT2xa7RMqgvbfiGYOIgEGKOS92Mj7idoYJQCEu6NyHRIJSHy0GAZ+AvkCtyH zbLBG5S9ZvvPkFQUWhJ/yfkNV4mwbSd7OogdccCwHQWLTKihm9zhnDLGlvh272rGvggB IB0uTLRcgIZd1JaGDiYILGy0hAo5PDk5VStubgM4zfSO42+SVrhVm400wrWyeSHaapA8 x2AJqq9fIntmbEscYooqoXIo9+WSrdrT18bpKzZpBzLpDsq/NMft7PCuYgu2OQEtJS4/ MduWFRNRsabTc/5R6cRzCfQbjdJs90NTzDhRYMb0A1qjJbCgmCzkg26+j10/N4kAcdl1 wW1A== 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:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=vNRfoX1ZhlDwmAbmXpnZyi9M2VJ05sUN3H4Hp0hspGE=; b=v4PpGCrC5ZaT5X3SKoPhyav2Z7Oeo2nZ1v0nM/1jl1+1ghaRdjQ8S3XX4RYOCZzH7S wbgqcuD3yHOJGNNqfINisl6uTyclS/oZA6ngpsHPNMTB8CFi9stqOKq+i9NhG2uX/ZJk E0EXA7iIxWsLBjVF/3kxxcIejBJTLOrduCEA5xrKNAEEGjDHqtQ6VPcImvafJM29rVTL 5zGQ2jNxjQqI5NrDgPoHUlvgiaxWCQCGOrWwtDjj+EMo37AQFiurF+MoSVQ3AWjWBnDB 0x0LjOQX6l+7Wbeq3bNcGYkW8n/dgW0SVmTlDh8u5GdRPvifRv/fx27MxatvSpr5kogC efew== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=T4pl3HSQ; 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 d3si189322pjo.73.2019.08.14.10.09.19; Wed, 14 Aug 2019 10:09:35 -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=@kernel.org header.s=default header.b=T4pl3HSQ; 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 S1730076AbfHNRIK (ORCPT + 99 others); Wed, 14 Aug 2019 13:08:10 -0400 Received: from mail.kernel.org ([198.145.29.99]:57954 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729518AbfHNRIG (ORCPT ); Wed, 14 Aug 2019 13:08:06 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 0DE1B216F4; Wed, 14 Aug 2019 17:08:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1565802485; bh=0cb6Sy4FNqj3n85nkDZG+leLNXb1tr//0ldqxlU9NSs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=T4pl3HSQ0+L7HEPvzhpxdYJo9Dq3myEInGl1eQiaW8bBN8nrcmeem7Kf3INDKvUIE S2LyNDnWJSpIa+Lxr87bH9TVf9VjrgRtz6lQ7vJKZnjxdwnvJUVnTbvypr81yqacj9 YrP2nRFbcQ/muo3eRwSkuVUAamNcwnpMjteTyShQ= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Roderick Colenbrander , Jiri Kosina Subject: [PATCH 5.2 118/144] HID: sony: Fix race condition between rumble and device remove. Date: Wed, 14 Aug 2019 19:01:14 +0200 Message-Id: <20190814165804.867815773@linuxfoundation.org> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190814165759.466811854@linuxfoundation.org> References: <20190814165759.466811854@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Roderick Colenbrander commit e0f6974a54d3f7f1b5fdf5a593bd43ce9206ec04 upstream. Valve reported a kernel crash on Ubuntu 18.04 when disconnecting a DS4 gamepad while rumble is enabled. This issue is reproducible with a frequency of 1 in 3 times in the game Borderlands 2 when using an automatic weapon, which triggers many rumble operations. We found the issue to be a race condition between sony_remove and the final device destruction by the HID / input system. The problem was that sony_remove didn't clean some of its work_item state in "struct sony_sc". After sony_remove work, the corresponding evdev node was around for sufficient time for applications to still queue rumble work after "sony_remove". On pre-4.19 kernels the race condition caused a kernel crash due to a NULL-pointer dereference as "sc->output_report_dmabuf" got freed during sony_remove. On newer kernels this crash doesn't happen due the buffer now being allocated using devm_kzalloc. However we can still queue work, while the driver is an undefined state. This patch fixes the described problem, by guarding the work_item "state_worker" with an initialized variable, which we are setting back to 0 on cleanup. Signed-off-by: Roderick Colenbrander CC: stable@vger.kernel.org Signed-off-by: Jiri Kosina Signed-off-by: Greg Kroah-Hartman --- drivers/hid/hid-sony.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) --- a/drivers/hid/hid-sony.c +++ b/drivers/hid/hid-sony.c @@ -585,10 +585,14 @@ static void sony_set_leds(struct sony_sc static inline void sony_schedule_work(struct sony_sc *sc, enum sony_worker which) { + unsigned long flags; + switch (which) { case SONY_WORKER_STATE: - if (!sc->defer_initialization) + spin_lock_irqsave(&sc->lock, flags); + if (!sc->defer_initialization && sc->state_worker_initialized) schedule_work(&sc->state_worker); + spin_unlock_irqrestore(&sc->lock, flags); break; case SONY_WORKER_HOTPLUG: if (sc->hotplug_worker_initialized) @@ -2558,13 +2562,18 @@ static inline void sony_init_output_repo static inline void sony_cancel_work_sync(struct sony_sc *sc) { + unsigned long flags; + if (sc->hotplug_worker_initialized) cancel_work_sync(&sc->hotplug_worker); - if (sc->state_worker_initialized) + if (sc->state_worker_initialized) { + spin_lock_irqsave(&sc->lock, flags); + sc->state_worker_initialized = 0; + spin_unlock_irqrestore(&sc->lock, flags); cancel_work_sync(&sc->state_worker); + } } - static int sony_input_configured(struct hid_device *hdev, struct hid_input *hidinput) {