Received: by 2002:a89:288:0:b0:1f7:eeee:6653 with SMTP id j8csp552049lqh; Tue, 7 May 2024 07:16:38 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWoKgQG3tpaSGxHDvI+afi9WxCxtL1qR8AzqN0xqATiA/Oj2w9/nNVMwlaxBD//kBjERoFolp8r8NQVkpbadxgdjruDBQyTYajrQOCMIQ== X-Google-Smtp-Source: AGHT+IH+WNzQPMLQ/h+rluG4ZNMYK5W1Sb/8PPeOd8BFCTA4kX9i4DG1fMqevN3aReGQmPyUkWe8 X-Received: by 2002:a9d:7645:0:b0:6f0:1653:6a25 with SMTP id o5-20020a9d7645000000b006f016536a25mr9078974otl.31.1715091398680; Tue, 07 May 2024 07:16:38 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715091398; cv=pass; d=google.com; s=arc-20160816; b=AwmupTu2WHh7RGZ+xWzxe1jcf54bavOh2WPP3nE3PA35XREjvIFUhmtKUoS5weP7YP 35ry66Topcb90btsUgOI710+wJT+cW3Ap2rfCzMoJORZbw6boPtOEIqXk2cCXcypa/sj tAiNDFoFzDya7dTKD5m/HAnXtepEArjM2Pqthzr4wd48ImzMoFmpI32FpxToRyyrEmiK xZltGow1EkMs/o7KdkOYrw7s6ICA9Ze3GEvtkjUWMQeDGZmZhuAmKTy8QdyJELvNGmIo GTezhhhngHsSQiP8405lMGeFtBb77krlejzOk7TfiQLyqjZm88c2L3Va3BXZopblVkZ1 t3SQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:user-agent:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=5djo2Zp+45ks19hrk70qURDJ2ojZiSWr4t/DeRh9VHQ=; fh=SIgps5XdV0XNwjZfT2uAI7g3mrspDldK9Qs8qQAfoa4=; b=eIcT2bJsian1gIcsPODQNcMXZvRnJKl/zzFlIa4X8Ki7ybL+U8TdU/fleX6tHBWQQx bEHO/Gh0k69Rk0tyqTGVuG4NMb4USXbYOq73PDkzCd72xAJAfhQwMlMXRlPZa5gjvvpi ulBr5tktn9wTptbegmHo7bzBqyc8YOct9UTfP5TJJa6htsQsoFtii1ALCY4gsxbgvR5P Au4rJ1r5Ej+qFHRumh7rd9+nTDveRvqJMjv4P7OdpxH09YJ7D9efykJRbR1KKFLhegud JXdHRTTh8dIWor+YyREqMNEdNLMyAfPqDvc/9wHAGDAVUzdiTFq2d8H8iQPwiGWB/7Nk q2fQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Nds1pTYM; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-171518-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-171518-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id d5-20020a9d5e05000000b006eb7b009bdesi3633228oti.94.2024.05.07.07.16.38 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 May 2024 07:16:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-171518-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Nds1pTYM; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-171518-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-171518-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id BD6A6288EC5 for ; Tue, 7 May 2024 14:12:05 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D089716192D; Tue, 7 May 2024 14:10:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Nds1pTYM" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DD73615FA7E; Tue, 7 May 2024 14:10:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715091039; cv=none; b=YGq/UQE8SvkXPxMszyJ8MhwMdAXPe4w4vu4pwQ4d7m+iwfsWjGuDQm2tihNWVp2domYD+D8hadI0D1s9Fgm5lSxFPzlG0JfNrGkWacp6DDlNz82yn1ksmD2l3vagGnCsMOXDZk5+D11NeUdVW3y3T7M7G57fG2XpXSz6v/aQWcM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715091039; c=relaxed/simple; bh=5nInqF9qURKOZ0esvHEpkoi/4RvEUChykR3zq5VSXc0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=gAGJ0c7x+CrOPgnwjCwRf84fZjYqfpLrkFzQvSA46B+JpDjb8MaCf2sU3B2gteBNwbJzJ9RMm/G/cxrLChyP3D6tsujteWplEGhtGV8rY+IPHnoL5R/sBymP5ceDbLs3LN8MOcGmJnXavsUCnBnKxxwLBK7kQpUh3B/YXNrOgTk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Nds1pTYM; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id E5FD5C2BBFC; Tue, 7 May 2024 14:10:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1715091038; bh=5nInqF9qURKOZ0esvHEpkoi/4RvEUChykR3zq5VSXc0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Nds1pTYMbVbld+y7/r7Hsscs1PhGVez7VFIpNH81TAeYGbWCyn9tiuIMN43leBNO7 Tzjvl9kzmQTllGluctP7eyAGibYkOD5ALCyr4YsFDvlGE9+yDKbQbFJx0R8qMDJVba QIO9kpuACGUUPaXJETN5e4QpSeCy6jMzm7g2Me2Y5+O0c1u13B3pxlhYyPKBpKcF4N p45JB4dG3xKgIzj5nb6NJscy+A135DIM1VuG3lDNd7k+5FFG4LFxsIAUB0xfdZ7Uca 373pdQd9j5x0WAZdD6qVGSkgSofNB9/KfCU/IefiAl+RXutcKaujfPWG+xrSRuGttG SpMbPEBVMgTLQ== From: "Masami Hiramatsu (Google)" To: Alexei Starovoitov , Steven Rostedt , Florent Revest Cc: linux-trace-kernel@vger.kernel.org, LKML , Martin KaFai Lau , bpf , Sven Schnelle , Alexei Starovoitov , Jiri Olsa , Arnaldo Carvalho de Melo , Daniel Borkmann , Alan Maguire , Mark Rutland , Peter Zijlstra , Thomas Gleixner , Guo Ren Subject: [PATCH v10 13/36] function_graph: Use a simple LRU for fgraph_array index number Date: Tue, 7 May 2024 23:10:32 +0900 Message-Id: <171509103267.162236.6885097397289135378.stgit@devnote2> X-Mailer: git-send-email 2.34.1 In-Reply-To: <171509088006.162236.7227326999861366050.stgit@devnote2> References: <171509088006.162236.7227326999861366050.stgit@devnote2> User-Agent: StGit/0.19 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit From: Masami Hiramatsu (Google) Since the fgraph_array index is used for the bitmap on the shadow stack, it may leave some entries after a function_graph instance is removed. Thus if another instance reuses the fgraph_array index soon after releasing it, the fgraph may confuse to call the newer callback for the entries which are pushed by the older instance. To avoid reusing the fgraph_array index soon after releasing, introduce a simple LRU table for managing the index number. This will reduce the possibility of this confusion. Signed-off-by: Masami Hiramatsu (Google) --- Changes in v8: - Add a WARN_ON_ONCE() if fgraph_lru_table[] is broken when releasing index, and remove WARN_ON_ONCE() from unregister_ftrace_graph() - Fix to release allocated index if register_ftrace_graph() fails. - Add comments and code cleanup. Changes in v5: - Fix the underflow bug in fgraph_lru_release_index() and return 0 if the release is succeded. Changes in v4: - Newly added. --- kernel/trace/fgraph.c | 71 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 50 insertions(+), 21 deletions(-) diff --git a/kernel/trace/fgraph.c b/kernel/trace/fgraph.c index b6949e4fda79..c00a299decb1 100644 --- a/kernel/trace/fgraph.c +++ b/kernel/trace/fgraph.c @@ -97,10 +97,48 @@ enum { DEFINE_STATIC_KEY_FALSE(kill_ftrace_graph); int ftrace_graph_active; -static int fgraph_array_cnt; - static struct fgraph_ops *fgraph_array[FGRAPH_ARRAY_SIZE]; +/* LRU index table for fgraph_array */ +static int fgraph_lru_table[FGRAPH_ARRAY_SIZE]; +static int fgraph_lru_next; +static int fgraph_lru_last; + +/* Initialize fgraph_lru_table with unused index */ +static void fgraph_lru_init(void) +{ + int i; + + for (i = 0; i < FGRAPH_ARRAY_SIZE; i++) + fgraph_lru_table[i] = i; +} + +/* Release the used index to the LRU table */ +static int fgraph_lru_release_index(int idx) +{ + if (idx < 0 || idx >= FGRAPH_ARRAY_SIZE || + WARN_ON_ONCE(fgraph_lru_table[fgraph_lru_last] != -1)) + return -1; + + fgraph_lru_table[fgraph_lru_last] = idx; + fgraph_lru_last = (fgraph_lru_last + 1) % FGRAPH_ARRAY_SIZE; + return 0; +} + +/* Allocate a new index from LRU table */ +static int fgraph_lru_alloc_index(void) +{ + int idx = fgraph_lru_table[fgraph_lru_next]; + + /* No id is available */ + if (idx == -1) + return -1; + + fgraph_lru_table[fgraph_lru_next] = -1; + fgraph_lru_next = (fgraph_lru_next + 1) % FGRAPH_ARRAY_SIZE; + return idx; +} + static inline int get_frame_offset(struct task_struct *t, int offset) { return t->ret_stack[offset] & FGRAPH_FRAME_OFFSET_MASK; @@ -365,7 +403,7 @@ int function_graph_enter(unsigned long ret, unsigned long func, if (offset < 0) goto out; - for (i = 0; i < fgraph_array_cnt; i++) { + for (i = 0; i < FGRAPH_ARRAY_SIZE; i++) { struct fgraph_ops *gops = fgraph_array[i]; if (gops == &fgraph_stub) @@ -917,7 +955,7 @@ int register_ftrace_graph(struct fgraph_ops *gops) { int command = 0; int ret = 0; - int i; + int i = -1; mutex_lock(&ftrace_lock); @@ -933,21 +971,16 @@ int register_ftrace_graph(struct fgraph_ops *gops) /* The array must always have real data on it */ for (i = 0; i < FGRAPH_ARRAY_SIZE; i++) fgraph_array[i] = &fgraph_stub; + fgraph_lru_init(); } - /* Look for an available spot */ - for (i = 0; i < FGRAPH_ARRAY_SIZE; i++) { - if (fgraph_array[i] == &fgraph_stub) - break; - } - if (i >= FGRAPH_ARRAY_SIZE) { + i = fgraph_lru_alloc_index(); + if (i < 0 || WARN_ON_ONCE(fgraph_array[i] != &fgraph_stub)) { ret = -ENOSPC; goto out; } fgraph_array[i] = gops; - if (i + 1 > fgraph_array_cnt) - fgraph_array_cnt = i + 1; gops->idx = i; ftrace_graph_active++; @@ -971,6 +1004,7 @@ int register_ftrace_graph(struct fgraph_ops *gops) if (ret) { fgraph_array[i] = &fgraph_stub; ftrace_graph_active--; + fgraph_lru_release_index(i); } out: mutex_unlock(&ftrace_lock); @@ -980,25 +1014,20 @@ int register_ftrace_graph(struct fgraph_ops *gops) void unregister_ftrace_graph(struct fgraph_ops *gops) { int command = 0; - int i; mutex_lock(&ftrace_lock); if (unlikely(!ftrace_graph_active)) goto out; - if (unlikely(gops->idx < 0 || gops->idx >= fgraph_array_cnt)) + if (unlikely(gops->idx < 0 || gops->idx >= FGRAPH_ARRAY_SIZE || + fgraph_array[gops->idx] != gops)) goto out; - WARN_ON_ONCE(fgraph_array[gops->idx] != gops); + if (fgraph_lru_release_index(gops->idx) < 0) + goto out; fgraph_array[gops->idx] = &fgraph_stub; - if (gops->idx + 1 == fgraph_array_cnt) { - i = gops->idx; - while (i >= 0 && fgraph_array[i] == &fgraph_stub) - i--; - fgraph_array_cnt = i + 1; - } ftrace_graph_active--;