Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S938703AbcJYNVg (ORCPT ); Tue, 25 Oct 2016 09:21:36 -0400 Received: from mout.kundenserver.de ([212.227.126.187]:54178 "EHLO mout.kundenserver.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932522AbcJYNVe (ORCPT ); Tue, 25 Oct 2016 09:21:34 -0400 From: Arnd Bergmann To: Binoy Jayan Cc: Doug Ledford , Sean Hefty , Hal Rosenstock , linux-rdma@vger.kernel.org, Linux kernel mailing list Subject: Re: [PATCH v2 6/8] IB/hns: Replace counting semaphore event_sem with wait_event Date: Tue, 25 Oct 2016 15:21:19 +0200 Message-ID: <3400101.E3rphEBHnx@wuerfel> User-Agent: KMail/5.1.3 (Linux/4.4.0-34-generic; KDE/5.18.0; x86_64; ; ) In-Reply-To: References: <1477396919-27669-1-git-send-email-binoy.jayan@linaro.org> <5375848.bxLv0aDzDv@wuerfel> MIME-Version: 1.0 Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" X-Provags-ID: V03:K0:CPhfWNJ3gr5fBy7XuODVoIyOn+Io5LqO7mDbDAAbIuioVzoMekf O53z4i3b6xin/hoTezcLxjO/T7IgiQztUeJIyeSH9Aa3EXF8dxdVn3osVPk5cNftj3+LADI gTRzvexcx3hT8Q5bsGugH+Fld6qwKOTnbTlVkiwAswD0AeVcLmCfUtc96JCm5OCogu13Eth 5erGHj97eQV7dvheSAnEA== X-UI-Out-Filterresults: notjunk:1;V01:K0:MyZEGdFteW8=:LcJR8S8SWHJI38bpbw6a5v fDBKaM7KDdO/mv5DtMa0/Rbt/5mI5xjAC5ACI2Z6lV5dYj4NeiHBLXX7HXRlsasaChUck6HYx /tpAG5KHe1Hj9sQ85/anTY80VEeR9zjorNTZ2izFpxjyOG/BymFrkOATHfFQpqxug/FiIDvtx u/wUVYxPXIXLHNQVxOgPurHDqdBOJl567AJ7J6DVJWk2Z8jylpFd/pNvgZT/kkO1df+pjAjXy p383F6x1UHqMEfYIYSSoQX7DMY5ROg18VUVavDjCrJtPCRbvWLjSVWBYi2VljB2e4k9QvGWam uNminU0LE1iTbdw4wazzZwjY4WT3cPnAQBn+v/Rexr+cRWQQY90rTA0h45xXqX1HS3UuJwO2d VoSi533ZoPz1vOf4x+VQ9ASreO0J8/vECJDU2K+a6CNpq/jjqA9cViCtnpRkgX4tmH+DCL3NR ZpQDO9GqTf/6BNGvsVJsr5UPvR7rDDgrdvPNa/tGQSQHaFo1cStxBegStfQDrw2QLRn7mTyJZ WOmS66aeCU46LKE/nbzXkd56wcsN9Ej5lcm/2GH4ZJSc4PXdlTMp8Q2OHCHWTGl2IvpcRUX0w EUYTrbX9AaclfnSYVqvY4MPfYeK9BPVNKzdjkRpHPMu6VgaduBmmzNQbT3XuOECer8nfKKIVT cXyM48CxyVeoDc/2QGwiiuqghMoQOpZ0nYziehJrZNQLmiqn8Sf+DMl/MyJ2zVMALrU9/YYeT b47jyNghqKZLGIhu Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2330 Lines: 71 On Tuesday, October 25, 2016 6:29:45 PM CEST Binoy Jayan wrote: > On 25 October 2016 at 17:58, Arnd Bergmann wrote: > > On Tuesday, October 25, 2016 5:31:57 PM CEST Binoy Jayan wrote: > >> static int __hns_roce_cmd_mbox_wait(struct hns_roce_dev *hr_dev, u64 in_param, > >> u64 out_param, unsigned long in_modifier, > >> @@ -198,11 +218,12 @@ static int __hns_roce_cmd_mbox_wait(struct hns_roce_dev *hr_dev, u64 in_param, > >> struct hns_roce_cmdq *cmd = &hr_dev->cmd; > >> struct device *dev = &hr_dev->pdev->dev; > >> struct hns_roce_cmd_context *context; > >> - int ret = 0; > >> + int orig_free_head, ret = 0; > >> + > >> + wait_event(cmd->wq, (orig_free_head = atomic_free_node(cmd, -1)) != -1); > >> > >> spin_lock(&cmd->context_lock); > >> - WARN_ON(cmd->free_head < 0); > >> - context = &cmd->context[cmd->free_head]; > >> + context = &cmd->context[orig_free_head]; > >> context->token += cmd->token_mask + 1; > >> cmd->free_head = context->next; > >> spin_unlock(&cmd->context_lock); > >> > > > > You get the lock in atomic_free_node() and then again right after that. > > Why not combine the two and only take the lock inside of that > > function that returns a context? > > > Hi Arnd, > > I couldn't figure out a way to wait for a node to be free followed by > acquiring a lock > in an atomic fashion. If the lock is acquired after the wait_event, > there could be race > between the wait_event and acquiring the lock. If the lock is acquired > before the > wait_event, the process may goto sleep with the lock held which is not desired. > Could you suggest me of some way to circumvent this? Something like static struct hns_roce_cmd_context *hns_roce_try_get_context(struct hns_roce_cmdq *cmd) { struct hns_roce_cmd_context *context = NULL; spin_lock(&cmd->context_lock); if (cmd->free_head < 0) goto out; context = &cmd->context[cmd->free_head]; ... /* update free_head */ out: spin_unlock(&cmd->context_lock); return context; } ... static struct hns_roce_cmd_context *hns_roce_get_context(struct hns_roce_cmdq *cmd) { struct hns_roce_cmd_context *context; wait_event(cmd->wq, (context = hns_roce_try_get_context(cmd))); return context; } Arnd