From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f53.google.com (mail-wr1-f53.google.com [209.85.221.53]) (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 0C2C2267F64 for ; Thu, 10 Apr 2025 11:04:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.53 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744283083; cv=none; b=LhsYX25L7tZPHDEnM6lFVXApag7eup7bdV5lyI+UK93qrgG3PnPjBnU3URAkV6TSGdbK5c/M/CKrX2eX8S3+xH5eAbhjLVO1AKciMgX8uNXh761OJ6I6FpX0mf9qvQZQXjArwpjhxv3UtMfcSElSmzxRbvp6zro5v0CHJM8BugY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744283083; c=relaxed/simple; bh=kOlPkOa9RAWaHmxwXq9vXkGSssRFc7sUC8EQNbcnfPY=; h=Mime-Version:Content-Type:Date:Message-Id:Cc:To:From:Subject: References:In-Reply-To; b=AV7GgTzXfRu0a4QEN2vat+RjLDYUcLlUi+TGpEFDFvjB7Xte/Idq86Yazcc19Wqm7xkq8OXx0OqDnkjSGceKSMB2S3HxRGAzhMKIP4KWFgqSOBUQiCw6Y70GWqCRdGMT+EpKqafokv3DZ8nm4DsvQwjlL9yEEDh1DjgyPkCSExs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ventanamicro.com; spf=pass smtp.mailfrom=ventanamicro.com; dkim=pass (2048-bit key) header.d=ventanamicro.com header.i=@ventanamicro.com header.b=pVFHYrgW; arc=none smtp.client-ip=209.85.221.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ventanamicro.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ventanamicro.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ventanamicro.com header.i=@ventanamicro.com header.b="pVFHYrgW" Received: by mail-wr1-f53.google.com with SMTP id ffacd0b85a97d-391295490c8so23863f8f.3 for ; Thu, 10 Apr 2025 04:04:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; t=1744283080; x=1744887880; darn=vger.kernel.org; h=in-reply-to:references:subject:from:to:cc:message-id:date :content-transfer-encoding:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=FwTqTcXuZVwmB1gK8gAQzMNft2dcqvTwt7yn+4sQNuo=; b=pVFHYrgW6CsPBVfhDp8H0SHh07ORi0+k92JSM7WHod+l6GpALDm6Af/8R4cZodTjSI tJhmQC0ISSnfE51tew20VXscex2ugzAatyW+gaNQVdqFVsPCNEtL0hzsectLv90gFFtt oVuwwysWrn9jEcSsg7T6KkbCebzr4gntAQJheGXE5uqMjbzNUT2JWYiPhRLq9y69avjt blLUmxwM5ibExPlEoUUbX0wnvPL/rJd7iUzELewMF/309FFzjGxOdZqxZeMaqwE83MP6 zgXi1m4nmEywod9fgORmfD+WzNDeuueMfkonfaKqfm00t7VTa2R1DFvAman71MpISxGx m5fQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744283080; x=1744887880; h=in-reply-to:references:subject:from:to:cc:message-id:date :content-transfer-encoding:mime-version:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=FwTqTcXuZVwmB1gK8gAQzMNft2dcqvTwt7yn+4sQNuo=; b=hQOEZeNIaaXPE0H+dnH0FangxnOoQZbuNNwQQtuflYlzNv4R7nYLL12ZZIJ6qywhBR mC4UQ8p78XSEQYdg0WraRDHbZ928yjK5xAEVUW+X6KwAjoA45Fb4yUzCFY0v49+UN4FZ NSPPfT+0LnL26K6ZcvxntFgC53+CjNzi1r8+s8AgknyTkvaWJgv1xGU/0ahrc9BwLjH2 K46oC25LxtERBr6H78lAT/xyOLhZqm02+Iz9lEqUB8FEeEYPP2LcqllGMDhSvE6evyzf 3xEwt54D+567SZz3XKzv0QAhvsW0x1vZj5ILoLaz6Q2UQynrydsh2GgamlIPmvrEriyH a5Sg== X-Gm-Message-State: AOJu0YxftrCNpG9pyrz0Y/EXVWvFVw3yzopqmsw56GnZapqA+jlNbJ+2 +2Ag5qIk2BImwDBay86rweVWaDIwGFrQTcfYgZumG6DkDCngCAT5RtWaikMVL8o= X-Gm-Gg: ASbGncvqpBC3G0wsZjQUoHxoCvPzgcQRrjX6KhEGUfsiZYhQqYHTsrpV7MlftQDCd/G 1Sk+O5FNIDKzxVAl/jtUsOS4fJMGyxPu75ZugECN5bk4RdU1hb8ZgyYzmpp7GSLSDsU1Cc/sPw7 X2fxxPM4TqgGoL4/AkXOerFRs/tOpl0hIJyydkzUskrOI/C503Tp1f/utI7ZyEdObotfWsYb7BZ dUa7oXEno/0YF9d/4HxkJ2qYmdnv6ScMaYEpA0/wJv5BzaGQMzJHwKID9rTzAPZ0u5Ajv3xcKjF lbs7RErppQLgUGFNIfdTVjOIi1Ecb9nxoTZ4T4fiY3A0Oit3 X-Google-Smtp-Source: AGHT+IFTHVjvMm7SI9zG+YTo6xP+VuwjrIfNbL14H9cY5sGUQSewGH6hwjaZZsvjM4ZfhfO2SM4RNA== X-Received: by 2002:a05:6000:18ac:b0:39c:13fa:3e0 with SMTP id ffacd0b85a97d-39d87adcd02mr1983393f8f.9.1744283080239; Thu, 10 Apr 2025 04:04:40 -0700 (PDT) Received: from localhost ([2a02:8308:a00c:e200:7d22:13bb:e539:15ee]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-39d89361186sm4476526f8f.14.2025.04.10.04.04.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Apr 2025 04:04:39 -0700 (PDT) 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 Date: Thu, 10 Apr 2025 13:04:39 +0200 Message-Id: Cc: , , , , , , , , , , , , , , , , , , , , , "Zong Li" , "linux-riscv" To: "Deepak Gupta" , "Thomas Gleixner" , "Ingo Molnar" , "Borislav Petkov" , "Dave Hansen" , , "H. Peter Anvin" , "Andrew Morton" , "Liam R. Howlett" , "Vlastimil Babka" , "Lorenzo Stoakes" , "Paul Walmsley" , "Palmer Dabbelt" , "Albert Ou" , "Conor Dooley" , "Rob Herring" , "Krzysztof Kozlowski" , "Arnd Bergmann" , "Christian Brauner" , "Peter Zijlstra" , "Oleg Nesterov" , "Eric Biederman" , "Kees Cook" , "Jonathan Corbet" , "Shuah Khan" , "Jann Horn" , "Conor Dooley" From: =?utf-8?q?Radim_Kr=C4=8Dm=C3=A1=C5=99?= Subject: Re: [PATCH v12 05/28] riscv: usercfi state for task and save/restore of CSR_SSP on trap entry/exit References: <20250314-v5_user_cfi_series-v12-0-e51202b53138@rivosinc.com> <20250314-v5_user_cfi_series-v12-5-e51202b53138@rivosinc.com> In-Reply-To: <20250314-v5_user_cfi_series-v12-5-e51202b53138@rivosinc.com> 2025-03-14T14:39:24-07:00, Deepak Gupta : > diff --git a/arch/riscv/include/asm/thread_info.h b/arch/riscv/include/as= m/thread_info.h > @@ -62,6 +62,9 @@ struct thread_info { > long user_sp; /* User stack pointer */ > int cpu; > unsigned long syscall_work; /* SYSCALL_WORK_ flags */ > +#ifdef CONFIG_RISCV_USER_CFI > + struct cfi_status user_cfi_state; > +#endif I don't think it makes sense to put all the data in thread_info. kernel_ssp and user_ssp is more than enough and the rest can comfortably live elsewhere in task_struct. thread_info is supposed to be as small as possible -- just spanning multiple cache-lines could be noticeable. > diff --git a/arch/riscv/kernel/entry.S b/arch/riscv/kernel/entry.S > @@ -147,6 +147,20 @@ SYM_CODE_START(handle_exception) > =20 > REG_L s0, TASK_TI_USER_SP(tp) > csrrc s1, CSR_STATUS, t0 > + /* > + * If previous mode was U, capture shadow stack pointer and save it awa= y > + * Zero CSR_SSP at the same time for sanitization. > + */ > + ALTERNATIVE("nop; nop; nop; nop", > + __stringify( \ > + andi s2, s1, SR_SPP; \ > + bnez s2, skip_ssp_save; \ > + csrrw s2, CSR_SSP, x0; \ > + REG_S s2, TASK_TI_USER_SSP(tp); \ > + skip_ssp_save:), > + 0, > + RISCV_ISA_EXT_ZICFISS, > + CONFIG_RISCV_USER_CFI) (I'd prefer this closer to the user_sp and kernel_sp swap, it's breaking the flow here. We also already know if we've returned from userspace or not even without SR_SPP, but reusing the information might tangle the logic.) > csrr s2, CSR_EPC > csrr s3, CSR_TVAL > csrr s4, CSR_CAUSE > @@ -236,6 +250,18 @@ SYM_CODE_START_NOALIGN(ret_from_exception) > csrw CSR_SCRATCH, tp > + > + /* > + * Going back to U mode, restore shadow stack pointer > + */ Are we? I think we can be just as well returning back to kernel-space. Similar to how we can enter the exception handler from kernel-space. > + ALTERNATIVE("nop; nop", > + __stringify( \ > + REG_L s3, TASK_TI_USER_SSP(tp); \ > + csrw CSR_SSP, s3), > + 0, > + RISCV_ISA_EXT_ZICFISS, > + CONFIG_RISCV_USER_CFI) > + Thanks.