Received: by 2002:ac0:98c7:0:0:0:0:0 with SMTP id g7-v6csp178811imd; Wed, 31 Oct 2018 16:54:50 -0700 (PDT) X-Google-Smtp-Source: AJdET5cuFJBied4L6N7s47iepIArCc7DJBEyhULKGzoG29wnbPUxZU0IpRsGUfnbhB6zc4ufNz02 X-Received: by 2002:a63:bd01:: with SMTP id a1-v6mr5078748pgf.58.1541030089964; Wed, 31 Oct 2018 16:54:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1541030089; cv=none; d=google.com; s=arc-20160816; b=fMJrhAkJZ5Q1D7W9pS0qnoXWFGFaFpZjrJRpRVp697FMOGpgTo4goXryoIym3GHe/5 bhWwbbQlm2xc/KTQeb6BYzx4xg+2w6RQvFzcsPs4vtjE9JZUTEhgCgRRQdKP9GxCzYjb Mz7HDUwY6ztFv6hi/GhD45EvpIpfGiseBnHDoFKiKJV5nqvjicGRMbOzek4+0DJkJvb4 pa9mjZ0dLbBMb0h57SUUBRilXiDL3gK4XfhLytdC6dgvNLuGKahqWNi1PJob7ucn/FLU lj0QjCg8H6Uu39Bs0jf3MX+wziDgrlW8Wq3BN/rTldrKbX1sO5gI9pIv2jtE3Kj6q82o ZlMw== 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=nEDtHiulzRlafBgmaErt6JHmJX+Zp0SukzqbHV9BTwQ=; b=WzUqTpADupyEwG3TCUnioV5T9eDNhUBjIkP/p405zqVoR+rt7MP1jFJRgI6MPaW5xT kIevRyzo3qj7Hu+i0rWpTJavejg3ft57X8dJNN9qDyndoyUQkw7ZoFGFW61E1K7ij8Ti NI8B9tugRV1DPHgb7VLHccpsJgkJ9CnZpD9VAQwcmCeC0YUGKru33lOZapzPmyG2COab eBP0y1+ZNgWu6rQzt0XTyDrBQ2B3L9BxFmZM5iZrQ3SeUS3LytrYKkGeNCqGFWVHIWp/ ggYwksKok+RlRJtLArIpJ/+ONJJtJ91GT/C6II/k6wjTzEOzDy3N1wa8M8/s0XUi4H6M /8Qw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=z3KBEurY; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v7-v6si28711415plp.420.2018.10.31.16.54.35; Wed, 31 Oct 2018 16:54:49 -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=z3KBEurY; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727875AbeKAIyj (ORCPT + 99 others); Thu, 1 Nov 2018 04:54:39 -0400 Received: from mail.kernel.org ([198.145.29.99]:52790 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727417AbeKAIGF (ORCPT ); Thu, 1 Nov 2018 04:06:05 -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 EFF3620823; Wed, 31 Oct 2018 23:05:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1541027155; bh=oWbdDq+iHpSgGXWpuZGrdKnhqz/tA/mZEIhJ5VoFtog=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=z3KBEurYHBUbkm3LvPP33z9N0WwsKr0ubfNE8ffYq7vn+y96LhbUzIeUpe4pC+pYX sTfu2B7+W+KdRhclcnCzc8PLf9TqY4jj9vE2Zc1M7bdwEHPPI2MCC8ZfKAYoVqoTiM +gWKUSMeVP8YGjz5X6svQW5h4FAYj+sZJEzqTgqo= From: Sasha Levin To: stable@vger.kernel.org, linux-kernel@vger.kernel.org Cc: =?UTF-8?q?Javier=20Gonz=C3=A1lez?= , =?UTF-8?q?Javier=20Gonz=C3=A1lez?= , =?UTF-8?q?Matias=20Bj=C3=B8rling?= , Jens Axboe , Sasha Levin Subject: [PATCH AUTOSEL 4.19 014/146] lightnvm: pblk: fix race on sysfs line state Date: Wed, 31 Oct 2018 19:03:29 -0400 Message-Id: <20181031230541.28822-14-sashal@kernel.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181031230541.28822-1-sashal@kernel.org> References: <20181031230541.28822-1-sashal@kernel.org> 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: Javier González [ Upstream commit 44cdbdc657b23f75736eca3e88b781f009104363 ] pblk exposes a sysfs interface that represents its internal state. Part of this state is the map bitmap for the current open line, which should be protected by the line lock to avoid a race when freeing the line metadata. Currently, it is not. This patch makes sure that the line state is consistent and NULL bitmap pointers are not dereferenced. Signed-off-by: Javier González Signed-off-by: Matias Bjørling Signed-off-by: Jens Axboe Signed-off-by: Sasha Levin --- drivers/lightnvm/pblk-core.c | 5 +++-- drivers/lightnvm/pblk-sysfs.c | 8 +++++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/drivers/lightnvm/pblk-core.c b/drivers/lightnvm/pblk-core.c index 00984b486fea..2940cdc87af1 100644 --- a/drivers/lightnvm/pblk-core.c +++ b/drivers/lightnvm/pblk-core.c @@ -1539,13 +1539,14 @@ struct pblk_line *pblk_line_replace_data(struct pblk *pblk) struct pblk_line *cur, *new = NULL; unsigned int left_seblks; - cur = l_mg->data_line; new = l_mg->data_next; if (!new) goto out; - l_mg->data_line = new; spin_lock(&l_mg->free_lock); + cur = l_mg->data_line; + l_mg->data_line = new; + pblk_line_setup_metadata(new, l_mg, &pblk->lm); spin_unlock(&l_mg->free_lock); diff --git a/drivers/lightnvm/pblk-sysfs.c b/drivers/lightnvm/pblk-sysfs.c index 9fc3dfa168b4..8d2ed510c04b 100644 --- a/drivers/lightnvm/pblk-sysfs.c +++ b/drivers/lightnvm/pblk-sysfs.c @@ -262,8 +262,14 @@ static ssize_t pblk_sysfs_lines(struct pblk *pblk, char *page) sec_in_line = l_mg->data_line->sec_in_line; meta_weight = bitmap_weight(&l_mg->meta_bitmap, PBLK_DATA_LINES); - map_weight = bitmap_weight(l_mg->data_line->map_bitmap, + + spin_lock(&l_mg->data_line->lock); + if (l_mg->data_line->map_bitmap) + map_weight = bitmap_weight(l_mg->data_line->map_bitmap, lm->sec_per_line); + else + map_weight = 0; + spin_unlock(&l_mg->data_line->lock); } spin_unlock(&l_mg->free_lock); -- 2.17.1