From nobody Tue Dec 2 01:03:32 2025 Received: from mail-pg1-f178.google.com (mail-pg1-f178.google.com [209.85.215.178]) (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 D62AC2BE64A for ; Tue, 25 Nov 2025 06:32:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764052349; cv=none; b=f3yGWJfE2BYyk2BVpZ+V3osV+VUOfU+hU3C9s9KInhDnUhPTug9q8VlijQYlJphaCkAfsT2DAUlUAcjrRAJhhosRixvBGYMx5ANaj3BW+kRkp4bxCWVMjDM9JkI8vOBjKtoEQW39YASpQA/ROmqQCOBH4aayqzOjDb+DxF6+mFw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764052349; c=relaxed/simple; bh=iWOhizPZ5b8MlafjCIMS4s/7fP/UjGGHBzJjqhvrQMo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=JnX/13YJTthFwQw7roC6+9SvUXPSs+OnL/aOK8HKR3gZdgEfc3BzEG8CJs1USa1QaUQdmtiRErd/wR7Lc6YDIEV4BWXGYrnX8o+SvqseNcvR+x8ZO+y3hbSdKewOoJyzxRT1qQ0qMdv3Lj9YqHGWO3q5WIv9BhVJzpuDJ4VzZrE= 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=YLobadO1; arc=none smtp.client-ip=209.85.215.178 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="YLobadO1" Received: by mail-pg1-f178.google.com with SMTP id 41be03b00d2f7-bc0e89640b9so3294880a12.1 for ; Mon, 24 Nov 2025 22:32:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1764052347; x=1764657147; 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=boOqRcUxmNCW9UWJksRZe9rQfSJ3rUsWo5BpoBtUE1Q=; b=YLobadO1lIAyf4fSJUauBdtmLtCwJYL63rTZfchbHlHZVGeHGtJqrjqPhg6mKWmopR UvWJDkReGgTNbKDpH/RBiMgUp83a9EZpBFRohj6+LyJaEuMt/g5Bo/oWhCTWLYRuK5xm BIMJ/b8y1cDK8szqmu8JRj8DPLsblod2MIMksa+ser6apI90rU57TH3Em/DpHT6zaZNo td+8xgiknKL0KrDwRG7CTofb/WJASF0FsmhI2oIof75l9Dg6gWC1dAEna6nTuTPLw4+x Ud9d9T8K1xyQ+1nD0csLO/NrVI4IzCTAqmFbpGyyo8OEQqvcMf5SBILMWQ+mBcAdA3RS 0VXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764052347; x=1764657147; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=boOqRcUxmNCW9UWJksRZe9rQfSJ3rUsWo5BpoBtUE1Q=; b=fablWaIPaGtQwfNwhWGLmHx6W+aw1gDNoxaigZvUIZORQYkZZyD+hjKgJKYJ45+nU7 aKp0APKFKa9/d71yBty3E5LJh0OyI6gTKTZplk5txUtFv+UQsMFY8luPR6KPqGYts7c6 0KN6pCPb36SFnpWQ2GktON6fPnzAEnu3jUnJLqXBx9Z2afka/KWb6zVQNhl8ADYPrXX/ aQxgZjDIDeiM3O04BcaCC5vfPipNbLYIgvY0Yo3a6KhjcMBFXfUvJRLymLoqR3w4rm0O U+xNVpA9lXnmRWUilL6LxDg5pqQfHPtKStNSei5TaKHedNe3qmAljJFQsEvGnvVqlJOr hRcQ== X-Gm-Message-State: AOJu0YyqnsOMMTYaEUdF0z/OzLYnNLPBVxp/GUgxixpUqZmcXKZBXl9l KdqL6LtIapAJYqZMTFy9HUdcbEyF0oWstf4S/tDJJAl/r89reJqN0KKBJsJhi6nf X-Gm-Gg: ASbGncu+n8ppU6Te3l2jQ2skctmjqUwNvxR2kDtnj5XCs/kOSb8QbuTEYezZ9YI1sh3 EPba5PsHdaTyrBgfkwoxyofUKPPIqXH/Oa19ezq1Czt4DCHxNzrHNfyiXDlKrFa/gC1CVosvSJ2 z8JQH6FHnVGBiNNcBHyt+BADqZxkGP7b3jw3sgKnP46NsiM5g0FW8amUtja5oXVwG5di3GusY4l f/X6XV3miXi5MeGhbTxu6RtPgRprnHwlDgVW//ytR+sf+gZ06gZOxNplhTMlUUUAikQj2YlDEn+ ERLc8Se3KpwvpXrNd+UKlCmEBg9SpdqrHZIPxbd9UWQNPW1u8vpEsFKaqvghC7XAi0rIb65LhEy hPhwImbYrDFP7Kg6BZNuPu8N8vmxruQN1djsb9ldOinXOIKfZlq3pG2Ub6Ca0vGsFcKcSBcckMR kqpdOi6Kk74Yo= X-Google-Smtp-Source: AGHT+IExsyhi3g7T+UDHjmiRwUzOanMTtYwusz0GkcrpQIjjZD5JSIpoM6DBI1uQBTh72N3IrO7FBQ== X-Received: by 2002:a05:701b:2719:b0:11b:b2f5:a1f4 with SMTP id a92af1059eb24-11c9d863125mr9036261c88.39.1764052346769; Mon, 24 Nov 2025 22:32:26 -0800 (PST) Received: from localhost ([240b:4004:a2:7900:ecf3:dec8:8c1e:4f5]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-11cc631c236sm6681959c88.7.2025.11.24.22.32.26 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 24 Nov 2025 22:32:26 -0800 (PST) From: Lai Jiangshan To: linux-kernel@vger.kernel.org Cc: Tejun Heo , ying chen , Lai Jiangshan , Lai Jiangshan Subject: [PATCH V4 3/4] workqueue: Limit number of processed works in rescuer per turn Date: Tue, 25 Nov 2025 14:36:16 +0800 Message-Id: <20251125063617.671199-4-jiangshanlai@gmail.com> X-Mailer: git-send-email 2.19.1.6.gb485710b In-Reply-To: <20251125063617.671199-1-jiangshanlai@gmail.com> References: <20251125063617.671199-1-jiangshanlai@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Lai Jiangshan Currently the rescuer keeps looping until all work on a PWQ is done, and this may hurt fairness among PWQs, as the rescuer could remain stuck on one PWQ indefinitely. Introduce RESCUER_BATCH to control the maximum number of work items the rescuer processes in each turn, and move on to other PWQs when the limit is reached. Signed-off-by: Lai Jiangshan --- kernel/workqueue.c | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/kernel/workqueue.c b/kernel/workqueue.c index 06cd3d6ff7e1..7cec9755b4e1 100644 --- a/kernel/workqueue.c +++ b/kernel/workqueue.c @@ -117,6 +117,8 @@ enum wq_internal_consts { MAYDAY_INTERVAL =3D HZ / 10, /* and then every 100ms */ CREATE_COOLDOWN =3D HZ, /* time to breath after fail */ =20 + RESCUER_BATCH =3D 16, /* process items per turn */ + /* * Rescue workers are used only on emergencies and shared by * all cpus. Give MIN_NICE. @@ -3450,7 +3452,15 @@ static int worker_thread(void *__worker) goto woke_up; } =20 -static bool assign_rescuer_work(struct pool_workqueue *pwq, struct worker = *rescuer) +/* + * Try to assign one work item from @pwq to @rescuer. + * + * Returns true if a work item was successfully assigned, false otherwise. + * If @throttled and other PWQs are in mayday, requeue mayday for this PWQ + * and let the rescuer handle other PWQs first. + * If this is the only PWQ in mayday, process it regardless of @throttled. + */ +static bool assign_rescuer_work(struct pool_workqueue *pwq, struct worker = *rescuer, bool throttled) { struct worker_pool *pool =3D pwq->pool; struct work_struct *cursor =3D &pwq->mayday_cursor; @@ -3471,7 +3481,21 @@ static bool assign_rescuer_work(struct pool_workqueu= e *pwq, struct worker *rescu =20 /* find the next work item to rescue */ list_for_each_entry_safe_from(work, n, &pool->worklist, entry) { - if (get_work_pwq(work) =3D=3D pwq && assign_work(work, rescuer, &n)) { + if (get_work_pwq(work) !=3D pwq) + continue; + /* + * If throttled, update the cursor, requeue a mayday for this + * PWQ, and move on to other PWQs. If there are no other PWQs + * in mayday, continue processing this one. + */ + if (throttled && !list_empty(&pwq->wq->maydays)) { + list_add_tail(&cursor->entry, &work->entry); + raw_spin_lock(&wq_mayday_lock); /* for wq->maydays */ + send_mayday(work); + raw_spin_unlock(&wq_mayday_lock); + return false; + } + if (assign_work(work, rescuer, &n)) { pwq->stats[PWQ_STAT_RESCUED]++; /* put the cursor for next search */ list_add_tail(&cursor->entry, &n->entry); @@ -3536,6 +3560,7 @@ static int rescuer_thread(void *__rescuer) struct pool_workqueue *pwq =3D list_first_entry(&wq->maydays, struct pool_workqueue, mayday_node); struct worker_pool *pool =3D pwq->pool; + unsigned int count =3D 0; =20 __set_current_state(TASK_RUNNING); list_del_init(&pwq->mayday_node); @@ -3548,7 +3573,7 @@ static int rescuer_thread(void *__rescuer) =20 WARN_ON_ONCE(!list_empty(&rescuer->scheduled)); =20 - while (assign_rescuer_work(pwq, rescuer)) + while (assign_rescuer_work(pwq, rescuer, ++count > RESCUER_BATCH)) process_scheduled_works(rescuer); =20 /* --=20 2.19.1.6.gb485710b