From owner-freebsd-hackers@freebsd.org Wed Feb 26 06:45:48 2020 Return-Path: Delivered-To: freebsd-hackers@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 6E74224E365; Wed, 26 Feb 2020 06:45:48 +0000 (UTC) (envelope-from babupalit@gmail.com) Received: from mail-oi1-x241.google.com (mail-oi1-x241.google.com [IPv6:2607:f8b0:4864:20::241]) (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 48S5sk3M9fz3Hms; Wed, 26 Feb 2020 06:45:46 +0000 (UTC) (envelope-from babupalit@gmail.com) Received: by mail-oi1-x241.google.com with SMTP id q81so2018595oig.0; Tue, 25 Feb 2020 22:45:46 -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=SfN7kmW5vQGwFT6YDq8c+tKesMlItz1oVGpb3hZhW+I=; b=OPtwSHPJVjipis5cQ5QOYXRDBxugn4ctknx7epnIKeYk/HFfFDlHA24f8XMt4enJxr gKYqFBfLhOAqRM5JomEPKC1jooGR1vKBQC0LMv2CwJooeUB6EH8QO0TAH95FWtaIE5js SrcHonPPeoiJSVqomv6Z60qljus5gX+mX+MmVzzU1+DC6KxW+uVbiubTTi07triJc0jH goO3qNJuZILODmTqANrRRcOcrDaFn6ca6cbz27SbE+ptjwmWyK72YgQXEQ0Y/WHXtkmv RGPCSb3FcRQuBgMNq/yKxBDlwyasb7tqbhqte5fQKLsJck6m8UqGc3oGNOiraHe9PtXz t09Q== 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=SfN7kmW5vQGwFT6YDq8c+tKesMlItz1oVGpb3hZhW+I=; b=PawBS5ZcCMBA7N2jrxS6jJEDZtNGa2F0oOgaFj+8YfEWtFNtEa2oicGNQ2viZX6eXk pzdGKRmWQhS3e1FaB210tTh9FLCjIIj5+nvCQxQiqZZYqb/76fothQ0FX/q19Tb2JyEo uI3hnTy48JsmUknOytNuOgbHt0UzBZEEyQJ215Zldbrs6tmbLTlLK2zziy7uHxe20rDF Jro+juLJtACah2e23mz5pEWmNNA+Qi+CXsUmkjVCbWbGYUMNgJO/P7ZNJfN9wlCBBg91 7H63UPQ0R16RXEyzs9Ofynigs4Wk50b9+Jte67jVLjShrv1l8j3I9DdZvrIeiGNREPV2 yosw== X-Gm-Message-State: APjAAAXKuFnjnUvJweySEdPAjURzm41HSxaSlpmQbvoFl97Gvur73PyY gl7eijwcmyAFTg1Ss9TvvzaAsqo3kEm6uHR+XlLCbQ== X-Google-Smtp-Source: APXvYqzOP/bPihUs2b0FKkWGOw39v+cUws0b8X3HQeuH4R0Y61eAKO4bfZWU+YQE0OnOk30709/s8ISN5f5OCTem7IE= X-Received: by 2002:aca:bb54:: with SMTP id l81mr1831238oif.175.1582699544834; Tue, 25 Feb 2020 22:45:44 -0800 (PST) MIME-Version: 1.0 References: <890a299c074fc83a02911583531d686257924be8.camel@freebsd.org> <08fd4dcf7231237abf749834d6bce006892eac26.camel@freebsd.org> In-Reply-To: <08fd4dcf7231237abf749834d6bce006892eac26.camel@freebsd.org> From: Arpan Palit Date: Wed, 26 Feb 2020 12:15:33 +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: 48S5sk3M9fz3Hms X-Spamd-Bar: - Authentication-Results: mx1.freebsd.org; dkim=pass header.d=gmail.com header.s=20161025 header.b=OPtwSHPJ; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (mx1.freebsd.org: domain of babupalit@gmail.com designates 2607:f8b0:4864:20::241 as permitted sender) smtp.mailfrom=babupalit@gmail.com X-Spamd-Result: default: False [-2.00 / 15.00]; ARC_NA(0.00)[]; NEURAL_HAM_MEDIUM(-1.00)[-0.999,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)[]; URI_COUNT_ODD(1.00)[3]; IP_SCORE_FREEMAIL(0.00)[]; IP_SCORE(0.00)[ip: (2.14), 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)[1.4.2.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-hackers@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 26 Feb 2020 06:45:48 -0000 Thanks for the clarification Ian. The issue is not happening now. I have ran the test for more than 1000 iterations multiple times now, but looks like some device specific internal issue has come up, working on it. If I need any more help in future, I hope I can ask you again. Thanks again for the quick response. -Arpan On Mon, Feb 24, 2020 at 1:18 AM Ian Lepore wrote: > On Sun, 2020-02-23 at 12:30 +0530, Arpan Palit wrote: > > 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 > > > > That shows two different mutexes being used, which violates the safe > pattern for doing conditional wakeups. The same mutex that protects > the setting of the 'completion' variable in the interrupt handler must > be used to protect the clearing and testing of that variable in the > mainline code, to avoid missed wakeups. That means using msleep_spin() > with cmd_lock in the mainline code. > > -- Ian > > > > > 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 > > > > > > > > > > > > > _______________________________________________ > > freebsd-hackers@freebsd.org mailing list > > https://lists.freebsd.org/mailman/listinfo/freebsd-hackers > > To unsubscribe, send any mail to " > > freebsd-hackers-unsubscribe@freebsd.org" > >