Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp873334pxk; Thu, 17 Sep 2020 19:56:24 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyFybO5FLpxdIoAIZH4sOknBxYmkXxjCVrV8MK184ny3FdDZyAvXetLnT4xGs2CrT6ebGh3 X-Received: by 2002:a50:aaca:: with SMTP id r10mr37526731edc.307.1600397784057; Thu, 17 Sep 2020 19:56:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600397784; cv=none; d=google.com; s=arc-20160816; b=rYSWpXzvz/U1mUTsBF63wlEhPBXZrOz7YKi3YbHONZgoEJtYkICKDfpn/eRmKJOkcr iCc/bDCYL+KiYEXsAxYGqbispnxkxXNmuf+hvfis+rcpuvGryR7GOV6Sos8FAPir5HwL bleCnNgsPPtsvdH+OOOzqjgrZX+is4IkRYygolzxGqXILSpsRkMgbCYfFwWl60Z4p7Ee W1LyNxL6SlSV2E6R+tjg7Jl5wbZRQda4cIu3ml6q/t9Pq8tSeQi9Qe6bfNQjRFy8bjG3 DezoMSH/jYqdLuyRyKIbI5bLUkPQhIBuxfjaKnjyWPESLFng9fvTM9yTpdC69T+F37gs yPCg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=iKePHd343AFqEH3iUHAtavzq0X0qWXENzcBrGA4u0yc=; b=S1cnCgMcHUzUmNRRPF3ta521HNqFQSccwhrSHJoL977v7NttNPnL/qJUw3MvjdmSa+ cNfNE20UyMG9sZm0XVvlTCqgpJUTWNDcVTYHJLUK3wInvXG6RzSa5Uj53QW5fAQor6cN rT88g/Fz4CV0qxk7li0uU7y8/7uQv6irPWU97q88oNcDq7W7V/bGw8jDL17aMi3nitdF 84LsyOd5IVpGWc1wdkLfnaoHYAYJ5wmdMtSFR/L3u842a1PSTX1Ll7Co9ZiZOh87MEmz SNQxIhCbIw+PFmg/vXJeUiutmmDSDXtICqmzVXRzhoCE/vD384O5pMiiZXZaRG5/I7bb JS7g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=ijg3GOKN; 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 mh15si1263325ejb.389.2020.09.17.19.56.01; Thu, 17 Sep 2020 19:56:24 -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=ijg3GOKN; 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 S1728462AbgIRCxS (ORCPT + 99 others); Thu, 17 Sep 2020 22:53:18 -0400 Received: from mail.kernel.org ([198.145.29.99]:58576 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728008AbgIRCHw (ORCPT ); Thu, 17 Sep 2020 22:07:52 -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 EBBD4239A1; Fri, 18 Sep 2020 02:07:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1600394871; bh=+O8ocp5By/WmYy8EcFwnY9hB1yiYzE6Ub4ilQkH/beU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ijg3GOKNnLO40pLfDY2ffmncxeRne1QtXDl/H4HqmR4NYJmfSZhxb6IunFJsueAS8 T6hXj57J3ySfspjv8Gw0oxU6KxZokyiZHTgFAE6G8+JF79uycL2QKcIakcHJc6Foz8 Mb99qL1DhvqiTuYwOYBTyu4/4ZtsFkk/3VI/HjRQ= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Hou Tao , syzbot+f317896aae32eb281a58@syzkaller.appspotmail.com, Sascha Hauer , Richard Weinberger , Sasha Levin , linux-mtd@lists.infradead.org Subject: [PATCH AUTOSEL 5.4 323/330] ubi: fastmap: Free unused fastmap anchor peb during detach Date: Thu, 17 Sep 2020 22:01:03 -0400 Message-Id: <20200918020110.2063155-323-sashal@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200918020110.2063155-1-sashal@kernel.org> References: <20200918020110.2063155-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Hou Tao [ Upstream commit c16f39d14a7e0ec59881fbdb22ae494907534384 ] When CONFIG_MTD_UBI_FASTMAP is enabled, fm_anchor will be assigned a free PEB during ubi_wl_init() or ubi_update_fastmap(). However if fastmap is not used or disabled on the MTD device, ubi_wl_entry related with the PEB will not be freed during detach. So Fix it by freeing the unused fastmap anchor during detach. Fixes: f9c34bb52997 ("ubi: Fix producing anchor PEBs") Reported-by: syzbot+f317896aae32eb281a58@syzkaller.appspotmail.com Reviewed-by: Sascha Hauer Signed-off-by: Hou Tao Signed-off-by: Richard Weinberger Signed-off-by: Sasha Levin --- drivers/mtd/ubi/fastmap-wl.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/drivers/mtd/ubi/fastmap-wl.c b/drivers/mtd/ubi/fastmap-wl.c index 426820ab9afe1..b486250923c5a 100644 --- a/drivers/mtd/ubi/fastmap-wl.c +++ b/drivers/mtd/ubi/fastmap-wl.c @@ -39,6 +39,13 @@ static struct ubi_wl_entry *find_anchor_wl_entry(struct rb_root *root) return victim; } +static inline void return_unused_peb(struct ubi_device *ubi, + struct ubi_wl_entry *e) +{ + wl_tree_add(e, &ubi->free); + ubi->free_count++; +} + /** * return_unused_pool_pebs - returns unused PEB to the free tree. * @ubi: UBI device description object @@ -52,8 +59,7 @@ static void return_unused_pool_pebs(struct ubi_device *ubi, for (i = pool->used; i < pool->size; i++) { e = ubi->lookuptbl[pool->pebs[i]]; - wl_tree_add(e, &ubi->free); - ubi->free_count++; + return_unused_peb(ubi, e); } } @@ -361,6 +367,11 @@ static void ubi_fastmap_close(struct ubi_device *ubi) return_unused_pool_pebs(ubi, &ubi->fm_pool); return_unused_pool_pebs(ubi, &ubi->fm_wl_pool); + if (ubi->fm_anchor) { + return_unused_peb(ubi, ubi->fm_anchor); + ubi->fm_anchor = NULL; + } + if (ubi->fm) { for (i = 0; i < ubi->fm->used_blocks; i++) kfree(ubi->fm->e[i]); -- 2.25.1