From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) (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 7FA383A9610 for ; Thu, 23 Apr 2026 17:40:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776966043; cv=none; b=FUEH58Op9edURDWru6IC6mBRlWFIhXf5SjJDomOgFqrYpRHEdclW5s0OjUZi76Aj7coOALDz5ucxucXy0gsnxvPR1F2r43s3p1IcxXEGo7/mNIECFquSet+49vBHcREXyoBXyLWRdZryE+MiUhLfPz6uKNO160vZFRvnikV5Jd0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776966043; c=relaxed/simple; bh=OdCkp+U7UboftnRj6WMJLwEh7MCo7Yte9AXWP+965JY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=KyZswn/B8eu1dsjDa3yFqvn9AtQJK46WQy927exu9cMa8fJZhrVbGLsb8xprYkbVVRtefuOjBT4Ef12VEtnSEU8WRzpRzU62ywku5zhzwD/6XJIoTUEJQ+JQNhXXZlDXbVkcyjA1/dY6EHIpmuHZ9ml/Umiwec0qdCfmzhXjVKY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--dmatlack.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=iRyYcm9A; arc=none smtp.client-ip=209.85.210.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--dmatlack.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="iRyYcm9A" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-82f6b984b3aso3818230b3a.3 for ; Thu, 23 Apr 2026 10:40:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1776966041; x=1777570841; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Dl3LvDDtjuuVJwbcBevbqQWcUiD6cgLHLyDDYMqvvE0=; b=iRyYcm9A244x51SjhIFkyHLG76irZ97gWhzQMkQtD2dRbuT4nv1NFIBcDEzfubbG90 r+d1xrB44ovUmrUCboY9Apz39VurxyO+fBBCD5MVjR+wUIWb9ZaDfWL5J2GGZGbmUv1s 44HI5wjUXt3yLBpVHy7vGvL1ulmKcjZAgiA23ZdOFVSlT2yK8Mx2E/WPqQHaxJAKYL4W 4St73qhEB4XHiFUUKDNzIS6P3sH9Os4L7L1uYEQrKZkU7ioW0/LFqGxds5Cm+5C3wX0m IgG59Uy4M+fnPTbfCWysvS2Pd53XVpSgiN+eN/mPZtj5PRVtZD9SrULemjhD0GTWsypj 4XHg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776966041; x=1777570841; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Dl3LvDDtjuuVJwbcBevbqQWcUiD6cgLHLyDDYMqvvE0=; b=Jzfd84PDyjpPPjhpuj1/0SBsXMSkohCo/0tv3M5aUCO0unLRZmqt6hShk7RI9JxXhQ yLkwv9MXR5tRES4ZlG5r/WcE8GJqIW2GZXgLYCfOvR3Yi3hywK4uo0wJBHe96MJOOoBG a7eipYGRj0VeX6kIH/gM98/hQ9Q3ruDrcKQm108cWJPWceLxjl8Phw1bFxoJ1g6DPnDH mj5m4h7iuTYow0hktFobUu2W7aIG11FiiVOWbpdE3STyyGqfloUV7fwuji1kZGrP5sSR YYJNvb6Oe1naXrVRrHkyPlIeoweYEcfbsCjnRpRB3zMmHohmcmwFdqU+MnzUkrjsAatP Vd2A== X-Gm-Message-State: AOJu0YxbpIbZu5d7KCZesCcfB3nbjauLu8jfTGPPL6bH/+qHWranTlS4 zWRH1Ixpv2jwzh7NwpZJG0K5go7GHhBg78X0naDrr0r5gfkpNjLAD7MzEdZHf6RvTl59YCOp2Yd KgEALCBBg+qWLnRHl6duLWqc4y96P/clVUvviT/ZfylgS340KbN0VlwtnNpoChQpeb61TX93/qc wHrIsJ3BHkHfSNmPsMa8vXd0yV5uu1rodckl74MrHPaPW8Kjqm1p2OO9Y= X-Received: from pfkm16.prod.google.com ([2002:a05:6a00:810:b0:82f:ac74:4db5]) (user=dmatlack job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:2d8d:b0:82c:1cd0:2f5e with SMTP id d2e1a72fcca58-82f8c96eb30mr30470988b3a.44.1776966040415; Thu, 23 Apr 2026 10:40:40 -0700 (PDT) Date: Thu, 23 Apr 2026 17:40:28 +0000 In-Reply-To: <20260423174032.3140399-1-dmatlack@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260423174032.3140399-1-dmatlack@google.com> X-Mailer: git-send-email 2.54.0.rc2.544.gc7ae2d5bb8-goog Message-ID: <20260423174032.3140399-2-dmatlack@google.com> Subject: [PATCH 1/2] liveupdate: Use refcount_t for FLB reference counts From: David Matlack To: linux-kernel@vger.kernel.org Cc: Andrew Morton , Mike Rapoport , Pasha Tatashin , Pratyush Yadav , Samiullah Khawaja , David Matlack Content-Type: text/plain; charset="UTF-8" Use refcount_t instead of a raw integer to keep track of references on incoming and outgoing FLBs. Using refcount_t provides protection from overflow, underflow, and other issues. Fixes: cab056f2aae7 ("liveupdate: luo_flb: introduce File-Lifecycle-Bound global state") Signed-off-by: David Matlack --- include/linux/liveupdate.h | 3 ++- kernel/liveupdate/luo_flb.c | 22 ++++++++++------------ 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/include/linux/liveupdate.h b/include/linux/liveupdate.h index 30c5a39ff9e9..8d3bbc35c828 100644 --- a/include/linux/liveupdate.h +++ b/include/linux/liveupdate.h @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -175,7 +176,7 @@ struct liveupdate_flb_ops { * @retrieved: True once the FLB's retrieve() callback has run. */ struct luo_flb_private_state { - long count; + refcount_t count; u64 data; void *obj; struct mutex lock; diff --git a/kernel/liveupdate/luo_flb.c b/kernel/liveupdate/luo_flb.c index 00f5494812c4..59c5f31ab767 100644 --- a/kernel/liveupdate/luo_flb.c +++ b/kernel/liveupdate/luo_flb.c @@ -111,7 +111,7 @@ static int luo_flb_file_preserve_one(struct liveupdate_flb *flb) struct luo_flb_private *private = luo_flb_get_private(flb); scoped_guard(mutex, &private->outgoing.lock) { - if (!private->outgoing.count) { + if (!refcount_read(&private->outgoing.count)) { struct liveupdate_flb_op_args args = {0}; int err; @@ -126,8 +126,10 @@ static int luo_flb_file_preserve_one(struct liveupdate_flb *flb) } private->outgoing.data = args.data; private->outgoing.obj = args.obj; + refcount_set(&private->outgoing.count, 1); + } else { + refcount_inc(&private->outgoing.count); } - private->outgoing.count++; } return 0; @@ -138,8 +140,7 @@ static void luo_flb_file_unpreserve_one(struct liveupdate_flb *flb) struct luo_flb_private *private = luo_flb_get_private(flb); scoped_guard(mutex, &private->outgoing.lock) { - private->outgoing.count--; - if (!private->outgoing.count) { + if (refcount_dec_and_test(&private->outgoing.count)) { struct liveupdate_flb_op_args args = {0}; args.flb = flb; @@ -178,7 +179,7 @@ static int luo_flb_retrieve_one(struct liveupdate_flb *flb) for (int i = 0; i < fh->header_ser->count; i++) { if (!strcmp(fh->ser[i].name, flb->compatible)) { private->incoming.data = fh->ser[i].data; - private->incoming.count = fh->ser[i].count; + refcount_set(&private->incoming.count, fh->ser[i].count); found = true; break; } @@ -208,12 +209,8 @@ static int luo_flb_retrieve_one(struct liveupdate_flb *flb) static void luo_flb_file_finish_one(struct liveupdate_flb *flb) { struct luo_flb_private *private = luo_flb_get_private(flb); - u64 count; - scoped_guard(mutex, &private->incoming.lock) - count = --private->incoming.count; - - if (!count) { + if (refcount_dec_and_test(&private->incoming.count)) { struct liveupdate_flb_op_args args = {0}; if (!private->incoming.retrieved) { @@ -652,12 +649,13 @@ void luo_flb_serialize(void) guard(rwsem_read)(&luo_register_rwlock); list_private_for_each_entry(gflb, &luo_flb_global.list, private.list) { struct luo_flb_private *private = luo_flb_get_private(gflb); + long count = refcount_read(&private->outgoing.count); - if (private->outgoing.count > 0) { + if (count > 0) { strscpy(fh->ser[i].name, gflb->compatible, sizeof(fh->ser[i].name)); fh->ser[i].data = private->outgoing.data; - fh->ser[i].count = private->outgoing.count; + fh->ser[i].count = count; i++; } } -- 2.54.0.rc2.544.gc7ae2d5bb8-goog