From nobody Fri Jul 10 12:56:19 2026 Received: from mail-pf1-f171.google.com (mail-pf1-f171.google.com [209.85.210.171]) (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 8899313AA31; Mon, 27 May 2024 11:54:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716810884; cv=none; b=MR09q4QFHeXdQWtm5OQsaNMK/nwW+/sV6iIwRMGWqFFE2Ulfe7mp0gugBnhPLV2F96Qn7Ta2MxQIvhf6DZXgq++NpAqvf6J9heSBEIsJ8FPs0YAUMnxAwzrMDeJAndIyzbDg17Vqz+4pus5GiqJc9Y4OfqgIRBwSfpajRCpAD4Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716810884; c=relaxed/simple; bh=UeKu1anSSx/heAnksOb9OY/e1ocsm3apk+xTukJJzeY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=iqvQ6FDTUlqeXCXao/uVT02gg15eHsBycI0Pw/UJJz5D/Qc0s+OsswRYE7vzH+34VtZcFTCY+/4DE5y4+y4xopHlmv0laq/NmNInGShpN/+tO0DPScRyl3iJqPZC9qAizHAgeA2I4Z+DycsU32piPT6bbQ5FjmZVOeQ99PKwT0A= 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=KG64d+DC; arc=none smtp.client-ip=209.85.210.171 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="KG64d+DC" Received: by mail-pf1-f171.google.com with SMTP id d2e1a72fcca58-6f8ec7e054dso1964658b3a.2; Mon, 27 May 2024 04:54:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716810883; x=1717415683; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=4jYNZGuufvcnAd1nYnzFJWOOAgslXCStrEQYQHkxTy0=; b=KG64d+DCxca9U6AdJWl9146yeeIsvLTJnahgEQY2G04c3Y7QmZ8WDGEPqJOFylDS+S 9ucQYJJ8CMWutZeiqw0Ni1g6y3a0dDxoZ3n9hsPq+iIE7fDPZQKCcgA/u2axdqP6oXcJ neyVlDoHFfS5iVhqo+TPql5RgQaWaiP9E8TQeH24i74gxhyx9cujspLdS9vxvQOjO2fK cAUNIbygRzHsdOGCC3ctemlxfzRgEdwCpzcqnxzL9C2k7NAwgIHt8s5stI3rPa+HF182 0yWYtCqCWDJZVGw9lfkXu+Koe/cOlfoGsRHbH+6n0CBVt8C+IfbgjNjsifG6KHE8l1+8 H7Lg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716810883; x=1717415683; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=4jYNZGuufvcnAd1nYnzFJWOOAgslXCStrEQYQHkxTy0=; b=B59Zr1SVtP9jKlmRls+rO0/R9LPViqlV289VsyaaRo9hBnYGpYiF2hMBLGnf0zAm0M j9ShPVbx0KYtliDtHx248Ari11oNcH8F1g/UrEdjYlfznyKAdZKzQOyIHkbtPXsFagAq s3GiFZwgkwh9HS8DEE2HooZMEviWZV8hPlBnt4vxonkGzb+D6T9/CZQ6omolyHFHSFQP IhfIw7HSiHB8Q/b6GPgOfwLzLHWOjFWgNTJWHxNcDA0CY6X/XGKeP9AkDkwywCf6Wk7m D2C/ODz9c/IJ7gflJBAlOBlrdYDpq2ksKdKFpuRh/Gd99XSRE4nmSrfymLjOkhdRTcye 5tAg== X-Forwarded-Encrypted: i=1; AJvYcCVxLu3BlTSlXMj68ZxHX9TszKNGBcbX3O0dX344n/KOHzODBOX81qy7RxN+ROqHdPdc6k3Ea45TeNXvFfZbeCTh6BvtJsCIIGCvGA== X-Gm-Message-State: AOJu0Yz7jvnjNlFkObU6tMGVJHxUiyljF6r3ln12gcDu68VMxl8UYkFK aU4WIK8Xjkn7+Hgsnp1X38iP2BsVOhIM1kKdXeJXAUXYeQ5RPvIGDy52EA== X-Google-Smtp-Source: AGHT+IFA14Kq8CMO1mVJAzbZbOFEgQFNvES93H+6KUiHExk9/vPw6dyfDPxBDNlv2Mgs4WrQgzyv9A== X-Received: by 2002:a05:6a00:27a9:b0:6ed:d8d2:5040 with SMTP id d2e1a72fcca58-6f8f3b38a8emr13144173b3a.21.1716810882608; Mon, 27 May 2024 04:54:42 -0700 (PDT) Received: from rigel.home.arpa (60-241-107-82.static.tpgi.com.au. [60.241.107.82]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-6f8fcbea8a9sm4749256b3a.139.2024.05.27.04.54.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 May 2024 04:54:42 -0700 (PDT) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, brgl@bgdev.pl, linus.walleij@linaro.org Cc: Kent Gibson Subject: [PATCH 1/3] gpiolib: cdev: Add INIT_KFIFO() for linereq events Date: Mon, 27 May 2024 19:54:17 +0800 Message-Id: <20240527115419.92606-2-warthog618@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240527115419.92606-1-warthog618@gmail.com> References: <20240527115419.92606-1-warthog618@gmail.com> 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" The initialisation of the linereq events kfifo relies on the struct being zeroed and a subsequent call to kfifo_alloc(). The call to kfifo_alloc() is deferred until edge detection is first enabled for the linereq. If the kfifo is inadvertently accessed before the call to kfifo_alloc(), as was the case in a recently discovered bug, it behaves as a FIFO of size 1 with an element size of 0, so writes and reads to the kfifo appear successful but copy no actual data. As a defensive measure, initialise the kfifo with INIT_KFIFO() when the events kfifo is constructed. This initialises the kfifo element size and zeroes its data pointer, so any inadvertant access prior to the kfifo_alloc() call will trigger an oops. Signed-off-by: Kent Gibson --- drivers/gpio/gpiolib-cdev.c | 1 + 1 file changed, 1 insertion(+) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index 9dad67ea2597..d4e47960cc98 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -1774,6 +1774,7 @@ static int linereq_create(struct gpio_device *gdev, v= oid __user *ip) =20 mutex_init(&lr->config_mutex); init_waitqueue_head(&lr->wait); + INIT_KFIFO(lr->events); lr->event_buffer_size =3D ulr.event_buffer_size; if (lr->event_buffer_size =3D=3D 0) lr->event_buffer_size =3D ulr.num_lines * 16; --=20 2.39.2 From nobody Fri Jul 10 12:56:19 2026 Received: from mail-pf1-f171.google.com (mail-pf1-f171.google.com [209.85.210.171]) (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 5FA5813AA31; Mon, 27 May 2024 11:55:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716810924; cv=none; b=JXemSbVODmGbdlI+f7oIvkn5forzGrZtuqMAIdyc9brNMavsDq0DxBguAmk/KM4Qk7XlqKda5ZtKOnogBFQOXIquRnE9jfIxpyt42wOxYWfex+XbPbCeseWMp6cPGQOFMNYita81g88GAid6dhp/4HxoMz3Uw3pv9oO0UiwozM0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716810924; c=relaxed/simple; bh=GvLaDcjKLfnwaJVSlLjzndx0IQJS/o8D81GqI6tIXLQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=EGEWGSMO4SsagR8z1Z9nrDBRxT+6WCW4WvlJT1os7pHdc0b6X5faj2z3Bg+nH/omtCv2JsSJ/xyPettpckyZATaxamHFFuR4fpWj4ytajygyBrQNfOe2unWTuU+c8Tfk1q4gfZW3su0JInvm47l0u0kqebjipjH0SfAuUQK21Rc= 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=nQYD9Ca2; arc=none smtp.client-ip=209.85.210.171 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="nQYD9Ca2" Received: by mail-pf1-f171.google.com with SMTP id d2e1a72fcca58-6f8e9878514so2419550b3a.1; Mon, 27 May 2024 04:55:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716810922; x=1717415722; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=4ZOr6a7LSObd/CX+qTtuisaH25RwcFQcUhPSvOG66+M=; b=nQYD9Ca2vc7/H0yYKpFTYvaUDEgOmbHpYJc3XWdXZSBuiBaUesYdMf8tbZ3poWDl4z 6HSRNjSkRMNDumT8FgJyBDKKcFP/k4V2DBJrltJom3FcnLmrtiOyCura5RfxeckwIFDV t623gjHRiBuWgv8PWPN3kMtDLLeDZ2TmGDpphaiHwhNbJg/w+1NrI6pzgR3Y+DAz0Qa2 69cpH2G+oMUabhyE/m8XqoXBwtgkdpO2xL9GdBjrLqy6Dxm7MrFeDE+z7KrOc6nzlTF4 KTW0LK6+ptPaoUi58w+CukPKM/o2c3nH0j31rs6GR/brXdl+xspiUcOFL4T+vE7qlo5E teJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716810922; x=1717415722; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=4ZOr6a7LSObd/CX+qTtuisaH25RwcFQcUhPSvOG66+M=; b=eQGX6nQ6OxMg6Fe/egCHUmBJX2e5W/MKR7F3a6QFo+ApUFYC9KU/ZSMRLs/uu1G5o0 ADAD+PhCP9ZE4OQk/FyN0iZgjZBzKTn0O15fEQG2zIjr9BmAd0QUtFnI73SNTp0Q2cNm oheuQYjjO8JFJQJkIVJEu0ySF2lh4d/ElEcqpNrGzghKGlSEJM/4i1onbmG/qBdAEwn0 ZMEbGNcBV5ZSrjH5KDSOk6fccGSgGv3sBdcHTeN/ZCpY4Z3nvGeECgn0aZlLfmqtan5Y pRgr0voF42bjD8xivK3CvFhRHeI2IYafHoeaALc/qF805TjtfhbHUdNX5/AVBe7UpDuz qQWg== X-Forwarded-Encrypted: i=1; AJvYcCWX4MldRGe1GU4vo1Bk9ZmX0xaqI0t3HUDdkW7z4NywLgx7nBB568wPPx11AIdh9QjNTVyfTeiSGJUJPoKcu5deo4AQV8+VybjJrA== X-Gm-Message-State: AOJu0YyjLPeLV637v1AYo0P5osLZkONdB7+JJPdeLAN6ygumeGhMAlSZ xWa88f69mlm/GJ2fEiiTG7OqEgn6ftxk3hTUe1Vp1LQfAZYajArBZxMc1A== X-Google-Smtp-Source: AGHT+IFCtb1fKdoC2nUkvjQkZOqrmQiriOB2vGJ3xHV3iK2oaGdJ5vBFsrCsG+8vTyfIg8LYlXdFCw== X-Received: by 2002:a05:6a00:1d8d:b0:6e5:43b5:953b with SMTP id d2e1a72fcca58-6f8f34bcd74mr9225270b3a.14.1716810922533; Mon, 27 May 2024 04:55:22 -0700 (PDT) Received: from rigel.home.arpa (60-241-107-82.static.tpgi.com.au. [60.241.107.82]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-6f8fcbea8a9sm4749256b3a.139.2024.05.27.04.55.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 May 2024 04:55:22 -0700 (PDT) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, brgl@bgdev.pl, linus.walleij@linaro.org Cc: Kent Gibson Subject: [PATCH 2/3] gpiolib: cdev: Refactor allocation of linereq events kfifo Date: Mon, 27 May 2024 19:54:18 +0800 Message-Id: <20240527115419.92606-3-warthog618@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240527115419.92606-1-warthog618@gmail.com> References: <20240527115419.92606-1-warthog618@gmail.com> 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" The allocation of the linereq events kfifo is performed in two separate places. Add a helper function to remove the duplication. Signed-off-by: Kent Gibson --- drivers/gpio/gpiolib-cdev.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index d4e47960cc98..c7218c9f2c5e 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -1128,6 +1128,14 @@ static void edge_detector_stop(struct line *line) /* do not change line->level - see comment in debounced_value() */ } =20 +static int edge_detector_fifo_init(struct linereq *req) +{ + if (kfifo_initialized(&req->events)) + return 0; + + return kfifo_alloc(&req->events, req->event_buffer_size, GFP_KERNEL); +} + static int edge_detector_setup(struct line *line, struct gpio_v2_line_config *lc, unsigned int line_idx, u64 edflags) @@ -1139,9 +1147,8 @@ static int edge_detector_setup(struct line *line, char *label; =20 eflags =3D edflags & GPIO_V2_LINE_EDGE_FLAGS; - if (eflags && !kfifo_initialized(&line->req->events)) { - ret =3D kfifo_alloc(&line->req->events, - line->req->event_buffer_size, GFP_KERNEL); + if (eflags) { + ret =3D edge_detector_fifo_init(line->req); if (ret) return ret; } @@ -1193,8 +1200,6 @@ static int edge_detector_update(struct line *line, struct gpio_v2_line_config *lc, unsigned int line_idx, u64 edflags) { - u64 eflags; - int ret; u64 active_edflags =3D READ_ONCE(line->edflags); unsigned int debounce_period_us =3D gpio_v2_line_config_debounce_period(lc, line_idx); @@ -1210,14 +1215,9 @@ static int edge_detector_update(struct line *line, * ensure event fifo is initialised if edge detection * is now enabled. */ - eflags =3D edflags & GPIO_V2_LINE_EDGE_FLAGS; - if (eflags && !kfifo_initialized(&line->req->events)) { - ret =3D kfifo_alloc(&line->req->events, - line->req->event_buffer_size, - GFP_KERNEL); - if (ret) - return ret; - } + if (edflags & GPIO_V2_LINE_EDGE_FLAGS) + return edge_detector_fifo_init(line->req); + return 0; } =20 --=20 2.39.2 From nobody Fri Jul 10 12:56:19 2026 Received: from mail-pf1-f173.google.com (mail-pf1-f173.google.com [209.85.210.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 5905A13AA31; Mon, 27 May 2024 11:55:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716810958; cv=none; b=Nl12LRGWhL40Hh/+Br2B/vrvbAKzPDe6/XqyT5YutsYc2kVZjX0vNtJMG2Z0VGJ7ThUnwY0/M57DI5X62sImE1JRxQvvljb6X+jxmRtjolxMlUBnrxiVWOb6YeoXDo2w7SxjBcT9/VolBFoekCIgZZtDHPdw4WGjFbllm9f48Bw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716810958; c=relaxed/simple; bh=XmO9cPon2zzzsUypK4aJv1Cb6tsJcKac4jB3cPU6A0w=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=oEw1husUm5VJOhn1G7h9vqycE0oqYbpRKbCapMpz/P57mgHSqnW3/eK5LHgPX8+IAlY81wvpbt8FNVs38DDzkKmHvfldYSvuIa0Kua+I/0jH98bqY9IGIzoo7TcJqOdYDcq9wVge9rp7yYO8Vp5V0SG3Lfwbe29f3sjg/EIGNTg= 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=l7GALYZP; arc=none smtp.client-ip=209.85.210.173 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="l7GALYZP" Received: by mail-pf1-f173.google.com with SMTP id d2e1a72fcca58-70109d34a16so795446b3a.2; Mon, 27 May 2024 04:55:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716810956; x=1717415756; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ZUX9Qc2CwQXtipTZ8HtlhuYljQCKpgoWARDk9odQFco=; b=l7GALYZP+OUhuHez1HW3VilpPi0IEi7Iw4MUFbWTlCXFXbKafHinjju8OL48MldMQf aVlzLg2bs8GdU/sQqn4C9BWWeMtBDiYSNAvtmIMfbJGSP7yjdiyO/h+mFdRKyvVSNa+p sGgWYCZUx8g203DXooncnMvmNhw2tXarBAqKtI8Cwv96KQV2oC2duLSi7NQa6SRbTs5m RmOEomb8suyYkdgEr3EUT3EGXqj8/yLSxVjmgzpObhw1y1BTnJmcLlItNraj2t3x9PxT WF+YvoeH+DHubZFspskoQCMSJG0pZoXyvGpfyLpfw0+MzYk3WVAzz60kCTiaMfp78B03 eJPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716810956; x=1717415756; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ZUX9Qc2CwQXtipTZ8HtlhuYljQCKpgoWARDk9odQFco=; b=GbusqBkir1s5ji/IUuDNgNRmN1QcbKLB1NQ+kXQn348t+YqeE3gp8g+91EIXHL9cwM 2G02K2iQ3oVrXzJdUSz/RlS9KTDqFYuN9KiEuItyaOM3PsXTKpMFlikJ3jSnlyKiFUmM xvc4m6eMGc5DerdnpiLMArL3T/vKcwM++AtzuF6yYWJbrUq2iu2UC5mCkmvJRT32/wI8 Liy/P5TfSMG3FlMbpZCzAmbiGpmZOWhTtDjnFOKKwibiPr9BfhVl2wHwjEQLZweuLIbR 1d6CzRT4eq/kHjKx8wnKCUQ0SqBliHFYtbxk0ohhmVvEGQXytKCfiMJkMZnpei0O7asr ffLQ== X-Forwarded-Encrypted: i=1; AJvYcCVFfFPxmkHWK0Vc6GU9Yhse1HXlsBNgTfKcSAFfRxr5Pq/w/VExUCVPbSUqBcUymTCWL4XWdvmjGRbgkv7P/zSmpF5kiIt1TZ8DaA== X-Gm-Message-State: AOJu0YxzjhTyzVRMyqzMQXpNQv+nvWJ35wnRfuAUYsMuRDPAVJkz52aX 5tOlrTMze7JdrEBvvB0T9KoM5MKky1K79akUcOdjK4TdZWuPwrcQmtwHxA== X-Google-Smtp-Source: AGHT+IGZUTwDxxXl3RLQDctVHB+qoPi1AFwLPqsOY6ZQkg0fTEVpKIgWAqbW7ykw4YpzLa3JbLh5TQ== X-Received: by 2002:a05:6a20:8409:b0:1af:b0b6:a35f with SMTP id adf61e73a8af0-1b212ccf7d3mr11944205637.2.1716810956234; Mon, 27 May 2024 04:55:56 -0700 (PDT) Received: from rigel.home.arpa (60-241-107-82.static.tpgi.com.au. [60.241.107.82]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-6f8fcbea8a9sm4749256b3a.139.2024.05.27.04.55.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 May 2024 04:55:56 -0700 (PDT) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, brgl@bgdev.pl, linus.walleij@linaro.org Cc: Kent Gibson Subject: [PATCH 3/3] gpiolib: cdev: Cleanup kfifo_out() error handling Date: Mon, 27 May 2024 19:54:19 +0800 Message-Id: <20240527115419.92606-4-warthog618@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240527115419.92606-1-warthog618@gmail.com> References: <20240527115419.92606-1-warthog618@gmail.com> 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" The handling of kfifo_out() errors in read functions obscures any error. The error condition should never occur but, while a ret is set to -EIO, it is subsequently ignored and the read functions instead return the number of bytes copied to that point, potentially masking the fact that any error occurred. Return -EIO in the case of a kfifo_out() error to make it clear something very odd is going on here. Signed-off-by: Kent Gibson --- drivers/gpio/gpiolib-cdev.c | 47 +++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 26 deletions(-) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index c7218c9f2c5e..6a986d7f1f2f 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -1642,16 +1642,13 @@ static ssize_t linereq_read(struct file *file, char= __user *buf, return ret; } =20 - ret =3D kfifo_out(&lr->events, &le, 1); - } - if (ret !=3D 1) { - /* - * This should never happen - we were holding the - * lock from the moment we learned the fifo is no - * longer empty until now. - */ - ret =3D -EIO; - break; + if (kfifo_out(&lr->events, &le, 1) !=3D 1) + /* + * This should never happen - we hold the + * lock from the moment we learned the fifo + * is no longer empty until now. + */ + return -EIO; } =20 if (copy_to_user(buf + bytes_read, &le, sizeof(le))) @@ -1995,16 +1992,13 @@ static ssize_t lineevent_read(struct file *file, ch= ar __user *buf, return ret; } =20 - ret =3D kfifo_out(&le->events, &ge, 1); - } - if (ret !=3D 1) { - /* - * This should never happen - we were holding the lock - * from the moment we learned the fifo is no longer - * empty until now. - */ - ret =3D -EIO; - break; + if (kfifo_out(&le->events, &ge, 1) !=3D 1) + /* + * This should never happen - we hold the + * lock from the moment we learned the fifo + * is no longer empty until now. + */ + return -EIO; } =20 if (copy_to_user(buf + bytes_read, &ge, ge_size)) @@ -2707,12 +2701,13 @@ static ssize_t lineinfo_watch_read(struct file *fil= e, char __user *buf, if (count < event_size) return -EINVAL; #endif - ret =3D kfifo_out(&cdev->events, &event, 1); - } - if (ret !=3D 1) { - ret =3D -EIO; - break; - /* We should never get here. See lineevent_read(). */ + if (kfifo_out(&cdev->events, &event, 1) !=3D 1) + /* + * This should never happen - we hold the + * lock from the moment we learned the fifo + * is no longer empty until now. + */ + return -EIO; } =20 #ifdef CONFIG_GPIO_CDEV_V1 --=20 2.39.2