From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f42.google.com (mail-wm1-f42.google.com [209.85.128.42]) (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 965E8276045 for ; Mon, 19 May 2025 12:39:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.42 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747658353; cv=none; b=eRNM5nA/F2GyDZOsZG64FsYC/lBP0mPYCify1JKXufWJ14ANREjemTySn2PNc46MyhmSlVKtHE1p1Qf5bCFDPgQixDN4BuYihyz5GJ7kNKCFqxilyPsoXf/75I15+kHVuanj74gyMnpwiHxgYxZ0OcVeXHHT25AcjolgN1Jhke8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747658353; c=relaxed/simple; bh=wAgE1d1Nn9Gs3icpp4n2PghbKllsJSUIZKEeigydyr0=; h=Mime-Version:Content-Type:Date:Message-Id:From:Subject:Cc:To: References:In-Reply-To; b=bRklg4CJ8c+juI9HlUOYxBfD5/+fnM6CRO6/6IsfLIZT7v2W1MbBuuzCcSlubSdSj5ITxvn87RL6W+UEX1DmlDo33o87tfEO/3AN/josG//8eLcAG/XhYgHvAQYTsg1rwGyIalvbkDY6Y+xjSo+J0heXE7rOgufc3p25QmccE30= 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=c0asuxeh; arc=none smtp.client-ip=209.85.128.42 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="c0asuxeh" Received: by mail-wm1-f42.google.com with SMTP id 5b1f17b1804b1-43cfe99f2a7so4354095e9.2 for ; Mon, 19 May 2025 05:39:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; t=1747658350; x=1748263150; darn=vger.kernel.org; h=in-reply-to:references:to:cc:subject:from:message-id:date :content-transfer-encoding:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=8TID/8Or9YAzKWADzCmDKYmYbQ3PtBl4rxavpksxXcw=; b=c0asuxeh9zKDI4BFM+0+N/0QABL/hv9jgthEHTsCykJZ49PXAo5NS4AbsKwWngXmAP XUSqAurvr+cBeuW9/QQotGhmFmgPGhn+d6Ja/1dA8PBwkUcBy8CC2B6VeGNshgTdK6Pw tcaZU8hVqAxZaK4Ca5Nih6cf3qFBrNvw0fSbId0IPZZxi//uKN5+xqRuG2TtIfzxzYun F2Pv86q84JNAlfLHK2+scX0atyD/IwnGhwqe9AlZlqwfGNiW3jScfqEuzgkvlPIvlu9s LafrHDMlIQymbeapTyTmKUweUd38786lwxgaaJWyenB/0Z+5jDAYxkMK3BAs9NMI+z21 2hfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747658350; x=1748263150; h=in-reply-to:references:to:cc:subject:from:message-id:date :content-transfer-encoding:mime-version:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=8TID/8Or9YAzKWADzCmDKYmYbQ3PtBl4rxavpksxXcw=; b=VZOkbNvN2+5kYWWnpygECN9UWbiINGnbt1pxvQozJeP9ZXJDnyUQ9NhgO2ubbiHy72 5zHsdnhZ2mUKj/+Gq/4HG7Gc7WHeaB7+nqkVR31g1KO3BlWUVx2vhtagdW3yx0KFbtBR AvG+eGSW1a8vQukqrUADhpNStK7yQNXnG4XpoLEaXR8HNhz7xTo9Q7ukmCYXgsDNmNlh Tz+sW1A7sYay/jq8VSiUZjrezNGxMOkih2vvomg8tSo592HFMdPqX7RYy4OlIeBFOyak Vk8NrG/fTZaoCaZVoCBYkI+nyTsbUi/JNSMytFO3RZelC8kTSVYKI5BsoKK5WIrTJ4qd gWeQ== X-Forwarded-Encrypted: i=1; AJvYcCVeqZ0X/xnEa47ZNzgmTpNi+y1R/KLsKxde9TZYY7kJS2+r6G/c1db//BK161SuHeGoEdPYOqg5BGc=@vger.kernel.org X-Gm-Message-State: AOJu0YzVIAyijWMxobG9LLcD1SN3BwkEDo/h42JoRUGxP05BCLEX20Mc MclYafFMuSO+oB33NO9uZBZka/0eYqsofOoVQ6lccdM4GOmicO9znZb7jScUOKJpfwc= X-Gm-Gg: ASbGncuFpkJoxLxiDqWGTsSDrVlVEP73ZbrRaTqbiUudjbdo9u+YZ6c0OWA+keTFPU7 WNAvwjYGs66H7YQkfRoGMLjpSluKoGe9OWG7ToINgEW7KyAPWA5ALNXUhJTy+0aKZ+GVD6j84l/ HtBPCLf/S4gdMdbYhHrMJln45FBw+TPEnF56grnFLxIPDlOn2u1SXiIWCHhrighIX3q3nz+VRR7 BJFFpLO5fVfgmlO82A4vrYhoy+mXE6AWO0N6eURp15ygDbYTTwobd2Es1BKn9ideA88EO65vRoo cWCoP5oeCRNUhBDOjEaSBBos9tBOpPN5pGgowyedkNSJ0KZOWk4Wn0pIzIA= X-Google-Smtp-Source: AGHT+IFfWi9oi0Jcra9sXbpqrxyg3hOkHnBuCeesfYKMaPVxn3iQR7mctJDpcqzl/F4hYBE/NRWxrg== X-Received: by 2002:a05:600d:108:10b0:43b:c0fa:f9bf with SMTP id 5b1f17b1804b1-442fd7165b7mr23100365e9.3.1747658349729; Mon, 19 May 2025 05:39:09 -0700 (PDT) Received: from localhost ([2a02:8308:a00c:e200:29b7:4911:a29c:2135]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-442fd583f20sm136362615e9.28.2025.05.19.05.39.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 May 2025 05:39:09 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-doc@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: Mon, 19 May 2025 14:39:08 +0200 Message-Id: From: =?utf-8?q?Radim_Kr=C4=8Dm=C3=A1=C5=99?= Subject: Re: [PATCH v15 05/27] riscv: usercfi state for task and save/restore of CSR_SSP on trap entry/exit Cc: "Alexandre Ghiti" , "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" , "Miguel Ojeda" , "Alex Gaynor" , "Boqun Feng" , "Gary Guo" , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , "Benno Lossin" , "Andreas Hindborg" , "Alice Ryhl" , "Trevor Gross" , , , , , , , , , , , , , , , , , , , , , , , "Zong Li" , "linux-riscv" To: "Deepak Gupta" References: <20250502-v5_user_cfi_series-v15-0-914966471885@rivosinc.com> <20250502-v5_user_cfi_series-v15-5-914966471885@rivosinc.com> <122fc6cd-2e21-4fca-979d-bcf558107b81@ghiti.fr> In-Reply-To: 2025-05-16T08:34:25-07:00, Deepak Gupta : > On Thu, May 15, 2025 at 10:48:35AM +0200, Radim Kr=C4=8Dm=C3=A1=C5=99 wro= te: >>2025-05-15T09:28:25+02:00, Alexandre Ghiti : >>> On 06/05/2025 12:10, Radim Kr=C4=8Dm=C3=A1=C5=99 wrote: >>>> 2025-05-02T16:30:36-07:00, Deepak Gupta : >>>>> diff --git a/arch/riscv/kernel/entry.S b/arch/riscv/kernel/entry.S >>>>> @@ -91,6 +91,32 @@ >>>>> +.macro restore_userssp tmp >>>>> + ALTERNATIVE("nops(2)", >>>>> + __stringify( \ >>>>> + REG_L \tmp, TASK_TI_USER_SSP(tp); \ >>>>> + csrw CSR_SSP, \tmp), >>>>> + 0, >>>>> + RISCV_ISA_EXT_ZICFISS, >>>>> + CONFIG_RISCV_USER_CFI) >>>>> +.endm >>>> Do we need to emit the nops when CONFIG_RISCV_USER_CFI isn't selected? >>>> >>>> (Why not put #ifdef CONFIG_RISCV_USER_CFI around the ALTERNATIVES?) >>> >>> The alternatives are used to create a generic kernel that contains the >>> code for a large number of extensions and only enable it at runtime >>> depending on the platform capabilities. This way distros can ship a >>> single kernel that works on all platforms. >> >>Yup, and if a kernel is compiled without CONFIG_RISCV_USER_CFI, the nops >>will only enlarge the binary and potentially slow down execution. >>In other words, why we don't do something like this >> >> (!CONFIG_RISCV_USER_CFI ? "" : >> (RISCV_ISA_EXT_ZICFISS ? __stringify(...) : "nops(x)")) >> >>instead of the current >> >> (CONFIG_RISCV_USER_CFI && >> RISCV_ISA_EXT_ZICFISS ? __stringify(...) : "nops(x)") >> >>It could be a new preprocessor macro in case we wanted to make it nice, >>but it's probably not a common case, so an ifdef could work as well. >> >>Do we just generally not care about such minor optimizations? > > On its own just for this series, I am not sure if I would call it even a > minor optimization. This patch uses ifdef in thread_info, but not here. Both places minimize the runtime impact on kernels that don't have CONFIG_RISCV_USER_CFI, so I would like to understand the reasoning behind the decision to include one and not the other. > But sure, it may (or may not) have noticeable effect if someone were > to go around and muck with ALTERNATIVES macro and emit `old_c` only > if config were selected. That should be a patch set on its own with > data providing benefits from it. The difference is small and each build and implementation can behave differently, so code analysis seems the most appropriate tool here. We must still do a lot of subjective guesswork, because it is hard to predict the future development. We should be moving on the pareto front and there are 3 roughly optimization parameters in this case: the C code, the binary code, and the work done by the programmer. The current patch is forgoing the binary quality (nops are strictly worse). The ifdef and the macro solutions prefer binary quality, and then differ if they consider work minimization (ifdef) or nice C (macro). Does the current patch represent the ideal compromise? (I can just recalibrate my values for future reviews...) Thanks.