From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f181.google.com (mail-pf1-f181.google.com [209.85.210.181]) (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 6363D38E5FE for ; Mon, 22 Jun 2026 08:07:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.181 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782115659; cv=none; b=RToUtpDJz/EgJUdudtHkw74nXG5K9NAd7PAkjVFi3OxRPBrQcr0pUuoWsXW+rqiw3Xdu49yy820/Z7c8JOhFYOaaTDJYjfxngUP2wIVrNHW9Vy5NbnIpM3Ovu+9pH0X8m6YpAE+eRpJWapcPEMWtJ002ue5S4I+plmfO17jKMhs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782115659; c=relaxed/simple; bh=ixZklIegwP1IHDJlid6jVapKS2KVUPy7cX75DC99+MQ=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=uP10QNVwSQdXBagNATjT1FKNeSi8tat+PNTt0THPjjhaxRAsNFLYWvB2PObIoGW5/6dzg5Uyacfa6FSmxWhz8VI/0LD2yQrE5NMCodRvG/43x44I+calbjM+HIh4MI1R+hidfadzekCO2lEEhJNRI+f866gRkL7WNYTlQbkBuXI= 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=oCM52UFa; arc=none smtp.client-ip=209.85.210.181 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="oCM52UFa" Received: by mail-pf1-f181.google.com with SMTP id d2e1a72fcca58-842307473b5so2894777b3a.2 for ; Mon, 22 Jun 2026 01:07:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1782115658; x=1782720458; 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=rlV3+D4ZZRALKldfUDeeoVyJTWnvlXeJCAWh7uoewIg=; b=oCM52UFawNeEMZYI/0BorHH7HfKTcvM5P5VJkM9C1S3cjZ+x/0Pp/X0cuVQWXUzyk4 rCeCkwPxT7romuvZdjmHkGFtI/alx66sbLR1ablAelRZJhvqKtL9PhgEb62HbXtKpWdI ZwkuoScw4ZZ/ytgkG+VCSoRiap0YffRFVZcM87y3KUtc4ND2YUsZxhQRMdz5J1XalXML GwwdI7llo+tobqACtwv6DRxE2KZ+Z9SIyZWtr9Qtg56dUbdUsj7goMTFikw/2d+mHu18 Ueo+7abiFUkj43WYHPZ4bigimF8ldZoteAb8MM1ThJZoQFythdPLjiGSnMLOQ2G8oggA d+Vw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1782115658; x=1782720458; 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=rlV3+D4ZZRALKldfUDeeoVyJTWnvlXeJCAWh7uoewIg=; b=Op7uoauaF4bOo3274Jv7omAG2TZXEmWtqZOS5YQOZQdC7WgdIzabeNWRN9cXwYsIu/ C81E4o8Hr9pR9UXx23qpoxQh2qyCwmYBwoPKo3MuGlUkIFVHouc/sR5hn2jIuNs/Py1Z m3vN2keog7rCdAakZcgk8VCycTZ49RS82auBIFbuQGHJHK54t+znSg1rOnKXkexPGjz/ eTyK6Dzy0eOlLbkjIuSKascsTFSqgErGle5oj4+CXDZQsa2mPqrV1Q5Pw+ljXXMxe2xP f/PGc6d5frXaeQoBe8MToA6NR4p/Op2feqX+fUwzUeKJqgGuANPBn40+vIxNoEoIbPKu tuaw== X-Forwarded-Encrypted: i=1; AFNElJ8d1HXyvyU/8r/2d5IsAbX0I+mn1XwJDBvyJ6taooj9qw6kRQr3Es4haz6gxHNoCujxkJSAj4hGxUKCPrg=@vger.kernel.org X-Gm-Message-State: AOJu0YySGa1JDRpVA1d7BCjCrRykNMI6oshzh4wej3Q+oDOE7078MnVc WmVi1TUXCoHQEYEjEGvPz2zAjGBHrj7yy8qCH2erVTVl3F1guQhcCHDe X-Gm-Gg: AfdE7ckeTcOc2NcWTVjQhV0Hq8nc/GlMrb0KlvgTSZtMK3vMazRkjrg8bGk5/lycspN /jEw/DE0vaVzMSPs9oa3lXNDttVgHNizp88+RI/0NM0IERyK08UJZPuJkm53qgHuUUDUwRc+v0f 5MEy0r+pxWUlvja2sSk+2lJOsxI3CWo++1IcZuzAIzf7azbViFEEbKmuv5GwUcvQky6zLDOy8tG GUE1vVo5pfht5EFT9Pn/9Nhft9fSXUM0US2YiTFOm2OSsoXZg+OG56kX34A2cEeVfu20uEPfQ8W EPRRQo3ZySn6PeW5ky/1bkTwhdoiFA9pPGt04TUBqZw6A60VvZvH7Qmbc9rBojpaaJkccRgBK8d 7s8/8cTHXFasHpM8owjbw3Z+w3G15+6lvXCTYirRRyRRoVwhD5d4qJonxUZS4V6n/IW9JDvbxVI 9NRozYQlRiPjq4oKpHn4DRuiwJ X-Received: by 2002:a05:6a00:1d8c:b0:842:2ddb:e303 with SMTP id d2e1a72fcca58-845560736bbmr13315970b3a.12.1782115657543; Mon, 22 Jun 2026 01:07:37 -0700 (PDT) Received: from fedora ([106.219.196.189]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-84564ed9516sm6688804b3a.57.2026.06.22.01.07.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jun 2026 01:07:37 -0700 (PDT) From: Sajal Gupta To: linux-raid@vger.kernel.org, song@kernel.org Cc: yukuai3@huawei.com, tomasz.majchrzak@intel.com, linux-kernel@vger.kernel.org, error27@gmail.com, skhan@linuxfoundation.org, me@brighamcampbell.com, linux-kernel-mentees@lists.linux.dev, Sajal Gupta Subject: [PATCH] md/raid5-ppl: convert pending_flushes from atomic_t to refcount_t Date: Mon, 22 Jun 2026 13:34:32 +0530 Message-ID: <20260622080656.22786-1-sajal2005gupta@gmail.com> X-Mailer: git-send-email 2.54.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit The old atomic_t based counter allowed ppl_do_flush() to continue using io after it could already have been freed by ppl_io_unit_finished(), leading to a use-after-free. Convert pending_flushes from atomic_t to refcount_t with a proper ownership model. The creator holds a reference for the duration of ppl_do_flush(), and each submitted flush bio holds a reference until its endio callback runs. This makes the io lifetime explicit and removes the need for the second loop in ppl_do_flush(). Fixes: 1532d9e87e8b ("raid5-ppl: PPL support for disks with write-back cache enabled") Reported-by: Dan Carpenter Closes: https://lore.kernel.org/all/ajJF2wKYWRk4GGCK@stanley.mountain/ Signed-off-by: Sajal Gupta --- drivers/md/raid5-ppl.c | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/drivers/md/raid5-ppl.c b/drivers/md/raid5-ppl.c index a70cbec12ed0..157a89edd9c8 100644 --- a/drivers/md/raid5-ppl.c +++ b/drivers/md/raid5-ppl.c @@ -145,7 +145,7 @@ struct ppl_io_unit { struct list_head stripe_list; /* stripes added to the io_unit */ atomic_t pending_stripes; /* how many stripes not written to raid */ - atomic_t pending_flushes; /* how many disk flushes are in progress */ + refcount_t pending_flushes; /* how many disk flushes are in progress */ bool submitted; /* true if write to log started */ @@ -249,7 +249,7 @@ static struct ppl_io_unit *ppl_new_iounit(struct ppl_log *log, INIT_LIST_HEAD(&io->log_sibling); INIT_LIST_HEAD(&io->stripe_list); atomic_set(&io->pending_stripes, 0); - atomic_set(&io->pending_flushes, 0); + refcount_set(&io->pending_flushes, 1); bio_init(&io->bio, log->rdev->bdev, io->biovec, PPL_IO_INLINE_BVECS, REQ_OP_WRITE | REQ_FUA); @@ -599,7 +599,7 @@ static void ppl_flush_endio(struct bio *bio) bio_put(bio); - if (atomic_dec_and_test(&io->pending_flushes)) { + if (refcount_dec_and_test(&io->pending_flushes)) { ppl_io_unit_finished(io); md_wakeup_thread(conf->mddev->thread); } @@ -611,11 +611,8 @@ static void ppl_do_flush(struct ppl_io_unit *io) struct ppl_conf *ppl_conf = log->ppl_conf; struct r5conf *conf = ppl_conf->mddev->private; int raid_disks = conf->raid_disks; - int flushed_disks = 0; int i; - atomic_set(&io->pending_flushes, raid_disks); - for_each_set_bit(i, &log->disk_flush_bitmap, raid_disks) { struct md_rdev *rdev; struct block_device *bdev = NULL; @@ -632,20 +629,18 @@ static void ppl_do_flush(struct ppl_io_unit *io) GFP_NOIO, &ppl_conf->flush_bs); bio->bi_private = io; bio->bi_end_io = ppl_flush_endio; + refcount_inc(&io->pending_flushes); pr_debug("%s: dev: %ps\n", __func__, bio->bi_bdev); submit_bio(bio); - flushed_disks++; } } log->disk_flush_bitmap = 0; - for (i = flushed_disks ; i < raid_disks; i++) { - if (atomic_dec_and_test(&io->pending_flushes)) - ppl_io_unit_finished(io); - } + if (refcount_dec_and_test(&io->pending_flushes)) + ppl_io_unit_finished(io); } static inline bool ppl_no_io_unit_submitted(struct r5conf *conf, -- 2.54.0