mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Lai Jiangshan <laijs@cn.fujitsu.com>
To: Tejun Heo <tj@kernel.org>, <linux-kernel@vger.kernel.org>
Cc: Lai Jiangshan <laijs@cn.fujitsu.com>, <stable@vger.kernel.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	Jean Delvare <jdelvare@suse.de>,
	Monam Agarwal <monamagarwal123@gmail.com>,
	Jeff Layton <jlayton@redhat.com>,
	Andreas Gruenbacher <agruen@linbit.com>,
	Stephen Hemminger <stephen@networkplumber.org>
Subject: [PATCH 1/9 V2] idr: fix overflow bug during maximum ID calculation at maximum height
Date: Sat, 19 Apr 2014 19:38:08 +0800	[thread overview]
Message-ID: <1397907496-5993-2-git-send-email-laijs@cn.fujitsu.com> (raw)
In-Reply-To: <1397907496-5993-1-git-send-email-laijs@cn.fujitsu.com>

idr_replace() open-codes the logic to calculate the maximum valid ID
given the height of the idr tree; unfortunately, the open-coded logic
doesn't account for the fact that the top layer may have unused slots
and over-shifts the limit to zero when the tree is at its maximum
height.

The following test code shows it fails to replace the value for id=((1<<27)+42):

static void test5(void)
{
	int id;
	DEFINE_IDR(test_idr);
#define TEST5_START ((1<<27)+42) /* use the highest layer */

	printk(KERN_INFO "Start test5\n");
	id = idr_alloc(&test_idr, (void *)1, TEST5_START, 0, GFP_KERNEL);
	BUG_ON(id != TEST5_START);
	TEST_BUG_ON(idr_replace(&test_idr, (void *)2, TEST5_START) != (void *)1);
	idr_destroy(&test_idr);
	printk(KERN_INFO "End of test5\n");
}

Fix the bug by using idr_max() which correctly takes into account the
maximum allowed shift.

sub_alloc() shares the same problem and may incorrectly fail with
-EAGAIN; however, this bug doesn't affect correct operation because
idr_get_empty_slot(), which already uses idr_max(), retries with the
increased @id in such cases.

tj: Updated patch description.

CC: stable@vger.kernel.org
Signed-off-by: Lai Jiangshan <laijs@cn.fujitsu.com>
Acked-by: Tejun Heo <tj@kernel.org>
---
 lib/idr.c |    8 +++-----
 1 files changed, 3 insertions(+), 5 deletions(-)

