Received: by 10.192.165.148 with SMTP id m20csp2220599imm; Thu, 3 May 2018 12:27:05 -0700 (PDT) X-Google-Smtp-Source: AB8JxZqQ1rniHO5f+a2BlCSulT0xRgl+jBHM1L21ZK6VgBmXyDoKkbv1s6bOjNsqUl51AK/hPKHb X-Received: by 2002:a17:902:bf41:: with SMTP id u1-v6mr25378290pls.257.1525375625427; Thu, 03 May 2018 12:27:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525375625; cv=none; d=google.com; s=arc-20160816; b=e49bB6qaHyWfRk/gzft7KNDLtr2rh01Axpm66j9qBsqPLqTYjLxk2b9kAxNHl91J8k ArNEr32OnBIB8E/m8UUr1nywCaJWpXDDsVq1gtSLmlWm8QGVcWXv985ZzTDDLJHtYrJ1 RN68UMa5zaHlNyYHifQM1IWBT6PmG4smTMf61pnxEPhR2/fBQRo5L5z1bG/9BDiQikOl AyVbuOTjlmFWomepssNRweS8AE98+66cXAJEGCDQCTigd+rHL+5+w6j/P0AcXaxZpZe0 sCPUfeUqxfSNuwqIAWfGd5Pe7IK8aCmqiV7mfR4xFg/vk8yv4h1CsVfC4Bzs68s0qzEt mHhg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=gToRSOZaTFIbxwSLZgVdCWJZ+QQO4S8xJGeNOt3hbzU=; b=0rOOvvmzPTqRTmuYQrB/x/Em1JvzXkZRoxa8nClKyvIYamyitbFjtcxgxFDTYJyi1R QARsYhhGBm1zg6j8BSzcyixKW9d6r5MVsJV2RB4wGopBcvl4JDHWAKKZNPTUakmxdLEF UqVM+ACQZMkkDPRdwpVkCkEeUYfzKN8yu6tVBCtIRv6XkWbuer1A2P/wLfXa2PeSDKwL vHV65pRNhEje24Rs5CQm6hbrxkYJlOMckZdbXufDdZLS7jbkUhEmHQW4orVHxD/KTbAO QAGrj7GeQp+3JPiyf+FMCFAKX047ZhiaZF81m/MMEXLd6vYQzDdMHFoaWThmWkFM4qQr BMCQ== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id c8si14525664pfj.138.2018.05.03.12.26.20; Thu, 03 May 2018 12:27:05 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751674AbeECTY6 (ORCPT + 99 others); Thu, 3 May 2018 15:24:58 -0400 Received: from mga02.intel.com ([134.134.136.20]:8165 "EHLO mga02.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751316AbeECTYh (ORCPT ); Thu, 3 May 2018 15:24:37 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by orsmga101.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 03 May 2018 12:24:36 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.49,359,1520924400"; d="scan'208";a="225573205" Received: from theros.lm.intel.com ([10.232.112.164]) by fmsmga005.fm.intel.com with ESMTP; 03 May 2018 12:24:35 -0700 From: Ross Zwisler To: Andrew Morton , linux-kernel@vger.kernel.org, Matthew Wilcox Cc: Ross Zwisler , Christoph Hellwig , Dan Williams , Dave Chinner , Jan Kara , linux-nvdimm@lists.01.org, stable@vger.kernel.org Subject: [PATCH 5/5] radix tree: fix multi-order iteration race Date: Thu, 3 May 2018 13:24:30 -0600 Message-Id: <20180503192430.7582-6-ross.zwisler@linux.intel.com> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20180503192430.7582-1-ross.zwisler@linux.intel.com> References: <20180503192430.7582-1-ross.zwisler@linux.intel.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Fix a race in the multi-order iteration code which causes the kernel to hit a GP fault. This was first seen with a production v4.15 based kernel (4.15.6-300.fc27.x86_64) utilizing a DAX workload which used order 9 PMD DAX entries. The race has to do with how we tear down multi-order sibling entries when we are removing an item from the tree. Remember for example that an order 2 entry looks like this: struct radix_tree_node.slots[] = [entry][sibling][sibling][sibling] where 'entry' is in some slot in the struct radix_tree_node, and the three slots following 'entry' contain sibling pointers which point back to 'entry.' When we delete 'entry' from the tree, we call : radix_tree_delete() radix_tree_delete_item() __radix_tree_delete() replace_slot() replace_slot() first removes the siblings in order from the first to the last, then at then replaces 'entry' with NULL. This means that for a brief period of time we end up with one or more of the siblings removed, so: struct radix_tree_node.slots[] = [entry][NULL][sibling][sibling] This causes an issue if you have a reader iterating over the slots in the tree via radix_tree_for_each_slot() while only under rcu_read_lock()/rcu_read_unlock() protection. This is a common case in mm/filemap.c. The issue is that when __radix_tree_next_slot() => skip_siblings() tries to skip over the sibling entries in the slots, it currently does so with an exact match on the slot directly preceding our current slot. Normally this works: V preceding slot struct radix_tree_node.slots[] = [entry][sibling][sibling][sibling] ^ current slot This lets you find the first sibling, and you skip them all in order. But in the case where one of the siblings is NULL, that slot is skipped and then our sibling detection is interrupted: V preceding slot struct radix_tree_node.slots[] = [entry][NULL][sibling][sibling] ^ current slot This means that the sibling pointers aren't recognized since they point all the way back to 'entry', so we think that they are normal internal radix tree pointers. This causes us to think we need to walk down to a struct radix_tree_node starting at the address of 'entry'. In a real running kernel this will crash the thread with a GP fault when you try and dereference the slots in your broken node starting at 'entry'. We fix this race by fixing the way that skip_siblings() detects sibling nodes. Instead of testing against the preceding slot we instead look for siblings via is_sibling_entry() which compares against the position of the struct radix_tree_node.slots[] array. This ensures that sibling entries are properly identified, even if they are no longer contiguous with the 'entry' they point to. Signed-off-by: Ross Zwisler Reported-by: CR, Sapthagirish Fixes: commit 148deab223b2 ("radix-tree: improve multiorder iterators") Cc: --- lib/radix-tree.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/radix-tree.c b/lib/radix-tree.c index da9e10c827df..43e0cbedc3a0 100644 --- a/lib/radix-tree.c +++ b/lib/radix-tree.c @@ -1612,11 +1612,9 @@ static void set_iter_tags(struct radix_tree_iter *iter, static void __rcu **skip_siblings(struct radix_tree_node **nodep, void __rcu **slot, struct radix_tree_iter *iter) { - void *sib = node_to_entry(slot - 1); - while (iter->index < iter->next_index) { *nodep = rcu_dereference_raw(*slot); - if (*nodep && *nodep != sib) + if (*nodep && !is_sibling_entry(iter->node, *nodep)) return slot; slot++; iter->index = __radix_tree_iter_add(iter, 1); @@ -1631,7 +1629,7 @@ void __rcu **__radix_tree_next_slot(void __rcu **slot, struct radix_tree_iter *iter, unsigned flags) { unsigned tag = flags & RADIX_TREE_ITER_TAG_MASK; - struct radix_tree_node *node = rcu_dereference_raw(*slot); + struct radix_tree_node *node; slot = skip_siblings(&node, slot, iter); -- 2.14.3