From nobody Sun Feb 8 11:21:33 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 7A299318130 for ; Wed, 24 Dec 2025 12:51:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766580684; cv=none; b=XmIEtGbv3D03eABynn0t1q2mcQ7hl3NkZLNQIfvYPlzg8eIjoxrR6eTtvr5Zg9VhQlO77dWgp9AXU5FwLeg5us85n6UYWXoTmaW+bcpIuZhCrTjydVwjiEIai3ErZyW2pF5R/4X2r/Y90nY1cY3hqUv1ZhhCtbuwLMZ1pm2flAg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766580684; c=relaxed/simple; bh=dP8hT5Iu0RAejp/EVTjdBuZy8bVHWAsAm0/tcmV/4y4=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=DuaQJHO511bbfDdc2Q0Ko43ryJzAkh8XPzEMGUrYFfI/STPP3VgsW0IQaOZIHzTisQkOpFmwgWY8V0pwj5laHlBbg5oOF8sRLsuJkTEvvwGGv8LfXDPcl+EhnMMi4jW6rSRu+h6ORZSHOVP4jdJGhvzKkpUqka4WHdjHB8zlHSw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=Lnr4dFzI; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=rF9GjQ5y; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Lnr4dFzI"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="rF9GjQ5y" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1766580680; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=jdolHyh9rh5rJ5MkRWABLiPIWLLCxDNdClFu6iuDitI=; b=Lnr4dFzI9iAsJTKRpCVvlwtVARjVUnxl0tREmq3lORNhJptDp6CMVEoQna/qUIH0PCPk43 Bsv8B7Xhwwk1mjZ2tAiBbiu9QLuIj2IBdencFcEJDNX1LLe7do2SP5QV94oJ+xhJz3sGoV TlI7KovUGmeL+skpBnRfvMWWbLicKYw= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-114-nojxqOq6Pz2534NlogwZOA-1; Wed, 24 Dec 2025 07:51:19 -0500 X-MC-Unique: nojxqOq6Pz2534NlogwZOA-1 X-Mimecast-MFC-AGG-ID: nojxqOq6Pz2534NlogwZOA_1766580678 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-4779b3749a8so41793835e9.1 for ; Wed, 24 Dec 2025 04:51:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1766580678; x=1767185478; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=jdolHyh9rh5rJ5MkRWABLiPIWLLCxDNdClFu6iuDitI=; b=rF9GjQ5yczRIWbmrGO4ARWFDGTAWALO+OfPHKjfLuLNktus8itLoVvI37erWSPkW5R UIw7SmYPpbPMLlk8Z0ZxThBYaDg/HvseQGCm17YEz6UxRrUyhm47EfOluKfIaocMwuHy xT6tBmNPZj2Taae2AOyDcUvLTgLV6DIK8eQnFlOvnn+uN19IRMR8W7isZyjJ53+CNHQP RlIGvbCLUvEP/9nl6m3vBaPqLGLi0N/7vJTmmwBDibAJKg6SQuidqzPf2/vj3KypCfpn Ldl0ec6bP5GHWnE6TDuB+++0sCMSDAVeG+4rtI0i6W3B+vYsoxikPL5UJKrD8ztsZVVr t3tg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1766580678; x=1767185478; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=jdolHyh9rh5rJ5MkRWABLiPIWLLCxDNdClFu6iuDitI=; b=bCAEzOn3zkYiDUXLEWNNxyFKVBNQIGYoGc++cGousiECP1aCHvGRTZIcAWXvCm1AEt tL93/pUP6jkW8+lLr7YTDK+04zi6YqD1XYmK5HzFL5mrlreU+MQfs+IbQuKWkMxixyna 9LGTM3K/ql2biIU/w169V1uO3Tu60L4XpW7y0Rt7X03Uqh2STcqL8dDae8+BYPUeu3qs oyKyTpixgIT/tjmxU+PgrsaN6zSean3qamGKp8mwuvzQG+jT73jhU9LJ+6hzqbnGZr7y 2qqb2QLdfeDr8twL8rf2bUjHHdJuAavGIi5A8jXIhHY5pRQzA/a1GPHIg6un5/b8oCmJ 8MqA== X-Forwarded-Encrypted: i=1; AJvYcCV8qjZCEjFIqXGE/Hi6UeW1pwpl5IRO2bmO8fBlQP2wwta/95J5KHPEipQ4V4K33lFqoszu1PPQ+MSqD/8=@vger.kernel.org X-Gm-Message-State: AOJu0Yx18kTAuDXlrpuY4bRaz+U2H61N5goxxOxVdXiZCFGsqCQyIgsl sFHOv+QAFlu1jKbkhp5mQg5QWZZXhz3S5FLbErel6OLJIw/HuB/aMEijMldGzEtzG/CzMVD9YDZ MKU1ebQ9FPwTq8w0+CTqc5hAmI7l9dMFWyLAApak+sOjY2hMY9AOW1xCAxwmC21BPsZW/r6txzg == X-Gm-Gg: AY/fxX6qidiXg0hQuQyR020n8E3M6jReEoi5AE8jBK/eX/8k2aCT4wbg/e/doxf6D08 gm996flVW7ZO/Bs+lu6C6gyzqkIyTUWXT/qHnP92CsrVx8GSGQmm3mVqTLe+ud8RcdZCaCEOvWI KJ9a+bBjjsehwTzI9z6+sLPFGo4AYmOwUmgY8LPgCkRBffMwMfvcyTDFSX9Cinr1DQCGIVNQg5N OeyGe3hUsgS2TYkH/2tsnGtpv+cCc+otAUcGozL3Tk/GFPYI5e7FdWpiUE2gTS7E9WtD88rL2MS hcxfI1T5jh3oxD/0art5U9f0kNF+UY/3WRb/KQrfNkXtPRU8k4p4YgBD6I6sFGpl5im8VsfblpC eIz1qJhYpxfwCTbtwNelYhCQE2Is= X-Received: by 2002:a05:600c:c105:b0:47d:3ffb:16c9 with SMTP id 5b1f17b1804b1-47d3ffb184amr10424985e9.23.1766580677573; Wed, 24 Dec 2025 04:51:17 -0800 (PST) X-Google-Smtp-Source: AGHT+IFf6nRlud3tbCmYPb3EeHZKF6Drkld0nQn08zBBkGr5/rtpie5AQr3WYCUYqGyri/LayKcyVg== X-Received: by 2002:a05:600c:c105:b0:47d:3ffb:16c9 with SMTP id 5b1f17b1804b1-47d3ffb184amr10424725e9.23.1766580677104; Wed, 24 Dec 2025 04:51:17 -0800 (PST) Received: from costa-tp.redhat.com ([2a00:a041:e294:5000:b694:8e49:4f51:966d]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4327791d2f3sm699415f8f.11.2025.12.24.04.51.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Dec 2025 04:51:16 -0800 (PST) From: Costa Shulyupin To: Steven Rostedt , Tomas Glozar , Costa Shulyupin , Tiezhu Yang , Ivan Pravdin , linux-trace-kernel@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v1] tools/rtla: Deduplicate cgroup path opening code Date: Wed, 24 Dec 2025 14:50:56 +0200 Message-ID: <20251224125058.1771519-1-costa.shul@redhat.com> X-Mailer: git-send-email 2.52.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Both set_pid_cgroup() and set_comm_cgroup() functions contain identical code for opening the cgroup.procs file. Extract this common code into a new helper function open_cgroup_procs() to reduce code duplication and improve maintainability. Signed-off-by: Costa Shulyupin --- tools/tracing/rtla/src/utils.c | 65 +++++++++++++++++----------------- 1 file changed, 32 insertions(+), 33 deletions(-) diff --git a/tools/tracing/rtla/src/utils.c b/tools/tracing/rtla/src/utils.c index 9cf5a0098e9a..0b84e02b13df 100644 --- a/tools/tracing/rtla/src/utils.c +++ b/tools/tracing/rtla/src/utils.c @@ -784,27 +784,27 @@ static int get_self_cgroup(char *self_cg, int sizeof_= self_cg) } =20 /* - * set_comm_cgroup - Set cgroup to pid_t pid + * open_cgroup_procs - Open the cgroup.procs file for the given cgroup * - * If cgroup argument is not NULL, the threads will move to the given cgro= up. - * Otherwise, the cgroup of the calling, i.e., rtla, thread will be used. + * If cgroup argument is not NULL, the cgroup.procs file for that cgroup + * will be opened. Otherwise, the cgroup of the calling, i.e., rtla, thread + * will be used. * * Supports cgroup v2. * - * Returns 1 on success, 0 otherwise. + * Returns the file descriptor on success, -1 otherwise. */ -int set_pid_cgroup(pid_t pid, const char *cgroup) +static int open_cgroup_procs(const char *cgroup) { char cgroup_path[MAX_PATH - strlen("/cgroup.procs")]; char cgroup_procs[MAX_PATH]; - char pid_str[24]; int retval; int cg_fd; =20 retval =3D find_mount("cgroup2", cgroup_path, sizeof(cgroup_path)); if (!retval) { err_msg("Did not find cgroupv2 mount point\n"); - return 0; + return -1; } =20 if (!cgroup) { @@ -812,7 +812,7 @@ int set_pid_cgroup(pid_t pid, const char *cgroup) sizeof(cgroup_path) - strlen(cgroup_path)); if (!retval) { err_msg("Did not find self cgroup\n"); - return 0; + return -1; } } else { snprintf(&cgroup_path[strlen(cgroup_path)], @@ -824,6 +824,29 @@ int set_pid_cgroup(pid_t pid, const char *cgroup) debug_msg("Using cgroup path at: %s\n", cgroup_procs); =20 cg_fd =3D open(cgroup_procs, O_RDWR); + if (cg_fd < 0) + return -1; + + return cg_fd; +} + +/* + * set_pid_cgroup - Set cgroup to pid_t pid + * + * If cgroup argument is not NULL, the threads will move to the given cgro= up. + * Otherwise, the cgroup of the calling, i.e., rtla, thread will be used. + * + * Supports cgroup v2. + * + * Returns 1 on success, 0 otherwise. + */ +int set_pid_cgroup(pid_t pid, const char *cgroup) +{ + char pid_str[24]; + int retval; + int cg_fd; + + cg_fd =3D open_cgroup_procs(cgroup); if (cg_fd < 0) return 0; =20 @@ -853,8 +876,6 @@ int set_pid_cgroup(pid_t pid, const char *cgroup) */ int set_comm_cgroup(const char *comm_prefix, const char *cgroup) { - char cgroup_path[MAX_PATH - strlen("/cgroup.procs")]; - char cgroup_procs[MAX_PATH]; struct dirent *proc_entry; DIR *procfs; int retval; @@ -866,29 +887,7 @@ int set_comm_cgroup(const char *comm_prefix, const cha= r *cgroup) return 0; } =20 - retval =3D find_mount("cgroup2", cgroup_path, sizeof(cgroup_path)); - if (!retval) { - err_msg("Did not find cgroupv2 mount point\n"); - return 0; - } - - if (!cgroup) { - retval =3D get_self_cgroup(&cgroup_path[strlen(cgroup_path)], - sizeof(cgroup_path) - strlen(cgroup_path)); - if (!retval) { - err_msg("Did not find self cgroup\n"); - return 0; - } - } else { - snprintf(&cgroup_path[strlen(cgroup_path)], - sizeof(cgroup_path) - strlen(cgroup_path), "%s/", cgroup); - } - - snprintf(cgroup_procs, MAX_PATH, "%s/cgroup.procs", cgroup_path); - - debug_msg("Using cgroup path at: %s\n", cgroup_procs); - - cg_fd =3D open(cgroup_procs, O_RDWR); + cg_fd =3D open_cgroup_procs(cgroup); if (cg_fd < 0) return 0; =20 --=20 2.52.0