diff --git a/lib/idr.c b/lib/idr.c
index 2642fa8..4df6792 100644
--- a/lib/idr.c
+++ b/lib/idr.c
@@ -249,7 +249,7 @@ static int sub_alloc(struct idr *idp, int *starting_id, struct idr_layer **pa,
 			id = (id | ((1 << (IDR_BITS * l)) - 1)) + 1;
 
 			/* if already at the top layer, we need to grow */
-			if (id >= 1 << (idp->layers * IDR_BITS)) {
+			if (id > idr_max(idp->layers)) {
 				*starting_id = id;
 				return -EAGAIN;
 			}
@@ -811,12 +811,10 @@ void *idr_replace(struct idr *idp, void *ptr, int id)
 	if (!p)
 		return ERR_PTR(-EINVAL);
 
-	n = (p->layer+1) * IDR_BITS;
-
-	if (id >= (1 << n))
+	if (id > idr_max(p->layer + 1))
 		return ERR_PTR(-EINVAL);
 
-	n -= IDR_BITS;
+	n = p->layer * IDR_BITS;
 	while ((n > 0) && p) {
 		p = p->ary[(id >> n) & IDR_MASK];
 		n -= IDR_BITS;
-- 
1.7.4.4


  reply	other threads:[~2014-04-19 11:34 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-04-18 12:49 [PATCH 0/8] idr: fix & cleanup Lai Jiangshan
2014-04-18 12:49 ` [PATCH 1/8] idr: fix overflow bug for the max-high layer Lai Jiangshan
2014-04-18 16:29   ` Tejun Heo
2014-04-18 17:08     ` Lai Jiangshan
2014-04-18 17:10       ` Tejun Heo
2014-04-18 12:49 ` [PATCH 2/8] idr: fix unexpected id-removal when idr_remove(unallocated_id) Lai Jiangshan
2014-04-18 16:57   ` Tejun Heo
2014-04-18 12:49 ` [PATCH 3/8] idr: fix NULL pointer dereference when ida_remove(unallocated_id) Lai Jiangshan
2014-04-18 17:09   ` Tejun Heo
2014-04-18 12:49 ` [PATCH 4/8] idr: fix idr_replace()'s returned error code Lai Jiangshan
2014-04-18 17:12   ` Tejun Heo
2014-04-18 12:49 ` [PATCH 5/8] idr: covert BUG_ON() to WARN_ON_ONCE() if the argument is invalid Lai Jiangshan
2014-04-18 17:14   ` Tejun Heo
2014-04-18 12:49 ` [PATCH 6/8] idr: avoid ping-pong Lai Jiangshan
2014-04-18 17:17   ` Tejun Heo
2014-04-19 10:43     ` Lai Jiangshan
2014-04-19 13:01       ` Tejun Heo
2014-04-19 14:23         ` Lai Jiangshan
2014-04-18 12:49 ` [PATCH 7/8] idr: don't need to shink the free list when idr_remove() Lai Jiangshan
2014-04-18 17:19   ` Tejun Heo
2014-04-18 12:49 ` [PATCH 8/8] idr: reduce the unneeded check in free_layer() Lai Jiangshan
2014-04-18 17:21   ` Tejun Heo
2014-04-19 11:38 ` [PATCH 0/9 V2] idr: fix & cleanup Lai Jiangshan
2014-04-19 11:38   ` Lai Jiangshan [this message]
2014-04-19 11:38   ` [PATCH 2/9 V2] idr: fix unexpected ID-removal when idr_remove(unallocated_id) Lai Jiangshan
2014-04-19 11:38   ` [PATCH 3/9 V2] idr: fix NULL pointer dereference when ida_remove(unallocated_id) Lai Jiangshan
2014-04-19 11:38   ` [PATCH 4/9 V2] idr: fix idr_replace()'s returned error code Lai Jiangshan
2014-04-19 11:38   ` [PATCH 5/9 V2] idr: covert BUG_ON() to WARN_ON_ONCE() if the argument is invalid Lai Jiangshan
2014-04-19 13:07     ` Tejun Heo
2014-04-19 14:04       ` Lai Jiangshan
2014-04-19 23:47         ` Tejun Heo
2014-04-19 11:38   ` [PATCH 6/9 V2] idr: avoid ping-pong Lai Jiangshan
2014-04-19 11:38   ` [PATCH 7/9 V2] idr: don't need to shink the free list when idr_remove() Lai Jiangshan
2014-04-19 11:38   ` [PATCH 8/9 V2] idr: reduce the unneeded check in free_layer() Lai Jiangshan
2014-04-19 11:38   ` [PATCH 9/9 V2] idr: remove useless C-PreProcessor branch Lai Jiangshan
2014-04-19 23:51     ` Tejun Heo
2014-04-20  3:56       ` Lai Jiangshan
2014-04-20 11:29         ` Tejun Heo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1397907496-5993-2-git-send-email-laijs@cn.fujitsu.com \
    --to=laijs@cn.fujitsu.com \
    --cc=agruen@linbit.com \
    --cc=akpm@linux-foundation.org \
    --cc=jdelvare@suse.de \
    --cc=jlayton@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=monamagarwal123@gmail.com \
    --cc=stable@vger.kernel.org \
    --cc=stephen@networkplumber.org \
    --cc=tj@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®