Received: by 10.192.165.156 with SMTP id m28csp1270571imm; Wed, 18 Apr 2018 07:05:31 -0700 (PDT) X-Google-Smtp-Source: AIpwx4/FdSUG7zHHHseX+8OGrlCRuhh2wwGHFhShY5SRM62um3xXC2eg5YzQ4bx+5cV6gXA3HTAS X-Received: by 10.99.2.202 with SMTP id 193mr1871237pgc.117.1524060330989; Wed, 18 Apr 2018 07:05:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1524060330; cv=none; d=google.com; s=arc-20160816; b=V+FB9zqRTeV6ZSLLM/V1w+6EUwyeCFqQtUc+SAhkKeTg1sElvy5kf6gX1NfJPahCdj tH3US5DrnLQBcw8L5I9DkqjBGVH9NZgLWYVl0F+jUhRg/kIQPpXx+yZPulcGCyh+Tbf8 2dfYn/MiP2adxTcJOTrAug2wM62VRvtHZy+ctW/jElksEMb2yyLKwPeG046AeHdi2UFA CM9GMjGADNVlaB6s+HssRYnzHtb3kD47iz162t0cJ7vsteWmA+zyEPXAZ99yBEC+4U8K h33xDbYzpUAXKEmFNKpgxVV7PPa8UZJWb471MipwAnAoKEzABilxr8V1L0pcDiijd266 DvAQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :references:in-reply-to:mime-version:dkim-signature :arc-authentication-results; bh=33kHDjrT30DyPw+5CzmDVxcxvqsRsJEzIIBJap38yRQ=; b=N+IrA2FOtECX5d3v6zXPl99fUQI2kxpw1CjDeCeQD+Jc09lFHvMQpMbn3hfEfOVpGX CP2/mSX1uhgWz8reuCn7Ofdkx9aP8Urf6Rkj8rWDv38oJCROaxL0m48XUar2zAvCF3Ie zUUz8oZcSB6SIqx6wixqF0JGTEUlwrgM4+U5vItfPHzKBoNatwQwxE+yyD1QgKn9/Fky frW/0w7QA41EBiTKyWx1tpOrt6yt5wZxGF4YVOR9X0HwC38EK4jjrzdLiZXohBRwu9cX zsWIzQs/N8yBLYw513GceR8X9zVw/sOx/b83XRVX3KC2+bTWmVtwwDO4NAl0KuPHopFz rI7Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=temperror (no key for signature) header.i=@szeredi.hu header.s=google header.b=TOFUpvY9; 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 v7si1235791pfi.28.2018.04.18.07.05.16; Wed, 18 Apr 2018 07:05:30 -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; dkim=temperror (no key for signature) header.i=@szeredi.hu header.s=google header.b=TOFUpvY9; 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 S1753551AbeDRODs (ORCPT + 99 others); Wed, 18 Apr 2018 10:03:48 -0400 Received: from mail-yb0-f196.google.com ([209.85.213.196]:41516 "EHLO mail-yb0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753185AbeDRODp (ORCPT ); Wed, 18 Apr 2018 10:03:45 -0400 Received: by mail-yb0-f196.google.com with SMTP id v14-v6so612206ybk.8 for ; Wed, 18 Apr 2018 07:03:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=szeredi.hu; s=google; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=33kHDjrT30DyPw+5CzmDVxcxvqsRsJEzIIBJap38yRQ=; b=TOFUpvY9QHjYZGIWduQ5+MfE0qNwl5ihrG6SNEGCknHCIyh3RLi0yWZbP0/wcsTFIi QT5iLvvBVeG64WEIjjGZmjblKJyV5QUQxinYLa/HK4CtlkZQJgQHJcJfjx8OHki1lSxw OGdSucfCY3L6GccljnzawZq9b6pSix0RBVQhk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=33kHDjrT30DyPw+5CzmDVxcxvqsRsJEzIIBJap38yRQ=; b=Vvrfu/MwHKXfspOInUPVP7bomhp1AL0gqmyWkUgdSLM45885xvd5VdAunfPan3C/JZ UMy+xSzdCRVU9TRh2JwTRGjGbi/EI+k4P3flqrFq0syx0hIBTHUQFOlkOBB42gkuGWll KYiRAbn5QKovIPVMVuj9FnNCa6fUmMkaud4n6bLceuaAzR2+OVQnvpcBoGYSjJCZ9S2w Wjut5og7S/Yl4fQPmGKGN+dzshYy3rARj5a1yoexwwXFwbhA+CHSwMcCb4eCx0/tEM7+ QWqtryzraMyBAGJS+LDoa6JjwkFk7+OaxiCXjOQJIsOdPJCX/ZvoV98u18Rj7NO++GOi 7FDw== X-Gm-Message-State: ALQs6tAAJfUH4+4GuZVM0PUMjNz56YtJPfIq1BGnxNezpIL+m2i1+tRY HtxDQWPKufShIsnVf0ujVfnYXtGi X-Received: by 2002:a5b:7ce:: with SMTP id t14-v6mr1148231ybq.186.1524060223917; Wed, 18 Apr 2018 07:03:43 -0700 (PDT) Received: from mail-yb0-f182.google.com (mail-yb0-f182.google.com. [209.85.213.182]) by smtp.gmail.com with ESMTPSA id t66-v6sm473222ywe.8.2018.04.18.07.03.43 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 18 Apr 2018 07:03:43 -0700 (PDT) Received: by mail-yb0-f182.google.com with SMTP id z1-v6so613124ybn.11 for ; Wed, 18 Apr 2018 07:03:43 -0700 (PDT) X-Received: by 2002:a25:850b:: with SMTP id w11-v6mr1120349ybk.325.1524060222828; Wed, 18 Apr 2018 07:03:42 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a25:ca52:0:0:0:0:0 with HTTP; Wed, 18 Apr 2018 07:03:42 -0700 (PDT) In-Reply-To: <20180418062907.3210386-1-songliubraving@fb.com> References: <20180418062907.3210386-1-songliubraving@fb.com> From: Miklos Szeredi Date: Wed, 18 Apr 2018 16:03:42 +0200 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH 1/2] tracing: fix bad use of igrab in trace_uprobe.c To: Song Liu Cc: LKML , kernel-team , Steven Rostedt , Ingo Molnar , Howard McLauchlan , Josef Bacik , Srikar Dronamraju Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Apr 18, 2018 at 8:29 AM, Song Liu wrote: > As Miklos reported and suggested: > > This pattern repeats two times in trace_uprobe.c and in > kernel/events/core.c as well: > > ret = kern_path(filename, LOOKUP_FOLLOW, &path); > if (ret) > goto fail_address_parse; > > inode = igrab(d_inode(path.dentry)); > path_put(&path); > > And it's wrong. You can only hold a reference to the inode if you > have an active ref to the superblock as well (which is normally > through path.mnt) or holding s_umount. > > This way unmounting the containing filesystem while the tracepoint is > active will give you the "VFS: Busy inodes after unmount..." message > and a crash when the inode is finally put. > > Solution: store path instead of inode. > > This patch fixes two instances in trace_uprobe.c. > > Fixes: f3f096cfedf8 ("tracing: Provide trace events interface for uprobes") > Fixes: 33ea4b24277b ("perf/core: Implement the 'perf_uprobe' PMU") > Cc: Steven Rostedt > Cc: Ingo Molnar > Cc: Howard McLauchlan > Cc: Josef Bacik > Cc: Srikar Dronamraju > Reported-by: Miklos Szeredi > Signed-off-by: Song Liu > --- > kernel/trace/trace_uprobe.c | 42 ++++++++++++++---------------------------- > 1 file changed, 14 insertions(+), 28 deletions(-) > > diff --git a/kernel/trace/trace_uprobe.c b/kernel/trace/trace_uprobe.c > index 0d450b4..80dfcdf 100644 > --- a/kernel/trace/trace_uprobe.c > +++ b/kernel/trace/trace_uprobe.c > @@ -55,7 +55,7 @@ struct trace_uprobe { > struct list_head list; > struct trace_uprobe_filter filter; > struct uprobe_consumer consumer; > - struct inode *inode; > + struct path path; > char *filename; > unsigned long offset; > unsigned long nhit; > @@ -289,7 +289,7 @@ static void free_trace_uprobe(struct trace_uprobe *tu) > for (i = 0; i < tu->tp.nr_args; i++) > traceprobe_free_probe_arg(&tu->tp.args[i]); > > - iput(tu->inode); > + path_put(&tu->path); > kfree(tu->tp.call.class->system); > kfree(tu->tp.call.name); > kfree(tu->filename); > @@ -363,7 +363,6 @@ static int register_trace_uprobe(struct trace_uprobe *tu) > static int create_trace_uprobe(int argc, char **argv) > { > struct trace_uprobe *tu; > - struct inode *inode; > char *arg, *event, *group, *filename; > char buf[MAX_EVENT_NAME_LEN]; > struct path path; > @@ -371,7 +370,6 @@ static int create_trace_uprobe(int argc, char **argv) > bool is_delete, is_return; > int i, ret; > > - inode = NULL; > ret = 0; > is_delete = false; > is_return = false; > @@ -448,14 +446,6 @@ static int create_trace_uprobe(int argc, char **argv) > if (ret) > goto fail_address_parse; > > - inode = igrab(d_inode(path.dentry)); This is not against -linus tree. > - path_put(&path); > - > - if (!inode || !S_ISREG(inode->i_mode)) { > - ret = -EINVAL; > - goto fail_address_parse; > - } > - > ret = kstrtoul(arg, 0, &offset); > if (ret) > goto fail_address_parse; > @@ -490,7 +480,8 @@ static int create_trace_uprobe(int argc, char **argv) > goto fail_address_parse; > } > tu->offset = offset; > - tu->inode = inode; > + tu->path.mnt = path.mnt; > + tu->path.dentry = path.dentry; You can just assign the whole structure. No need to mess with individual members. tu->path = path; > tu->filename = kstrdup(filename, GFP_KERNEL); > > if (!tu->filename) { > @@ -558,7 +549,7 @@ static int create_trace_uprobe(int argc, char **argv) > return ret; > > fail_address_parse: > - iput(inode); > + path_put(&path); > > pr_info("Failed to parse address or file.\n"); > > @@ -937,7 +928,8 @@ probe_event_enable(struct trace_uprobe *tu, struct trace_event_file *file, > goto err_flags; > > tu->consumer.filter = filter; > - ret = uprobe_register(tu->inode, tu->offset, &tu->consumer); > + ret = uprobe_register(d_inode(tu->path.dentry), tu->offset, > + &tu->consumer); It is not entirely clear how the lifetime of uprobe relates to the lifetime of trace_uprobe. Is the uprobe object never going to survive its creator trace_uprobe object? If that's the case, it warrants a comment. If that's not the case, then the path would need to be passed to uprobe_resister() which would need to obtain its own reference. > if (ret) > goto err_buffer; > > @@ -981,7 +973,7 @@ probe_event_disable(struct trace_uprobe *tu, struct trace_event_file *file) > > WARN_ON(!uprobe_filter_is_empty(&tu->filter)); > > - uprobe_unregister(tu->inode, tu->offset, &tu->consumer); > + uprobe_unregister(d_inode(tu->path.dentry), tu->offset, &tu->consumer); > tu->tp.flags &= file ? ~TP_FLAG_TRACE : ~TP_FLAG_PROFILE; > > uprobe_buffer_disable(); > @@ -1056,7 +1048,8 @@ static int uprobe_perf_close(struct trace_uprobe *tu, struct perf_event *event) > write_unlock(&tu->filter.rwlock); > > if (!done) > - return uprobe_apply(tu->inode, tu->offset, &tu->consumer, false); > + return uprobe_apply(d_inode(tu->path.dentry), tu->offset, > + &tu->consumer, false); > > return 0; > } > @@ -1088,7 +1081,8 @@ static int uprobe_perf_open(struct trace_uprobe *tu, struct perf_event *event) > > err = 0; > if (!done) { > - err = uprobe_apply(tu->inode, tu->offset, &tu->consumer, true); > + err = uprobe_apply(d_inode(tu->path.dentry), > + tu->offset, &tu->consumer, true); > if (err) > uprobe_perf_close(tu, event); > } > @@ -1352,7 +1346,6 @@ struct trace_event_call * > create_local_trace_uprobe(char *name, unsigned long offs, bool is_return) > { > struct trace_uprobe *tu; > - struct inode *inode; > struct path path; > int ret; > > @@ -1360,14 +1353,6 @@ create_local_trace_uprobe(char *name, unsigned long offs, bool is_return) > if (ret) > return ERR_PTR(ret); > > - inode = igrab(d_inode(path.dentry)); > - path_put(&path); > - > - if (!inode || !S_ISREG(inode->i_mode)) { > - iput(inode); > - return ERR_PTR(-EINVAL); > - } > - > /* > * local trace_kprobes are not added to probe_list, so they are never > * searched in find_trace_kprobe(). Therefore, there is no concern of > @@ -1383,7 +1368,8 @@ create_local_trace_uprobe(char *name, unsigned long offs, bool is_return) > } > > tu->offset = offs; > - tu->inode = inode; > + tu->path.mnt = path.mnt; > + tu->path.dentry = path.dentry; tu->path = path > tu->filename = kstrdup(name, GFP_KERNEL); > init_trace_event_call(tu, &tu->tp.call); > > -- > 2.9.5 >