Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp808535pxu; Fri, 4 Dec 2020 16:43:24 -0800 (PST) X-Google-Smtp-Source: ABdhPJyWGH9A45/gyvOQWJYKAsSawMQ5SFolFyQYGwfp6PudQIvtlDxIyhZrKrtvPuH+CdxqU4Vz X-Received: by 2002:a17:906:339a:: with SMTP id v26mr9424260eja.107.1607129004315; Fri, 04 Dec 2020 16:43:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1607129004; cv=none; d=google.com; s=arc-20160816; b=ewT0YaQbS/f6qxEDVQ3X9X0Lnp/iRbWhr+1EzbiM4Tg3I2yUDQkp9xbuKaacT6yvi6 tav5cnj4uuNcXZ0EcIL6NR7zK2NN4gMXeO1mhLkl1dGbXHrmoOGbOzb45Dc2K6LVD6sP RaRgm4xZh4Z+bXn5jMwu7EIxfzCaobVscXNMleHucX+OwKXHcGZZ2xqS03+2Ng05dskA HuUrxF7ZSw8gf1+M/xUqqcfdU0p1eEqcpQKVWQHK+YaC94b8reUs3aog2GdeZQNiUBsM GXVtNJWEukUhVQufd7WMT6rxprXXAHoMLinAACZZmN+AVDZqqDGNXOGETsGwHMmE+CPM pIoQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from; bh=AaTs/LVsYBnzQ+wo9uvuXAde0mj+iBddsaPfumS0y30=; b=EQXtRH0wnQnwEqAYY0cRP2SzlxRUyoCso+viptJh/cKh8eIuio4iziRCgPnJJXuuwa I2M8YAxBU22KK7km+TkKMu6yJ1pXL3Rdoazh8aC3/51Wgm0R0fumdSZvXLBGKJyHtIBV D3LaxGA+H6cqklXZ8QDmAqB0sXuS81ddtYmIYeIPh4cvZ0RDlGUQL3psy2DPdwDM7R0j WfKbWp/Lr9XRb2vtCYXdNVaVX3XqSCzzmDYVglmcQAvokAy3QJkXNkV5BIlTrL+gsh+a CzUrbrfHWiphtU6K3R8C0wHIYWpxrthFZaLj6vo78ImBZNaKCAKSzFtGtXFxBounek7G FTSg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id y26si2402797ejb.149.2020.12.04.16.43.02; Fri, 04 Dec 2020 16:43:24 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730885AbgLEAlo (ORCPT + 99 others); Fri, 4 Dec 2020 19:41:44 -0500 Received: from mail.kernel.org ([198.145.29.99]:48974 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727566AbgLEAln (ORCPT ); Fri, 4 Dec 2020 19:41:43 -0500 From: paulmck@kernel.org Authentication-Results: mail.kernel.org; dkim=permerror (bad message/signature format) To: rcu@vger.kernel.org Cc: linux-kernel@vger.kernel.org, kernel-team@fb.com, mingo@kernel.org, jiangshanlai@gmail.com, akpm@linux-foundation.org, mathieu.desnoyers@efficios.com, josh@joshtriplett.org, tglx@linutronix.de, peterz@infradead.org, rostedt@goodmis.org, dhowells@redhat.com, edumazet@google.com, fweisbec@gmail.com, oleg@redhat.com, joel@joelfernandes.org, "Paul E. McKenney" , Ming Lei , Jens Axboe Subject: [PATCH sl-b 6/6] percpu_ref: Print stack trace upon reference-count underflow Date: Fri, 4 Dec 2020 16:40:57 -0800 Message-Id: <20201205004057.32199-6-paulmck@kernel.org> X-Mailer: git-send-email 2.9.5 In-Reply-To: <20201205004022.GA31166@paulmck-ThinkPad-P72> References: <20201205004022.GA31166@paulmck-ThinkPad-P72> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: "Paul E. McKenney" In some cases, the allocator return address is in a common function, so that more information is desired. For example, percpu_ref reference-count underflow happens in an RCU callback function having access only to a block of memory that is always allocated in percpu_ref_init(). This information is unhelpful. This commit therefore causes the percpu_ref_switch_to_atomic_rcu() function to use the new kmem_last_alloc_stack() function to collect and print a stack trace upon reference-count underflow. This requires the kernel use the slub allocator and be built with CONFIG_STACKTRACE=y. As always, slub debugging must be enabled one way or another, for example, by booting with the "slub_debug=U" kernel boot parameter. Cc: Ming Lei Cc: Jens Axboe Reported-by: Andrii Nakryiko Signed-off-by: Paul E. McKenney --- lib/percpu-refcount.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/lib/percpu-refcount.c b/lib/percpu-refcount.c index 8c7b21a0..ebdfa47 100644 --- a/lib/percpu-refcount.c +++ b/lib/percpu-refcount.c @@ -169,8 +169,6 @@ static void percpu_ref_switch_to_atomic_rcu(struct rcu_head *rcu) struct percpu_ref *ref = data->ref; unsigned long __percpu *percpu_count = percpu_count_ptr(ref); unsigned long count = 0; - void *allocaddr; - const char *allocerr; int cpu; for_each_possible_cpu(cpu) @@ -194,14 +192,26 @@ static void percpu_ref_switch_to_atomic_rcu(struct rcu_head *rcu) atomic_long_add((long)count - PERCPU_COUNT_BIAS, &data->count); if (atomic_long_read(&data->count) <= 0) { - allocaddr = kmem_last_alloc(data); + void *allocaddr; + const char *allocerr; + void *allocstack[8]; + int i; + + allocaddr = kmem_last_alloc_stack(data, allocstack, ARRAY_SIZE(allocstack)); allocerr = kmem_last_alloc_errstring(allocaddr); - if (allocerr) + if (allocerr) { WARN_ONCE(1, "percpu ref (%ps) <= 0 (%ld) after switching to atomic (%s)", data->release, atomic_long_read(&data->count), allocerr); - else - WARN_ONCE(1, "percpu ref (%ps) <= 0 (%ld) after switching to atomic (allocated at %pS)", - data->release, atomic_long_read(&data->count), allocaddr); + } else { + pr_err("percpu ref (%ps) <= 0 (%ld) after switching to atomic (allocated at %pS)\n", + data->release, atomic_long_read(&data->count), allocaddr); + for (i = 0; i < ARRAY_SIZE(allocstack); i++) { + if (!allocstack[i]) + break; + pr_err("\t%pS\n", allocstack[i]); + } + WARN_ON_ONCE(1); + } } /* @ref is viewed as dead on all CPUs, send out switch confirmation */ -- 2.9.5