From patchwork Fri Feb 7 15:17:10 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Shevchenko X-Patchwork-Id: 863398 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.18]) (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 6BB78238D58; Fri, 7 Feb 2025 15:18:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738941512; cv=none; b=WJxKocXaMC1JEu+FFc6pmeYTmxmPNn/Ll3MvCkmw8i7FyNZCz4Ir2snDbOpEy0qEE6Cne4EQuiqbefrm6FmEq3k3XuNDpntPfMI0RabkFJ3KTe78VeiAiBLsw0TnARORMODNGByJSMQ626dYMwXVtCQO9xhn/Qow2v8x48iqVVI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738941512; c=relaxed/simple; bh=MprMiMuV+6PSWzh5qcNiBjouyTdWbepTzVpCMiEoexc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=FksCTGLUDqzs5BJ8t6XrANgCPfWEQ5b/6C7tKJJxZOZRs+DL6T+wI7/X4dnelTcvzEp6D4Mkbr4BuP4P0NfeP2diKMv1Rb2CpCpi9P1Gy7NeLgccBTvXCl8pe6kvoRfKm1tVPouCAxUrFNjBy2346io5s33WzPX0JInhgKwC16A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=HPVUQb9i; arc=none smtp.client-ip=192.198.163.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="HPVUQb9i" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1738941511; x=1770477511; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=MprMiMuV+6PSWzh5qcNiBjouyTdWbepTzVpCMiEoexc=; b=HPVUQb9iyebAe94yXz6yhRqyPqgk75KgnZx6BwcFsAhVJTpe7yA9W0Ep 8AFygC4SI1N6Kr1F9MFsqWkfE73CPt1YYZfJ/cEcQU0XWjwBsItIG9i1X SmUO6E6U9dYqL+H/RFB3+/vM5xY87X2CiMEmodHgvUFLyYdvyqsg0JCcK PyF+FrzCQGo2Z7pMgdOk1RPsqcZumVPAjiBTE8tsb2E7Nl5E/bCa+crSA ACi78WiYJLUBnMM6DCPuAnCI3wQhtVHmcoNA0CbEmd+TGH8rHqcDXXuYS x0pl33CRUBJPzG9vTMMLLlXldkdXKI+vVgDEOnku5xCuUa0NMlzDeC/ee A==; X-CSE-ConnectionGUID: Fh7RfJLuS96ejanzLUZU6A== X-CSE-MsgGUID: d7E1tk1oRwOGY0yaKfeUsQ== X-IronPort-AV: E=McAfee;i="6700,10204,11338"; a="38815609" X-IronPort-AV: E=Sophos;i="6.13,267,1732608000"; d="scan'208";a="38815609" Received: from fmviesa004.fm.intel.com ([10.60.135.144]) by fmvoesa112.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Feb 2025 07:18:30 -0800 X-CSE-ConnectionGUID: nkcY8q7tR4alKYqFWIgdIQ== X-CSE-MsgGUID: SofFb9WSQj6+qh13Aet7QA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.13,267,1732608000"; d="scan'208";a="116599312" Received: from black.fi.intel.com ([10.237.72.28]) by fmviesa004.fm.intel.com with ESMTP; 07 Feb 2025 07:18:28 -0800 Received: by black.fi.intel.com (Postfix, from userid 1003) id A286F428; Fri, 07 Feb 2025 17:18:26 +0200 (EET) From: Andy Shevchenko To: Andy Shevchenko , linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Cc: Linus Walleij , Bartosz Golaszewski , Kees Cook , "Gustavo A. R. Silva" Subject: [PATCH v2 3/7] gpio: 74x164: Annotate buffer with __counted_by() Date: Fri, 7 Feb 2025 17:17:10 +0200 Message-ID: <20250207151825.2122419-4-andriy.shevchenko@linux.intel.com> X-Mailer: git-send-email 2.43.0.rc1.1336.g36b5255a03ac In-Reply-To: <20250207151825.2122419-1-andriy.shevchenko@linux.intel.com> References: <20250207151825.2122419-1-andriy.shevchenko@linux.intel.com> Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Add the __counted_by() compiler attribute to the flexible array member volumes to improve access bounds-checking via CONFIG_UBSAN_BOUNDS and CONFIG_FORTIFY_SOURCE. Use struct_size() instead of manually calculating the number of bytes to allocate the private structure with a buffer. Reviewed-by: Gustavo A. R. Silva Signed-off-by: Andy Shevchenko --- drivers/gpio/gpio-74x164.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/drivers/gpio/gpio-74x164.c b/drivers/gpio/gpio-74x164.c index 70c662bbca7b..7844f8a58834 100644 --- a/drivers/gpio/gpio-74x164.c +++ b/drivers/gpio/gpio-74x164.c @@ -30,7 +30,7 @@ struct gen_74x164_chip { * register at the end of the transfer. So, to have a logical * numbering, store the bytes in reverse order. */ - u8 buffer[]; + u8 buffer[] __counted_by(registers); }; static int __gen_74x164_write_config(struct gen_74x164_chip *chip) @@ -97,6 +97,7 @@ static int gen_74x164_direction_output(struct gpio_chip *gc, static int gen_74x164_probe(struct spi_device *spi) { + struct device *dev = &spi->dev; struct gen_74x164_chip *chip; u32 nregs; int ret; @@ -116,10 +117,12 @@ static int gen_74x164_probe(struct spi_device *spi) return -EINVAL; } - chip = devm_kzalloc(&spi->dev, sizeof(*chip) + nregs, GFP_KERNEL); + chip = devm_kzalloc(dev, struct_size(chip, buffer, nregs), GFP_KERNEL); if (!chip) return -ENOMEM; + chip->registers = nregs; + chip->gpiod_oe = devm_gpiod_get_optional(&spi->dev, "enable", GPIOD_OUT_LOW); if (IS_ERR(chip->gpiod_oe)) @@ -133,10 +136,7 @@ static int gen_74x164_probe(struct spi_device *spi) chip->gpio_chip.set = gen_74x164_set_value; chip->gpio_chip.set_multiple = gen_74x164_set_multiple; chip->gpio_chip.base = -1; - - chip->registers = nregs; chip->gpio_chip.ngpio = GEN_74X164_NUMBER_GPIOS * chip->registers; - chip->gpio_chip.can_sleep = true; chip->gpio_chip.parent = &spi->dev; chip->gpio_chip.owner = THIS_MODULE; From patchwork Fri Feb 7 15:17:11 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Shevchenko X-Patchwork-Id: 863397 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.18]) (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 C4E0E1EB19C; Fri, 7 Feb 2025 15:18:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738941514; cv=none; b=BFA2DfQfFQDRiMRMvRrDLFr5wduqpTWIXu/hlC+VktbKTEol/ucW6sVVM7N4iqdQ8XrT+ZW/kTagIlmIMI5sOk8ZkqU78nQ0tcuwzfFG+OuST8LLhWqZ+3HAab0uiIByiyNJb6Kryc7/s2cTtOJG8mukLH96xmzocTej5AhC9ic= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738941514; c=relaxed/simple; bh=ENGCXMU4WTWaSHW6BE0m953OZ1GFz2d8DBXIDnt7ISw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=NNTeQjJ3kZkikLTdi/84oGsaB4JWVRhlrIl5IsL2Q1HkYqD3MBnEv+32Uu4x1+nAB32NsdzaPhbvk3B6OcjPQl8TVEh4CpTNwUiWlPVUXSeuvIj4TiIZsLzSYtupVvfPatwagrqhlnCfEE9JVl5Z6ckjpv4rAyn5JynE2wCqSSw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=kqMjOydc; arc=none smtp.client-ip=192.198.163.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="kqMjOydc" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1738941513; x=1770477513; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ENGCXMU4WTWaSHW6BE0m953OZ1GFz2d8DBXIDnt7ISw=; b=kqMjOydcMjMQlCfi6pmPhU9yorYx8ZdHS5p5NFyiLpW5c+Q9QleNFMMS jYcln41GWEDXGq651UMJxzZUL//Q/rWVY3TflT06UkYGVDWn2QSzFsOii 9HdkeXrAULW1U6eHrNtJaN0wsFQgAFB8cF5mao50s4GBXJ3/Jx8vmiNiu +HIne9WuRwfTBPwcwERYEGpbmnl3h6pD1qIqlF4Fr4mjA5dRuvmw6NqeR 6RgQECA1yA3tMZ0m8LGTeWqA+fooAW2Q5VVJbqoWnYTEM4kPxGuDSNzhD qcfHeKUkffVONmgN9WXiL6eIKHEs2S5N9iZM+bwW/u/E88LsTinM9urao w==; X-CSE-ConnectionGUID: fYf7hs+MQ3S8GlzqJVgwaA== X-CSE-MsgGUID: j6CBdRIoQteIhTASPF5I7w== X-IronPort-AV: E=McAfee;i="6700,10204,11338"; a="38815615" X-IronPort-AV: E=Sophos;i="6.13,267,1732608000"; d="scan'208";a="38815615" Received: from fmviesa004.fm.intel.com ([10.60.135.144]) by fmvoesa112.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Feb 2025 07:18:30 -0800 X-CSE-ConnectionGUID: vlwryk4wSr65t8RLyzDhIw== X-CSE-MsgGUID: lGAcB5d9RHqi8L4OxamRHg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.13,267,1732608000"; d="scan'208";a="116599314" Received: from black.fi.intel.com ([10.237.72.28]) by fmviesa004.fm.intel.com with ESMTP; 07 Feb 2025 07:18:28 -0800 Received: by black.fi.intel.com (Postfix, from userid 1003) id A85AD322; Fri, 07 Feb 2025 17:18:26 +0200 (EET) From: Andy Shevchenko To: Andy Shevchenko , linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Cc: Linus Walleij , Bartosz Golaszewski , Kees Cook , "Gustavo A. R. Silva" Subject: [PATCH v2 4/7] gpio: 74x164: Make use of the macros from bits.h Date: Fri, 7 Feb 2025 17:17:11 +0200 Message-ID: <20250207151825.2122419-5-andriy.shevchenko@linux.intel.com> X-Mailer: git-send-email 2.43.0.rc1.1336.g36b5255a03ac In-Reply-To: <20250207151825.2122419-1-andriy.shevchenko@linux.intel.com> References: <20250207151825.2122419-1-andriy.shevchenko@linux.intel.com> Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Make use of BIT() and GENMASK() where it makes sense. Signed-off-by: Andy Shevchenko --- drivers/gpio/gpio-74x164.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/drivers/gpio/gpio-74x164.c b/drivers/gpio/gpio-74x164.c index 7844f8a58834..0f720d539fa7 100644 --- a/drivers/gpio/gpio-74x164.c +++ b/drivers/gpio/gpio-74x164.c @@ -47,7 +47,7 @@ static int gen_74x164_get_value(struct gpio_chip *gc, unsigned offset) guard(mutex)(&chip->lock); - return (chip->buffer[bank] >> pin) & 0x1; + return !!(chip->buffer[bank] & BIT(pin)); } static void gen_74x164_set_value(struct gpio_chip *gc, @@ -60,9 +60,9 @@ static void gen_74x164_set_value(struct gpio_chip *gc, guard(mutex)(&chip->lock); if (val) - chip->buffer[bank] |= (1 << pin); + chip->buffer[bank] |= BIT(pin); else - chip->buffer[bank] &= ~(1 << pin); + chip->buffer[bank] &= ~BIT(pin); __gen_74x164_write_config(chip); } From patchwork Fri Feb 7 15:17:12 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Shevchenko X-Patchwork-Id: 863395 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.18]) (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 63BC61F37C6; Fri, 7 Feb 2025 15:18:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738941516; cv=none; b=a/7Vk1/OR2TQ0N7aVzrtpwd0Ohj/bRlolLHu7nSvP3Q6dNsLi6hi4gSVeWlDADmAC5XMYnUkqTeZlwIQiS1vjtXlFCuJkONORim+ccf8rujAH6mACYZkOFg5UjwPxGsOls+v0WlOFw1A/nEfapqYqH1F6OTBUGPmAY/1L2GYQMc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738941516; c=relaxed/simple; bh=RpIKikbcDWkM5Hzt3hYTUmGzsoMxr9PAEMTrIc2EYnU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=hY8/+vvuSUcJSaqaex7TZW1tUehAMS2rmcwuXaTbDC41ncf2Aq5dlziqhPrKEYdbODjkEFHc5GV6kGoj1zkC2TJCxUDegIdIg5xxOgFvxL/jGmHWh6yRm+1lIyZr6U8j8FuQFU7UFIBeDWJZOzDhU+nEHHO0zBSm/n0FAN5rroY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=W+FdVtAb; arc=none smtp.client-ip=192.198.163.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="W+FdVtAb" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1738941515; x=1770477515; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=RpIKikbcDWkM5Hzt3hYTUmGzsoMxr9PAEMTrIc2EYnU=; b=W+FdVtAbalaTEOR6VIttCkrFgW++fUZxxD9afYxoNYwaVa3sijgiOU2l dIk8wKVJwYdHonCkyd51i95VosPNDV1oEIsMbWNCPbF6cZK/RpgNJCRR3 4s/Grig1rguqOFXqRb3dmNcmsR/HLLoXOjO1Nq2t+Cy6QM9QhAKWO8a6x ee957hbLwB7IOBINaFxUDbC+eyEnklUjU+QxACi1wBvZ+zHzximYgzoCO V9T88ZoKYUQaXOBpRA2x4d0fGgbFI85JQmOCZG4mbbjclD53FFwWrnm0h T4vgIdI5PqrCSNU1aLVRjnIIOwuINaVmJN/hTlGxKBCxy4uuE4xEXWItg A==; X-CSE-ConnectionGUID: qjSxxWLyQ9qK3g36kvPWrg== X-CSE-MsgGUID: GbxcWY9LSoWCFZFmo8Yg0g== X-IronPort-AV: E=McAfee;i="6700,10204,11338"; a="38815627" X-IronPort-AV: E=Sophos;i="6.13,267,1732608000"; d="scan'208";a="38815627" Received: from fmviesa004.fm.intel.com ([10.60.135.144]) by fmvoesa112.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Feb 2025 07:18:33 -0800 X-CSE-ConnectionGUID: 2IVECy69Q06iB9ObmdsC9A== X-CSE-MsgGUID: 5HKF5HQ7S12bcVbnP0DVXQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.13,267,1732608000"; d="scan'208";a="116599318" Received: from black.fi.intel.com ([10.237.72.28]) by fmviesa004.fm.intel.com with ESMTP; 07 Feb 2025 07:18:31 -0800 Received: by black.fi.intel.com (Postfix, from userid 1003) id BD382498; Fri, 07 Feb 2025 17:18:26 +0200 (EET) From: Andy Shevchenko To: Andy Shevchenko , linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Cc: Linus Walleij , Bartosz Golaszewski , Kees Cook , "Gustavo A. R. Silva" Subject: [PATCH v2 5/7] gpio: 74x164: Fully convert to use managed resources Date: Fri, 7 Feb 2025 17:17:12 +0200 Message-ID: <20250207151825.2122419-6-andriy.shevchenko@linux.intel.com> X-Mailer: git-send-email 2.43.0.rc1.1336.g36b5255a03ac In-Reply-To: <20250207151825.2122419-1-andriy.shevchenko@linux.intel.com> References: <20250207151825.2122419-1-andriy.shevchenko@linux.intel.com> Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Convert the driver probe stage to use managed resources. Signed-off-by: Andy Shevchenko --- drivers/gpio/gpio-74x164.c | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/drivers/gpio/gpio-74x164.c b/drivers/gpio/gpio-74x164.c index 0f720d539fa7..ecd691de8539 100644 --- a/drivers/gpio/gpio-74x164.c +++ b/drivers/gpio/gpio-74x164.c @@ -95,6 +95,19 @@ static int gen_74x164_direction_output(struct gpio_chip *gc, return 0; } +static void gen_74x164_deactivate(void *data) +{ + struct gen_74x164_chip *chip = data; + + gpiod_set_value_cansleep(chip->gpiod_oe, 0); +} + +static int gen_74x164_activate(struct device *dev, struct gen_74x164_chip *chip) +{ + gpiod_set_value_cansleep(chip->gpiod_oe, 1); + return devm_add_action_or_reset(dev, gen_74x164_deactivate, chip); +} + static int gen_74x164_probe(struct spi_device *spi) { struct device *dev = &spi->dev; @@ -128,8 +141,6 @@ static int gen_74x164_probe(struct spi_device *spi) if (IS_ERR(chip->gpiod_oe)) return PTR_ERR(chip->gpiod_oe); - spi_set_drvdata(spi, chip); - chip->gpio_chip.label = spi->modalias; chip->gpio_chip.direction_output = gen_74x164_direction_output; chip->gpio_chip.get = gen_74x164_get_value; @@ -149,18 +160,13 @@ static int gen_74x164_probe(struct spi_device *spi) if (ret) return dev_err_probe(&spi->dev, ret, "Config write failed\n"); - gpiod_set_value_cansleep(chip->gpiod_oe, 1); + ret = gen_74x164_activate(dev, chip); + if (ret) + return ret; return devm_gpiochip_add_data(&spi->dev, &chip->gpio_chip, chip); } -static void gen_74x164_remove(struct spi_device *spi) -{ - struct gen_74x164_chip *chip = spi_get_drvdata(spi); - - gpiod_set_value_cansleep(chip->gpiod_oe, 0); -} - static const struct spi_device_id gen_74x164_spi_ids[] = { { .name = "74hc595" }, { .name = "74lvc594" }, @@ -181,7 +187,6 @@ static struct spi_driver gen_74x164_driver = { .of_match_table = gen_74x164_dt_ids, }, .probe = gen_74x164_probe, - .remove = gen_74x164_remove, .id_table = gen_74x164_spi_ids, }; module_spi_driver(gen_74x164_driver);