Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3411359imu; Mon, 28 Jan 2019 04:29:59 -0800 (PST) X-Google-Smtp-Source: ALg8bN5A9yfxZV2O9BZB7yrzDVrRI+rxGsDRc3ysDw7xnYA4+/YS0CDTDOpIv9MkEm9uF0hnvMSe X-Received: by 2002:a62:1b83:: with SMTP id b125mr22529177pfb.42.1548678599872; Mon, 28 Jan 2019 04:29:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548678599; cv=none; d=google.com; s=arc-20160816; b=dKm97f7Qh7DPydmlimf8cXta+eQZ/WHx/SZFcblaGjOac4WX/JCzoVN2zXA+G3ZoOi PbxjJeZ41lx6+n9qVkqGhQKuUyYO7ErZS0SRj1K58jSCjklqGK9EdBSDAxPtT1leBkfr s4p9OiE6qM1IU7g0FyzbgdMAo8SDJ2VPJZBePqM9veFD1J7GhEhZFwh016o65Cz3jYmC wO//Tu2CnIB3Uf+85Vls0TdJ17+4pQ4YoEnGlBG/sZ1BnMztULUpyXyQ0r7F3sbN4fnn yp0dUde79b639IB+74ZsUM2IMdFeY6xWePjqMS814wzWGCaRhwz2GRAoE5G7P/n6h8UA Ca6A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from; bh=0nCBQo50PZE7uFn8g3kownP1KxL+TYpb+KJWPsXMH80=; b=z3i5YMUCBfTK/ADA+uwqPh+xPY1qBZQwzMxpbom82NcyjhOEUyr8FSFaGi2pB8ay7N GbAGEwTEcmjBrqjWldJ/X2QJj4aCBU1OmXx6FEo9c4j6f/HRsxDQPDumNzPhdheuOTu3 wlvTBdHaebf73ajC24giKy8uOaXNrP++1IIHqMALf4Mr/22lqbGg85nCn6GuintWJXNf 3+FDtFNjIpN0F4YZfQz5wy74G+1G4pTYjGKcX2xJO+XcA2zp3wctPMoVkWwrSyAj3bQj EcWhfzVkNowvcerLhRfQu5B5YOK0rajOE5cIUv0TMQhdHY80ZZmn7ltjSwivVGb+Qnla Bdsw== 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 y73si4002689pgd.478.2019.01.28.04.29.44; Mon, 28 Jan 2019 04:29:59 -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 S1726937AbfA1M14 (ORCPT + 99 others); Mon, 28 Jan 2019 07:27:56 -0500 Received: from mga02.intel.com ([134.134.136.20]:21827 "EHLO mga02.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726415AbfA1M1w (ORCPT ); Mon, 28 Jan 2019 07:27:52 -0500 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga101.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 28 Jan 2019 04:27:44 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.56,534,1539673200"; d="scan'208";a="139402570" Received: from elena-thinkpad-x230.fi.intel.com ([10.237.68.34]) by fmsmga004.fm.intel.com with ESMTP; 28 Jan 2019 04:27:42 -0800 From: Elena Reshetova To: peterz@infradead.org Cc: linux-kernel@vger.kernel.org, mingo@redhat.com, acme@kernel.org, namhyung@kernel.org, alexander.shishkin@linux.intel.com, jolsa@redhat.com, keescook@chromium.org, tglx@linutronix.de, Elena Reshetova Subject: [PATCH 1/3] perf: convert perf_event_context.refcount to refcount_t Date: Mon, 28 Jan 2019 14:27:26 +0200 Message-Id: <1548678448-24458-2-git-send-email-elena.reshetova@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1548678448-24458-1-git-send-email-elena.reshetova@intel.com> References: <1548678448-24458-1-git-send-email-elena.reshetova@intel.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org atomic_t variables are currently used to implement reference counters with the following properties: - counter is initialized to 1 using atomic_set() - a resource is freed upon counter reaching zero - once counter reaches zero, its further increments aren't allowed - counter schema uses basic atomic operations (set, inc, inc_not_zero, dec_and_test, etc.) Such atomic variables should be converted to a newly provided refcount_t type and API that prevents accidental counter overflows and underflows. This is important since overflows and underflows can lead to use-after-free situation and be exploitable. The variable perf_event_context.refcount is used as pure reference counter. Convert it to refcount_t and fix up the operations. **Important note for maintainers: Some functions from refcount_t API defined in lib/refcount.c have different memory ordering guarantees than their atomic counterparts. Please check Documentation/core-api/refcount-vs-atomic.rst for more information. Normally the differences should not matter since refcount_t provides enough guarantees to satisfy the refcounting use cases, but in some rare cases it might matter. Please double check that you don't have some undocumented memory guarantees for this variable usage. For the perf_event_context.refcount it might make a difference in following places: - get_ctx(), perf_event_ctx_lock_nested(), perf_lock_task_context() and __perf_event_ctx_lock_double(): increment in refcount_inc_not_zero() only guarantees control dependency on success vs. fully ordered atomic counterpart - put_ctx(): decrement in refcount_dec_and_test() provides RELEASE ordering and ACQUIRE ordering + control dependency on success vs. fully ordered atomic counterpart Suggested-by: Kees Cook Reviewed-by: David Windsor Reviewed-by: Hans Liljestrand Signed-off-by: Elena Reshetova --- include/linux/perf_event.h | 3 ++- kernel/events/core.c | 12 ++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h index 1d5c551..6a94097 100644 --- a/include/linux/perf_event.h +++ b/include/linux/perf_event.h @@ -55,6 +55,7 @@ struct perf_guest_info_callbacks { #include #include #include +#include #include struct perf_callchain_entry { @@ -737,7 +738,7 @@ struct perf_event_context { int nr_stat; int nr_freq; int rotate_disable; - atomic_t refcount; + refcount_t refcount; struct task_struct *task; /* diff --git a/kernel/events/core.c b/kernel/events/core.c index 3cd13a3..a1e87d2 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -1171,7 +1171,7 @@ static void perf_event_ctx_deactivate(struct perf_event_context *ctx) static void get_ctx(struct perf_event_context *ctx) { - WARN_ON(!atomic_inc_not_zero(&ctx->refcount)); + WARN_ON(!refcount_inc_not_zero(&ctx->refcount)); } static void free_ctx(struct rcu_head *head) @@ -1185,7 +1185,7 @@ static void free_ctx(struct rcu_head *head) static void put_ctx(struct perf_event_context *ctx) { - if (atomic_dec_and_test(&ctx->refcount)) { + if (refcount_dec_and_test(&ctx->refcount)) { if (ctx->parent_ctx) put_ctx(ctx->parent_ctx); if (ctx->task && ctx->task != TASK_TOMBSTONE) @@ -1267,7 +1267,7 @@ perf_event_ctx_lock_nested(struct perf_event *event, int nesting) again: rcu_read_lock(); ctx = READ_ONCE(event->ctx); - if (!atomic_inc_not_zero(&ctx->refcount)) { + if (!refcount_inc_not_zero(&ctx->refcount)) { rcu_read_unlock(); goto again; } @@ -1400,7 +1400,7 @@ perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags) } if (ctx->task == TASK_TOMBSTONE || - !atomic_inc_not_zero(&ctx->refcount)) { + !refcount_inc_not_zero(&ctx->refcount)) { raw_spin_unlock(&ctx->lock); ctx = NULL; } else { @@ -4056,7 +4056,7 @@ static void __perf_event_init_context(struct perf_event_context *ctx) INIT_LIST_HEAD(&ctx->event_list); INIT_LIST_HEAD(&ctx->pinned_active); INIT_LIST_HEAD(&ctx->flexible_active); - atomic_set(&ctx->refcount, 1); + refcount_set(&ctx->refcount, 1); } static struct perf_event_context * @@ -10391,7 +10391,7 @@ __perf_event_ctx_lock_double(struct perf_event *group_leader, again: rcu_read_lock(); gctx = READ_ONCE(group_leader->ctx); - if (!atomic_inc_not_zero(&gctx->refcount)) { + if (!refcount_inc_not_zero(&gctx->refcount)) { rcu_read_unlock(); goto again; } -- 2.7.4