Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp999455imm; Thu, 6 Sep 2018 13:36:47 -0700 (PDT) X-Google-Smtp-Source: ANB0Vdaa5niDarifRtF1pY65KVu+rzD9KP112V7wWoPVNSZU9m97GJhbZbOOFhl89w0q6fih1Mo9 X-Received: by 2002:a62:848e:: with SMTP id k136-v6mr4813442pfd.231.1536266207375; Thu, 06 Sep 2018 13:36:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536266207; cv=none; d=google.com; s=arc-20160816; b=wgQksN4snqV2vPvh6X6LWT9zhKZ6s5jsLQ735c1aXe8VIp+BVKv+MPMDHecZfqYhmw mwhtShVdVqYDhIbQCMunwTDNqXYDlsDdEXVHLuAW0fILYWEQknkEFKVfc3QMYCF3ncus Qh7If03U2ibMIfhLpVZ+xwGvyvCapP/qupGn8DsoIp2YPiDSh8pYMlyZ9580wVcFYHKV gESVtRBf2+HgzMtdbi3jgCFb1lm7MDLVQYb4y5ofpetLhbJSuKSR97G5Qb7iWJZbWaPS t0CwbFgluC5CxXS7CfereAAOf6RWoRhXA9wwf9Zp15TtAqS/6HFmiecDidfbvoT7w+Nd zxbA== 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 :in-reply-to:references:mime-version:dkim-signature; bh=vDgs0JSFi3+yNN105xhhVZSUB5brvMtE41ecnMoS628=; b=dV6xMLNjWmsfRTsBiu+l1H7P8T85YmhdUua6IMgznzdOx7C97Qji07Z37aQdTriO9H DaslKu+WuiZRN/qpwrXKhEJrNSVPMd7L4oBCtD/ASo+fjGyCCPscJeavObz/9nzoO9rR /O3qt7PTUOBYjhaCem7kN47h9Ab9J+CzS0I3Ns3MnW1+3PVEgOMoQxAhkSRasDKyGW3p rP8lVEtgIA8lNL1UGcTcCvfsPAVZNSRMTYDmbFR924JkIgQTSrwfkg/ylLvr4W7SLXHc 8Bpme9UBF5tEdR346vJjxqkW23+3E9EJpDJkQAc5U1EUTsY0wZU/9vXx4EElrpURwipk 0vqQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=vWfzLnhJ; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l185-v6si6164389pfl.134.2018.09.06.13.36.28; Thu, 06 Sep 2018 13:36:47 -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=pass header.i=@google.com header.s=20161025 header.b=vWfzLnhJ; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730219AbeIFTjb (ORCPT + 99 others); Thu, 6 Sep 2018 15:39:31 -0400 Received: from mail-wm0-f67.google.com ([74.125.82.67]:51942 "EHLO mail-wm0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725819AbeIFTja (ORCPT ); Thu, 6 Sep 2018 15:39:30 -0400 Received: by mail-wm0-f67.google.com with SMTP id y2-v6so11748459wma.1 for ; Thu, 06 Sep 2018 08:03:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=vDgs0JSFi3+yNN105xhhVZSUB5brvMtE41ecnMoS628=; b=vWfzLnhJW0TlH3Jhg9urQ4OdUOIGQEop+hlH8c+kru3kRkAf6eQ1sHHhDe+T+AvHVp e0iWCAQDW0v64PlOPhu3NIHfZiB5Fz6IiKCMRou+yPa7AtrLMpIYRY0DknpzpWhYfWzy yLXV1ZavyFJRTGq/Q2hEE84ObRr1WDihERcEqgMXLSeHrErikcb/+Yf9NEdNb/f1iZ0q DNeiaMUQL9qyFWhwk4jw2pzEB1WVvhOBVJGz2J9Orvlb0/eiiAq8SSvqmt6ldYkZS5HP qOchJWVNT/rmT/7MCXiGC6sTa2U9jG/QKPjlyCx/GslZj6qU4ZwkwKZ6+/cs3nKy0uNa RwKw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=vDgs0JSFi3+yNN105xhhVZSUB5brvMtE41ecnMoS628=; b=ngAI38d/8lg4ROf2S9pvl5Wf2V3fzjkz/ftPefLQnaJDjTqtkt5EMkyuSTtjJsS7CW dfYtCGEEPFpwH3jzUY0U17kw+KPAgyFUnV47JLj9PMnccYlu1eDY6zkHMdDf34JUBI5P rd+HkL1dyc+b1fafRl9Brmo3E5Nmd5ULnauKVsciUAM1ZS2bqtI5+52FzLn5ryrkuNhN jyEjXTXylc6DtqJxGJHrYKXTEd+CLxZfYL9F7JPBdO+t4HG5PmMmkLBiIt68v07FIR1J DwVoaD+k7UGoPJfRHQp1vht0nYJ1ocOgkZRaUgcHXRD8YL+l6fTP6HFa8Of9kXIWCoRA AFzg== X-Gm-Message-State: APzg51CKjNnR6XmAxWxbqj8vcHaI3AObMluBtnes7Lhx8LxZxYe2rQSd KHP20MGlYX3SLCd6BMaNF9ThixnkXUU83YGvee05tw== X-Received: by 2002:a1c:9d02:: with SMTP id g2-v6mr2228025wme.122.1536246211708; Thu, 06 Sep 2018 08:03:31 -0700 (PDT) MIME-Version: 1.0 References: <20180810115431.GA4162@krava> <20180813130446.GA8685@krava> <20180815084825.GD3180@krava> <20180827092818.GA3725@krava> <20180827170543.GA31347@krava> <20180902144738.GA28012@krava> <20180906132859.GA9577@krava> In-Reply-To: <20180906132859.GA9577@krava> From: Stephane Eranian Date: Thu, 6 Sep 2018 08:04:02 -0700 Message-ID: Subject: Re: [PATCHv3] perf tools: Add struct ordered_events_buffer layer To: Jiri Olsa Cc: LKML , Arnaldo Carvalho de Melo , Peter Zijlstra , mingo@elte.hu, Namhyung Kim 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 Thu, Sep 6, 2018 at 6:29 AM Jiri Olsa wrote: > > On Mon, Sep 03, 2018 at 07:37:56PM -0700, Stephane Eranian wrote: > > SNIP > > > > > I think the code is correct now for the issue related to uninitialized pointer. > > But there is still one problem I found stressing the code with max_alloc_size. > > The way the following is written: > > > > if (!list_empty(cache)) { > > new = list_entry(cache->next, struct ordered_event, list); > > list_del(&new->list); > > } else if (oe->buffer) { > > new = oe->buffer + oe->buffer_idx; > > if (++oe->buffer_idx == MAX_SAMPLE_BUFFER) > > oe->buffer = NULL; > > } else if (oe->cur_alloc_size < oe->max_alloc_size) { > > size_t size = sizeof(*oe->buffer) MAX_SAMPLE_BUFFER * > > sizeof(*new); > > > > oe->buffer = malloc(size); > > if (!oe->buffer) { > > free_dup_event(oe, new_event); > > return NULL; > > } > > > > pr("alloc size %" PRIu64 "B (+%zu), max %" PRIu64 "B\n", > > oe->cur_alloc_size, size, oe->max_alloc_size); > > > > oe->cur_alloc_size += size; > > > > You can end up with oe->cur_alloc_size > oe->max_alloc_size in case > > the max limit is > > really low (< size_t size = sizeof (*oe->buffer) + MAX_SAMPLE_BUFFER * > > sizeof(*new); > > So I think to make sure you can never allocate more than the max, you > > have to do: > > > > size_t size = sizeof(*oe->buffer) MAX_SAMPLE_BUFFER * sizeof(*new); > > if (!list_empty(cache)) { > > new = list_entry(cache->next, struct ordered_event, list); > > list_del(&new->list); > > } else if (oe->buffer) { > > new = oe->buffer + oe->buffer_idx; > > if (++oe->buffer_idx == MAX_SAMPLE_BUFFER) > > oe->buffer = NULL; > > } else if ((oe->cur_alloc_size + size) < oe->max_alloc_size) { > > > > Then you will never allocate more than the max. > > I think with this change, we are okay. > > Tested-by: Stephane Eranian > > yep, makes sense.. something like below then > I'll post it on top of the previous patch > Yes. This works. Thanks. > thanks, > jirka > > > --- > diff --git a/tools/perf/util/ordered-events.c b/tools/perf/util/ordered-events.c > index 87171e8fd70d..2d1d0f3c8f77 100644 > --- a/tools/perf/util/ordered-events.c > +++ b/tools/perf/util/ordered-events.c > @@ -101,6 +101,7 @@ static struct ordered_event *alloc_event(struct ordered_events *oe, > struct list_head *cache = &oe->cache; > struct ordered_event *new = NULL; > union perf_event *new_event; > + size_t size; > > new_event = dup_event(oe, event); > if (!new_event) > @@ -133,6 +134,8 @@ static struct ordered_event *alloc_event(struct ordered_events *oe, > * Removal of ordered event object moves it from events to > * the cache list. > */ > + size = sizeof(*oe->buffer) + MAX_SAMPLE_BUFFER * sizeof(*new); > + > if (!list_empty(cache)) { > new = list_entry(cache->next, struct ordered_event, list); > list_del(&new->list); > @@ -140,10 +143,7 @@ static struct ordered_event *alloc_event(struct ordered_events *oe, > new = &oe->buffer->event[oe->buffer_idx]; > if (++oe->buffer_idx == MAX_SAMPLE_BUFFER) > oe->buffer = NULL; > - } else if (oe->cur_alloc_size < oe->max_alloc_size) { > - size_t size = sizeof(*oe->buffer) + > - MAX_SAMPLE_BUFFER * sizeof(*new); > - > + } else if ((oe->cur_alloc_size + size) < oe->max_alloc_size) { > oe->buffer = malloc(size); > if (!oe->buffer) { > free_dup_event(oe, new_event);