From nobody Fri Dec 26 15:23:44 2025 Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) (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 E261B1D522 for ; Wed, 3 Jan 2024 20:22:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rivosinc.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b="NBk189ic" Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-1d3eb299e2eso52717895ad.2 for ; Wed, 03 Jan 2024 12:22:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1704313324; x=1704918124; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=tLVjvsKzWbuu1Tn6vfa7DAnWzdMErwoBxrG3TwM6ilc=; b=NBk189icOCbpjEiyIHfGUJaUW2XTJ7RuibADJ2XFiRXBXMJ75ORsfQWil8q13vlI9I 0Mrxg1/oAolHp5QCv3RNgjRPbNzNxVwU8tGaucOOnjcrQ+ds5QC8CaJmfgnAhI7Wmqkl 5me2OFIYC050i4/7ySoGGNKcrW1bINZm55c1t1BzRjat2z37/EMyR3mwVe9Zb0NPfTU+ NUx0gCPLWEHDe5AHdhm+YHl1ADcdYDTnD/0W7LyqONYxiVwxZ7NaJuqXeWubB5C3KGR8 0GJIQN08NCMHxzk7QZneELB1lvdMd/7VMmeHm/MYHPS5IxJcPdaK3GDrJr04uPMaxKUw 1j5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704313324; x=1704918124; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=tLVjvsKzWbuu1Tn6vfa7DAnWzdMErwoBxrG3TwM6ilc=; b=cYrkOg2wcmusn0rD3IgsM5v3qRB/4nD3drB8LMrqZNJ9o5+dsRn+wHVhPhq38RonQt e6wsn1VN7ogizMDS95/BHLIdD/UT9GA/gvxn642ybSVONREPI8V3hUsCHJgTz37SEVs5 FhZ+WLL7I+uz+VF+l8JequzFocdZjZngmURC70HsKCRSkSTrb+I5VxOEXSjsAPtCniE5 mt6c8IM8rwlOEqu/FIsE14c6S3zWc65ydrtQNzm3V9yZpi1rztW/DTLMvC86HbrF/xRZ Ryv0LTPjwaY24IYNptnX90SvA6j2deZn1sM73SxujO4oE1XTpflJ3tl1rSosVfgskUb3 T7pw== X-Gm-Message-State: AOJu0YwI61Q/b6p0AnhihvpXwJD9m4yl4R+Eu2o4IQSaQJwGcNnnkJFl Jl9AqAgFq0ZJhKJ/qOxfhSojw7OZ6jNqvQ== X-Google-Smtp-Source: AGHT+IHTSHatXOKK2rJLaTQuI7ebBUzqUQe4BzYMpC03oBwA/JUbZVZY+uhrsIRUZgibxJ4Y3J1btw== X-Received: by 2002:a17:902:e544:b0:1d2:fa24:2266 with SMTP id n4-20020a170902e54400b001d2fa242266mr10957069plf.6.1704313324207; Wed, 03 Jan 2024 12:22:04 -0800 (PST) Received: from charlie.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d13-20020a170903230d00b001d3fa5acf71sm24278739plh.12.2024.01.03.12.22.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Jan 2024 12:22:03 -0800 (PST) From: Charlie Jenkins Date: Wed, 03 Jan 2024 12:22:00 -0800 Subject: [PATCH v2 1/4] riscv: Fix module loading free order 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="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20240103-module_loading_fix-v2-1-292b160552c9@rivosinc.com> References: <20240103-module_loading_fix-v2-0-292b160552c9@rivosinc.com> In-Reply-To: <20240103-module_loading_fix-v2-0-292b160552c9@rivosinc.com> To: Paul Walmsley , Palmer Dabbelt , Albert Ou Cc: Palmer Dabbelt , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Charlie Jenkins , kernel test robot , Dan Carpenter , Julia Lawall X-Mailer: b4 0.12.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1704313322; l=1064; i=charlie@rivosinc.com; s=20231120; h=from:subject:message-id; bh=ChQMN8HXWHXJl0z5+9ukmO+5sTC3VuwHdA0xAgdz31A=; b=eGXoLh3RaVKJ+iWJ01D8+xrpOG2nvatttc/x3QBl+mQVjpXaGPIZMjUPquFofeleKnCdAjlgc stOmeal7/ylCU+9I0MA0q1qU3cXe7vaOsm6yfJVVzZ1gEdWsFVIIwEn X-Developer-Key: i=charlie@rivosinc.com; a=ed25519; pk=t4RSWpMV1q5lf/NWIeR9z58bcje60/dbtxxmoSfBEcs= Reverse order of kfree calls to resolve use-after-free error. Signed-off-by: Charlie Jenkins Fixes: d8792a5734b0 ("riscv: Safely remove entries from relocation list") Reported-by: kernel test robot Reported-by: Dan Carpenter Closes: https://lore.kernel.org/r/202312132019.iYGTwW0L-lkp@intel.com/ Reported-by: kernel test robot Reported-by: Julia Lawall Closes: https://lore.kernel.org/r/202312120044.wTI1Uyaa-lkp@intel.com/ --- arch/riscv/kernel/module.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/riscv/kernel/module.c b/arch/riscv/kernel/module.c index aac019ed63b1..21c7a773a8ef 100644 --- a/arch/riscv/kernel/module.c +++ b/arch/riscv/kernel/module.c @@ -723,8 +723,8 @@ static int add_relocation_to_accumulate(struct module *= me, int type, =20 if (!bucket) { kfree(entry); - kfree(rel_head); kfree(rel_head->rel_entry); + kfree(rel_head); return -ENOMEM; } =20 --=20 2.43.0 From nobody Fri Dec 26 15:23:44 2025 Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) (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 C2E561D52D for ; Wed, 3 Jan 2024 20:22:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rivosinc.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b="FLV/hZyk" Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-1d3f29fea66so45576105ad.3 for ; Wed, 03 Jan 2024 12:22:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1704313325; x=1704918125; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=3+J/P1s8879Vp+fzlyGqr2WWOHGZX3A10V1/imIkjd0=; b=FLV/hZyki3DS42L108bYaVqELfYGcoo38cDngIFDoqnBaGHXW8JJH/KAZlPL+gqC+q ROkTAoHqB1Jd6qRDW1VEcWVjrz6pRBcM/HNiNSH0uXRK7izlEFaTozPsXBOdLbu5kiEp R1N9/tf9C+hD2JHLitFdbfmNcs/En99/rKKoK7G8WEApCBWxKyTIkvZem9P0khVsIZYR 5HW8cFILBXlYKvQGxXzfjgGHYF3tjsIxdI0ojDJ3Fpdrxu02I/INzW0ugGI6OEW+yhZO bfYbpSKF+g8YUaQ6lUU4y0i46+uxCe8P+iKIef7lh8euEA/MfkOcE3t9JJuhyQ9IcK3K pDLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704313325; x=1704918125; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=3+J/P1s8879Vp+fzlyGqr2WWOHGZX3A10V1/imIkjd0=; b=rMtSniqRx39s254Q8nV6QZF4IEVbfbi+h27L+W+wPYxV3/lzneW/1D3GFv3AMIKsKr Sn2KSjJaSf4r7vyc6pQ5bSGJgelSdB/Q2b+47uL/beta3r/uwC6Svu/fLtU+5xvofsoH 7dFd8kGW5qhzVY389BaW8Ev/s5kewsEJNNvF9kBigQvkqZY5yhsM5ez7IT/m4mMhC4KN 4o5u23Gglr9BUfS5dq2G/j8n6S946v0nvBRjeiOnptznQ4Mf5h0irTI/dayo2zc01AXi vdWKskun8webuB3VAY6yVZ8UVK6NuCoZM0t7uzr9xN4FJfPlvqRtwlzgG+kk16+m2vdI uytQ== X-Gm-Message-State: AOJu0YwtpZT2gHbcMS/FgqBHok5g1ldoE0uhoHvtmlZlUxuz5htqJMS6 NF+d4CLCiuMiOzanqY/OdkcxIc1pY4l7Bg== X-Google-Smtp-Source: AGHT+IEDrbgjgNBUvvv9hWVeThqI6ijQ3EA3FWsa0h7uYxJIJYCUwnKf8WIXdqpmv94uscNsjpViJw== X-Received: by 2002:a17:902:da81:b0:1d4:c2ad:8ff8 with SMTP id j1-20020a170902da8100b001d4c2ad8ff8mr2710218plx.34.1704313325161; Wed, 03 Jan 2024 12:22:05 -0800 (PST) Received: from charlie.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d13-20020a170903230d00b001d3fa5acf71sm24278739plh.12.2024.01.03.12.22.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Jan 2024 12:22:04 -0800 (PST) From: Charlie Jenkins Date: Wed, 03 Jan 2024 12:22:01 -0800 Subject: [PATCH v2 2/4] riscv: Correctly free relocation hashtable on error 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="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20240103-module_loading_fix-v2-2-292b160552c9@rivosinc.com> References: <20240103-module_loading_fix-v2-0-292b160552c9@rivosinc.com> In-Reply-To: <20240103-module_loading_fix-v2-0-292b160552c9@rivosinc.com> To: Paul Walmsley , Palmer Dabbelt , Albert Ou Cc: Palmer Dabbelt , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Charlie Jenkins , kernel test robot , Dan Carpenter , Julia Lawall X-Mailer: b4 0.12.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1704313322; l=2207; i=charlie@rivosinc.com; s=20231120; h=from:subject:message-id; bh=9/Y3gMnmhvp1aRf3YmkeyxHlGtG/ubuu7urxufGjwTE=; b=oEe24hSZfsF4mLEENjvlrgBmLszzS6CujSYHcb5xYW5WEK65TSlLI3Drvqi0xuE88wUYf0yle tAaS12iJPF5DN1hbXifh/mprzWl2DKH2wdFUJ5myCBMAR510KON/qKu X-Developer-Key: i=charlie@rivosinc.com; a=ed25519; pk=t4RSWpMV1q5lf/NWIeR9z58bcje60/dbtxxmoSfBEcs= When there is not enough allocatable memory for the relocation hashtable, module loading should exit gracefully. Previously, this was attempted to be accomplished by checking if an unsigned number is less than zero which does not work. Instead have the caller check if the hashtable was correctly allocated and add a comment explaining that hashtable_bits that is 0 is valid. Signed-off-by: Charlie Jenkins Fixes: d8792a5734b0 ("riscv: Safely remove entries from relocation list") Reported-by: kernel test robot Reported-by: Dan Carpenter Closes: https://lore.kernel.org/r/202312132019.iYGTwW0L-lkp@intel.com/ Reported-by: kernel test robot Reported-by: Julia Lawall Closes: https://lore.kernel.org/r/202312120044.wTI1Uyaa-lkp@intel.com/ --- arch/riscv/kernel/module.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/arch/riscv/kernel/module.c b/arch/riscv/kernel/module.c index 21c7a773a8ef..32743180e8ef 100644 --- a/arch/riscv/kernel/module.c +++ b/arch/riscv/kernel/module.c @@ -747,6 +747,10 @@ initialize_relocation_hashtable(unsigned int num_reloc= ations, { /* Can safely assume that bits is not greater than sizeof(long) */ unsigned long hashtable_size =3D roundup_pow_of_two(num_relocations); + /* + * When hashtable_size =3D=3D 1, hashtable_bits =3D=3D 0. + * This is valid because the hashing algorithm returns 0 in this case. + */ unsigned int hashtable_bits =3D ilog2(hashtable_size); =20 /* @@ -763,7 +767,7 @@ initialize_relocation_hashtable(unsigned int num_reloca= tions, sizeof(*relocation_hashtable), GFP_KERNEL); if (!*relocation_hashtable) - return -ENOMEM; + return 0; =20 __hash_init(*relocation_hashtable, hashtable_size); =20 @@ -789,8 +793,8 @@ int apply_relocate_add(Elf_Shdr *sechdrs, const char *s= trtab, hashtable_bits =3D initialize_relocation_hashtable(num_relocations, &relocation_hashtable); =20 - if (hashtable_bits < 0) - return hashtable_bits; + if (!relocation_hashtable) + return -ENOMEM; =20 INIT_LIST_HEAD(&used_buckets_list); =20 --=20 2.43.0 From nobody Fri Dec 26 15:23:44 2025 Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.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 C3E2D1D544 for ; Wed, 3 Jan 2024 20:22:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rivosinc.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b="FvoiyVz8" Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-1d3ef33e68dso6104365ad.1 for ; Wed, 03 Jan 2024 12:22:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1704313326; x=1704918126; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=zwpW8R3xYRcFeijiUFHaM884VPc1V+tX+SXLWnbPyzE=; b=FvoiyVz8QnxDL5wEGzQ3TCOKVI9NfYSTikBJGbzZmBwfKlR3gI6A39PqTm0JX9wGjZ M+X7YUXZBjN7XH0xdTvfNJJpRnHauLCSlsZ0z/RFWRshinvQ6CTD0Fw3IIxNWFdedomM cdg9QUVo+tkA9H618VV/1t8zA1dowPiSay+HRmzF8Svxp0YNEK8Tkk9kH3ZNL/HAe8Hm Yp6wFznUv1hPEDkqzmuhDwnE/vNORsJNU0r6AT1HYiDYrTUkFgptCvVZHKZ7L+ejqhNX 8YqxiJTvTXtlxpVBVSWGtYXTeZJgygwkTWo968opX5baKExExtJnxCj86nPi2ZDpN1rd j/mQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704313326; x=1704918126; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=zwpW8R3xYRcFeijiUFHaM884VPc1V+tX+SXLWnbPyzE=; b=IZ8XwrleDN0r81+T9e4UnDiw8zrb2naROT/dD+2eoxkhsLfpAQVRrCS0QQnk5rQoez BbfBWzqsa7ZDvnCZghDue3RJ8qyKwX3fbUCsLUDjtB1mfHyb33v8EXTZjpdSXLFQfw55 U8IH1cxTXrL23s0n1lqZQPnjMxTVtMfsUwktF9W4uN42NVCyuvqoVI8lYheGXs/9eH0h DWYMO1oHaa8ezXgEYGb325C8eJ7vLzYotV3t/54gBlc/kz1lgIQ/w6oOocIdybscL3Uh xZHAqtyMemwfhimKs+YhDrnJVRHhfccavHMebPmD3Sz/BluoY+YufzLTAI4qfwxaTeq+ btIg== X-Gm-Message-State: AOJu0YxrYr11zkC7CwQPyseu7WrQTSAWfdmNQqkjNGZoVExfkuLFtZHb 0+0oNJq/ma0nrdBL1s+AtsNNhY9ONjMDxA== X-Google-Smtp-Source: AGHT+IEGKKEZm+PrXKULnqsmwhajVZq7IxX/xIJuHJ6YD4deMqXOED2G1ZGcg6ONgzGmqEbcfIT/jg== X-Received: by 2002:a17:902:748c:b0:1d3:edd9:1f13 with SMTP id h12-20020a170902748c00b001d3edd91f13mr1728858pll.67.1704313326148; Wed, 03 Jan 2024 12:22:06 -0800 (PST) Received: from charlie.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d13-20020a170903230d00b001d3fa5acf71sm24278739plh.12.2024.01.03.12.22.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Jan 2024 12:22:05 -0800 (PST) From: Charlie Jenkins Date: Wed, 03 Jan 2024 12:22:02 -0800 Subject: [PATCH v2 3/4] riscv: Fix relocation_hashtable size 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="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20240103-module_loading_fix-v2-3-292b160552c9@rivosinc.com> References: <20240103-module_loading_fix-v2-0-292b160552c9@rivosinc.com> In-Reply-To: <20240103-module_loading_fix-v2-0-292b160552c9@rivosinc.com> To: Paul Walmsley , Palmer Dabbelt , Albert Ou Cc: Palmer Dabbelt , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Charlie Jenkins , kernel test robot , Julia Lawall X-Mailer: b4 0.12.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1704313322; l=1020; i=charlie@rivosinc.com; s=20231120; h=from:subject:message-id; bh=js9HmT4o7o8yl7lcSP3vcOE6sOgYhIQV9xZrhoMilzw=; b=SCb2uRI3tDQm0ffD82YNnM5V07YHtzrz/w1KT4oUaF3KND9VKC87pPJdypEGMgzkkPQF9fFHn 3U5g8/i3hp1DCZbGzDsWDLQbXyWpmz3T+J+fT5KLQK7xulsUwWWSBLe X-Developer-Key: i=charlie@rivosinc.com; a=ed25519; pk=t4RSWpMV1q5lf/NWIeR9z58bcje60/dbtxxmoSfBEcs= A second dereference is needed to get the accurate size of the relocation_hashtable. Signed-off-by: Charlie Jenkins Fixes: d8792a5734b0 ("riscv: Safely remove entries from relocation list") Reported-by: kernel test robot Reported-by: Julia Lawall Closes: https://lore.kernel.org/r/202312120044.wTI1Uyaa-lkp@intel.com/ --- arch/riscv/kernel/module.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/riscv/kernel/module.c b/arch/riscv/kernel/module.c index 32743180e8ef..ceb0adb38715 100644 --- a/arch/riscv/kernel/module.c +++ b/arch/riscv/kernel/module.c @@ -764,7 +764,7 @@ initialize_relocation_hashtable(unsigned int num_reloca= tions, hashtable_size <<=3D should_double_size; =20 *relocation_hashtable =3D kmalloc_array(hashtable_size, - sizeof(*relocation_hashtable), + sizeof(**relocation_hashtable), GFP_KERNEL); if (!*relocation_hashtable) return 0; --=20 2.43.0 From nobody Fri Dec 26 15:23:44 2025 Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) (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 CE30F1D6A5 for ; Wed, 3 Jan 2024 20:22:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rivosinc.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b="So4npkJ4" Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-1d45f182fa2so56410265ad.3 for ; Wed, 03 Jan 2024 12:22:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1704313327; x=1704918127; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=EcPZp/8DlGxkljJ2IULxF796JgdWAzcJATDANUPFHbg=; b=So4npkJ41GmU4sK6gZvgfKT6DgrLFhP1EmmXbRBj9NV0kRWFVAFpcgqgcqgHSQQCIo LZIDiUlzKJgyq0B6GI1ihtSGHJduvk7s/3tyELsbVoU9WizlNOoAVMwdwqQDW6B94pP9 U8pI/793ONLMWAIljC8/nNm3hYGfJCjTNprhX2+LME//f0443SKOaPN/Q3nZERhwqJmE xGAvcrnwD4hCRm8RSAieFfn92rORHMZK8KCJRiCaF1X1f4vTSbUqvn6KNW98938qDzuR NyuWLeZxCLxQhLa+qP8yV9spDwKxPE4tVFTJpUHOqOUwTYcRdtItG9nt4CZd2BJ3FMu4 Qghw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704313327; x=1704918127; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=EcPZp/8DlGxkljJ2IULxF796JgdWAzcJATDANUPFHbg=; b=kxKIh2D5qUEKKMbnED6X/T2+/ptbh2y8HBM1aIGzUchpRXOyPnQeqbHlpdCPWx3c9u fM6QeC9wx5B6rkLSYglMu1UWvfCK5mcJctdgMikCZFnLhmKJQiiCSkCGIhyVjKWxAAqr ElyMic5cK1boAICzDsxD1aN+TJtzWrZcF2eNWTBL7DdtquM7wLYMXyFyJjMlfCtEMcVY Yq9/mOjwmRzUAy0YjrDw06oXP+OKT6qYE4mVgUo98CsM+ppi3eij4HiwxlxyQIJ7NGTh AVinae0cf9Pt2qmgOByHoC9hdw6OWB98+ht5raCCVktCH8Dtgi93KHRnOhbvJCXlGP0L r7hw== X-Gm-Message-State: AOJu0Yw9QkOA7Y0kCD7zcfhXGFkWk/ShtHJKwT+9AQJ9oXHN4+9cfc63 mbsO+VmEvatUb4g0WqS+W3JfkZchtHDz3w== X-Google-Smtp-Source: AGHT+IHcWutJo6xSTujomFEKk6dSK8nUjnWN4ho0sIQ5KDgWpHxK2VZNM4O1sbbudAJfyt88IVHcOA== X-Received: by 2002:a17:903:98b:b0:1d4:d0e8:f9d2 with SMTP id mb11-20020a170903098b00b001d4d0e8f9d2mr1463670plb.93.1704313327127; Wed, 03 Jan 2024 12:22:07 -0800 (PST) Received: from charlie.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d13-20020a170903230d00b001d3fa5acf71sm24278739plh.12.2024.01.03.12.22.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Jan 2024 12:22:06 -0800 (PST) From: Charlie Jenkins Date: Wed, 03 Jan 2024 12:22:03 -0800 Subject: [PATCH v2 4/4] riscv: Convert relocation iterator to do-while 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="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20240103-module_loading_fix-v2-4-292b160552c9@rivosinc.com> References: <20240103-module_loading_fix-v2-0-292b160552c9@rivosinc.com> In-Reply-To: <20240103-module_loading_fix-v2-0-292b160552c9@rivosinc.com> To: Paul Walmsley , Palmer Dabbelt , Albert Ou Cc: Palmer Dabbelt , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Charlie Jenkins , kernel test robot , Dan Carpenter X-Mailer: b4 0.12.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1704313322; l=2036; i=charlie@rivosinc.com; s=20231120; h=from:subject:message-id; bh=/ulcCnMFIkQarQGjz3X7NHIbZYgPXF/TzHke91JiC70=; b=2fwTTwWMrRHS1yLamkUKZvd9RFwXcDTwRTA8f+jCiAzGbZeyRz8gSU1eE+TKXv6+1XNA9u1WE i2oJi82DcH9AhH3z6euJBaUPy3PMy9P/+ZV8YT9MxUbCORDtooywt6E X-Developer-Key: i=charlie@rivosinc.com; a=ed25519; pk=t4RSWpMV1q5lf/NWIeR9z58bcje60/dbtxxmoSfBEcs= Use a do-while loop to iterate through relocation entries to prevent curr_type from being marked as uninitialized. Signed-off-by: Charlie Jenkins Fixes: d8792a5734b0 ("riscv: Safely remove entries from relocation list") Reported-by: kernel test robot Reported-by: Dan Carpenter Closes: https://lore.kernel.org/r/202312130859.wnkuzVWY-lkp@intel.com/ --- arch/riscv/kernel/module.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/arch/riscv/kernel/module.c b/arch/riscv/kernel/module.c index ceb0adb38715..581e425686ab 100644 --- a/arch/riscv/kernel/module.c +++ b/arch/riscv/kernel/module.c @@ -633,15 +633,31 @@ process_accumulated_relocations(struct module *me, bucket_iter->bucket, node) { buffer =3D 0; location =3D rel_head_iter->location; - list_for_each_entry_safe(rel_entry_iter, - rel_entry_iter_tmp, - rel_head_iter->rel_entry, - head) { + rel_entry_iter =3D + list_first_entry(rel_head_iter->rel_entry, + typeof(*rel_entry_iter), head); + rel_entry_iter_tmp =3D + list_next_entry(rel_entry_iter, head); + + /* + * Iterate through all relocation entries that share + * this location. This uses a do-while loop instead of + * list_for_each_entry_safe since it is known that there + * is at least one entry and curr_type needs to be the + * value of the last entry when the loop exits. + */ + do { curr_type =3D rel_entry_iter->type; reloc_handlers[curr_type].reloc_handler( me, &buffer, rel_entry_iter->value); kfree(rel_entry_iter); - } + + rel_entry_iter =3D rel_entry_iter_tmp; + rel_entry_iter_tmp =3D list_next_entry(rel_entry_iter_tmp, head); + } while (!list_entry_is_head(rel_entry_iter, + rel_head_iter->rel_entry, + head)); + reloc_handlers[curr_type].accumulate_handler( me, location, buffer); kfree(rel_head_iter); --=20 2.43.0