From nobody Sun Nov 24 23:52:50 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=reject dis=none) header.from=google.com ARC-Seal: i=1; a=rsa-sha256; t=1719425489; cv=none; d=zohomail.com; s=zohoarc; b=l31PMkAxRAdx0VQz3kWz20D36vRymbamGa/vL/2qu033XEiwmWcSb6OmR7YWlyzTNi3iqyNbo1xK8jaJ+EKRswMbifQZAYMbqeG99mVdxshf14LyNG9FUz16JDCY+1nSGTZsCxYyOl25XmrbYDhBOdu8sJ8xZDt6P4BtefvTUCU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1719425489; h=Content-Type:Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=FldijschhLQ+XaonRMFWfUWX1O0P0VnZhMNiod+FW8k=; b=I8ySWtvsFOg9v64UAgIOdMpDxKyL8vkpBlmfKvPU/QY+STqbP0XuvAAhcme7lcrhEkRjYE+lYgyq2e+LtqJJzTL30RjTwNagAA26Qa5lA/hDX6QVmtuLxTVa8AESAVRIEjt0nKQK0DvRTAUs77bOr1RVpJDWZiM2fmqzzFQF5cc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1719425489441828.7758019154304; Wed, 26 Jun 2024 11:11:29 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sMX68-00047r-CJ; Wed, 26 Jun 2024 14:10:24 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from <3iFl8ZgQKCmcWPNWLTTLQJ.HTRVJRZ-IJaJQSTSLSZ.TWL@flex--rkir.bounces.google.com>) id 1sMX66-00047Z-J6 for qemu-devel@nongnu.org; Wed, 26 Jun 2024 14:10:22 -0400 Received: from mail-yw1-x1149.google.com ([2607:f8b0:4864:20::1149]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from <3iFl8ZgQKCmcWPNWLTTLQJ.HTRVJRZ-IJaJQSTSLSZ.TWL@flex--rkir.bounces.google.com>) id 1sMX64-0002zy-SI for qemu-devel@nongnu.org; Wed, 26 Jun 2024 14:10:22 -0400 Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-648cabfaeecso10570407b3.0 for ; Wed, 26 Jun 2024 11:10:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1719425417; x=1720030217; darn=nongnu.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=FldijschhLQ+XaonRMFWfUWX1O0P0VnZhMNiod+FW8k=; b=UNvUhReaRNPgEo4IbHZWmutN99wYC4L63allfG7StgqWOVb/qnG2SgYpi+AoONKJaY 0qCTXhTbP/IjMhri0Ng7QnR8K3ZC+aZWzq2WXo9Q23Q/aP7G6nkMHhEsMzkFptFt4NH5 RFrxdFQ6/I9lnc5M1w+hgdMqgkkOQMoRwNJGquCFYLuvf871rjnLLCApQ2laBqAY8j+i HLeit8fAb5E6bcG6VpnT5419J0SFOyp83cuM0fBtprFrihgfswkzj0h+hO1Q2yqE/qZK 0tYiKXhisahKK1H8hCY+uIoPl32BSEgihQHLG6iCQ/rT1Rs12whwcTpKPahuL/awWqtG jVqw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719425417; x=1720030217; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=FldijschhLQ+XaonRMFWfUWX1O0P0VnZhMNiod+FW8k=; b=pf5cVCCou9ORnMQwrmkodEsahlbrUwbefj4kirDga1r8bimo6nI+m9xm6MZuGSEQJt tmywu7duvg02tD6werY5k/cZhDn/m8X1VNPxHz/xtMLsGosOmm8cX7CwfQZWypuEl3M/ r3mexW6rGN2kHIgoAQfCwLStWfqj3+1aYvD0zTNO+7fRRhrtoW/9ymWQHwQvHzDhlzW7 H7GqBWbtYXJlXcoTj+ms9hwAoV6uZ09Vkg/v0eDk1nFkUfKoWROg+/Kqnue9ER/4vQLB Ulo3VVJww2yCddusTnFgkLhfmlPyeh+46JMZu8YuHbbjukrcm5xbIdNNe2YtuI0rquTj oF3Q== X-Gm-Message-State: AOJu0YxIcKLiwwffmLoDxlAWpruYmTgITkKYEVAUWummiziMlFJyOfdt a0vbQUvGYqocAtVJ7Z1IYCiAxzppDULO5c/aWXft/zogQDnt3DNh4g0gUBPHBpY2DBCUajIfzk/ V95qydvrqWIdUrBEIZloPy10jdAT66OcbshBTCZMXo3ECc5sGdt2ifdcImh5DEHZuZdzKrkmwNI yMp6o4jaOqw7kl2T0VZSiH X-Google-Smtp-Source: AGHT+IE6rncvHu/b1HiG7lnftLgvaBJdBnVVeZXN5KoMOiDbOe9nj6gW5SppnCUX8hiIZN7VzfvYz+Dg X-Received: from rkir98.kir.corp.google.com ([2620:15c:7d:4:e7b4:877a:d463:4598]) (user=rkir job=sendgmr) by 2002:a05:690c:4909:b0:62f:5d17:3628 with SMTP id 00721157ae682-643a8a205b4mr4590557b3.0.1719425416975; Wed, 26 Jun 2024 11:10:16 -0700 (PDT) Date: Wed, 26 Jun 2024 11:10:12 -0700 Mime-Version: 1.0 X-Mailer: git-send-email 2.45.2.741.gdbec12cfda-goog Message-ID: <20240626181012.3321084-1-rkir@google.com> Subject: [PATCH] timer: Fix a race condition between timer's callback and destroying code From: Roman Kiryanov To: qemu-devel@nongnu.org Cc: jansene@google.com, mett@google.com, jpcottin@google.com, Roman Kiryanov Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::1149; envelope-from=3iFl8ZgQKCmcWPNWLTTLQJ.HTRVJRZ-IJaJQSTSLSZ.TWL@flex--rkir.bounces.google.com; helo=mail-yw1-x1149.google.com X-Spam_score_int: -100 X-Spam_score: -10.1 X-Spam_bar: ---------- X-Spam_report: (-10.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_MED=-0.454, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, USER_IN_DEF_DKIM_WL=-7.5 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @google.com) X-ZM-MESSAGEID: 1719425492788100003 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" `timerlist_run_timers` provides no mechanism to make sure the data pointed by `opaque` is valid when calling timer's callback: there could be another thread running which is destroying timer's opaque data. With this change `timer_del` becomes blocking if timer's callback is running and it will be safe to destroy timer's data once `timer_del` returns. Google-Bug-Id: 348598513 Change-Id: I3cd6350587fb67c73bbee7b934d2c0c1acb27229 Signed-off-by: Roman Kiryanov --- include/qemu/timer.h | 4 ++++ util/qemu-timer.c | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/include/qemu/timer.h b/include/qemu/timer.h index 9a366e551f..de82ed78ae 100644 --- a/include/qemu/timer.h +++ b/include/qemu/timer.h @@ -3,6 +3,7 @@ =20 #include "qemu/bitops.h" #include "qemu/notify.h" +#include "qemu/thread.h" #include "qemu/host-utils.h" =20 #define NANOSECONDS_PER_SECOND 1000000000LL @@ -86,9 +87,12 @@ struct QEMUTimer { QEMUTimerList *timer_list; QEMUTimerCB *cb; void *opaque; + QemuMutex opaque_lock; + QemuCond cb_done; QEMUTimer *next; int attributes; int scale; + bool cb_running; }; =20 extern QEMUTimerListGroup main_loop_tlg; diff --git a/util/qemu-timer.c b/util/qemu-timer.c index 6a0de33dd2..9eb6fca5fa 100644 --- a/util/qemu-timer.c +++ b/util/qemu-timer.c @@ -369,7 +369,10 @@ void timer_init_full(QEMUTimer *ts, ts->opaque =3D opaque; ts->scale =3D scale; ts->attributes =3D attributes; + qemu_mutex_init(&ts->opaque_lock); + qemu_cond_init(&ts->cb_done); ts->expire_time =3D -1; + ts->cb_running =3D false; } =20 void timer_deinit(QEMUTimer *ts) @@ -394,6 +397,12 @@ static void timer_del_locked(QEMUTimerList *timer_list= , QEMUTimer *ts) } pt =3D &t->next; } + + qemu_mutex_lock(&ts->opaque_lock); + while (ts->cb_running) { + qemu_cond_wait(&ts->cb_done, &ts->opaque_lock); + } + qemu_mutex_unlock(&ts->opaque_lock); } =20 static bool timer_mod_ns_locked(QEMUTimerList *timer_list, @@ -571,9 +580,22 @@ bool timerlist_run_timers(QEMUTimerList *timer_list) cb =3D ts->cb; opaque =3D ts->opaque; =20 + /* Mark the callback as running to prevent + * destroying `opaque` in another thread. + */ + qemu_mutex_lock(&ts->opaque_lock); + ts->cb_running =3D true; + qemu_mutex_unlock(&ts->opaque_lock); + /* run the callback (the timer list can be modified) */ qemu_mutex_unlock(&timer_list->active_timers_lock); cb(opaque); + + qemu_mutex_lock(&ts->opaque_lock); + ts->cb_running =3D false; + qemu_cond_broadcast(&ts->cb_done); + qemu_mutex_unlock(&ts->opaque_lock); + qemu_mutex_lock(&timer_list->active_timers_lock); =20 progress =3D true; --=20 2.45.2.741.gdbec12cfda-goog