From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f47.google.com (mail-wm1-f47.google.com [209.85.128.47]) (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 03362214813 for ; Mon, 8 Dec 2025 20:29:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.47 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765225782; cv=none; b=bIZb7NRCcEmel/xEkOnsqrBYrfT2SkK6nDALqOqskH8+/6BBW/1LH0Qf0J1XCE0NlZc1GBHa0ELRoGkCnuMr6u+xA6POCpMM+NGbFMNXaTLW97sn/4+TFeA9hxtAQSrv668eCGcSW1sxJSMawLrn+x6U+RBdG1lV0f3LSNG2GPw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765225782; c=relaxed/simple; bh=cRXEqLTl8nKNK5sqLxJ9IzcJyurosEv+Mkm1Q1IfyGY=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version:Content-Type; b=ih+EfKmS+jLtVi9a38q0JdPbC3eP44Rl4Fsnr+Hg3h2/6DaCR2TnOpZQ/+FaY1Mu6UJi9Y/dVRuhzYZU4ikGfbXaVJBuzvFf5wr595zcEeOvxLwAVy+laSfSx5r7Uj6oI6790dtjC4YFqnCmKj2AAybaWTMMsjfM+lVrqECC1UI= 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=Zr0PRVUU; arc=none smtp.client-ip=209.85.128.47 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="Zr0PRVUU" Received: by mail-wm1-f47.google.com with SMTP id 5b1f17b1804b1-47774d3536dso46341715e9.0 for ; Mon, 08 Dec 2025 12:29:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1765225779; x=1765830579; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=/mBGIBF3oA9cTumt7bC1c5ATAauWokOcCLgIyHn3XOg=; b=Zr0PRVUUVADyA4GFlUy6XLAoW1qKCWJ9eQmdoMNb4E/N3ohV31VP8bOimeIdXsHmuv FzSZjMxKXtaAicEqpxzZhRXXAa1JzBR+UUVp3eMepeecPQ43FtYFNdkSFiIw/AU55Pkb Bb2nHJnoYK4O0jCD1LHyH0FNn56g8GhHSCEL8+jylul/4Yfqqs3ypz4sbZs4O7lHfJgv o2S7JjYXUVU5lPNwBqrOFa5lSuGFyPgwU0LJW0EwLak+KulK+omzRO7znvk9OpbADRuk /vwQ+ialNpsfIMaodY726URvymWB9BMUs/HK92chsZzPaVc9d57O4bI8T0AvBtDncnCo 7IDw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1765225779; x=1765830579; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=/mBGIBF3oA9cTumt7bC1c5ATAauWokOcCLgIyHn3XOg=; b=EThTW7Rtu2HMeP5KXA0bFnVdvu9gZUAEuz47N/C7Qi/9Kh6CkfXydtjuLG824/3s5I kYnWYaoew+he2wEHyvmhiaeKHTNqLthwh0XV1f8eLt2VxZBAkqIklPCwJZ2cKHY4GAZP aw2wXQr6jSELN5W0zZO8N2Kd5o+swUiECfidfdoooiYlkKGKXDIU9sz6U3JPfzkARRvH pKTMkjS9LkNAca4tAJSexUDfaW4KUJmfyvIfWPTGBrNq4JqGbr77xfKvGdhlpGh/qUmN jla63BK+Ay3Q23+GQHguo8+46iLYzXq4l3W7xUmniyXhIzwot228ZhjCOdkZ88YBy4u8 ufJQ== X-Forwarded-Encrypted: i=1; AJvYcCV+qJrvjk6W1jldQuLB4BYcIq7wQNj9s3m1bnBQ0CMcuAHVQrlbMmyJeLE4KpKbIX3TJQ4VBwwWFc0Om+0=@vger.kernel.org X-Gm-Message-State: AOJu0YyoSMs76Tlm2YqjebdAx7xPUPpa3tkq8H1Evrn12jzDQwWEpzUv eLeDuu7DwQshS3JWlh0Q3I4PXrJ1y0TsrGA9ZptEijqI5+FuMBCfJ7Eq43ulYw== X-Gm-Gg: ASbGncs5IfE5xpsIGqVs0zeaF+S+vZqM92TLduFqLKzxdQMxJovClgUPD3Jm7RWmtiU EeOK4NLdTg039dBzvT5cKCgceAjMht1oajeo5ImXngpaRhLbVDZ/nI73hp3DU1qRBVjUdav9/ez t+N/5/v/qofEz2AVbkg6zkvdft+yIyYvAiQtzpwt/QpzOT6iuM4RdkBda3+i7U9MtNN2uORwrLc ZB9kS0XF2lZdZPxaZ3tdsfw5DsDUVTTd1jQnZ35yi5cLYUO1fwMx//Xr9K0y/jA1Z/gjV7NXek3 LKrEYbRzQLKZqqJOnaerlyVs5KHq41tt7mTGIpD2M/4+2P5jsDTDcUuvFCGUKKQ0jf2Njwp3yFj oGwpczBtXT7vhGvS/x/pzf92ln6zrrhSpr06sB7k0n4Uaun8zVOkr613wdLDigJlPtw6ANAbuae OBq561xKwtZkFtKDdgKnLq2Y1/CNgUu2nSBErcx9VM X-Google-Smtp-Source: AGHT+IEKDx/1dxmNE7pkzM+rqLy+yyQ8ZgSKG4iaxF5gmOpfwiDY7sMOq9KBgxDqap/FwtfHvwcaVA== X-Received: by 2002:a05:600c:331b:b0:477:7588:c8cc with SMTP id 5b1f17b1804b1-47a7ce482bemr4515105e9.7.1765225779252; Mon, 08 Dec 2025 12:29:39 -0800 (PST) Received: from Ansuel-XPS24 (93-34-88-81.ip49.fastwebnet.it. [93.34.88.81]) by smtp.googlemail.com with ESMTPSA id 5b1f17b1804b1-47a7d6a19cesm2867855e9.15.2025.12.08.12.29.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Dec 2025 12:29:38 -0800 (PST) From: Christian Marangi To: Christian Marangi , Andy Shevchenko , Andrew Morton , linux-kernel@vger.kernel.org Cc: =?UTF-8?q?Ilpo=20J=C3=A4rvinen?= Subject: [PATCH] resource: add WARN_ON_ONCE for resource_size() and document misusage Date: Mon, 8 Dec 2025 21:29:19 +0100 Message-ID: <20251208202921.16819-1-ansuelsmth@gmail.com> X-Mailer: git-send-email 2.51.0 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: 8bit To catch any possible misusage of resource_size() helper, emit a WARN if we detect the passed resource descriptor have zeroed flags. This would signal the resource descriptor is not correctly inizialized and will probably result in resource_size() returning unexpected values. (for example returning 1 if the resource descriptor is all set to zero) Historically, it was assumed that resource_size was ALWAYS used AFTER correct API fill the data of the resource descriptor (or errors out) But lack of comments might have introduced some logic error and confusion in any user of resource_size() with it used on resource description initialized to all zero. The normal way to inizialize an "uninizialized" resource descriptor would be to use DEFINE_RES macro or resource_set_range() ideally with a proper flag set to it. Hence initializing a resource descriptor to all zero and passing it to resource_size() would actually produce a size of 1. Correct comments on the usage of this helper in conjunction of WARN should prevent from now on any possible misusage of this and permit to catch and fix any possible BUG caused by this logic confusion. Link: https://lore.kernel.org/all/20251207215359.28895-1-ansuelsmth@gmail.com/T/#m990492684913c5a158ff0e5fc90697d8ad95351b Suggested-by: Ilpo Järvinen Signed-off-by: Christian Marangi --- include/linux/ioport.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/include/linux/ioport.h b/include/linux/ioport.h index e8b2d6aa4013..8b60f820993c 100644 --- a/include/linux/ioport.h +++ b/include/linux/ioport.h @@ -286,8 +286,20 @@ static inline void resource_set_range(struct resource *res, resource_set_size(res, size); } +/** + * resource_size - Get the size of the resource + * @res: Resource descriptor + * + * This MUST be used ONLY with correctly inizialized resource descriptor. + * Passing a resource descriptor with zeroed flags will produce a WARN + * signaling a misusage of this helper and probably a BUG in the user + * of this helper. + * + * Return: Size of the resource calculated from resource end - start + 1. + */ static inline resource_size_t resource_size(const struct resource *res) { + WARN_ON_ONCE(!res->flags); return res->end - res->start + 1; } static inline unsigned long resource_type(const struct resource *res) -- 2.51.0