mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Liang, Kan" <kan.liang@linux.intel.com>
To: Peter Zijlstra <peterz@infradead.org>
Cc: mingo@redhat.com, acme@kernel.org, tglx@linutronix.de,
	bp@alien8.de, linux-kernel@vger.kernel.org, eranian@google.com,
	alexey.budankov@linux.intel.com, vitaly.slobodskoy@intel.com,
	ak@linux.intel.com
Subject: Re: [RFC PATCH 2/8] perf: Helpers for alloc/init/fini PMU specific data
Date: Mon, 2 Dec 2019 15:35:00 -0500	[thread overview]
Message-ID: <2d036aa5-542a-8c01-762c-3b68136887f5@linux.intel.com> (raw)
In-Reply-To: <20191202131646.GD2827@hirez.programming.kicks-ass.net>



On 12/2/2019 8:16 AM, Peter Zijlstra wrote:
> On Thu, Nov 28, 2019 at 07:14:25AM -0800, kan.liang@linux.intel.com wrote:
> 
>> +static int
>> +__alloc_task_ctx_data_rcu(struct task_struct *task,
>> +			  size_t ctx_size, gfp_t flags)
>> +{
>> +	struct perf_ctx_data *ctx_data = task->perf_ctx_data;
>> +	int ret;
>> +
>> +	lockdep_assert_held_once(&task->perf_ctx_data_lock);
>> +
>> +	ret = alloc_perf_ctx_data(ctx_size, flags, &ctx_data);
>> +	if (ret)
>> +		return ret;
>> +
>> +	ctx_data->refcount = 1;
>> +
>> +	rcu_assign_pointer(task->perf_ctx_data, ctx_data);
>> +
>> +	return 0;
>> +}
> 
>> +static int
>> +__init_task_ctx_data_rcu(struct task_struct *task, size_t ctx_size, gfp_t flags)
>> +{
>> +	struct perf_ctx_data *ctx_data = task->perf_ctx_data;
>> +
>> +	lockdep_assert_held_once(&task->perf_ctx_data_lock);
>> +
>> +	if (ctx_data) {
>> +		ctx_data->refcount++;
>> +		return 0;
>> +	}
>> +
>> +	return __alloc_task_ctx_data_rcu(task, ctx_size, flags);
>> +}
> 
>> +/**
>> + * Free perf_ctx_data RCU pointer for a task
>> + * @task:        Target Task
>> + * @force:       Unconditionally free perf_ctx_data
>> + *
>> + * If force is set, free perf_ctx_data unconditionally.
>> + * Otherwise, free perf_ctx_data when there are no users.
>> + * Lock is required to sync the writers of perf_ctx_data RCU pointer
>> + * and refcount.
>> + */
>> +static void
>> +fini_task_ctx_data_rcu(struct task_struct *task, bool force)
>> +{
>> +	struct perf_ctx_data *ctx_data;
>> +	unsigned long flags;
>> +
>> +	raw_spin_lock_irqsave(&task->perf_ctx_data_lock, flags);
>> +
>> +	ctx_data = task->perf_ctx_data;
>> +	if (!ctx_data)
>> +		goto unlock;
>> +
>> +	if (!force && --ctx_data->refcount)
>> +		goto unlock;
>> +
>> +	RCU_INIT_POINTER(task->perf_ctx_data, NULL);
>> +	call_rcu(&ctx_data->rcu_head, free_perf_ctx_data);
>> +
>> +unlock:
>> +	raw_spin_unlock_irqrestore(&task->perf_ctx_data_lock, flags);
>> +}
> 
> All this refcount under lock is an anti-pattern. Also the naming is
> insane.
> 

Could you please give me an example?

I think we do need something to protect the refcount. Are you suggesting 
atomic_*?

Thanks,
Kan


  parent reply	other threads:[~2019-12-02 20:35 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-11-28 15:14 [RFC PATCH 1/8] perf: Save PMU specific data in task_struct kan.liang
2019-11-28 15:14 ` [RFC PATCH 2/8] perf: Helpers for alloc/init/fini PMU specific data kan.liang
2019-12-02 13:16   ` Peter Zijlstra
2019-12-02 15:25     ` Alexey Budankov
2019-12-02 20:35     ` Liang, Kan [this message]
2019-12-04 12:36       ` Peter Zijlstra
2019-11-28 15:14 ` [RFC PATCH 3/8] perf: Init/fini " kan.liang
2019-12-02 12:40   ` Peter Zijlstra
2019-12-02 14:59     ` Andi Kleen
2019-12-02 16:21       ` Peter Zijlstra
2019-12-02 19:15         ` Andi Kleen
2019-12-02 20:13           ` Liang, Kan
2019-12-02 20:25             ` Andi Kleen
2019-12-02 20:44               ` Liang, Kan
2019-12-04 13:12                 ` Peter Zijlstra
2019-12-02 16:38       ` Alexey Budankov
2019-12-02 16:43         ` Peter Zijlstra
2019-12-02 17:42           ` Alexey Budankov
2019-11-28 15:14 ` [RFC PATCH 4/8] perf: Supply task information to sched_task() kan.liang
2019-11-28 15:14 ` [RFC PATCH 5/8] perf/x86/lbr: Fix shorter LBRs call stacks for system-wide mode kan.liang
2019-11-28 15:14 ` [RFC PATCH 6/8] perf/x86: Remove swap_task_ctx() kan.liang
2019-11-28 15:14 ` [RFC PATCH 7/8] perf: Clean up pmu specific data kan.liang
2019-11-28 15:14 ` [RFC PATCH 8/8] perf: Clean up event context from sched_task() kan.liang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2d036aa5-542a-8c01-762c-3b68136887f5@linux.intel.com \
    --to=kan.liang@linux.intel.com \
    --cc=acme@kernel.org \
    --cc=ak@linux.intel.com \
    --cc=alexey.budankov@linux.intel.com \
    --cc=bp@alien8.de \
    --cc=eranian@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=tglx@linutronix.de \
    --cc=vitaly.slobodskoy@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

Powered by JetHome