Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp1089495ybt; Wed, 1 Jul 2020 18:38:13 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxyQ2VtnsM5c9EJCfZpFi1DeGqe9xKtSTMSKNDXa5a3XjYC/PhX8Z0kuhDxkduwtkFQMzA+ X-Received: by 2002:a17:906:33ca:: with SMTP id w10mr17985888eja.171.1593653893448; Wed, 01 Jul 2020 18:38:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1593653893; cv=none; d=google.com; s=arc-20160816; b=sBOPQM+73aMyB9PIVA+n+kEUCg6YxGB2sXR7uk9WPnvfcbj7ud2kAaB6IbDkXB4BUT 9YSqjKq3WRcLm0xenPhVnN4bTE5zL/VMTpz60wNfkhGRRznb/8AR/KAeLD+HQHJZRSF+ yb2SquODa0kVTx+cSj/mUpCnj/Ra9YVzF6C06+jRtLskuSKKDHMsXCrjb5dmLN2P/tXb 5HEezElLVIeESQ/zZXCbfQGsoL/PZb4c5p5qybuBQcLGRedb+Yb4wXElbb0LfYcmMm8w jVUIPNRYVPGZ1+dasE4K8g2ihzJh3XBmybd7HERogO2frPANokuhEPQzDEvZel6lxUTb ElZg== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=6irwv18cY7j7p6Cwmlli3P/AlVBp+4cr15hq27NEjDY=; b=HuWCEgk8dmB/FIoRQ7lNWIVTqkAtyJjlnaiRMEmcCDqAX4t/eNZDkODC+sEfN7FuHK EFXFL7oSTDqYbxahwHjLbQdUIy1spcylHuHHKEp4ViD+ZrV+jKNb/u8EApXgLJ0dsom6 hM1bZg8JfoFuHcGb8OnnmDHdQdqLn1uzFVmksJGt4OvOot3DaHBP1sBwryWRU8dBYfBu nM2ZBlSQ97gwXbIVmC0iOkiiygOlgKIWr3ATPfpmm2myxVLnVog0OyVzuDx6NJK/b8qp K2TJ1HfWsI36NqIY/zAef3MJV/4kbmTkLg34jNTRmTLRZ2yG2/m0/dbY+8Ec/wcGElW+ l2KQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=OarYbh6y; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id de2si4774417edb.198.2020.07.01.18.37.50; Wed, 01 Jul 2020 18:38:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=OarYbh6y; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728966AbgGBBgD (ORCPT + 99 others); Wed, 1 Jul 2020 21:36:03 -0400 Received: from mail.kernel.org ([198.145.29.99]:53888 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728291AbgGBBXU (ORCPT ); Wed, 1 Jul 2020 21:23:20 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 4A2DE2085B; Thu, 2 Jul 2020 01:23:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1593653000; bh=EiZ93p2usv/LoFYCN6aaZNeVxDqwVAnsSNJ+CTiv9dc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OarYbh6yCv6Lijr+mNvV/rOKcNmA1xGjCXjzztGyL2WDmd8aGx4pnrTejmyzMCbvv UqRA+KvyIdrcuAu/JDeWJZEvfvFypFsvFTJn60bjtfq1VVQ3oPHwe0VD9kn7oD5Ucg 9/UcN/Qm8yG3ufahpstkybsN43tSSYpiTkVKO8yo= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Zhenzhong Duan , Mark Brown , Sasha Levin , linux-spi@vger.kernel.org Subject: [PATCH AUTOSEL 5.7 20/53] spi: spidev: fix a race between spidev_release and spidev_remove Date: Wed, 1 Jul 2020 21:21:29 -0400 Message-Id: <20200702012202.2700645-20-sashal@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200702012202.2700645-1-sashal@kernel.org> References: <20200702012202.2700645-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Zhenzhong Duan [ Upstream commit abd42781c3d2155868821f1b947ae45bbc33330d ] Imagine below scene, spidev is referenced after it's freed. spidev_release() spidev_remove() ... spin_lock_irq(&spidev->spi_lock); spidev->spi = NULL; spin_unlock_irq(&spidev->spi_lock); mutex_lock(&device_list_lock); dofree = (spidev->spi == NULL); if (dofree) kfree(spidev); mutex_unlock(&device_list_lock); mutex_lock(&device_list_lock); list_del(&spidev->device_entry); device_destroy(spidev_class, spidev->devt); clear_bit(MINOR(spidev->devt), minors); if (spidev->users == 0) kfree(spidev); mutex_unlock(&device_list_lock); Fix it by resetting spidev->spi in device_list_lock's protection. Signed-off-by: Zhenzhong Duan Link: https://lore.kernel.org/r/20200618032125.4650-1-zhenzhong.duan@gmail.com Signed-off-by: Mark Brown Signed-off-by: Sasha Levin --- drivers/spi/spidev.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/spi/spidev.c b/drivers/spi/spidev.c index 80dd1025b9530..82e6481fdf950 100644 --- a/drivers/spi/spidev.c +++ b/drivers/spi/spidev.c @@ -786,13 +786,13 @@ static int spidev_remove(struct spi_device *spi) { struct spidev_data *spidev = spi_get_drvdata(spi); + /* prevent new opens */ + mutex_lock(&device_list_lock); /* make sure ops on existing fds can abort cleanly */ spin_lock_irq(&spidev->spi_lock); spidev->spi = NULL; spin_unlock_irq(&spidev->spi_lock); - /* prevent new opens */ - mutex_lock(&device_list_lock); list_del(&spidev->device_entry); device_destroy(spidev_class, spidev->devt); clear_bit(MINOR(spidev->devt), minors); -- 2.25.1