Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp444695ybl; Mon, 2 Dec 2019 13:10:31 -0800 (PST) X-Google-Smtp-Source: APXvYqxtUCeeQ+9BJmgq5U2gpJ/z7xSm0yShER/OYJE4zwO75Sxl61fvWc/HEKKuTK4x/kGgh/UF X-Received: by 2002:a1c:9cce:: with SMTP id f197mr25445872wme.133.1575321031550; Mon, 02 Dec 2019 13:10:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1575321031; cv=none; d=google.com; s=arc-20160816; b=Xm8S2rKyOIBN5kcSSULzyt2AYJ57vuSFBsUyBnYhPaSfZfL57iUmC/4fzN5MnKeVI6 MNJN1zkt2VvM7Fz/txX6o2+bfBCGJglHXzGg3SXjIEie6mubiUnM6qRrWVQpt6i4+vhL +xg50EPH6ymFK5r1VfqN98GLtFPOxP4kZaxZGQ4TTbWDZCORAx5oASqPEXw8Jtr34xpF OGddBTSWWURwJ1hb/e3FXh5Nl3m+WLfK+K8kAIer6CdFw1Ww8dM2dp9g2LmSX6SWVldy SbrYlQXYlLzp+4X/EoEXg3WhetXWeY3RLgfUebEyXjr3VhzYKcWHBA1R5juEOamSpdBU qGGQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=k9dGjGCO+7zbmccYGCfjWcaU0D0XLobeWL4r3GmgINY=; b=QG372EpFk5RBDXRHv8mPWwhWAO67TUignAgBX72cm4GasIH44H8nIji32pIqdcBYAd Sns9wGwpDh4byXJu+9lx6VeTJHt5gB/dt2yIhwsTHTsu0yv8HunjSZ0H8L1lm/DMTngV Yq3sgV+0RgCZse4bbAP9k40FRROG+KqgLbh4C1fvW9eeDNNL3Libcd0nbDi2dZ/uCryH ruUp8JYW/tR+i0opgHscVUkCHJNBV82hwdO30VmeW1+4JY6ksA+7kNUVFuQSppdAgFFA 8hvZehj8+N/tx7/ETw/g4WbDzokhdGyQ8wZeCleaETxvyI1ynt8EKpY0CTWi/YK3lwUe dzyA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@joelfernandes.org header.s=google header.b=qdzvA3VM; 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 rl6si581750ejb.108.2019.12.02.13.10.06; Mon, 02 Dec 2019 13:10:31 -0800 (PST) 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; dkim=pass header.i=@joelfernandes.org header.s=google header.b=qdzvA3VM; 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 S1726057AbfLBVI5 (ORCPT + 99 others); Mon, 2 Dec 2019 16:08:57 -0500 Received: from mail-pj1-f68.google.com ([209.85.216.68]:38223 "EHLO mail-pj1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725834AbfLBVI5 (ORCPT ); Mon, 2 Dec 2019 16:08:57 -0500 Received: by mail-pj1-f68.google.com with SMTP id l4so336718pjt.5 for ; Mon, 02 Dec 2019 13:08:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=joelfernandes.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=k9dGjGCO+7zbmccYGCfjWcaU0D0XLobeWL4r3GmgINY=; b=qdzvA3VMi0NIkfoVj0gAQ6zt/Nx13PBrCL4NXRabzZ2Tcq93XlDbRhvK4CtOR0s9HX tihGgm5JB6w5gv/0Sx8sZwswkYEd+QYOWSAoJCsxJAh07EEB9mNAB0soEqUAkOo9l/ny MEU5xGJj0H74QKwnrSMd8hT5ahqUoAZWhRXP0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=k9dGjGCO+7zbmccYGCfjWcaU0D0XLobeWL4r3GmgINY=; b=nyMqVGYR3f2ztovLELksk/z+/G9YEkOZqFCuCrUUVMWdWqNOskMykoPTRa2Rw8WAF7 5WQFPXOqVzvGSI0pHNNoKazm478MdP9tU2uzcJU3oxXGZom+XurAjK98cDLLLTELmf1G idONObcefqnQRbH8W8LFpYTR/LrPn+ew1V3lACK8TyR3vMuKuMDXXoJcf8nBZG4POtqW vcBF+r8xqLqm1E/3ADoFoI1Mey0UOl/zr810btAVpcBHoGFIR+5Uhz8UDeRNhk/Fawid /ueONIZpGbwnF4ph24I8rCrvpZmGXeQp2j08JhOuKfWxAoTd7z8k5I+OKR6BwQn5NeSy zkdg== X-Gm-Message-State: APjAAAV9LGSdgx80t1PSxk5gK3BDBtJzCIuLC52B7ZDf0qusZkk5AR+z SneO5/SwHCzvnWyzCg1QNSUNcyuczuw= X-Received: by 2002:a17:90b:252:: with SMTP id fz18mr1259557pjb.49.1575320936772; Mon, 02 Dec 2019 13:08:56 -0800 (PST) Received: from localhost ([2620:15c:6:12:9c46:e0da:efbf:69cc]) by smtp.gmail.com with ESMTPSA id w4sm264400pjt.21.2019.12.02.13.08.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Dec 2019 13:08:55 -0800 (PST) Date: Mon, 2 Dec 2019 16:08:54 -0500 From: Joel Fernandes To: Masami Hiramatsu Cc: Ingo Molnar , Anders Roxell , paulmck@kernel.org, "Naveen N . Rao" , Anil S Keshavamurthy , David Miller , Linux Kernel Mailing List Subject: Re: [PATCH -tip] kprobes: Lock rcu_read_lock() while searching kprobe Message-ID: <20191202210854.GD17234@google.com> References: <157527193358.11113.14859628506665612104.stgit@devnote2> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <157527193358.11113.14859628506665612104.stgit@devnote2> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Dec 02, 2019 at 04:32:13PM +0900, Masami Hiramatsu wrote: > Anders reported that the lockdep warns that suspicious > RCU list usage in register_kprobe() (detected by > CONFIG_PROVE_RCU_LIST.) This is because get_kprobe() > access kprobe_table[] by hlist_for_each_entry_rcu() > without rcu_read_lock. > > If we call get_kprobe() from the breakpoint handler context, > it is run with preempt disabled, so this is not a problem. > But in other cases, instead of rcu_read_lock(), we locks > kprobe_mutex so that the kprobe_table[] is not updated. > So, current code is safe, but still not good from the view > point of RCU. > > Let's lock the rcu_read_lock() around get_kprobe() and > ensure kprobe_mutex is locked at those points. > > Note that we can safely unlock rcu_read_lock() soon after > accessing the list, because we are sure the found kprobe has > never gone before unlocking kprobe_mutex. Unless locking > kprobe_mutex, caller must hold rcu_read_lock() until it > finished operations on that kprobe. > > Reported-by: Anders Roxell > Signed-off-by: Masami Hiramatsu Instead of this, can you not just pass the lockdep_is_held() expression as the last argument to list_for_each_entry_rcu() to silence the warning? Then it will be a simpler patch. thanks, - Joel > --- > kernel/kprobes.c | 18 ++++++++++++++---- > 1 file changed, 14 insertions(+), 4 deletions(-) > > diff --git a/kernel/kprobes.c b/kernel/kprobes.c > index 53534aa258a6..fd814ea7dbd8 100644 > --- a/kernel/kprobes.c > +++ b/kernel/kprobes.c > @@ -319,6 +319,7 @@ static inline void reset_kprobe_instance(void) > * - under the kprobe_mutex - during kprobe_[un]register() > * OR > * - with preemption disabled - from arch/xxx/kernel/kprobes.c > + * In both cases, caller must disable preempt (or acquire rcu_read_lock) > */ > struct kprobe *get_kprobe(void *addr) > { > @@ -435,6 +436,7 @@ static int kprobe_queued(struct kprobe *p) > /* > * Return an optimized kprobe whose optimizing code replaces > * instructions including addr (exclude breakpoint). > + * This must be called with locking kprobe_mutex. > */ > static struct kprobe *get_optimized_kprobe(unsigned long addr) > { > @@ -442,9 +444,12 @@ static struct kprobe *get_optimized_kprobe(unsigned long addr) > struct kprobe *p = NULL; > struct optimized_kprobe *op; > > + lockdep_assert_held(&kprobe_mutex); > + rcu_read_lock(); > /* Don't check i == 0, since that is a breakpoint case. */ > for (i = 1; !p && i < MAX_OPTIMIZED_LENGTH; i++) > p = get_kprobe((void *)(addr - i)); > + rcu_read_unlock(); /* We are safe because kprobe_mutex is held */ > > if (p && kprobe_optready(p)) { > op = container_of(p, struct optimized_kprobe, kp); > @@ -1478,18 +1483,21 @@ static struct kprobe *__get_valid_kprobe(struct kprobe *p) > { > struct kprobe *ap, *list_p; > > + lockdep_assert_held(&kprobe_mutex); > + rcu_read_lock(); > ap = get_kprobe(p->addr); > if (unlikely(!ap)) > - return NULL; > + goto out; > > if (p != ap) { > list_for_each_entry_rcu(list_p, &ap->list, list) > if (list_p == p) > /* kprobe p is a valid probe */ > - goto valid; > - return NULL; > + goto out; > + ap = NULL; > } > -valid: > +out: > + rcu_read_unlock(); /* We are safe because kprobe_mutex is held */ > return ap; > } > > @@ -1602,7 +1610,9 @@ int register_kprobe(struct kprobe *p) > > mutex_lock(&kprobe_mutex); > > + rcu_read_lock(); > old_p = get_kprobe(p->addr); > + rcu_read_unlock(); /* We are safe because kprobe_mutex is held */ > if (old_p) { > /* Since this may unoptimize old_p, locking text_mutex. */ > ret = register_aggr_kprobe(old_p, p); >