From owner-freebsd-drivers@freebsd.org Tue Jun 13 12:57:08 2017 Return-Path: Delivered-To: freebsd-drivers@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 6EA6CBF3AC2; Tue, 13 Jun 2017 12:57:08 +0000 (UTC) (envelope-from baijiaju1990@163.com) Received: from m12-13.163.com (m12-13.163.com [220.181.12.13]) by mx1.freebsd.org (Postfix) with ESMTP id 8345E7ADFE; Tue, 13 Jun 2017 12:57:06 +0000 (UTC) (envelope-from baijiaju1990@163.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=Message-ID:Date:From:MIME-Version:Subject; bh=FCpqg Q6/YH51f4vEb4KhA/0F3cbFe44Ny+1OZ9HhXeE=; b=RX2gfLmA6EPbXbPlM8+AF HZvibOTxeeBY66D3EWEKtoIoRe9nnK8gitb7nHNIAZOZ0CUYzH0NB1+M2xId9Xjf 3+bG+JvHayuU3d3tioOJokil0UglfGJa8V4jx/7TGF+Rp2ENx3uQ2WOcHWcxwkF1 f/0US7QR3FN2zmrFFqaIqk= Received: from [166.111.70.19] (unknown [166.111.70.19]) by smtp9 (Coremail) with SMTP id DcCowACHt+Mc4T9ZjLnlKQ--.54220S2; Tue, 13 Jun 2017 20:57:00 +0800 (CST) Message-ID: <593FE1C9.2060706@163.com> Date: Tue, 13 Jun 2017 20:59:53 +0800 From: Jia-Ju Bai User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:11.0) Gecko/20120410 Thunderbird/11.0.1 MIME-Version: 1.0 To: freebsd-questions@freebsd.org, freebsd-drivers@freebsd.org Subject: A question about in lock usage in FreeBSD Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-CM-TRANSID: DcCowACHt+Mc4T9ZjLnlKQ--.54220S2 X-Coremail-Antispam: 1Uf129KBjvdXoWrZF4fXr4kGF1kury8WF47XFb_yoWfJrg_Ca y0kFWDCa1agr4Sqa1jgF4fCr4jgF48ury5AFyrAF13Jw1fJa97WF1kua4xZF4fJF45Aw4D tr98Aa4a9rya9jkaLaAFLSUrUUUUUb8apTn2vfkv8UJUUUU8Yxn0WfASr-VFAUDa7-sFnT 9fnUUvcSsGvfC2KfnxnUUI43ZEXa7IU56uWJUUUUU== X-Originating-IP: [166.111.70.19] X-CM-SenderInfo: xedlyx5dmximizq6il2tof0z/1tbiYxv1elaDtXir4AAAsk X-BeenThere: freebsd-drivers@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Writing device drivers for FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 13 Jun 2017 12:57:08 -0000 Hi, I am a freshman in developing FreeBSD drivers, and I have a question in lock usage in FreeBSD. The kernel provides some kinds of locks in developing drivers, such as "mutex lock", "mutex spin lock", "rw lock" and "sx lock". I want to know which locks should be held when the thread can sleep. From my knowledge of FreeBSD document, I make the following points: (1) "rw lock" and "sx lock" can be used in this situation; (2) "mutex lock" and "mutex spin lock" are forbidden in this situation. If my points are right, I will make another point: mutex_lock will let the thread sleep when the lock is requested, so the mutex_lock can not be called in nested style (namely it is unsafe that mutex_lock is called again when a "mutex lock" is held). Are my points are right? I am looking forward to useful opinions and answers :) Thanks in advance :) Jia-Ju Bai From owner-freebsd-drivers@freebsd.org Thu Jun 15 07:00:01 2017 Return-Path: Delivered-To: freebsd-drivers@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id F06CDC31C04 for ; Thu, 15 Jun 2017 07:00:01 +0000 (UTC) (envelope-from jmg@gold.funkthat.com) Received: from gold.funkthat.com (gate2.funkthat.com [208.87.223.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "gate2.funkthat.com", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 6FE8A66DE6 for ; Thu, 15 Jun 2017 07:00:01 +0000 (UTC) (envelope-from jmg@gold.funkthat.com) Received: from gold.funkthat.com (localhost [127.0.0.1]) by gold.funkthat.com (8.15.2/8.15.2) with ESMTPS id v5F6nwms082485 (version=TLSv1.2 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Wed, 14 Jun 2017 23:49:58 -0700 (PDT) (envelope-from jmg@gold.funkthat.com) Received: (from jmg@localhost) by gold.funkthat.com (8.15.2/8.15.2/Submit) id v5F6nvV1082484; Wed, 14 Jun 2017 23:49:58 -0700 (PDT) (envelope-from jmg) Date: Wed, 14 Jun 2017 23:49:57 -0700 From: John-Mark Gurney To: Jia-Ju Bai Cc: freebsd-drivers@freebsd.org Subject: Re: A question about in lock usage in FreeBSD Message-ID: <20170615064957.GA2042@funkthat.com> Mail-Followup-To: Jia-Ju Bai , freebsd-drivers@freebsd.org References: <593FE1C9.2060706@163.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <593FE1C9.2060706@163.com> X-Operating-System: FreeBSD 11.0-RELEASE-p7 amd64 X-PGP-Fingerprint: D87A 235F FB71 1F3F 55B7 ED9B D5FF 5A51 C0AC 3D65 X-Files: The truth is out there X-URL: https://www.funkthat.com/ X-Resume: https://www.funkthat.com/~jmg/resume.html X-TipJar: bitcoin:13Qmb6AeTgQecazTWph4XasEsP7nGRbAPE X-to-the-FBI-CIA-and-NSA: HI! HOW YA DOIN? can i haz chizburger? User-Agent: Mutt/1.6.1 (2016-04-27) X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.4.3 (gold.funkthat.com [127.0.0.1]); Wed, 14 Jun 2017 23:49:58 -0700 (PDT) X-BeenThere: freebsd-drivers@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Writing device drivers for FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 15 Jun 2017 07:00:02 -0000 dropped -quesetions as that isn't the best list for this question. Jia-Ju Bai wrote this message on Tue, Jun 13, 2017 at 20:59 +0800: > I am a freshman in developing FreeBSD drivers, and I have a question in > lock usage in FreeBSD. > > The kernel provides some kinds of locks in developing drivers, such as > "mutex lock", "mutex spin lock", "rw lock" and "sx lock". I want to know > which locks should be held when the thread can sleep. > From my knowledge of FreeBSD document, I make the following points: > (1) "rw lock" and "sx lock" can be used in this situation; > (2) "mutex lock" and "mutex spin lock" are forbidden in this situation. > > If my points are right, I will make another point: > mutex_lock will let the thread sleep when the lock is requested, so the > mutex_lock can not be called in nested style (namely it is unsafe that > mutex_lock is called again when a "mutex lock" is held). > > Are my points are right? > I am looking forward to useful opinions and answers :) Have you read the locking(9)[1] man page? That should answer all of your questions wrt locking. [1] https://www.freebsd.org/cgi/man.cgi?query=locking&sektion=9&apropos=0&manpath=FreeBSD+11.0-RELEASE+and+Ports -- John-Mark Gurney Voice: +1 415 225 5579 "All that I will do, has been done, All that I have, has not." From owner-freebsd-drivers@freebsd.org Fri Jun 16 09:03:55 2017 Return-Path: Delivered-To: freebsd-drivers@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id F3ADFBFB7E4; Fri, 16 Jun 2017 09:03:54 +0000 (UTC) (envelope-from baijiaju1990@163.com) Received: from m12-18.163.com (m12-18.163.com [220.181.12.18]) by mx1.freebsd.org (Postfix) with ESMTP id 2F369772F9; Fri, 16 Jun 2017 09:03:53 +0000 (UTC) (envelope-from baijiaju1990@163.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:Subject:Message-ID:Date:MIME-Version; bh=FzeDY NBQjRxFdhEBSF9/lL4iz+nzdmMsnDa++ZYKbFo=; b=BasdRqUXKbsxB8BCIyy5A XUYUsNR+4pX5BiIKU+2VfnPihfK2Ko4PQctyjjxFceanPbod3Cs1pm2VdUmMxg1a eeKYODD/dPU17SpMuW0AKIY6ek5QZ3YpzLDwQICoQSLdzessEQaFlAnZnW/jmkHi 92NgXFUo8juuzFq4yqArgg= Received: from [166.111.70.34] (unknown [166.111.70.34]) by smtp14 (Coremail) with SMTP id EsCowACH1Rn3nkNZZ99uNA--.27125S2; Fri, 16 Jun 2017 17:03:51 +0800 (CST) To: freebsd-drivers@freebsd.org, freebsd-net@freebsd.org From: Jia-Ju Bai Subject: [Bug 220032] [if_alc] sys/dev/alc/if_alc.c: sleep-under-mutex bugs Message-ID: <7cc5abf5-f90c-c554-e54e-6f9534ef22ef@163.com> Date: Fri, 16 Jun 2017 17:03:51 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.2.0 MIME-Version: 1.0 Content-Language: en-US X-CM-TRANSID: EsCowACH1Rn3nkNZZ99uNA--.27125S2 X-Coremail-Antispam: 1Uf129KBjvdXoW7GF45Xw4fJFy5Jr1kZw18Krg_yoWfZwcE9F yrtry7Xw1xCFW2qa17AFWxuFW5WFW8Wa48ur4ruan8t34xXFs5tr4vvr13urZ3JayIvFnr Wr1Yg3y3CwnFvjkaLaAFLSUrUUUUUb8apTn2vfkv8UJUUUU8Yxn0WfASr-VFAUDa7-sFnT 9fnUUvcSsGvfC2KfnxnUUI43ZEXa7IU0yrW5UUUUU== X-Originating-IP: [166.111.70.34] X-CM-SenderInfo: xedlyx5dmximizq6il2tof0z/1tbiGBf4elXlaGaGGgAAsv Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Content-Filtered-By: Mailman/MimeDel 2.1.23 X-BeenThere: freebsd-drivers@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Writing device drivers for FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 16 Jun 2017 09:03:55 -0000 The alc driver may sleep under a mutex, and the function call paths in file "sys/dev/alc/if_alc.c" in FreeBSD 11.0 is: alc_resume [line 2648: acquire the mutex] alc_init_locked [line 2664] alc_init_rx_ring [line 3911] alc_newbuf [line 4469] bus_dmamap_load_mbuf_sg(BUS_DMA_WAITOK) [line 3472] --> may sleep alc_int_task [line 3330: acquire the mutex] alc_start_locked [line 3372] alc_encap [line 2942] bus_dmamap_load_mbuf_sg(BUS_DMA_WAITOK) [line 2780] --> may sleep The possible fix of these bugs is to set the last parameter in bus_dmamap_load_mbuf_sg to "BUS_DMA_NOWAIT". This bug is found by a static analysis tool written by myself, and it is checked by my review of the FreeBSD code. By the way, I am a freshman in developing FreeBSD drivers, and I am willing to submit a patch. But I do not know how to write and submit a patch, and where to submit, so I am looking forward to useful advice :) Jia-Ju Bai From owner-freebsd-drivers@freebsd.org Fri Jun 16 09:16:57 2017 Return-Path: Delivered-To: freebsd-drivers@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 83EE3BFBC3D; Fri, 16 Jun 2017 09:16:57 +0000 (UTC) (envelope-from baijiaju1990@163.com) Received: from m12-18.163.com (m12-18.163.com [220.181.12.18]) by mx1.freebsd.org (Postfix) with ESMTP id BE5F677998; Fri, 16 Jun 2017 09:16:56 +0000 (UTC) (envelope-from baijiaju1990@163.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:Subject:Message-ID:Date:MIME-Version; bh=yKLFn 01hX4duwPXV7AfqMAxpTXHOTXvVK0oZo/z+Wp0=; b=TWuM0rtpT7cHsIWnHog+V lxwV4EK+P1YX2gaQLjaOcGeY8CkTPVibbEkty7VdbqcMt1nquo1cJ9CKimvxWNrj JATaMeREptRNzPaMhh+/7bBkIYD/DkHrvLE/QK3nWTSFg9+aojYtT190U13qBXKX HLh1ZrVma3UHBSkVlSeGeY= Received: from [166.111.70.34] (unknown [166.111.70.34]) by smtp14 (Coremail) with SMTP id EsCowACnVa52nkNZ8rluNA--.63932S2; Fri, 16 Jun 2017 17:01:43 +0800 (CST) To: freebsd-drivers@freebsd.org, freebsd-net@freebsd.org From: Jia-Ju Bai Subject: [Bug 220033] [if_ixgb] sys/dev/ixgb/if_ixgb.c: a sleep-under-mutex bug in ixge_get_buf Message-ID: <4f7481c7-f70b-a76d-2169-8e57f9ba3cb8@163.com> Date: Fri, 16 Jun 2017 17:01:42 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.2.0 MIME-Version: 1.0 Content-Language: en-US X-CM-TRANSID: EsCowACnVa52nkNZ8rluNA--.63932S2 X-Coremail-Antispam: 1Uf129KBjvdXoW7GF4rAr1kKF4fXw1UGrWDXFb_yoW3ZFgE9F 1IvryUAw13WFW2ga13Cr48uFWF9FWfuFy8Wr4rXws0yFyxXFn5trsa9rWa9rWxG3yxuFn7 Zr1aq3y7C3ZrXjkaLaAFLSUrUUUUUb8apTn2vfkv8UJUUUU8Yxn0WfASr-VFAUDa7-sFnT 9fnUUvcSsGvfC2KfnxnUUI43ZEXa7IUnldgJUUUUU== X-Originating-IP: [166.111.70.34] X-CM-SenderInfo: xedlyx5dmximizq6il2tof0z/xtbBRRf4elO-7oHhLAAAse Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Content-Filtered-By: Mailman/MimeDel 2.1.23 X-BeenThere: freebsd-drivers@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Writing device drivers for FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 16 Jun 2017 09:16:57 -0000 The ixgb driver may sleep under a mutex, and the function call path in file "sys/dev/ixgb/if_ixgb.c" in FreeBSD 11.0 is: ixgb_init [line 750: acquire the mutex] ixgb_init_locked [line 751] ixgb_setup_receive_structures [line 699] ixgb_allocate_receive_structures [line 1913] ixgb_get_buf [line 1885] bus_dmamap_load(BUS_DMA_WAITOK) [line 1812] --> may sleep The possible fix of these bugs is to set the last parameter in bus_dmamap_load to "BUS_DMA_NOWAIT". This bug is found by a static analysis tool written by myself, and it is checked by my review of the FreeBSD code. By the way, I am a freshman in developing FreeBSD drivers, and I am willing to submit a patch. But I do not know how to write and submit a patch, and where to submit, so I am looking forward to useful advice :) Jia-Ju Bai