Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3410634imu; Mon, 28 Jan 2019 04:29:10 -0800 (PST) X-Google-Smtp-Source: ALg8bN5iA9vdFMgPQ/D7rEV791ETC2PouPcT04kZTviWLDvRMNsp422VH5Xl8qyN8f+Rg9DLrTkw X-Received: by 2002:a17:902:1008:: with SMTP id b8mr20864179pla.252.1548678550176; Mon, 28 Jan 2019 04:29:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548678550; cv=none; d=google.com; s=arc-20160816; b=vhx57qdIzgEj+vdKPiPmoXUU4912ieGRHAskBCynajuxMFAW3ivUERu2XQLW5hNE58 rbUM0aNc4wcvyaDeCm2ZwvfWhyhzBkb/SfUO6gE99S8a6wpd2TlKAQeX78RUc7kGmGku 1ZMXNyycX3kGmF4vcRsreGbYbTA8NxJ30FolmK7QqZHqV5f4Ep8F7chBBAMbxGGAO3BI Qm9MNPj3kV89n3e/9AtEeCt/rw/AMQ2pdW+rqhCuyw1oEqexLV4UbmA4UlaGcW0BOd5q 2kVk+9SeVK8VSyZpWLU7ZMYiJn/cx1NtbmZ29iTQRsjP2xn3ZZWt7vAqkbyNGvjuqt1m 1mcg== 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=yCdg+CyubhfGzzOYZ9ujkjLdiW/EmHbhW/rlWrhvzYI=; b=fowBIS+/URXtb54vcJNTY7NQvxIyE96bJu9bU6Z7kiYENc7FaoCOHbfDR8DTPrlyp0 37RCyAkpvFgXaLU4+igVxlz+i3seoTYXqWKg4G7c+pUfeuGEQ7HUGKK44/Y3z8d6YMtC x14Ari2cCgMAeSXLLpXbAbNQbCz0ac/NpFukcpYICOyH0TROu4D8FSezDKFdHMReujqb jXeQkHDGnvkNFpVDgegGp1tJCXZLqne4xlxBgguYHsJscOXm3yj0/Bbgeus89RLL8r+M IAR7N0FTVArC+duLnzQXan2rBhce43tCBRN3OFxACcQqeshy09MohL8GY0ogb5iSNxeR b6QA== 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 o16si31086010pgd.117.2019.01.28.04.28.54; Mon, 28 Jan 2019 04:29:10 -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 S1726877AbfA1M1s (ORCPT + 99 others); Mon, 28 Jan 2019 07:27:48 -0500 Received: from mga02.intel.com ([134.134.136.20]:21831 "EHLO mga02.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726695AbfA1M1r (ORCPT ); Mon, 28 Jan 2019 07:27:47 -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:47 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.56,534,1539673200"; d="scan'208";a="139402579" Received: from elena-thinkpad-x230.fi.intel.com ([10.237.68.34]) by fmsmga004.fm.intel.com with ESMTP; 28 Jan 2019 04:27:44 -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 2/3] perf/ring_buffer: convert ring_buffer.refcount to refcount_t Date: Mon, 28 Jan 2019 14:27:27 +0200 Message-Id: <1548678448-24458-3-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 ring_buffer.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 ring_buffer.refcount it might make a difference in following places: - ring_buffer_get(): increment in refcount_inc_not_zero() only guarantees control dependency on success vs. fully ordered atomic counterpart - ring_buffer_put(): decrement in refcount_dec_and_test() only 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 --- kernel/events/core.c | 4 ++-- kernel/events/internal.h | 3 ++- kernel/events/ring_buffer.c | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/kernel/events/core.c b/kernel/events/core.c index a1e87d2..963cee0 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -5388,7 +5388,7 @@ struct ring_buffer *ring_buffer_get(struct perf_event *event) rcu_read_lock(); rb = rcu_dereference(event->rb); if (rb) { - if (!atomic_inc_not_zero(&rb->refcount)) + if (!refcount_inc_not_zero(&rb->refcount)) rb = NULL; } rcu_read_unlock(); @@ -5398,7 +5398,7 @@ struct ring_buffer *ring_buffer_get(struct perf_event *event) void ring_buffer_put(struct ring_buffer *rb) { - if (!atomic_dec_and_test(&rb->refcount)) + if (!refcount_dec_and_test(&rb->refcount)) return; WARN_ON_ONCE(!list_empty(&rb->event_list)); diff --git a/kernel/events/internal.h b/kernel/events/internal.h index 6dc725a..4718de2 100644 --- a/kernel/events/internal.h +++ b/kernel/events/internal.h @@ -4,13 +4,14 @@ #include #include +#include /* Buffer handling */ #define RING_BUFFER_WRITABLE 0x01 struct ring_buffer { - atomic_t refcount; + refcount_t refcount; struct rcu_head rcu_head; #ifdef CONFIG_PERF_USE_VMALLOC struct work_struct work; diff --git a/kernel/events/ring_buffer.c b/kernel/events/ring_buffer.c index 4a99370..e841d48 100644 --- a/kernel/events/ring_buffer.c +++ b/kernel/events/ring_buffer.c @@ -285,7 +285,7 @@ ring_buffer_init(struct ring_buffer *rb, long watermark, int flags) else rb->overwrite = 1; - atomic_set(&rb->refcount, 1); + refcount_set(&rb->refcount, 1); INIT_LIST_HEAD(&rb->event_list); spin_lock_init(&rb->event_lock); -- 2.7.4