Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp1569579ybz; Thu, 30 Apr 2020 01:31:48 -0700 (PDT) X-Google-Smtp-Source: APiQypJ1tUlCtPoozUvb5mdqURiKHxHvH4Esj3aiDYnjVg4GoWOydoqlVtLh3K7GoL/8boM5GBrb X-Received: by 2002:a17:906:add7:: with SMTP id lb23mr1790997ejb.6.1588235508065; Thu, 30 Apr 2020 01:31:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588235508; cv=none; d=google.com; s=arc-20160816; b=qTTSCLux4fBrqgDYmsChmm1aIzwBIiaerv/ivCVmDRuO6xC4yyUWiMCZhcHIq/+6HX j4jdSKGL+0ByVDdeKuro01fx0I7ecj6Lq0diqIId9UdfOgFDsB1hPlSPz9OCUHul0TsR 9iwdlLzRNuzS44LcbMFIJWAvL9nZv4/o4/Cl2wPyUPip2E2Jt9jAeonR3JT5moxfcKqK FDJll0SQFkf/kAo2iUWFpYR2bDDfjdmAokg9XvRNSqv0L5NilUW6euZkNPtRjkTJscnQ Cs0+Xke0HKeu5wjuEnnJ6iSZ9l4+0vmbhnkmbWouevn7RJ7cTtTP1xPFNA4B/PBHJDRv sGgw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=B/DD4SwkH4dIKud6UgQC7/3WcU8JrYSfWRgnUCQdVtY=; b=XWSRxq0g4l7Gndj0GIns/bD3CxpMJAhzkb7jlRr7Qiz/kQbiEQF6g1b922LsFxjCoN rOAS5kuq4sIIFrJ+WB6oao/gxj+8yjFgJMeYg0Wo0SViqUVApV85u5kOn3V5KnsvWZ9s tReLCPIQvQ8pFF3/G/Khb/+HBs/gbYOWvFGegInbAnUK/DX0clhGGbDUtgza1EG5C6B7 cRjkb8Rq7koBlJU//4Y3YUIzUgiUkl64Sa8XPz1YSD3VsPM7VZxLS7e4hnkW9ytZ1f08 wD/+nMeD8s9RHotAlLnqHjJ8IQML2DympyBVEXIhlLoh42ypMWIPI4tvhmfIiIIOP+cu q//Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=roT+p3ew; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id rv23si5275546ejb.330.2020.04.30.01.31.22; Thu, 30 Apr 2020 01:31:48 -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=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=roT+p3ew; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726688AbgD3I1l (ORCPT + 99 others); Thu, 30 Apr 2020 04:27:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60572 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1726543AbgD3I1l (ORCPT ); Thu, 30 Apr 2020 04:27:41 -0400 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 09B8AC035494 for ; Thu, 30 Apr 2020 01:27:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=In-Reply-To:Content-Type:MIME-Version :References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=B/DD4SwkH4dIKud6UgQC7/3WcU8JrYSfWRgnUCQdVtY=; b=roT+p3ewK+JSVtLy4iBWyzQ9zA VWIpvFAOMCg8lGWrw9jw/agjv34+Z2ZL36EAfiVCYAcPHhImLhl7IcwtiDSHXLQ6Y9XRMC9z9vEO6 dL9acsSjpkXK5AIfYtXI9wrgeAUN5xpNUEZ8h36l2Sbn8oHWgOh/1xK/pGeeTb/qABuk3Wzlp/GKi geRvFKk9DO95HJeNzGTMsVikVEh/5spoEYMPqMala5Q0hOdweVfDRew1LK20xlNCInP4BOadpZZdU oe9WpeekexrNvAE2/0S0OcYSBt8kzqWnyl/hEgA4RqiIwuGCyVfBfnZwS1BIS32jDBOfHGEvi871t SAmH3u9g==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=noisy.programming.kicks-ass.net) by bombadil.infradead.org with esmtpsa (Exim 4.92.3 #3 (Red Hat Linux)) id 1jU4Xa-0007cs-0d; Thu, 30 Apr 2020 08:27:30 +0000 Received: from hirez.programming.kicks-ass.net (hirez.programming.kicks-ass.net [192.168.1.225]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id A44F8301224; Thu, 30 Apr 2020 10:27:27 +0200 (CEST) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id 8F7C9213923B9; Thu, 30 Apr 2020 10:27:27 +0200 (CEST) Date: Thu, 30 Apr 2020 10:27:27 +0200 From: Peter Zijlstra To: Michel Lespinasse Cc: Juri Lelli , LKML , Davidlohr Bueso , Ingo Molnar , Thomas Gleixner , Oleg Nesterov , irogers@google.com, Vincent Guittot Subject: Re: [RFC][PATCH 1/7] rbtree: Add generic add and find helpers Message-ID: <20200430082727.GP13592@hirez.programming.kicks-ass.net> References: <20200429153258.563269446@infradead.org> <20200429153549.006661686@infradead.org> <20200430072847.GA68379@localhost.localdomain> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Apr 30, 2020 at 12:51:02AM -0700, Michel Lespinasse wrote: > On Thu, Apr 30, 2020 at 12:28 AM Juri Lelli wrote: > > > --- a/include/linux/rbtree.h > > > +++ b/include/linux/rbtree.h > > > @@ -141,12 +141,18 @@ static inline void rb_insert_color_cache > > > rb_insert_color(node, &root->rb_root); > > > } > > > > > > -static inline void rb_erase_cached(struct rb_node *node, > > > +static inline bool rb_erase_cached(struct rb_node *node, > > > struct rb_root_cached *root) > > > { > > > - if (root->rb_leftmost == node) > > > + bool leftmost = false; > > > + > > > + if (root->rb_leftmost == node) { > > > root->rb_leftmost = rb_next(node); > > > > Think we need > > > > if (root->rb_leftmost) > > > > > + leftmost = true; > > > > DEADLINE crashes w/o that. Clearly boot testing doesn't cover that.. > I think Peter's code is correct; after removing the only node in an > rbtree rb_leftmost should be NULL. > > The issue appears to be in dequeue_pushable_dl_task unconditionally > dereferencing the pointer returned by rb_first_cached(), which may be > NULL. Oh right.. silly me. So yes, those rb_add_cached() / rb_erase_cached() return values are (currently) only used by deadline. Deadline keeps a leftmost based value cache and 'needs' this signal to update it; I can imagine there being others, I didn't look at the many (~70) other users of rb_erase_cached(). I briefly considered having rb_erase_cached() return the 'struct rb_node *' of the new leftmost; that would naturally return NULL for the empty tree. Maybe I should still do that -- see below. Another thing I noticed is that I'm inconsistend with argument order; rb_erase_cached(node, tree) vs rb_add_cached(tree, node). I'll go make the new stuff use the 'wrong' order stuff too. --- --- a/include/linux/rbtree.h +++ b/include/linux/rbtree.h @@ -141,8 +141,8 @@ static inline void rb_insert_color_cache rb_insert_color(node, &root->rb_root); } -static inline bool rb_erase_cached(struct rb_node *node, - struct rb_root_cached *root) +static inline struct rb_node * +rb_erase_cached(struct rb_node *node, struct rb_root_cached *root) { bool leftmost = false; @@ -152,7 +152,7 @@ static inline bool rb_erase_cached(struc } rb_erase(node, &root->rb_root); - return leftmost; + return leftmost ? root->rb_leftmost : NULL; } static inline void rb_replace_node_cached(struct rb_node *victim, @@ -164,8 +164,9 @@ static inline void rb_replace_node_cache rb_replace_node(victim, new, &root->rb_root); } -static inline bool rb_add_cached(struct rb_root_cached *tree, struct rb_node *node, - bool (*less)(struct rb_node *, const struct rb_node *)) +static inline struct rb_node * +rb_add_cached(struct rb_root_cached *tree, struct rb_node *node, bool + (*less)(struct rb_node *, const struct rb_node *)) { struct rb_node **link = &tree->rb_root.rb_node; struct rb_node *parent = NULL; @@ -184,7 +185,7 @@ static inline bool rb_add_cached(struct rb_link_node(node, parent, link); rb_insert_color_cached(node, tree, leftmost); - return leftmost; + return leftmost ? node : NULL; } static inline void rb_add(struct rb_root *tree, struct rb_node *node, --- a/kernel/sched/deadline.c +++ b/kernel/sched/deadline.c @@ -454,10 +454,14 @@ static inline bool __pushable_less(struc */ static void enqueue_pushable_dl_task(struct rq *rq, struct task_struct *p) { + struct rb_node *leftmost; + BUG_ON(!RB_EMPTY_NODE(&p->pushable_dl_tasks)); - if (rb_add_cached(&rq->dl.pushable_dl_tasks_root, &p->pushable_dl_tasks, - __pushable_less)) + leftmost = rb_add_cached(&rq->dl.pushable_dl_tasks_root, + &p->pushable_dl_tasks, + __pushable_less); + if (leftmost) rq->dl.earliest_dl.next = p->dl.deadline; } @@ -465,12 +469,14 @@ static void dequeue_pushable_dl_task(str { struct dl_rq *dl_rq = &rq->dl; struct rb_root_cached *root = &dl_rq->pushable_dl_tasks_root; + struct rb_node *leftmost; if (RB_EMPTY_NODE(&p->pushable_dl_tasks)) return; - if (rb_erase_cached(&p->pushable_dl_tasks, root)) - dl_rq->earliest_dl.next = __node_2_pdl(rb_first_cached(root))->dl.deadline; + leftmost = rb_erase_cached(&p->pushable_dl_tasks, root); + if (leftmost) + dl_rq->earliest_dl.next = __node_2_pdl(leftmost)->dl.deadline; RB_CLEAR_NODE(&p->pushable_dl_tasks); }