From nobody Fri Dec 19 15:47:52 2025 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2923F58221 for ; Thu, 21 Dec 2023 16:54:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="TX/zo8tL" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:In-Reply-To:References; bh=tkzpwTCTKgMRvEHeKlf1m6j/jIIKPQ0BhLx7GX4A+7k=; b=TX/zo8tLYimsmnvdGehpqBiFRZ KN1w0U3hQgZH7xrBtonrmpz3PnlEMoplaUhQ7OxLu2a4ktG43loZUGZO/Nw3r9HmMgybO8nDPaGMS IHsZ0aVRZRvEAKs1UmbLjCb6wesDZHETv/NHrmwTHIO/z5NhXSV6USM2ArdkM9rs6zZkesxIPY62l DX3lZrqYfQwXP7ka1h4ZsIunfVG4Nwb1ntOvEr7P1qm10dD6x5NvFURAnn8rto3cpwgn8tabcAsza uGljDSvL9dgTNseDeHqvsvg/Ns7JBIRirXEMSIDYltGA6KPruDGoEzmw71QxlJvO4Gqz/YJmVut/P /J4POlEA==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1rGMJB-005gY6-TA; Thu, 21 Dec 2023 16:54:05 +0000 From: "Matthew Wilcox (Oracle)" To: Linus Torvalds , linux-kernel@vger.kernel.org Cc: "Matthew Wilcox (Oracle)" , Zhenghan Wang Subject: [PATCH] ida: Fix crash in ida_free when the bitmap is empty Date: Thu, 21 Dec 2023 16:53:57 +0000 Message-Id: <20231221165357.1354847-1-willy@infradead.org> X-Mailer: git-send-email 2.37.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The IDA usually detects double-frees, but that detection failed to consider the case when there are no nearby IDs allocated and so we have a NULL bitmap rather than simply having a clear bit. Add some tests to the test-suite to be sure we don't inadvertently reintroduce this problem. Unfortunately they're quite noisy so include a message to disregard the warnings. Reported-by: Zhenghan Wang Signed-off-by: Matthew Wilcox (Oracle) --- lib/idr.c | 2 +- lib/test_ida.c | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/lib/idr.c b/lib/idr.c index 13f2758c2377..da36054c3ca0 100644 --- a/lib/idr.c +++ b/lib/idr.c @@ -508,7 +508,7 @@ void ida_free(struct ida *ida, unsigned int id) goto delete; xas_store(&xas, xa_mk_value(v)); } else { - if (!test_bit(bit, bitmap->bitmap)) + if (!bitmap || !test_bit(bit, bitmap->bitmap)) goto err; __clear_bit(bit, bitmap->bitmap); xas_set_mark(&xas, XA_FREE_MARK); diff --git a/lib/test_ida.c b/lib/test_ida.c index f946c80ced8b..072a49897e71 100644 --- a/lib/test_ida.c +++ b/lib/test_ida.c @@ -150,6 +150,45 @@ static void ida_check_conv(struct ida *ida) IDA_BUG_ON(ida, !ida_is_empty(ida)); } =20 +/* + * Check various situations where we attempt to free an ID we don't own. + */ +static void ida_check_bad_free(struct ida *ida) +{ + unsigned long i; + + printk("vvv Ignore \"not allocated\" warnings\n"); + /* IDA is empty; all of these will fail */ + ida_free(ida, 0); + for (i =3D 0; i < 31; i++) + ida_free(ida, 1 << i); + + /* IDA contains a single value entry */ + IDA_BUG_ON(ida, ida_alloc_min(ida, 3, GFP_KERNEL) !=3D 3); + ida_free(ida, 0); + for (i =3D 0; i < 31; i++) + ida_free(ida, 1 << i); + + /* IDA contains a single bitmap */ + IDA_BUG_ON(ida, ida_alloc_min(ida, 1023, GFP_KERNEL) !=3D 1023); + ida_free(ida, 0); + for (i =3D 0; i < 31; i++) + ida_free(ida, 1 << i); + + /* IDA contains a tree */ + IDA_BUG_ON(ida, ida_alloc_min(ida, (1 << 20) - 1, GFP_KERNEL) !=3D (1 << = 20) - 1); + ida_free(ida, 0); + for (i =3D 0; i < 31; i++) + ida_free(ida, 1 << i); + printk("^^^ \"not allocated\" warnings over\n"); + + ida_free(ida, 3); + ida_free(ida, 1023); + ida_free(ida, (1 << 20) - 1); + + IDA_BUG_ON(ida, !ida_is_empty(ida)); +} + static DEFINE_IDA(ida); =20 static int ida_checks(void) @@ -162,6 +201,7 @@ static int ida_checks(void) ida_check_leaf(&ida, 1024 * 64); ida_check_max(&ida); ida_check_conv(&ida); + ida_check_bad_free(&ida); =20 printk("IDA: %u of %u tests passed\n", tests_passed, tests_run); return (tests_run !=3D tests_passed) ? 0 : -EINVAL; --=20 2.43.0