Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp52752ybl; Mon, 2 Dec 2019 07:28:21 -0800 (PST) X-Google-Smtp-Source: APXvYqzKRBYzacYU22kh8keXwwoBk5x2UKhHn2IDFvfopZPBBkP23fGsLx6O9F0+wLDCTo3E7rcn X-Received: by 2002:a1c:a5c2:: with SMTP id o185mr27963889wme.35.1575300501392; Mon, 02 Dec 2019 07:28:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1575300501; cv=none; d=google.com; s=arc-20160816; b=RC20CC8Ao1dDVlHroRMQXF1HSvUavHdz4Oj30b/KOaoEpH/TNsTxy2rXZBSHiv1mHc ig3Qua8vfp/ACmQTrmmDtDxS97TMvPC5v7k0c8YSXs4nrg+oOBUKRgCSYxQwPcXZL261 Wb0s7wwgFTIp4r6azrQrMIWtIWHukKMRQYwLqgQTyHSOCbQbZZJln4o6soWdQXXk5YVg 29O9fjdvCxJpcHAFDZ9mBH9tw3ouI32IaNsINSpa68JuHZN59I79pj5yqoGXHgMZIFkc CkLxxbUjhPIfnChSSi8B0Pv7w6Wb0dTZVbqc1KxIYZ/pl/RXxiiLHYfAtpM74Qz5GDNK UbzA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:organization:from:references:cc:to:subject; bh=EH3V3Ry7vH8iNjCndCCxxynD3qeny0GyXVPTQZwLebc=; b=dKmyaaLzCu1ag9aF4eDHweDjHf1Di8ndUI7fu60DVdBos6EZNjYADLkvVjTxgGgPLx y3WmN1mY+1taHO3sYn4039SmC1gyiMSSZJ4cVVPLfmsEoTeIL3/b1tyljR6YZsphVtJh dQwRpJTKYH4pxskGRPFDO9hTJ63CYlnhEKAYXZWCIMU9Yts/ljm5Wnn6pg+e9AxiF5qL VSy9ZVgxKeoPkMpeWkJpK9ZDmS4UkFrdr5gZV12Ur/vHXl8H9wa6TVFRobDnS4VsA4jX kG7rgWaMY4K07oLJYbM2y/ekqFnCvcHGGAcYH/+wppGOqZs/Q+vi8RpqY2VnBo5dP/tF d+fA== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q9si12615709edn.121.2019.12.02.07.27.55; Mon, 02 Dec 2019 07:28:21 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727511AbfLBPZv (ORCPT + 99 others); Mon, 2 Dec 2019 10:25:51 -0500 Received: from mga14.intel.com ([192.55.52.115]:31689 "EHLO mga14.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727413AbfLBPZv (ORCPT ); Mon, 2 Dec 2019 10:25:51 -0500 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga103.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 02 Dec 2019 07:25:50 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.69,268,1571727600"; d="scan'208";a="384921871" Received: from linux.intel.com ([10.54.29.200]) by orsmga005.jf.intel.com with ESMTP; 02 Dec 2019 07:25:50 -0800 Received: from [10.125.253.1] (abudanko-mobl.ccr.corp.intel.com [10.125.253.1]) by linux.intel.com (Postfix) with ESMTP id 93D9458048A; Mon, 2 Dec 2019 07:25:47 -0800 (PST) Subject: Re: [RFC PATCH 2/8] perf: Helpers for alloc/init/fini PMU specific data To: Peter Zijlstra , kan.liang@linux.intel.com Cc: mingo@redhat.com, acme@kernel.org, tglx@linutronix.de, bp@alien8.de, linux-kernel@vger.kernel.org, eranian@google.com, vitaly.slobodskoy@intel.com, ak@linux.intel.com References: <1574954071-6321-1-git-send-email-kan.liang@linux.intel.com> <1574954071-6321-2-git-send-email-kan.liang@linux.intel.com> <20191202131646.GD2827@hirez.programming.kicks-ass.net> From: Alexey Budankov Organization: Intel Corp. Message-ID: <7a119a02-adb8-dc18-2d74-c71903e84afe@linux.intel.com> Date: Mon, 2 Dec 2019 18:25:46 +0300 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.9.1 MIME-Version: 1.0 In-Reply-To: <20191202131646.GD2827@hirez.programming.kicks-ass.net> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 02.12.2019 16:16, 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 provide proper patterning examples for such or similar cases? Thanks, Alexey