From patchwork Wed May 29 13:19:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 800345 Received: from mail-pf1-f180.google.com (mail-pf1-f180.google.com [209.85.210.180]) (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 4A4F213ADA; Wed, 29 May 2024 13:20:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716988843; cv=none; b=hcUCIshzk4Qu4cZOVyLME9sBmnDF5qblXaeTYgM/hlgtQkfCIFp3W9s32timGmVj9925rVxfo54bwz7kNYD0/suqqoyBNVhfKK3ORDZCozko7AkfOkqkrT/W/RmuLeia8m54+gtsp7wd3pp0QXdOpOW0FIWqM/unHRIMk6+BSVw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716988843; c=relaxed/simple; bh=N6T5FDrbz5fe4Yp9861iorHBz9ZBlTICU/yU1f1AqKI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Yv6UK7prnXVbU+qKvB7vW+fd1S0BPEgzheZ1SbLoNd6PgQfucXhlZeOCAaA4vszwE5Qo8eWSgW3iwV7/2hLDbgz1Q3lwCXLb+qJDcvQVS5i6bUPh8IE0TWJpnrgOA5Lz8XePn+tOeENcB/LjnMsyp5FdHf/bwJyfDJJ5k/4hV0w= 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=IIO25tVo; arc=none smtp.client-ip=209.85.210.180 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="IIO25tVo" Received: by mail-pf1-f180.google.com with SMTP id d2e1a72fcca58-701b0b0be38so1688071b3a.0; Wed, 29 May 2024 06:20:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716988841; x=1717593641; 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=IIO25tVoIo2a0xpU7fMUteWMHf562iovnqYmPo3jBdolcE2wFtDgkKORJ7Y9eXMQ7N NafTCx44xO4Pq391w3WNcgDGTE7daMUGXx9vk5ZZ+nmdt2JBMA1d0BGwR/I2lZ/q4PQ5 JuwGO1WZPgkm5sgHGVxASpTI6KV5hbF/DUpRsAAbqRvKbLKqkAITHRTOb3W4nX2Um/Nj zIxiNSk3lNv2iGFZXCY+i7Sr7rofrDggurjZQsAoPqZJh5IYMZybeqfhfudxBAhmnaxx MPSnaSaDr2oHXlsR+K+ZZHU3KKUgZpvYRrhCHD/0jbfVB9dak304itJRIMe8wN/NTppz u1UQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716988841; x=1717593641; 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=lhsdOxMMZjFKak3VZshJ4f67nicjffRNLnyCbjb0WaNUwBOVY7yQ79KMgROob8rWdv vM22iUdIEGptpQHyqFWLoKIbrUvSDWIP7gR/H7zWk/EbxFPe6zN2Jk/S3qM7HO9vkQ2c 2dOhT/xVyvh22gvNtVj13ztqY9+ld3mNcTeH7gZ/lnA3qJECIZlZat2BpRe3T+guoNgL WNl3OIYvtMF5uCkN0lxQRVVr5LIHhII3U0aIrlwMvxuoOl+eRdgEkj5+7sXEb1Saf8pc YJg1z6l48fXlq3pzaqzbVBaG1tYiXo9/dbMRA5KN0qUGf2+0rM876rQUhE5ZsBmYVI/Q 2CrQ== X-Forwarded-Encrypted: i=1; AJvYcCVLn+L3/dt0c5pl8eGAlLIJnhMtuTAEDSFvkvxqbd2Y0KhKf9BwRMdfbu9Mn/zwKKQlKWXq/MrVtx2KSDsgjNm1c4W0TKSp/BmlXw== X-Gm-Message-State: AOJu0Yy1SSkrHdx/9DhOVwCP/SkE+cWmyVClkRMHgGVaiLyqjTv9Q8Vu 2LvgyXHs91n6FJ0efQBkYgV4R3OZjm4Rn6I0tH8SbYzofZR93K3bak8SSA== X-Google-Smtp-Source: AGHT+IG7EEIFxhlMzFp785J0mllLtqrCV11rtj9I40DEOMCoddO1MKjYBD69W9ajszfw2I98boCVwg== X-Received: by 2002:a05:6a00:1d8f:b0:6f6:7a47:3686 with SMTP id d2e1a72fcca58-6f8f35dac57mr18388674b3a.9.1716988841424; Wed, 29 May 2024 06:20:41 -0700 (PDT) Received: from rigel.home.arpa (194-223-191-29.tpgi.com.au. [194.223.191.29]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-682227f1838sm9121844a12.46.2024.05.29.06.20.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 May 2024 06:20:41 -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 v2 1/3] gpiolib: cdev: Add INIT_KFIFO() for linereq events Date: Wed, 29 May 2024 21:19:51 +0800 Message-Id: <20240529131953.195777-2-warthog618@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240529131953.195777-1-warthog618@gmail.com> References: <20240529131953.195777-1-warthog618@gmail.com> Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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(+) -- 2.39.2 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, void __user *ip) mutex_init(&lr->config_mutex); init_waitqueue_head(&lr->wait); + INIT_KFIFO(lr->events); lr->event_buffer_size = ulr.event_buffer_size; if (lr->event_buffer_size == 0) lr->event_buffer_size = ulr.num_lines * 16; From patchwork Wed May 29 13:19:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 800060 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 94B1F17BAB; Wed, 29 May 2024 13:20:52 +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=1716988854; cv=none; b=nWyVox0vgybc9uZXLvZKCUnk/dTBRRzbFEW1aYq+boIaqX3InP3S5+BjSbXv5M+P6jhRu7CcypCmd+BiEsPbkNZ21SUqQw/Djt/A+zXicALxxPbAiVtEbFFIoLjOBu9Ppnsp/GO1K0Ouk5wmgf9X18rLd/aF+m7sruiFMXKB+1k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716988854; c=relaxed/simple; bh=aCFo+HiXciqO0WUakvM7h85kLLVZO69EHYlXJLQrl4s=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=lNJdKcQTrL8yRhS58xRBP4IX4bjlALukLCLKw/ukJ21vnWWFFjXxOLHLfu3J+pD6wqDbswm6cxOaoDuNLML7w7kNzVpEBZu5qbTHnP0h+o5E9Lmg4a46figU+7EoO+XLGOnttbP6srlSaxJeK6ZWShzMz0xT6tK9kUb0q3Z21KU= 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=NRxTGocS; 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="NRxTGocS" Received: by mail-pf1-f171.google.com with SMTP id d2e1a72fcca58-7022cabdc0aso14755b3a.1; Wed, 29 May 2024 06:20:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716988852; x=1717593652; 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=NRxTGocS9Gt0ClqGMzHHrEfSXoVW7xaVy4UjYVvLhODmLpseWwpvVby1IVuJN542fu mQ1Y9WnxD7InX9uJpwBRwGLVOJzpGY6XOptZlfVT0rHteeKRuDfrfi1RqrXvx/kNw1FT xf+W+zC/QVAM0huPxznguxmFgSJEtF1zNZCPlkRrMrDuAX7kHX9/0SVlrDwBHzNIYcv2 b2b7wUeDXgzt/LU6B4jdy/tNEYvRBV6KsuMXxRbgkUWa/xUulRNePEKLBRxM/5jAqpi9 OArJ9d2O0Sw2YR/YRhCRomskQGUy0wmko2o/oGWHuGk8YNySoNPEHiW14SrTPWkmW7NG wwpw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716988852; x=1717593652; 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=lwI/5FJaOekcrZqXk3kPya1MkQsmMBb1NMd2YARYr1sISwz3/7Cn8Xi0rxbfXlJ669 fARHaHcILelRvMzcgXeXIZlxMwnsLg1lIVb1c9mvirkRwy2E5hO0KRZQS8E5sYE9B9w2 IAGgX+kEyRDw1Tqj/DiJqIerdX4Oe7+/V7yNK6+ZXUQofboRkm9b3Pum9yhgjFXKgWiq KdT1dAUd/ZhBxUJG9SGEvON4blyhg8PgabZ8PNE2iaCTtnrNbfQV3iPBUpvdMkcyHvxr CnVlSA1eq+iMjDy7DoR2JQcWnq0QeqgdN5nUx7s/ctqGm7ahPd3aFOHiAQ7e4XzBP2Pe WfKw== X-Forwarded-Encrypted: i=1; AJvYcCXwtQt0u2XLzAjS4JEEmWlf+u1qUCm2JmDzzpNb9VsDpb8/2JDZqZSYuSMs6OdWxlfLkOTwmw8fVQgMD9Z3Bw5n5Aj4EnjEx742Zg== X-Gm-Message-State: AOJu0YwzxLNXBtWmijzCAH05xeN/eWb3ZlOXsx38cGKTCgNcTih+MEqU 85HDmgg2Oal6AbzDxm+P+m81L+jIHhwiazWIhY0w5AkYUeNJ0QO9MGpkOA== X-Google-Smtp-Source: AGHT+IFRcMRbDuD563FezoOQcQ257iLk4YBYxkwICvWox1goWIt7PQIHXeosx9gFT3I9YiV7UN0EWg== X-Received: by 2002:a05:6a21:78a2:b0:1af:9161:4048 with SMTP id adf61e73a8af0-1b259a091f3mr3491829637.1.1716988851757; Wed, 29 May 2024 06:20:51 -0700 (PDT) Received: from rigel.home.arpa (194-223-191-29.tpgi.com.au. [194.223.191.29]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-682227f1838sm9121844a12.46.2024.05.29.06.20.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 May 2024 06:20:51 -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 v2 2/3] gpiolib: cdev: Refactor allocation of linereq events kfifo Date: Wed, 29 May 2024 21:19:52 +0800 Message-Id: <20240529131953.195777-3-warthog618@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240529131953.195777-1-warthog618@gmail.com> References: <20240529131953.195777-1-warthog618@gmail.com> Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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(-) -- 2.39.2 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() */ } +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; eflags = edflags & GPIO_V2_LINE_EDGE_FLAGS; - if (eflags && !kfifo_initialized(&line->req->events)) { - ret = kfifo_alloc(&line->req->events, - line->req->event_buffer_size, GFP_KERNEL); + if (eflags) { + ret = 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 = READ_ONCE(line->edflags); unsigned int debounce_period_us = 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 = edflags & GPIO_V2_LINE_EDGE_FLAGS; - if (eflags && !kfifo_initialized(&line->req->events)) { - ret = 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; } From patchwork Wed May 29 13:19:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 800344 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) (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 8CBCF17C6C; Wed, 29 May 2024 13:21:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716988863; cv=none; b=fBee5TYM0lasuzRr/txFeHnCjoqdtlTAq2wC03weSsh1CGDPEVka68/KMMxP7WkJHYUSWaalpVflyvWP04NZx+y5Z1CqBSabf/e69cnCUbioIpjG5eoNvCoUoC25G56t1yGW80cFK8038pMCLBTz9/v3xV2uWGOWiRKfOPgWEuc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716988863; c=relaxed/simple; bh=zYMiCwE56OohnrxrdgwAfg+qSxvgxoW4HoLHgQdpU2I=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=T1m4CpXMvvrDLFu/LWepICN1Q5N+b8Qboiyxzyo0jmLZOQI+K0jyRcfn2HYqmt4baoEsD5htZeyOisHDvew4Ip9B4+YhboE0nti+GJshFpiOJe8EVoIRt8LnVOgISOC/R9974S9rvymnK+hxJK8rgDj0siO0CEyJCkaHL6+2k8w= 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=caYOEQv+; arc=none smtp.client-ip=209.85.210.179 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="caYOEQv+" Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-701b0b0be38so1688492b3a.0; Wed, 29 May 2024 06:21:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716988862; x=1717593662; 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=iqqCp8YkE8QN0J5Hn1yUPom3eC9D/QLg0KIhmBD/bc0=; b=caYOEQv+6cBdBsFpJz7zx3It35IlIUIy0zs78abmKcrc+J426gs8OBhsN+5raYa1O1 x1MxXwvewctzBBMwHBAxccypaF1G0ARTqxB0bQVlDp+liOwguywRmP49RB3BLgYDoE+I lM7a1a92nvHPPJgnq7RVKRL4ZJNLNntbi473eQNpOGSyeTnfXBKnm1nqs6N+Ycqc/Iqe mFwY9A38kdX5J6F/Ki27mDGzDUtRwDnFet1jj5gUejOs8Rcch5S8+UyhUamzTVwhHwVJ jBXEDyC2I/CvMXh9BB6xtN2EIfT0ESIQV7qdyxeh5Ua8vvY91KSAynRe0zEjNzPLcHIA ojfw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716988862; x=1717593662; 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=iqqCp8YkE8QN0J5Hn1yUPom3eC9D/QLg0KIhmBD/bc0=; b=avphEtlWwz+i9WN4v7ILkp1kzQe3JDga2jo9+Js9eCMUbeIDS9P/JOaPN3PYliliZN 3sYVcSZLwC8LzQ9tN83QGEiPYjZBFjYh94ZvYV1G8eCgiVZXZIjAi70xGOQizZvMZQzJ LgnAj8HO9heMnJmgW78fsPq1KpRnPiKZkO38cJ8I1AZkHrodvuiX9fQFypDlmB500e9i 1pz2zq0Kmdp1FLpou+lS0HnmW8q5QL3QmQMyI64QKoYvrs/b/zssQAfTR+ZiEZqIritM QUmjJ3ajAVdg133XdDhUdQgv0GunPSLty5ikCs0G/B5mvJ8M9g314BgfRr+fm0VWQ9Vg T9fw== X-Forwarded-Encrypted: i=1; AJvYcCWJ08TGqY7rosQx9gT5J0PlzJh9MaLHO2hGfR7w1g289b1uhgUM5SZf8R4rOd52328NRdvcSQFoJiJqeXBAg0MgV0/2M+8JpadGig== X-Gm-Message-State: AOJu0YxL/0/ZLyU5SzABUe7jAN6SoGfg7qCGEwaltYY9+Do+U6gBmFQd ClzfaawnKdqXB8fq4JwIaiKQGMmuTYUY9hb8YKvmAGuUNVfLMSUuKRLlNA== X-Google-Smtp-Source: AGHT+IGMGvvlVB+CMTQa0lnvTQ+546THlA+9C2/oQn2PkU84e8LaVTdfCCoVMARv9KR3JYNe8GY/Tw== X-Received: by 2002:a05:6a21:3a94:b0:1b2:1de2:7dfa with SMTP id adf61e73a8af0-1b21de27eb4mr17837121637.22.1716988861617; Wed, 29 May 2024 06:21:01 -0700 (PDT) Received: from rigel.home.arpa (194-223-191-29.tpgi.com.au. [194.223.191.29]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-682227f1838sm9121844a12.46.2024.05.29.06.20.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 May 2024 06:21:01 -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 v2 3/3] gpiolib: cdev: Cleanup kfifo_out() error handling Date: Wed, 29 May 2024 21:19:53 +0800 Message-Id: <20240529131953.195777-4-warthog618@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240529131953.195777-1-warthog618@gmail.com> References: <20240529131953.195777-1-warthog618@gmail.com> Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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. Log a warning and 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 | 53 +++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 26 deletions(-) -- 2.39.2 diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index c7218c9f2c5e..1cb952daacfb 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -1642,16 +1642,15 @@ static ssize_t linereq_read(struct file *file, char __user *buf, return ret; } - ret = kfifo_out(&lr->events, &le, 1); - } - if (ret != 1) { - /* - * This should never happen - we were holding the - * lock from the moment we learned the fifo is no - * longer empty until now. - */ - ret = -EIO; - break; + if (kfifo_out(&lr->events, &le, 1) != 1) { + /* + * This should never happen - we hold the + * lock from the moment we learned the fifo + * is no longer empty until now. + */ + WARN(1, "failed to read from non-empty kfifo"); + return -EIO; + } } if (copy_to_user(buf + bytes_read, &le, sizeof(le))) @@ -1995,16 +1994,15 @@ static ssize_t lineevent_read(struct file *file, char __user *buf, return ret; } - ret = kfifo_out(&le->events, &ge, 1); - } - if (ret != 1) { - /* - * This should never happen - we were holding the lock - * from the moment we learned the fifo is no longer - * empty until now. - */ - ret = -EIO; - break; + if (kfifo_out(&le->events, &ge, 1) != 1) { + /* + * This should never happen - we hold the + * lock from the moment we learned the fifo + * is no longer empty until now. + */ + WARN(1, "failed to read from non-empty kfifo"); + return -EIO; + } } if (copy_to_user(buf + bytes_read, &ge, ge_size)) @@ -2707,12 +2705,15 @@ static ssize_t lineinfo_watch_read(struct file *file, char __user *buf, if (count < event_size) return -EINVAL; #endif - ret = kfifo_out(&cdev->events, &event, 1); - } - if (ret != 1) { - ret = -EIO; - break; - /* We should never get here. See lineevent_read(). */ + if (kfifo_out(&cdev->events, &event, 1) != 1) { + /* + * This should never happen - we hold the + * lock from the moment we learned the fifo + * is no longer empty until now. + */ + WARN(1, "failed to read from non-empty kfifo"); + return -EIO; + } } #ifdef CONFIG_GPIO_CDEV_V1