From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-qt1-f180.google.com (mail-qt1-f180.google.com [209.85.160.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 B4FE23DA7CA for ; Tue, 4 Aug 2026 23:44:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.180 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785887047; cv=none; b=NC2X4jREZ6MNrTWXAOjaAq2byGkYFL099wWpbZJik+Gc47sWv0WgZaee7FyRE+hinMPBksZ25BaJpfODcOLApQ9bflwn+wRQWQRQqWQHIgz2PmkOP3hfzousutsRPUCOluGvrI5pH3cPreCDJTyCSBMJYqqUJvWaUY+s+1GCCLM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785887047; c=relaxed/simple; bh=DtTbt5i+D2OCvx/aRI5XRtXiLSS69WHUcjWzWwTq7qQ=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=LzN2hczGnUj0hdXIIy92Ggc6uP7rd8HzB6ej8+KdMYLYp0o1mJWq5Ht5k7hTDgh3xAR2q0rx+FYSYBbjbqWSiz4L1AbhK4ueSTunBIU9wU+iU9JV8zfSxb9Fz62u0mkhphmnquMoN62U4AqqV5bWfdXXKs5hIeSeA8aPrZBn9kI= 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=K92nFdT1; arc=none smtp.client-ip=209.85.160.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="K92nFdT1" Received: by mail-qt1-f180.google.com with SMTP id d75a77b69052e-527e352a167so2348531cf.1 for ; Tue, 04 Aug 2026 16:44:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1785887043; x=1786491843; darn=lists.linux.dev; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=wNEml4qcgpU6hriWeNPKlpjEWwzAoG1Z6BmVURttkRY=; b=K92nFdT1FjVSodnV1ZFkyfCaEwPL9o9OhAiNLP3pW2KhZa+BuQjrTPtFT/mPr3aQCB 2l0EK/U4vlUPQfTJdOIuj7mwvJ43xLRBPki2eMOVYZsDUW1rg7CO/gUdeFcbdKdoAbh9 J8QgPs/9xJOgu2X9I53mJLZV8ddd5k8X4KIS1MJBTRfp/r7Uf3HVlgDx7/OoPO6hsYPG RT591lYu4smNL5OvjlEzluzk7chvDLNqRXIg7aigu+cBLWkXR/VlV3orpl2/8JK6x36g /pc0ZL5pvJFxXS6yGl/bwv2T7BpzCLs4TlJ2yrqYegV4lVDfcjiO1DYle9OpCuVTtSKO nqmw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785887043; x=1786491843; 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:content-type; bh=wNEml4qcgpU6hriWeNPKlpjEWwzAoG1Z6BmVURttkRY=; b=SVipOcSTIiPYYqsReIBVsgXJlZt3+t3PYTjqwctJmyp0XNCJS1PeC6XpJLzCwk0Plv 4etjqX4kP5TezxHA2Hex7AuXe374Jwnl+9W0ijyBuYTVjmVA0bNbGHh+KMHMN7iDppK3 +tyiUl1A3G1GJqUTXdoYU7p/7SNke9MZTJQ7AsoL7sHH1ggId+iVPxdZ2rv2My5XdzKe AQsyH9Enwn4AbNocj4M46wuN7XJ9mw2/+nJv7REg/WgBWsu39WgZrZ256WKeGBVvaPBY A13ul/y+uDgnmNjlsGGvSdu6ncwyT4ZS6NLPawb1iVxIywi9IPBlFvHLpeFZWPmQ7kIU o+5Q== X-Forwarded-Encrypted: i=1; AHgh+RorSF6fPCaULbUZjRCwpMactQMATA4wX//YKvPOTSvJpQyE9wh/0enpMDaCWhQyo8JdeSNNJEzh2DtAsOrjfvFwgwMjbw==@lists.linux.dev X-Gm-Message-State: AOJu0YzIKQJieXndBed/OCzP2j4mb6wAWDNm7gQeDhH+qCi5wSU1cxOh qzdy6EHrxuV4tnKTDh0ZSlip1X5IANrgJYcy1xjlRkkkKYXfC9o+PDaO X-Gm-Gg: AR+sD11fGlA8lTyhcQ0Hsx1qUq4t6bP4Gj/b1Kj4Wg5PH4ohKQwFtY5G4kNV6avFXxd a3IurzyJfyxbB79ZHTt1y6L6BTVdsgRtdGB+u9oNtbkfgTRIWqFKRQn3gh+lBn78jfUegmbd/fV GVfGvb1oZnqHwOxdmBbvWKnDv1RjTEF1unpOYU9CqrHjP5oSi6iysTPO6O/gc/SHmA2biZt4nCY xXv7SC4ORijFaNS48CEJz9acBRoK9JJn9J51Xhd+k6q4HM/ZjGL+TpgnDfaCbgCgr0uvsNSyUXy 6ktB5y8YjrwFJSm3x0vc7wxRV0W4AGuv25in0z5duSZJFfonjIJ2D1P1Hljzm0BGd8Nsond/cGq oH/8xIQe3gMw9lCmGtIsqlUNCz8PkNisL8YnIRVYqO6ekxleBBO7GyFrkafVBMW0/NgquzCBqoq ZGd77dJR8DilrNO+7JyuXQ6qVGp4QwZlQ1J0QWjPWraXhYm9sNr3t1X/tdpTYMqrQ2TUw= X-Received: by 2002:ac8:5acc:0:b0:51c:61a:50e6 with SMTP id d75a77b69052e-52ce5f41d0dmr29604361cf.3.1785887043478; Tue, 04 Aug 2026 16:44:03 -0700 (PDT) Received: from localhost ([2600:4808:5693:8201:bb19:d9f0:6e53:91ed]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-9087ffe09f0sm13298226d6.22.2026.08.04.16.44.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Aug 2026 16:44:02 -0700 (PDT) From: Marco Chen To: jic23@kernel.org Cc: dlechner@baylibre.com, nuno.sa@analog.com, andy@kernel.org, pmeerw@pmeerw.net, matt@ranostay.sg, linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, skhan@linuxfoundation.org, linux-kernel-mentees@lists.linux.dev Subject: [PATCH v2] iio: health: max30102: fix NULL dereference in interrupt handler Date: Tue, 4 Aug 2026 19:43:55 -0400 Message-ID: <20260804234355.65319-1-marcochen.dev@gmail.com> X-Mailer: git-send-email 2.55.0 Precedence: bulk X-Mailing-List: linux-kernel-mentees@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit The interrupt is requested in max30102_probe() and stays enabled for the lifetime of the device, but indio_dev->active_scan_mask is only valid while a buffer is enabled. When an interrupt arrives while no buffer is enabled, the handler dereferences the NULL active_scan_mask: Unable to handle kernel NULL pointer dereference at virtual address 0000000000000000 pc : __bitmap_weight+0x64/0x98 lr : max30102_interrupt_handler+0x48/0x160 [max30102] Call trace: __bitmap_weight+0x64/0x98 (P) max30102_interrupt_handler+0x48/0x160 [max30102] irq_thread_fn+0x28/0xa8 irq_thread+0x184/0x30c kthread+0x118/0x124 ret_from_fork+0x10/0x20 Read the interrupt status register at the top of the handler. If FIFO_RDY is not set, return early because it isn't our interrupt. Because FIFO_RDY is the only interrupt source enabled by this driver in max30102_chip_init(), an invocation of max30102_interrupt_handler() without FIFO_RDY set carries no data to read and can return early before ever accessing active_scan_mask. This status read also deasserts the MAX30102's active-low interrupt pin. Since the interrupt status register is now read at the top of the handler, pass the status value into max30102_fifo_count() instead of having it read the register a second time. Fixes: 90579b69e94b ("iio: health: max30102: Add MAX30105 support") Signed-off-by: Marco Chen --- Changes in v2: - Check FIFO_RDY bit in the interrupt status register instead of active_scan_mask, because that was racy in v1, as pointed out by Jonathan and David. - Read interrupt status once and pass it into max30102_fifo_count(). - Check the regmap_read() return value and log on failure. Tested with a MAX30102 on a Raspberry Pi 4 over I2C. v1: https://lore.kernel.org/linux-iio/20260731184124.112124-1-marcochen.dev@gmail.com/ drivers/iio/health/max30102.c | 37 +++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/drivers/iio/health/max30102.c b/drivers/iio/health/max30102.c index c37316c86f14..b949ce36c248 100644 --- a/drivers/iio/health/max30102.c +++ b/drivers/iio/health/max30102.c @@ -235,17 +235,10 @@ static const struct iio_buffer_setup_ops max30102_buffer_setup_ops = { .predisable = max30102_buffer_predisable, }; -static inline int max30102_fifo_count(struct max30102_data *data) +static inline int max30102_fifo_count(unsigned int status) { - unsigned int val; - int ret; - - ret = regmap_read(data->regmap, MAX30102_REG_INT_STATUS, &val); - if (ret) - return ret; - /* FIFO has one sample slot left */ - if (val & MAX30102_REG_INT_STATUS_FIFO_RDY) + if (status & MAX30102_REG_INT_STATUS_FIFO_RDY) return 1; return 0; @@ -290,19 +283,39 @@ static irqreturn_t max30102_interrupt_handler(int irq, void *private) { struct iio_dev *indio_dev = private; struct max30102_data *data = iio_priv(indio_dev); - unsigned int measurements = bitmap_weight(indio_dev->active_scan_mask, - iio_get_masklength(indio_dev)); + unsigned int measurements, status; int ret, cnt = 0; + ret = regmap_read(data->regmap, MAX30102_REG_INT_STATUS, &status); + if (ret) { + dev_err_ratelimited(&data->client->dev, + "Failed to read IRQ status: %d\n", ret); + return IRQ_HANDLED; + } + if (!(status & MAX30102_REG_INT_STATUS_FIFO_RDY)) + return IRQ_HANDLED; + + measurements = bitmap_weight(indio_dev->active_scan_mask, + iio_get_masklength(indio_dev)); + mutex_lock(&data->lock); - while (cnt || (cnt = max30102_fifo_count(data)) > 0) { + while (cnt || (cnt = max30102_fifo_count(status)) > 0) { ret = max30102_read_measurement(data, measurements); if (ret) break; iio_push_to_buffers(data->indio_dev, data->processed_buffer); cnt--; + + ret = regmap_read(data->regmap, MAX30102_REG_INT_STATUS, + &status); + if (ret) { + dev_err_ratelimited(&data->client->dev, + "Failed to read IRQ status: %d\n", + ret); + break; + } } mutex_unlock(&data->lock); -- 2.55.0