Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp904529ybl; Sat, 18 Jan 2020 14:00:29 -0800 (PST) X-Google-Smtp-Source: APXvYqzkPACKoUDw2Gt+AqJmFKqpuIWrekHOP7ivkbwUMhcamgt4PQ4G3ImkbyOheWmJ7UIoGhZt X-Received: by 2002:aca:7244:: with SMTP id p65mr7892598oic.50.1579384828956; Sat, 18 Jan 2020 14:00:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1579384828; cv=none; d=google.com; s=arc-20160816; b=1BsryfWbgmSbnHxItiBwuSsxFKzj08IMNGTCHGQcRAyTV7yqSH86Aa2/bboQgC9+Nk 1BMemtkesrqD+is+ObW6arFEy/ASr1prhGV0OdZJNYIcW2CLqDozbwEhyzkrozrhOhzY B3MeWD1CR7qgvwDdhXeo5HJKTa/Tt94tIOPGWlnYAnrbQAmzOpgNMFnceC6vAjgQSiES Za5GsPv7vQbHARX6MeoUk+t6CaWGUycWoHUn07JDGcpfQCWIPad1QaUtWuFTeovrV1rc 9j39S0Q6GW+HDXsgY9azVLC2xc0NVEM8kQm4lhM2ifuzB1ixl/vxMhcDxRxSrSwsmnWZ hlnw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=UaeYX/Ff/wQaMg6DYHILaGZDP7ER1icKxMDfq0TG0SI=; b=lF4Us8P10FM47Utm0lcSJQM07Ufp/7eYyYlJSyecDFSKzLGVp8rCLQHrNUyguupxKU xozRThqgqIIbk77J1cerT8TGN0CCKXj42fqv/AuP1W0kZPD1p1qwk8FYTTAw+kD2ZVmN N5TFbBMRIEyhnN3r3dMHJyVzoNzammxtZFmp97IUUJkM6mOeHFUi1S2HnA9NvYNCnI5n kX+iZh0ykEqyfKH2rTkvPO6YmDiZlzUwEdImyXjJz6EVxug1iW/kf1jg+Md8JV3JBwQX jC6CalJF0L2LYkvHuuFQqrexp4mkOeZeonOHTXRCZta8fSZR8EoKoEjGpg4JRwri2rAl 5vCA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=Payn8D+Y; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b10si15585692oic.153.2020.01.18.14.00.13; Sat, 18 Jan 2020 14:00:28 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=Payn8D+Y; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727120AbgARV6k (ORCPT + 99 others); Sat, 18 Jan 2020 16:58:40 -0500 Received: from mail-wr1-f67.google.com ([209.85.221.67]:41373 "EHLO mail-wr1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727104AbgARV6k (ORCPT ); Sat, 18 Jan 2020 16:58:40 -0500 Received: by mail-wr1-f67.google.com with SMTP id c9so25915788wrw.8 for ; Sat, 18 Jan 2020 13:58:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=UaeYX/Ff/wQaMg6DYHILaGZDP7ER1icKxMDfq0TG0SI=; b=Payn8D+YqNMfkeO9DcSIuSyDncQC1hM2updNTA6l0CYGWvp9Dr5WjIH/mhBQFtU9iC ZJOw3RNITzEv8F2+lGVYKnsBCL1akhilV0x3qIIzfXY1L9VkamZkevNL5lrHs72PP7Yx firrtV8bCmQSmW81/BLHtMoBRqfMn+eC6i0qlfpiaOdUMeqS7Wkjfbpcu2v2wo4qSHyv 1kKOILEslyGbaQtic/myMt8moK0+GQEplz/SvwjxG8QvjM5ZZs0vh6Ju3OfN65jY5Kbm EEBnbwVdmNqKNlcK7k534qqNa8eeUIRMJKiAQeJh+SFo/HYw0O3WdaCP4/d1mP3dX2Wx bMsQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=UaeYX/Ff/wQaMg6DYHILaGZDP7ER1icKxMDfq0TG0SI=; b=qx00GUdLTc2ihwOStycXKOpRgFYOh40hMwDxmjDxFg+0bS6oUHoQPzen5r+vl5tP5x yFL3r929SlQuO1OOVoBIEdTNKJwNbvd6WAnzsZeF6FDaFWfSSOHp2EDut4AezL8mauV2 b3YWxD84JTEFTrGDVM7FyilhCvMOWYcSO6m4wtAhFqzdcVyikKonlj1qWkVs3wNycr9Z yZGFxB4KE+DrnBOlJmv3rMOMyEzBE9orS6p04ant4eENzGHKxc1vIiwXUmE5aB6U7TsS ruZvnqsEvxB8jqZoouvNXAR+200MlYKCM75Dm6bbKehHCQXLGtYsPyEH79JU3/S3Rwv7 fVdg== X-Gm-Message-State: APjAAAV894AeCs2FKCgi+gS7vEkjO2HcXh+zN+KpbKy23/aQv9SgbaRu we3Zily8J1eHSahqaEd8826ScJ35kNwP9Q== X-Received: by 2002:adf:ea8a:: with SMTP id s10mr9800867wrm.278.1579384717335; Sat, 18 Jan 2020 13:58:37 -0800 (PST) Received: from localhost.localdomain (ip-213-220-200-127.net.upcbroadband.cz. [213.220.200.127]) by smtp.gmail.com with ESMTPSA id z8sm39789567wrq.22.2020.01.18.13.58.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 Jan 2020 13:58:36 -0800 (PST) From: Andrea Parri To: linux-kernel@vger.kernel.org Cc: Tejun Heo , Lai Jiangshan , "Paul E . McKenney" , Andrea Parri Subject: [PATCH] workqueue: Document (some) memory-ordering properties of {queue,schedule}_work() Date: Sat, 18 Jan 2020 22:58:20 +0100 Message-Id: <20200118215820.7646-1-parri.andrea@gmail.com> X-Mailer: git-send-email 2.24.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org It's desirable to be able to rely on the following property: All stores preceding (in program order) a call to a successful queue_work() will be visible from the CPU which will execute the queued work by the time such work executes, e.g., { x is initially 0 } CPU0 CPU1 WRITE_ONCE(x, 1); [ "work" is being executed ] r0 = queue_work(wq, work); r1 = READ_ONCE(x); Forbids: r0 == true && r1 == 0 The current implementation of queue_work() provides such memory-ordering property: - In __queue_work(), the ->lock spinlock is acquired. - On the other side, in worker_thread(), this same ->lock is held when dequeueing work. So the locking ordering makes things work out. Add this property to the DocBook headers of {queue,schedule}_work(). Suggested-by: Paul E. McKenney Signed-off-by: Andrea Parri --- include/linux/workqueue.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/include/linux/workqueue.h b/include/linux/workqueue.h index 4261d1c6e87b1..4fef6c38b0536 100644 --- a/include/linux/workqueue.h +++ b/include/linux/workqueue.h @@ -487,6 +487,19 @@ extern void wq_worker_comm(char *buf, size_t size, struct task_struct *task); * * We queue the work to the CPU on which it was submitted, but if the CPU dies * it can be processed by another CPU. + * + * Memory-ordering properties: If it returns %true, guarantees that all stores + * preceding the call to queue_work() in the program order will be visible from + * the CPU which will execute @work by the time such work executes, e.g., + * + * { x is initially 0 } + * + * CPU0 CPU1 + * + * WRITE_ONCE(x, 1); [ @work is being executed ] + * r0 = queue_work(wq, work); r1 = READ_ONCE(x); + * + * Forbids: r0 == true && r1 == 0 */ static inline bool queue_work(struct workqueue_struct *wq, struct work_struct *work) @@ -546,6 +559,9 @@ static inline bool schedule_work_on(int cpu, struct work_struct *work) * This puts a job in the kernel-global workqueue if it was not already * queued and leaves it in the same position on the kernel-global * workqueue otherwise. + * + * Shares the same memory-ordering properties of queue_work(), c.f., the + * DocBook header of queue_work(). */ static inline bool schedule_work(struct work_struct *work) { -- 2.24.0