From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail.kernel.org ([198.145.29.99]:38244 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388517AbeGYCOb (ORCPT ); Tue, 24 Jul 2018 22:14:31 -0400 Date: Wed, 25 Jul 2018 10:05:20 +0900 From: Masami Hiramatsu To: Steven Rostedt Cc: Ingo Molnar , Shuah Khan , Tom Zanussi , Hiraku Toyooka , linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: Re: [PATCH 1/3] [BUGFIX] tracing: Fix double free of event_trigger_data Message-Id: <20180725100520.9370df20d29d529236f4a368@kernel.org> In-Reply-To: <20180724164959.3cbc1422@gandalf.local.home> References: <153149923649.11274.14970833360963898112.stgit@devbox> <153149926702.11274.12489440326560729788.stgit@devbox> <20180723221006.60cc7aa9@vmware.local.home> <20180725000909.6c8b2f3881ee75c4f6bd466b@kernel.org> <20180724164959.3cbc1422@gandalf.local.home> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: stable-owner@vger.kernel.org List-ID: On Tue, 24 Jul 2018 16:49:59 -0400 Steven Rostedt wrote: > On Wed, 25 Jul 2018 00:09:09 +0900 > Masami Hiramatsu wrote: > > > Hmm, your patch seems to leak a memory since event_trigger_init() will > > be called twice on same trigger_data (Note that event_trigger_init() > > does not init ref counter, but increment it.) So we should decrement > > it when we find it is succeeded. Moreover, if register_trigger() > > Good catch, and easily fixed. > > > fails before calling data->ops->init() (see -EEXIST case), the ref > > counter will be 0 (-1 +1). But if it fails after data->ops->init(), > > the ref counter will be 1 (-1 +1 +1). It still be unstable. > > (Ah, that means we may have another trouble...) > > I'm not sure there's a problem here. I now have: > > out_reg: > /* Up the trigger_data count to make sure reg doesn't free it on failuer */ > event_trigger_init(trigger_ops, trigger_data); > ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file); > /* > * The above returns on success the # of functions enabled, > * but if it didn't find any functions it returns zero. > * Consider no functions a failure too. > */ > if (!ret) { > ret = -ENOENT; > } else if (ret > 0) > ret = 0; Can we mixed up ret == 0 and ret > 0? It seems cmd_ops->reg() == 0 is a failure case. > > /* Down the counter of trigger_data or free it if not used anymore */ > event_trigger_free(trigger_ops, trigger_data); > out: > return ret; > > Thus we increment trigger_data before calling reg, and free it > afterward. But if reg() did an init too, then the event_trigger_free() > just decs the ref counter. To avoid confusion, I would like to suggest to rename those pair to event_trigger_data_get/put(). :) > > As for register_trigger() > > > > > > > > > > P.S. This brings up another minor bug. The failure should return ENOMEM > > > not ENOENT. > > > > Hmm it seems we should review the register_trigger() implementation. > > It should return the return value of trace_event_trigger_enable_disable(), > > shouldn't it? > > > > Yeah, that's not done well. I'll fix it up. Thanks! > > Thanks for pointing it out. > > -- Steve -- Masami Hiramatsu