From owner-freebsd-questions@freebsd.org Sun Feb 23 07:00:29 2020 Return-Path: Delivered-To: freebsd-questions@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id E14C625AC96; Sun, 23 Feb 2020 07:00:29 +0000 (UTC) (envelope-from babupalit@gmail.com) Received: from mail-ot1-x343.google.com (mail-ot1-x343.google.com [IPv6:2607:f8b0:4864:20::343]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "smtp.gmail.com", Issuer "GTS CA 1O1" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 48QGL459n4z4ZtR; Sun, 23 Feb 2020 07:00:28 +0000 (UTC) (envelope-from babupalit@gmail.com) Received: by mail-ot1-x343.google.com with SMTP id i6so5912484otr.7; Sat, 22 Feb 2020 23:00:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=u6RbcMvweXR7Vyl+3iO9jvT7UjWnMwtPQHuJ6MoW3+4=; b=KOHItPFdV/MIyBwi1ILDotGRgKcMxrkhj3aSngeIuH1N6nT/ROaGXmbD9LbzwocTWs Xz63YS/FwBklYGqgOq8Ny/G2NdpFaruKUDW7FTxW5KN2hWHGIkJHx0sBNwe2eSAKeW0r 42MKM6kSm744l+qAGBT8wVHzuHGaqwufSh77daQ6kMJNd7PMY59pae7joUsMqb4P2aHn nDzSyJW4EQ4tCuSSwCoNddAtz0yFJyukOG8zlQtbegyHLOYjoDz2b/Q4K45LJKwqhumF zhYjnOLsKUMGGsNsbBcI43/HlFLNT8iop7boqHvSd754Ukjp5nkmq18A2Vv58qqNctWd 3RNA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=u6RbcMvweXR7Vyl+3iO9jvT7UjWnMwtPQHuJ6MoW3+4=; b=YLxh65JHPPMEMfjQyt7VYlF2dYetH/kF7kPvVyzVGq4ugUmOMITxaUGx86nkpXZAnf qt8pT0FjH6hX308I2IF8WPa7KAk1aVln0oJmsd4g+K3kQ5V7OklqaoU78HpwXbdxs2C3 fDd4nUAb7DIDFzD4Qg87uGqfsBHYfnNKZSMTrn64g4jLZqrEELGvK4kEviYYVA2uaKRK Nt+R9wy+KXpympyYgfAnncjyrF8Jxe2L9nywY9fnAKAeP1P6NYIcRKIOYWcJtYXoXS66 tNDrFBJztOp+euIcr8tzWLDpxx8mbV623eSNlZmurn/bNwhwYpXD5kA7+L9b41r9Kki0 cZtQ== X-Gm-Message-State: APjAAAXoufapnxsd/ttmFVYBzggznuqPpYAeHxUqhUN079efJEs5GtXE cuOrGKAC2SbISqKry4qOvresqbbrhsln/lyZ6i2aghdw X-Google-Smtp-Source: APXvYqzpLC7Wjk92xcY1BZDAp9f83aQ1u1vMwM8RA/mm1f4lb/mW4cRpUwk+jpQFVdUvUozKARPQXOfgTkhHlId8qkI= X-Received: by 2002:a9d:4e82:: with SMTP id v2mr21085413otk.352.1582441226808; Sat, 22 Feb 2020 23:00:26 -0800 (PST) MIME-Version: 1.0 References: <890a299c074fc83a02911583531d686257924be8.camel@freebsd.org> In-Reply-To: <890a299c074fc83a02911583531d686257924be8.camel@freebsd.org> From: Arpan Palit Date: Sun, 23 Feb 2020 12:30:15 +0530 Message-ID: Subject: Re: msleep_spin is failed to waken up even after wakeup routine is invoked for the same. To: Ian Lepore Cc: freebsd-hackers@freebsd.org, freebsd-questions@freebsd.org X-Rspamd-Queue-Id: 48QGL459n4z4ZtR X-Spamd-Bar: -- Authentication-Results: mx1.freebsd.org; dkim=pass header.d=gmail.com header.s=20161025 header.b=KOHItPFd; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (mx1.freebsd.org: domain of babupalit@gmail.com designates 2607:f8b0:4864:20::343 as permitted sender) smtp.mailfrom=babupalit@gmail.com X-Spamd-Result: default: False [-3.00 / 15.00]; ARC_NA(0.00)[]; NEURAL_HAM_MEDIUM(-1.00)[-1.000,0]; R_DKIM_ALLOW(-0.20)[gmail.com:s=20161025]; FROM_HAS_DN(0.00)[]; RCPT_COUNT_THREE(0.00)[3]; R_SPF_ALLOW(-0.20)[+ip6:2607:f8b0:4000::/36]; FREEMAIL_FROM(0.00)[gmail.com]; MIME_GOOD(-0.10)[multipart/alternative,text/plain]; TO_MATCH_ENVRCPT_ALL(0.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000,0]; TO_DN_SOME(0.00)[]; IP_SCORE_FREEMAIL(0.00)[]; IP_SCORE(0.00)[ip: (2.88), ipnet: 2607:f8b0::/32(-1.88), asn: 15169(-1.67), country: US(-0.05)]; DKIM_TRACE(0.00)[gmail.com:+]; DMARC_POLICY_ALLOW(-0.50)[gmail.com,none]; RCVD_IN_DNSWL_NONE(0.00)[3.4.3.0.0.0.0.0.0.0.0.0.0.0.0.0.0.2.0.0.4.6.8.4.0.b.8.f.7.0.6.2.list.dnswl.org : 127.0.5.0]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+,1:+,2:~]; FREEMAIL_ENVFROM(0.00)[gmail.com]; ASN(0.00)[asn:15169, ipnet:2607:f8b0::/32, country:US]; RCVD_COUNT_TWO(0.00)[2]; RCVD_TLS_ALL(0.00)[]; DWL_DNSWL_NONE(0.00)[gmail.com.dwl.dnswl.org : 127.0.5.0] Content-Type: text/plain; charset="UTF-8" X-Content-Filtered-By: Mailman/MimeDel 2.1.29 X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 23 Feb 2020 07:00:30 -0000 Thanks for replying Ian. The code is similar to the fix structure code as you have mentioned above. Its happening after running the test for 5 to 10 iterations, out of 2k posted command to the hardware only 1 or 2 of them are failing to wake up properly. mutex lock has been properly introduced in both wait_for_completion code & interrupt handler part and also the doneflag is getting set from the interrupt context properly. I am running 16 thread of 2k command simultaneously. I have verified that from the hardware side the command completion is proper, its failing in waking up the sleep in software side. So I am suspecting some issue with the scheduler which failed to wake up the sleep in this case. Below is the current code structure: /* Wait for Completion */ mtx_lock(cmd_comp_lock); completion = false; ret = execute_cmd(cmd); if (ret == 0) { while ( !completion && err == 0) err = msleep(cmd, cmd_comp_lock, PCATCH, "cmd_xfer", hz); } mtx_unlock(cmd_comp_lock); /* Interrupt handler */ mtx_lock_spin(cmd_lock); *//Disable inerrupt *// error checks completion = true; wakeup_one(cmd); mtx_unlock_spin(cmd_lock); *//Enable interrupt On Wed, Feb 19, 2020 at 8:12 PM Ian Lepore wrote: > On Wed, 2020-02-19 at 14:13 +0530, Arpan Palit wrote: > > Hi, > > > > I am facing one issue where wakeup rountine call is unable to waken up a > > msleep_spin routine call with a timeout value of 10 Seconds. > > > > The real scenario is as follows: post a hardware command and sleep using > > msleep_spin routine till interrupt comes, After getting the interrupt > waken > > up the sleeping process using wakeup_one/wakeup routine call. As there > are > > more than 2048 command and 16 parallel threads are running, > > observed randomly *one or two of the posted command* is *timing out* for > > which the *interrupt has came and also wakeup routine is invoked *after > > getting the interrupt for the same command. > > > > Note: > > *The issue is not seen when number of commands are less than 2048 with > > timeout of 10 seconds. > > *The issue can be seen with less number of commands also when timeout > value > > 1 second. > > > > Can anyone please provide me an optimized way to schedule the process or > a > > better way to do the scheduling. > > > > Thanks, > > Arpan Palit > > > > Is there any chance this is the classic "missed wakeup" scenario, where > the wakeup happens before the thread enters msleep_spin()? That can > happen with code structured like > > enqueue_request(req); > err = msleep_spin(req, etc); > /* Handle done req or timeout */ > > and a fix is to structure the code using the same idiom required for > pthread_cond_wait() in userland, something like: > > req->doneflag = false; > enqueue_request(req); > while (!req->doneflag && err == 0) > err = msleep_spin(req, etc); > /* Handle done req or timeout */ > > and of course on the interrupt handler side you need something like > > /* lock mutex, do hardware stuff */ > req->doneflag = true; > wakeup(req); > /* unlock mutex */ > > -- Ian > > >