Received: by 2002:a5b:505:0:0:0:0:0 with SMTP id o5csp1167555ybp; Fri, 11 Oct 2019 09:57:11 -0700 (PDT) X-Google-Smtp-Source: APXvYqxNzJqDptgXdCkfdpm1tXqIGm3Wij09ZOHuNp5xm0OAb9ARZJbWfo2Ohw7lRHAwXjaWUsN5 X-Received: by 2002:a17:907:20c8:: with SMTP id qq8mr14647953ejb.311.1570813031267; Fri, 11 Oct 2019 09:57:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1570813031; cv=none; d=google.com; s=arc-20160816; b=idZntL5QthU8hp4VzaapLDbqqFUncTuStCFF6iw5GGXYOgo49B50lCLGTfcsbHTsO6 UfJ7LDDCQpIOJacuk9inuWeExUfOOP5G0QGmJEsj7aMSgoM4zEgpPTN8V27vvwM2YxM3 ijAqWC3CZ2jPQXo7YI7jr96IJPfEkROQzFZP7HgT3cGJq0IMrKxVZdlODeZHkeYwBSob YFUJeTXy5iwy6QL8EPR3zTYkUEEBSG9y7+DV9Jp+6qlFH1BQJAuYtVu2plO+30dCH9Ah 0vjID3UU/GT5kg4NtArbSkb7lcqYIMB00s/qrcJHCfynmYez90c8sHmYdBgCTT1j5G7Y zZhw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=hXvDHwEk7B/T0ORjxtSYZYPZF53KrfzIHp6UfpgePt0=; b=skA1GcHTkWCGhd/cZ0T0GWkJtL3mmOqXoEmRcid7APVAXAG6U7Pcnx9ll61DJ/3VpJ 7KRA3sOoAmrBXCMG1Qysk0SJBtXf+3+RHYfZa+QIObRMEdHwrS0F+jYNO+WMoabPMbbn y6f4SYD9N8MTzQzxEjaY20yo32INEaPYt6on7wAINIVD8N/LE/yae/Ssse4QOJhe34v0 6CbKnLVCVCmW7rVY+rAZYXamHa0riEjTr/I7E71o981dRmhBrSbKRqnistROdIvFVxn3 HJSPwYeegRRHApqzacn4vj7BJaHsr++aZtiz3H4aL46TEVIqHo8w0f9DjuRlwumwGltF tlFA== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y47si6449414edd.236.2019.10.11.09.56.48; Fri, 11 Oct 2019 09:57:11 -0700 (PDT) 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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728431AbfJKQ4m (ORCPT + 99 others); Fri, 11 Oct 2019 12:56:42 -0400 Received: from mx2.suse.de ([195.135.220.15]:37924 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727149AbfJKQ4m (ORCPT ); Fri, 11 Oct 2019 12:56:42 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 648B6AB91; Fri, 11 Oct 2019 16:56:40 +0000 (UTC) Date: Fri, 11 Oct 2019 09:55:27 -0700 From: Davidlohr Bueso To: Manfred Spraul Cc: LKML , Waiman Long , 1vier1@web.de, Andrew Morton , Peter Zijlstra , Jonathan Corbet Subject: Re: [PATCH 2/5] ipc/mqueue.c: Update/document memory barriers Message-ID: <20191011165527.bsdiw6gu2sk7yrnl@linux-p48b> References: <20191011112009.2365-1-manfred@colorfullife.com> <20191011112009.2365-3-manfred@colorfullife.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <20191011112009.2365-3-manfred@colorfullife.com> User-Agent: NeoMutt/20180716 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, 11 Oct 2019, Manfred Spraul wrote: >Update and document memory barriers for mqueue.c: >- ewp->state is read without any locks, thus READ_ONCE is required. In general we relied on the barrier for not needing READ/WRITE_ONCE, but I agree this scenario should be better documented with them. Similarly imo, the 'state' should also need them for write, even if under the lock -- consistency and documentation, for example. In addition, I think it makes sense to encapsulate some of the pipelined send/recv operations, that also can allow us to keep the barrier comments in pipelined_send(), which I wonder why you chose to remove. Something like so, before your changes: diff --git a/ipc/mqueue.c b/ipc/mqueue.c index 3d920ff15c80..be48c0ba92f7 100644 --- a/ipc/mqueue.c +++ b/ipc/mqueue.c @@ -918,17 +918,12 @@ SYSCALL_DEFINE1(mq_unlink, const char __user *, u_name) * The same algorithm is used for senders. */ -/* pipelined_send() - send a message directly to the task waiting in - * sys_mq_timedreceive() (without inserting message into a queue). - */ -static inline void pipelined_send(struct wake_q_head *wake_q, +static inline void __pipelined_op(struct wake_q_head *wake_q, struct mqueue_inode_info *info, - struct msg_msg *message, - struct ext_wait_queue *receiver) + struct ext_wait_queue *this) { - receiver->msg = message; - list_del(&receiver->list); - wake_q_add(wake_q, receiver->task); + list_del(&this->list); + wake_q_add(wake_q, this->task); /* * Rely on the implicit cmpxchg barrier from wake_q_add such * that we can ensure that updating receiver->state is the last @@ -937,7 +932,19 @@ static inline void pipelined_send(struct wake_q_head *wake_q, * yet, at that point we can later have a use-after-free * condition and bogus wakeup. */ - receiver->state = STATE_READY; + this->state = STATE_READY; +} + +/* pipelined_send() - send a message directly to the task waiting in + * sys_mq_timedreceive() (without inserting message into a queue). + */ +static inline void pipelined_send(struct wake_q_head *wake_q, + struct mqueue_inode_info *info, + struct msg_msg *message, + struct ext_wait_queue *receiver) +{ + receiver->msg = message; + __pipelined_op(wake_q, info, receiver); } /* pipelined_receive() - if there is task waiting in sys_mq_timedsend() @@ -955,9 +962,7 @@ static inline void pipelined_receive(struct wake_q_head *wake_q, if (msg_insert(sender->msg, info)) return; - list_del(&sender->list); - wake_q_add(wake_q, sender->task); - sender->state = STATE_READY; + __pipelined_op(wake_q, info, sender); } static int do_mq_timedsend(mqd_t mqdes, const char __user *u_msg_ptr,