From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (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 BC2B52D7817 for ; Wed, 19 Aug 2026 12:33:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787142841; cv=none; b=BpyoKL9v0sQFgIodHNt8pd5fOV3HdfkEnXoEfJ5w8vRRN9kMXXLK9/7fAL4YGJg0g50edWVhZtzAjUFteRErYX2UmOGqjvfyMwHVMi/qtqqvwqHCw+44Ut/ynQy9jsQnbXIvTdBppwt3wUrLMpyAka4s8o15BB9gX2IsZAVZyL4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787142841; c=relaxed/simple; bh=uNGm5WIP5Jndk1hTzciLaeliqnYG+SbNxP/S5PIEhUU=; h=From:To:Cc:Subject:In-Reply-To:References:Date:Message-ID: MIME-Version:Content-Type; b=FjS3WczM1gi9/3W9lfad7Rq5WTOrNFPdWReZermqVQZtXNGpU0WTKOEKODtjLqoQNvlkJOOhseu2OkFKmsuJTUF5I4THePof/H9xCDqiIb0nNq1wwbNYXG2bUfpho+X4Zuc3vZw5rbjzgDL7LkKQ6QqYyrqNXn9dv0fVAMbP3cU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=007vOlfs; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=MppqYGa2; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="007vOlfs"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="MppqYGa2" From: Nam Cao DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1787142835; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=GUmvpv/qhdwns6mT62MT5RE9RITs56cprUq89W6UKxM=; b=007vOlfsJ+ZARvErkPXbN5s7th0P6PbVX6fXMQOneWk3mrTrvVtdbJPelvhU0cqJaBYEHs NoP/g1dvlyEYDjX74SeyYhwieIxWTistB8ujlPFunohDnmYoei5VlJruP5MSfBpb3fXy/6 bc95O2RP2IROYzHYEVYcHSrZeJGMIBRsb05lUesomprYP7UUFAlheWmDDsSlt+wPJq+avX z7CdhdbtZhWO4qW/1C/DFpJBpNOoZXmg2nit7MG7+hlj17rzAoVYExJ3AoUeNJqYgli/rh U+gKsa2BLUDfRF6NMkDMugTrqGFyRDnO1581QZRthgoLV51d151EXOrczl2oFA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1787142835; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=GUmvpv/qhdwns6mT62MT5RE9RITs56cprUq89W6UKxM=; b=MppqYGa2QrwMSLNnWRKZsjPmuex86ragutfUN2PO8y2TjuTBVM1USSunmkR1dl/4qFyutH MVRLEx7ggiLDmxBw== To: Xiaofeng Yuan , Paul Walmsley , Palmer Dabbelt , Albert Ou Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, yuanxiaofeng@eswincomputing.com Subject: Re: [PATCH] riscv: probes: reject kprobes inside LR/SC sequences In-Reply-To: <20260819102922.1747-1-yuanxiaofeng@eswincomputing.com> References: <20260819102922.1747-1-yuanxiaofeng@eswincomputing.com> Date: Wed, 19 Aug 2026 14:33:55 +0200 Message-ID: <87tsoql26k.fsf@yellow.woof> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain Xiaofeng Yuan writes: > +/* > + * A trap taken in the middle of an LR/SC sequence clears the load > + * reservation, so an SC following the probed instruction would always > + * fail and the enclosing retry loop would re-enter the breakpoint. > + * Reject probes inside such a sequence. > + * > + * A constrained LR/SC loop (Zalrsc) is limited to 16 instructions placed > + * sequentially in memory (64 bytes in the base ISA), so scanning back > + * that far covers every possible enclosing sequence. > + */ > +#define MAX_ATOMIC_CONTEXT_SIZE 64 What about C extension? Does this 64 still work? > + > +static bool __kprobes riscv_probe_insn_in_atomic(unsigned long addr) > +{ > + unsigned long tmp, offset, scan_start; > + bool in_atomic = false; > + > + if (!kallsyms_lookup_size_offset(addr, NULL, &offset)) > + return false; > + > + tmp = addr - offset; /* function entry */ > + > + if (offset > MAX_ATOMIC_CONTEXT_SIZE) > + scan_start = addr - MAX_ATOMIC_CONTEXT_SIZE; What guarantees that 'scan_start' points to the beginning of an instruction? > + else > + scan_start = tmp; > + > + /* advance to the scan window, keeping instruction alignment */ > + while (tmp < scan_start) > + tmp += GET_INSN_LENGTH(*(u16 *)tmp); > + > + /* scan the window, tracking whether an LR is still outstanding */ > + while (tmp < addr) { > + if (GET_INSN_LENGTH(*(u16 *)tmp) == 4) { Why is tmp casted to (u16 *)? An instruction can be 32 bits. It still works, I think, but looks confusing. > + u32 insn = *(u32 *)tmp; Reuse this insn for the GET_INSN_LENGTH() above? Nam