From patchwork Tue Jan 14 02:06:50 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: lizetao X-Patchwork-Id: 13938285 Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3FA248493 for ; Tue, 14 Jan 2025 02:06:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.189 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736820416; cv=none; b=aXRx4q7+BFjM8TzH7btNWM5BjOgxvqEL0eg0sc6EsuF5JndMO5OYT4LNzq/wcqX1n/6XM+YJfh++qIhLzrJVulbVG9yC061xr11sQqx9Arj0bqOcdcVMjMbJg+P89+xHFnDOD1eqY3ZdX/w+iotIueBPSNsiiTJ99FG1iOVThU4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736820416; c=relaxed/simple; bh=YgaC0fzUTNraUPayw/nF1wraGESscFUv5sbDTjUJlII=; h=From:To:CC:Subject:Date:Message-ID:Content-Type:MIME-Version; b=mGJHvS07VCti6o7VjFDSaO2v1w8ggazQc5YZnjNmQ8BiO0vbZruxGIugDHg9CfjqiSP/FoSDy8HQc0Wpm22UUklu3fKx+rXXGOECxbCUbXCQzROg9etb7MX6l0Dfw5Pdbly4VpGfcpycPWHkfoU+rHAJMIoD8yH79+TaUVodEdc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.189 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Received: from mail.maildlp.com (unknown [172.19.163.48]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4YXC9n6PgrzRlHP; Tue, 14 Jan 2025 10:04:29 +0800 (CST) Received: from kwepemd100010.china.huawei.com (unknown [7.221.188.107]) by mail.maildlp.com (Postfix) with ESMTPS id 4158A1802E1; Tue, 14 Jan 2025 10:06:51 +0800 (CST) Received: from kwepemd500012.china.huawei.com (7.221.188.25) by kwepemd100010.china.huawei.com (7.221.188.107) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1258.34; Tue, 14 Jan 2025 10:06:50 +0800 Received: from kwepemd500012.china.huawei.com ([7.221.188.25]) by kwepemd500012.china.huawei.com ([7.221.188.25]) with mapi id 15.02.1258.034; Tue, 14 Jan 2025 10:06:50 +0800 From: lizetao To: Jens Axboe , Pavel Begunkov CC: "io-uring@vger.kernel.org" Subject: [PATCH] io_uring/io-wq: Fix a small time window for reading work->flags Thread-Topic: [PATCH] io_uring/io-wq: Fix a small time window for reading work->flags Thread-Index: AdtmKOA8sg05Tt97QvCw7GBc1hDbnw== Date: Tue, 14 Jan 2025 02:06:50 +0000 Message-ID: <5fd306d40ebb4da0a657da9a9be5cec1@huawei.com> Accept-Language: en-US Content-Language: zh-CN X-MS-Has-Attach: X-MS-TNEF-Correlator: Precedence: bulk X-Mailing-List: io-uring@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 There is a small time window that is modified by other tasks after reading work->flags. It is changed to read before use, which is more in line with the semantics of atoms. Fixes: 3474d1b93f89 ("io_uring/io-wq: make io_wq_work flags atomic") Signed-off-by: Li Zetao --- io_uring/io-wq.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/io_uring/io-wq.c b/io_uring/io-wq.c index a38f36b68060..75096e77b1fe 100644 --- a/io_uring/io-wq.c +++ b/io_uring/io-wq.c @@ -932,7 +932,6 @@ static bool io_wq_work_match_item(struct io_wq_work *work, void *data) void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work) { struct io_wq_acct *acct = io_work_get_acct(wq, work); - unsigned int work_flags = atomic_read(&work->flags); struct io_cb_cancel_data match = { .fn = io_wq_work_match_item, .data = work, @@ -945,7 +944,7 @@ void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work) * been marked as one that should not get executed, cancel it here. */ if (test_bit(IO_WQ_BIT_EXIT, &wq->state) || - (work_flags & IO_WQ_WORK_CANCEL)) { + (atomic_read(&work->flags) & IO_WQ_WORK_CANCEL)) { io_run_cancel(work, wq); return; } @@ -959,7 +958,7 @@ void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work) do_create = !io_wq_activate_free_worker(wq, acct); rcu_read_unlock(); - if (do_create && ((work_flags & IO_WQ_WORK_CONCURRENT) || + if (do_create && ((atomic_read(&work->flags) & IO_WQ_WORK_CONCURRENT) || !atomic_read(&acct->nr_running))) { bool did_create;