From owner-freebsd-current@FreeBSD.ORG Tue Aug 28 18:23:10 2012 Return-Path: Delivered-To: current@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id 5D6601065670; Tue, 28 Aug 2012 18:23:10 +0000 (UTC) (envelope-from luigi@onelab2.iet.unipi.it) Received: from onelab2.iet.unipi.it (onelab2.iet.unipi.it [131.114.59.238]) by mx1.freebsd.org (Postfix) with ESMTP id 193028FC08; Tue, 28 Aug 2012 18:23:09 +0000 (UTC) Received: by onelab2.iet.unipi.it (Postfix, from userid 275) id 2621B7300B; Tue, 28 Aug 2012 20:42:26 +0200 (CEST) Date: Tue, 28 Aug 2012 20:42:26 +0200 From: Luigi Rizzo To: Konstantin Belousov Message-ID: <20120828184226.GB68683@onelab2.iet.unipi.it> References: <20120827073403.GA49223@onelab2.iet.unipi.it> <201208271227.54785.jhb@freebsd.org> <20120828155025.GA66068@onelab2.iet.unipi.it> <201208281240.29612.jhb@freebsd.org> <20120828172606.GR33100@deviant.kiev.zoral.com.ua> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20120828172606.GR33100@deviant.kiev.zoral.com.ua> User-Agent: Mutt/1.4.2.3i Cc: Andriy Gapon , freebsd-current@freebsd.org, current@freebsd.org Subject: Re: per file descriptor device callbacks ? X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.5 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: Tue, 28 Aug 2012 18:23:10 -0000 On Tue, Aug 28, 2012 at 08:26:06PM +0300, Konstantin Belousov wrote: ... > > dev_clone() is rather gross and a lot harder to use than > > devfs_set_cdevpriv(). If you are fine with the inherent problems > > of the device pager (you can't ever make mappings go away), you can > > just assign each client a unique offset into your shared object's > > memory space. However, if you are exporting shared memory buffers, > > then a better model might be to let your clients use > > shm_open(SHM_ANON) to create buffers, then pass them into your driver > > via an ioctl() and use shm_map() to map them into the kernel. > > Well, there is a new OBJT_MGTDEVICE pager, which together with > d_mmap_single() allows to have even per-mapping data. i915kms uses it. > You do not need cdevpriv for the per-mapping data. > > Also, MGTDEVICE does track the mappings of the pages, so you can clean > up on device destruction. interesting, thanks for the pointer, i'll look it up in i915kms. Does this exist also in stable/9 ? It would be a shame otherwise... > Normal callbacks of the device pager allows to execute ctr/dtr methods > at the time of mapping creation and tear down. what would be a good way to install my own ctr/dtr methods ? I only found out a rather crude one, and it only works for the destructor: static struct cdev_pager_ops saved_cdev_pager_ops; static struct cdev_pager_ops netmap_cdev_pager_ops; static void netmap_dev_pager_dtor(void *handle) { saved_cdev_pager_ops.cdev_pg_dtor(handle); // my code here D("ready to release memory for %p", handle); } static int netmap_mmap_single(struct cdev *cdev, vm_ooffset_t *foff, vm_size_t objsize, vm_object_t *objp, int prot) { vm_object_t obj; /* XXX check size etc. */ obj = vm_pager_allocate(OBJT_DEVICE, cdev, objsize, prot, *foff, curthread->td_ucred); if (obj == NULL) return EINVAL; if (saved_cdev_pager_ops.cdev_pg_fault == NULL) { D("initialize cdev_pager_ops"); saved_cdev_pager_ops = *(obj->un_pager.devp.ops); netmap_cdev_pager_ops = *(obj->un_pager.devp.ops); netmap_cdev_pager_ops.cdev_pg_dtor = netmap_dev_pager_dtor; }; obj->un_pager.devp.ops = &netmap_cdev_pager_ops; *objp = obj; /* XXX perhaps do something more here, such as install * mappings for the pages so we have no faults later. */ return 0; } static struct cdevsw netmap_cdevsw = { .d_version = D_VERSION, .d_name = "netmap", .d_open = netmap_open, .d_mmap = netmap_mmap, .d_mmap_single = netmap_mmap_single, .d_ioctl = netmap_ioctl, .d_poll = netmap_poll, .d_close = netmap_close, }; cheers luigi