From owner-freebsd-current@freebsd.org Mon Jul 13 12:28:43 2015 Return-Path: Delivered-To: freebsd-current@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 521DB9999E7 for ; Mon, 13 Jul 2015 12:28:43 +0000 (UTC) (envelope-from rizzo.unipi@gmail.com) Received: from mail-la0-x232.google.com (mail-la0-x232.google.com [IPv6:2a00:1450:4010:c03::232]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority G2" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id CFC95A71; Mon, 13 Jul 2015 12:28:42 +0000 (UTC) (envelope-from rizzo.unipi@gmail.com) Received: by laem6 with SMTP id m6so8149431lae.0; Mon, 13 Jul 2015 05:28:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:date:message-id:subject:from:to:cc:content-type; bh=gC6dxNGYrnH55RTBhDmxdRdeLNyW2zyKcNvYhSgao78=; b=i9Jys0Wn7V6Tg0fGijo6+2lVf1o7we3X5tLX66ukR3pYgfPXPJx/W3HWW1Vq0lf1PT KvlUEklVqCOa02A4/yECsERtlI/Otl+Nzhpr1fIxrP5dr3Cn+ZVQVbv6HPu4FhCm5XRo Zo/Z8pJzguFPsDpVe+f6y+SfTM50xrrvcU5VKCDeBWcJHNoKA8IKM1HShzY8WoPCaCly JOJtvDh8xdhajOcBiBRL5AVKPzvMF69oR/z/MznadaOjdpxclyy+q74c9hAuCBbjYfvC E1yE/T+Hh+jhflmppGZqmI4zmeWiuTCzLGNejH+r41LuO7xLSxt15p7GShi4INZHef2Z vRBQ== MIME-Version: 1.0 X-Received: by 10.152.246.37 with SMTP id xt5mr31889530lac.83.1436790521021; Mon, 13 Jul 2015 05:28:41 -0700 (PDT) Sender: rizzo.unipi@gmail.com Received: by 10.114.230.103 with HTTP; Mon, 13 Jul 2015 05:28:40 -0700 (PDT) Date: Mon, 13 Jul 2015 14:28:40 +0200 X-Google-Sender-Auth: LPlpOWZjqOe1SBLiraPxClerUNM Message-ID: Subject: protection against module unloading ? From: Luigi Rizzo To: freebsd-current , Adrian Chadd , John Baldwin Cc: Luigi Rizzo Content-Type: text/plain; charset=UTF-8 X-Content-Filtered-By: Mailman/MimeDel 2.1.20 X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 13 Jul 2015 12:28:43 -0000 Hi, I am trying to understand how to protect efficiently against module removals when a device driver is in use. This issue came up some time ago when trying netmap loaded as a module. --- Summary of the current mode of operation (as i understand it): --- When a device is compiled as a module, all devfs calls in sys/fs/devfs/devfs_vnops.c are protected by setting dev->si_threadcount (within dev*_refthread()) for the duration of the call. This is implemented using devfs_fp_check()/dev_relthread() which in turn, for the module case, updates the dev->si_threadcount under dev_lock() which is a single shared mutex, devmtx . The above is problematic for driver modules that do a lot of I/O using poll, ioctl, read or write, because of the system-wide contention on devmtx To mitigate the contention, statically compiled modules have the SI_ETERNAL attribute that prevents the lock (major contention point) and si_threadcount update. --- Alternative ? I was hoping to make the protection mechanism cheaper by only increasing dev->si_threadcount on devfs_open() without calling dev_relthread() ), and then decreasing si_threadcount on defvs_close() . This way the reference is active for the lifetime of the file descriptor without needing to track individual high-frequency calls. This is probably not enough because there could be mmap handlers, which remain active even after the device is closed. However I do not think the current devfs_fp_check() suffices either, and so drivers that have their own mmap code in a module that can be unloaded must already implement their own mechanism to protect against unloading. In fact, this could be achieved easily by making these drivers use dev_refthread() to hold a refcount while active. Would the above work ? Anything missing ? cheers luigi -- -----------------------------------------+------------------------------- Prof. Luigi RIZZO, rizzo@iet.unipi.it . Dip. di Ing. dell'Informazione http://www.iet.unipi.it/~luigi/ . Universita` di Pisa TEL +39-050-2217533 . via Diotisalvi 2 Mobile +39-338-6809875 . 56122 PISA (Italy) -----------------------------------------+-------------------------------