From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-lj1-f174.google.com (mail-lj1-f174.google.com [209.85.208.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 72F8D3A5E89 for ; Tue, 21 Apr 2026 12:00:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.174 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776772842; cv=none; b=R6ZkVFUFrDHcrN+1awPVaF1QBS9KuR/cEt0SwpuKBThpZ8o5YDufMhIzyr79hQeURvyGXoGQ/4fAIf0LPEkrcq6HVpnMX1Zc2/Lvl6s4b2fWO0OWSWhsz9LW0W8TSDpa9POBpafdDGP3rqyZV/Ho+MgPDe7bfnu1WF2KFfXdE44= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776772842; c=relaxed/simple; bh=jUfz4y7jsTQO9NSAK/QsP1yvKBCcNop0ynn+1E2jZDo=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=jT80k18A61SJ3X2mlmHGuagGWUKSQjAaujTqeeoRwIzRjipzhgiUSQ/GUtsqn+o1tFBdnMNtYURIiqKfaBpHIlliyV3Z3F1hxiBEtW0uysRw21Jqj0eyOvK4h9KE0Nk/3sJYZSRVy1fK0gQr+JxooBlz1Lz80bRWcodkyqNsilM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=kjtVol15; arc=none smtp.client-ip=209.85.208.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="kjtVol15" Received: by mail-lj1-f174.google.com with SMTP id 38308e7fff4ca-38e84ed22bdso42007191fa.2 for ; Tue, 21 Apr 2026 05:00:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1776772838; x=1777377638; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=+HxesmLToutIBecL3NH9vQO9ZjV/Ox5UtNblW90mx2c=; b=kjtVol15XAiFevUFKY9Pr+ni6F87U3HsDXo6q0snAR2qxTyEqiVWHQH7OtldXgtjcd REI3ScXSrihFmDKCvA6U3TTiHF8x4Qr7D92xhZA64HDJYouvq94nmhZ5rbkzN266HO2C spWV6uENRws+1TVZv0A9zQJQUG2CU2+zUmRWGo4EOtwq7vjniRtA8GOPBP1iqRYRa00B sK88SEY3nPc8OZN2zAj7iF8jKAi8IglBiWgRTkeT6bmfCPCyfEUW83BLNcrGEfPqZmTl K+M6hOqkALlcTdg9DkGCsLSJMKmE5U4Y8D/oIHHTziUoGEOblaJT+fUvy6GYLaN1J3R6 M74g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776772838; x=1777377638; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=+HxesmLToutIBecL3NH9vQO9ZjV/Ox5UtNblW90mx2c=; b=gE737S2zGUvbTrqTF2CHrBWgtN5VH1E6eTY7H9B1HBUVmckPJWVTFNMkeKTQEQvy8G MfUV4Fj/MCg00h0/r/Qhxe4IV+OTQIcQzZ2KFITfc9U08NBEHkNOAnvX1yLwZX2v1Wvv LStV4NWG55zaIT7xWQKrBYfqIG5kBTo41V3cJGDHmijdw41ldvOkKqXjJ/t7/kCU1wMl AT3w9BfghS8WIAmvgLARjpuwqaOYx4GBqDi284fwI1CaFahqlGjkYSVYO1m6b0sO+7/z HmFQ1qeGwXi9OTeXCZnZJhlLVwMaUb0ZyZfZwhitnoMVcH6KKmLxMUt8te7UFiKpuEfY k14A== X-Forwarded-Encrypted: i=1; AFNElJ+qvPKMoVbGDng5k7tZXEAhYuSCKm1BKgnD0RC4CeTIeqNNWOydWZ+vCY9A43CVfFBLHygJn5gIl/cFRt0=@vger.kernel.org X-Gm-Message-State: AOJu0YylH9XW+3hl3TE5VI2qe7xW+FhUEGYWj2crWtqHWbzL/KP0ACFw hMdR6y38+29oA8npinb8xGMrwSyLhbeSxA0Ke+QCOV+vTDh7aUDrHW9x X-Gm-Gg: AeBDieuueHj8zoXHyFIeg1pK+SoXPsz/MGl8JiOYwwHf4PgzfRlSuzzCxmv8J/3/ngL oIbvDGjJH/D+bjqcgwtnH9vbfIbGzmVtxf62nCr4gvoW/irSHGIf/UFDnsp6BU0XzBj1YEAYfQ4 mdn5VrqF4Iy/i+SWDX2Y5CEowqbNbu0Om8DDLWtOi5V25+VtYSTDzbHXfD1Wdsg7COQYXTL9v14 qf8Vbi38dOmudwTmqCHFft3P+Kh1LHkRO2krAa7ugpJ0+GgfXX3BNXMbGPxtEYgUq/i5+LFBHb/ agz5/zObKdEWs4o+Hau6Brvd5TjojbINt17CAIvsoM1LFLt38nju/jcQGtR94kZrocSfgy0Wk5h dx0YCBh1FViioYvi5VTtYv8pXZwwf1TOS9+eGdTz2B6unoZ5dNAqgvbbnYjthJoNpiccu5/Zpn1 qWcXLu2CxzLlVPS77j5NBJiykNtK0LGGI7be/jZb3zZagLFiOrmA== X-Received: by 2002:a2e:be88:0:b0:38e:98b0:3a7e with SMTP id 38308e7fff4ca-38ec7ac89c3mr53414301fa.22.1776772838117; Tue, 21 Apr 2026 05:00:38 -0700 (PDT) Received: from imamay-pc ([46.73.175.180]) by smtp.gmail.com with ESMTPSA id 38308e7fff4ca-38ecb75b033sm29418791fa.39.2026.04.21.05.00.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Apr 2026 05:00:37 -0700 (PDT) Date: Tue, 21 Apr 2026 15:00:35 +0300 From: Ilya Mamay To: Himanshu Chauhan Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, pjw@kernel.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, alex@ghiti.fr, shuah@kernel.org Subject: Re: [PATCH v3 2/2] riscv: Add breakpoint and watchpoint test for riscv Message-ID: <20260421120035.xsbli7g35nbh7luu@imamay-pc> References: <20260223044918.1359983-1-himanshu.chauhan@oss.qualcomm.com> <20260223044918.1359983-3-himanshu.chauhan@oss.qualcomm.com> 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=us-ascii Content-Disposition: inline In-Reply-To: <20260223044918.1359983-3-himanshu.chauhan@oss.qualcomm.com> Hi Himanshu, I have been working on a system that guarantees the successful installation of hardware breakpoints in a tracee process (or ensures perf_event_open fails in the tracer otherwise). While using your patches for testing, I noticed two critical issues in the current implementation: 1) The test_func is a static empty function, which means the compiler may inline it or optimize it away entirely. If this happens, the hardware breakpoint will never fire. 2) Potential Deadlock: The test uses sem_wait to wait for the breakpoint to trigger. If the breakpoint fails to fire, the test will hang indefinitely. Regards, Ilya On Mon, Feb 23, 2026 at 10:19:18AM +0530, Himanshu Chauhan wrote: > Add self test for riscv architecture. It uses ptrace to ptrace framework > to set/unset break/watchpoint and uses signals to check triggers. > > Signed-off-by: Himanshu Chauhan > --- > tools/testing/selftests/breakpoints/Makefile | 5 + > .../breakpoints/breakpoint_test_riscv.c | 174 ++++++++++++++++++ > 2 files changed, 179 insertions(+) > create mode 100644 tools/testing/selftests/breakpoints/breakpoint_test_riscv.c > > diff --git a/tools/testing/selftests/breakpoints/Makefile b/tools/testing/selftests/breakpoints/Makefile > index 9ec2c78de8ca..97ab746908d8 100644 > --- a/tools/testing/selftests/breakpoints/Makefile > +++ b/tools/testing/selftests/breakpoints/Makefile > @@ -12,5 +12,10 @@ ifneq (,$(filter $(ARCH),aarch64 arm64)) > TEST_GEN_PROGS += breakpoint_test_arm64 > endif > > +ifneq (,$(filter $(ARCH),riscv)) > +CFLAGS += -static > +TEST_GEN_PROGS += breakpoint_test_riscv > +endif > + > include ../lib.mk > > diff --git a/tools/testing/selftests/breakpoints/breakpoint_test_riscv.c b/tools/testing/selftests/breakpoints/breakpoint_test_riscv.c > new file mode 100644 > index 000000000000..e07f6fb1cfbd > --- /dev/null > +++ b/tools/testing/selftests/breakpoints/breakpoint_test_riscv.c > @@ -0,0 +1,174 @@ > +// SPDX-License-Identifier: GPL-2.0-only > +/* > + * Copyright (C) 2026 Qualcomm Technologies, Inc. > + * > + * Author: Himanshu Chauhan > + */ > + > +#define _GNU_SOURCE > +#include /* Definition of PERF_* constants */ > +#include /* Definition of HW_* constants */ > +#include /* Definition of SYS_* constants */ > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > + > +static int gfd; > +sem_t ib_mtx, wp_mtx; > +static int bp_triggered, wp_triggered; > + > +int setup_bp(bool is_x, void *addr, int sig) > +{ > + struct perf_event_attr pe; > + int fd; > + > + memset(&pe, 0, sizeof(struct perf_event_attr)); > + pe.type = PERF_TYPE_BREAKPOINT; > + pe.size = sizeof(struct perf_event_attr); > + > + pe.config = 0; > + pe.bp_type = is_x ? HW_BREAKPOINT_X : HW_BREAKPOINT_W; > + pe.bp_addr = (unsigned long)addr; > + pe.bp_len = sizeof(long); > + > + pe.sample_period = 1; > + pe.sample_type = PERF_SAMPLE_IP; > + pe.wakeup_events = 1; > + > + pe.disabled = 1; > + pe.exclude_kernel = 1; > + pe.exclude_hv = 1; > + > + fd = syscall(SYS_perf_event_open, &pe, 0, -1, -1, 0); > + if (fd < 0) { > + printf("Failed to open event: %llx\n", pe.config); > + return -1; > + } > + > + fcntl(fd, F_SETFL, O_RDWR | O_NONBLOCK | O_ASYNC); > + fcntl(fd, F_SETSIG, sig); > + fcntl(fd, F_SETOWN, getpid()); > + > + ioctl(fd, PERF_EVENT_IOC_RESET, 0); > + > + return fd; > +} > + > +static void sig_handler_bp(int signum, siginfo_t *oh, void *uc) > +{ > + int ret; > + > + bp_triggered++; > + > + printf("Breakpoint triggered!\n"); > + ioctl(gfd, PERF_EVENT_IOC_DISABLE, 0); > + ret = sem_post(&ib_mtx); > + if (ret) { > + printf("Failed to report BP success\n"); > + return; > + } > +} > + > +static void sig_handler_wp(int signum, siginfo_t *oh, void *uc) > +{ > + int ret; > + > + printf("Watchpoint triggered!\n"); > + ioctl(gfd, PERF_EVENT_IOC_DISABLE, 0); > + wp_triggered++; > + > + ret = sem_post(&wp_mtx); > + > + if (ret) { > + printf("Failed to report WP success\n"); > + return; > + } > +} > + > +static void test_func(void) > +{ > +} > + > +static int trigger_bp(void) > +{ > + struct sigaction sa; > + > + memset(&sa, 0, sizeof(struct sigaction)); > + sa.sa_sigaction = (void *)sig_handler_bp; > + sa.sa_flags = SA_SIGINFO; > + > + if (sigaction(SIGIO, &sa, NULL) < 0) { > + printf("Failed to setup signal handler\n"); > + return -1; > + } > + > + gfd = setup_bp(1, test_func, SIGIO); > + > + if (gfd < 0) { > + printf("Failed to setup breakpoint.\n"); > + return -1; > + } > + > + ioctl(gfd, PERF_EVENT_IOC_ENABLE, 0); > + > + test_func(); > + > + ioctl(gfd, PERF_EVENT_IOC_DISABLE, 0); > + > + close(gfd); > +} > + > +static int trigger_wp(void) > +{ > + struct sigaction sa; > + unsigned long test_data; > + > + memset(&sa, 0, sizeof(struct sigaction)); > + sa.sa_sigaction = (void *)sig_handler_wp; > + sa.sa_flags = SA_SIGINFO; > + > + if (sigaction(SIGUSR1, &sa, NULL) < 0) { > + printf("Failed to setup signal handler\n"); > + return -1; > + } > + > + gfd = setup_bp(0, &test_data, SIGUSR1); > + > + if (gfd < 0) { > + printf("Failed to setup watchpoint\n"); > + return -1; > + } > + > + ioctl(gfd, PERF_EVENT_IOC_ENABLE, 0); > + test_data = 0xdeadbeef; > + ioctl(gfd, PERF_EVENT_IOC_DISABLE, 0); > +} > + > +int main(int argc, char *argv[]) > +{ > + struct sigaction sa; > + > + sem_init(&ib_mtx, 0, 0); > + trigger_bp(); > + sem_wait(&ib_mtx); > + > + if (bp_triggered) > + printf("Breakpoint test passed!\n"); > + > + sem_init(&wp_mtx, 0, 0); > + trigger_wp(); > + sem_wait(&wp_mtx); > + > + if (wp_triggered) > + printf("Watchpoint test passed!\n"); > + > + return 0; > +} > -- > 2.43.0 > > > _______________________________________________ > linux-riscv mailing list > linux-riscv@lists.infradead.org > http://lists.infradead.org/mailman/listinfo/linux-riscv