From: David Laight <David.Laight@ACULAB.COM>
To: "linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
"'linux-sctp@vger.kernel.org'" <linux-sctp@vger.kernel.org>,
Eric Biggers <ebiggers@kernel.org>,
'Marcelo Ricardo Leitner' <marcelo.leitner@gmail.com>,
'Catalin Marinas' <catalin.marinas@arm.com>,
"'kent.overstreet@gmail.com'" <kent.overstreet@gmail.com>,
Andrew Morton <akpm@linux-foundation.org>,
"'Neil Horman'" <nhorman@tuxdriver.com>
Subject: [PATCH 01/13] lib/generic-radix-tree: Fix potentially corrupt tree
Date: Tue, 25 Aug 2020 14:53:10 +0000 [thread overview]
Message-ID: <3184cab15fe94db58c7cf3fcf33f3875@AcuMS.aculab.com> (raw)
If the last cmpxchg() done when increasing the tree depth fails
(because another caller has done a concurrent update) the 'new_node'
is reused later for either a array of pointers or the user data.
However the new_node->children[0] has been set non-zero this will
either end up being treated as a pointer to another node or as user data.
Rather than just setting children[0] = NULL free back the unwanted page
in the very unusual case that the cmpxchg() fails.
This removes quite a few comparisons from the normal path.
Signed-off-by: David Laight <david.laight@aculab.com>
---
Although rather nasty not really worth a backport because neither of the
2 places this code is used can possibly hit the failing path.
lib/generic-radix-tree.c | 37 +++++++++++++++++--------------------
1 file changed, 17 insertions(+), 20 deletions(-)
diff --git a/lib/generic-radix-tree.c b/lib/generic-radix-tree.c
index f25eb111c051..5695fe547f9d 100644
--- a/lib/generic-radix-tree.c
+++ b/lib/generic-radix-tree.c
@@ -105,7 +105,7 @@ void *__genradix_ptr_alloc(struct __genradix *radix, size_t offset,
gfp_t gfp_mask)
{
struct genradix_root *v = READ_ONCE(radix->root);
- struct genradix_node *n, *new_node = NULL;
+ struct genradix_node *n, *new_node;
unsigned level;
/* Increase tree depth if necessary: */
@@ -118,20 +118,19 @@ void *__genradix_ptr_alloc(struct __genradix *radix, size_t offset,
if (n && ilog2(offset) < genradix_depth_shift(level))
break;
- if (!new_node) {
- new_node = genradix_alloc_node(gfp_mask);
- if (!new_node)
- return NULL;
- }
+ new_node = genradix_alloc_node(gfp_mask);
+ if (!new_node)
+ return NULL;
new_node->children[0] = n;
new_root = ((struct genradix_root *)
((unsigned long) new_node | (n ? level + 1 : 0)));
- if ((v = cmpxchg_release(&radix->root, r, new_root)) == r) {
+ v = cmpxchg_release(&radix->root, r, new_root);
+ if (v == r)
v = new_root;
- new_node = NULL;
- }
+ else
+ genradix_free_node(new_node);
}
while (level--) {
@@ -141,20 +140,18 @@ void *__genradix_ptr_alloc(struct __genradix *radix, size_t offset,
n = READ_ONCE(*p);
if (!n) {
- if (!new_node) {
- new_node = genradix_alloc_node(gfp_mask);
- if (!new_node)
- return NULL;
- }
-
- if (!(n = cmpxchg_release(p, NULL, new_node)))
- swap(n, new_node);
+ new_node = genradix_alloc_node(gfp_mask);
+ if (!new_node)
+ return NULL;
+
+ n = cmpxchg_release(p, NULL, new_node);
+ if (!n)
+ n = new_node;
+ else
+ genradix_free_node(new_node);
}
}
- if (new_node)
- genradix_free_node(new_node);
-
return &n->data[offset];
}
EXPORT_SYMBOL(__genradix_ptr_alloc);
--
2.25.1
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
reply other threads:[~2020-08-25 14:53 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=3184cab15fe94db58c7cf3fcf33f3875@AcuMS.aculab.com \
--to=david.laight@aculab.com \
--cc=akpm@linux-foundation.org \
--cc=catalin.marinas@arm.com \
--cc=ebiggers@kernel.org \
--cc=kent.overstreet@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-sctp@vger.kernel.org \
--cc=marcelo.leitner@gmail.com \
--cc=nhorman@tuxdriver.com \
/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;
as well as URLs for NNTP newsgroup(s).