From patchwork Fri Nov 8 07:18:54 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Hellstrom X-Patchwork-Id: 21423 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-yh0-f69.google.com (mail-yh0-f69.google.com [209.85.213.69]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 8007720BB3 for ; Fri, 8 Nov 2013 07:19:39 +0000 (UTC) Received: by mail-yh0-f69.google.com with SMTP id v1sf2583337yhn.8 for ; Thu, 07 Nov 2013 23:19:39 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:from:to:date:message-id:subject :precedence:list-id:list-unsubscribe:list-archive:list-post :list-help:list-subscribe:mime-version:errors-to:sender :x-original-sender:x-original-authentication-results:mailing-list :content-type:content-transfer-encoding; bh=sX2aME9HEn23sS0iBLiU1a8kGyBsQLgTw0ObAt68VgY=; b=ORc6QIcRdMusckHfXiK7cbO3w2O9Y+I32T6V7aW3y3mYknJuvN1R1HEVEb1IYAXknZ rZH7c14xzfXsnH91aNzzSdLyK4a3siZ98PQIo7u1eGi8o19X1jBDepjd0a9QnbYnOqOW nBg5y2CfMjMsG/MD7yLueD2MaCVnd85J3aU8PpuA+46/ex92ktBglZt/APHyu2em9rP+ Pxn6ZPuOgd39xFt24tAYgQlNpXsOXoINB3Raf1AqtlH4Rj97tusRK7TmVyx8dVBkrugf ECNs1e7ZRXM7fGgXKousXeholYseojlNAmw+NH8tXM3P0KxdRJQ2K5NtwHa4zIdL5nCL Noow== X-Gm-Message-State: ALoCoQlg743nzswubIrv3QrAjaRNzs+bT7faxo+zPv0BdXECwUQqopOPM6UYP4TflO06caUexaK2 X-Received: by 10.58.2.98 with SMTP id 2mr3968968vet.3.1383895178861; Thu, 07 Nov 2013 23:19:38 -0800 (PST) X-BeenThere: patchwork-forward@linaro.org Received: by 10.49.119.9 with SMTP id kq9ls1580500qeb.48.gmail; Thu, 07 Nov 2013 23:19:38 -0800 (PST) X-Received: by 10.221.44.136 with SMTP id ug8mr10599078vcb.13.1383895178760; Thu, 07 Nov 2013 23:19:38 -0800 (PST) Received: from mail-vb0-f45.google.com (mail-vb0-f45.google.com [209.85.212.45]) by mx.google.com with ESMTPS id mh9si3395641vec.64.2013.11.07.23.19.38 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 07 Nov 2013 23:19:38 -0800 (PST) Received-SPF: neutral (google.com: 209.85.212.45 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) client-ip=209.85.212.45; Received: by mail-vb0-f45.google.com with SMTP id p6so1126113vbe.32 for ; Thu, 07 Nov 2013 23:19:38 -0800 (PST) X-Received: by 10.221.40.10 with SMTP id to10mr10619853vcb.22.1383895178546; Thu, 07 Nov 2013 23:19:38 -0800 (PST) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patches@linaro.org Received: by 10.220.174.196 with SMTP id u4csp10188vcz; Thu, 7 Nov 2013 23:19:38 -0800 (PST) X-Received: by 10.194.89.233 with SMTP id br9mr10667473wjb.15.1383895177469; Thu, 07 Nov 2013 23:19:37 -0800 (PST) Received: from ip-10-141-164-156.ec2.internal (lists.linaro.org. [54.225.227.206]) by mx.google.com with ESMTPS id v2si4160602wje.154.2013.11.07.23.19.36 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Thu, 07 Nov 2013 23:19:37 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linaro-mm-sig-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) client-ip=54.225.227.206; Received: from localhost ([127.0.0.1] helo=ip-10-141-164-156.ec2.internal) by ip-10-141-164-156.ec2.internal with esmtp (Exim 4.76) (envelope-from ) id 1VegJS-0002m4-2M; Fri, 08 Nov 2013 07:17:02 +0000 Received: from smtp-outbound-1.vmware.com ([208.91.2.12]) by ip-10-141-164-156.ec2.internal with esmtp (Exim 4.76) (envelope-from ) id 1VegJM-0002lt-Cm for linaro-mm-sig@lists.linaro.org; Fri, 08 Nov 2013 07:16:56 +0000 Received: from sc9-mailhost3.vmware.com (sc9-mailhost3.vmware.com [10.113.161.73]) by smtp-outbound-1.vmware.com (Postfix) with ESMTP id BDF8B2982D; Thu, 7 Nov 2013 23:19:18 -0800 (PST) Received: from zcs-prod-ext-mta-2.vmware.com (zcs-prod-ext-mta-2.vmware.com [10.113.62.224]) by sc9-mailhost3.vmware.com (Postfix) with ESMTP id BC474403D0; Thu, 7 Nov 2013 23:19:18 -0800 (PST) Received: from zcs-prod-ext-mta-2.vmware.com (localhost.localdomain [127.0.0.1]) by zcs-prod-ext-mta-2.vmware.com (Postfix) with ESMTP id 8DF7BC005D; Thu, 7 Nov 2013 23:19:33 -0800 (PST) Received: from localhost.localdomain (zimbra-prod-ext-proxy-vip.vmware.com [10.113.63.87]) by zcs-prod-ext-mta-2.vmware.com (Postfix) with ESMTPSA; Thu, 7 Nov 2013 23:19:32 -0800 (PST) From: Thomas Hellstrom To: dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, linux-kernel@vger.kernel.org Date: Thu, 7 Nov 2013 23:18:54 -0800 Message-Id: <1383895134-4535-1-git-send-email-thellstrom@vmware.com> X-Mailer: git-send-email 1.7.10.4 Subject: [Linaro-mm-sig] [PATCH RFC] dma-buf/fs Add get_[file|dma_buf]_unless_doomed X-BeenThere: linaro-mm-sig@lists.linaro.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: , List-Help: , List-Subscribe: , MIME-Version: 1.0 Errors-To: linaro-mm-sig-bounces@lists.linaro.org Sender: linaro-mm-sig-bounces@lists.linaro.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: thellstrom@vmware.com X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 209.85.212.45 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 In this context, a "doomed" object is an object whose refcount has reached zero, but that has not yet been freed. To avoid mutual refcounting vmwgfx need to have a non-refcounted pointer to a dma-buf in a lookup structure. The pointer is removed in the dma-buf destructor. To allow lookup-structure private locks we need get_dma_buf_unless_doomed(). This common refcounting scenario is described with examples in detail in the kref documentaion. The solution with local locks is under kref_get_unless_zero(). See also kobject_get_unless_zero() and its commit message. Since dma-bufs are using the attached file for refcounting, get_dma_buf_unless_doomed maps directly to a get_file_unless_doomed. Signed-off-by: Thomas Hellstrom --- include/linux/dma-buf.h | 16 ++++++++++++++++ include/linux/fs.h | 15 +++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/include/linux/dma-buf.h b/include/linux/dma-buf.h index dfac5ed..6e58144 100644 --- a/include/linux/dma-buf.h +++ b/include/linux/dma-buf.h @@ -162,6 +162,22 @@ static inline void get_dma_buf(struct dma_buf *dmabuf) get_file(dmabuf->file); } +/** + * get_dma_buf_unless_doomed - convenience wrapper for get_file_unless_doomed + * + * @dmabuf: [in] pointer to dma_buf + * + * Obtain a dma-buf reference from a lookup structure that doesn't refcount + * the dma-buf, but synchronizes with its release method to make sure it has + * not been freed yet. See for example kref_get_unless_zero documentation. + * Returns true if refcounting succeeds, false otherwise. + */ +static inline bool __must_check +get_dma_buf_unless_doomed(struct dma_buf *dmabuf) +{ + return get_file_unless_doomed(dmabuf->file); +} + struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf, struct device *dev); void dma_buf_detach(struct dma_buf *dmabuf, diff --git a/include/linux/fs.h b/include/linux/fs.h index 3f40547..a96c333 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -824,6 +824,21 @@ static inline struct file *get_file(struct file *f) atomic_long_inc(&f->f_count); return f; } + +/** + * get_file_unless_doomed - convenience wrapper for get_file_unless_doomed + * @file: [in] pointer to file + * + * Obtain a file reference from a lookup structure that doesn't refcount + * the file, but synchronizes with its release method to make sure it has + * not been freed yet. See for example kref_get_unless_zero documentation. + * Returns true if refcounting succeeds, false otherwise. + */ +static inline bool __must_check get_file_unless_doomed(struct file *f) +{ + return atomic_long_inc_not_zero(&f->f_count) != 0L; +} + #define fput_atomic(x) atomic_long_add_unless(&(x)->f_count, -1, 1) #define file_count(x) atomic_long_read(&(x)->f_count)