Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp1945220ybk; Thu, 21 May 2020 20:35:55 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxKiGJzaNECa2zjbNLjIMZtMVee+rH1o+zRBzA8/1R1e9gX1VaMc46WCsK3L1et12N1dSn5 X-Received: by 2002:a17:906:355b:: with SMTP id s27mr6244861eja.436.1590118555226; Thu, 21 May 2020 20:35:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1590118555; cv=none; d=google.com; s=arc-20160816; b=JxDK5Gcobxk9j1YoXOTtftPadaEQmQpvQpasCDxeZIh3db+NJZLSqAqRalHWE6TviK IiHrLnu9re3xz8oSobPdQ8kk+sZyuiLhiuqJRRuAaMz5FJE/zoNvbxALBB5NK0F9Iwhx AbCWNstEI61OPUfQY9Hsn1jsF3pFF9DE1fTiAyK7wRqiOHlLeeN/+U1pu+3/63ONbyN3 maajsHMbK0tv3ss8S9QgdnvJeqIkQoAU+bcuBMyHgxmSkiPf2e/8LHLNzpxOlVrjOTvf gIzoDa2lhc8PJfN9K39Th42VZiu52Mfpw4BMXgy2g00br/jMozXh0kN/mA+dkH/AVBBG Q+sA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:ironport-sdr:ironport-sdr; bh=ZauiNr9g+ZVVIkwYQDNnDzKmB82saICaN8rvpUHOrck=; b=RD24BgD3vttRYIqCtN+kn7S8VFDcVcH3r2eKYn0upM0v15UOq9sPzUe3wj7X1cq65j 0EhInzn9E0SEL8LTX9AFnuFpmIU3A8xvvq57Pgs7+RcgEs33GKe0uuolNVUBznmaz0nA 9E4RqogWA9ai2lXOjCC1NKE2wExc6uXbK/LTjNsOrzTWSVMTNc7Xvotv3+Dvf6LiX8b6 Uoi6/zpFYQS1LxzKgfZizOSH4H/zRNffWWwYjy81BlbiwCq9qtarTWY2x3BGbvSIwX/Y l+cm5XtzaiM3V8hr9sv4sPlXJP0QtKWmfBOzV2MTuVgrcCQcxjFqM+JYVqfPazDvlCfK v42g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i3si3818820edv.382.2020.05.21.20.35.32; Thu, 21 May 2020 20:35:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728007AbgEVDdm (ORCPT + 99 others); Thu, 21 May 2020 23:33:42 -0400 Received: from mga03.intel.com ([134.134.136.65]:31472 "EHLO mga03.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727024AbgEVDdm (ORCPT ); Thu, 21 May 2020 23:33:42 -0400 IronPort-SDR: H/ihoj3BjQqTboe2SNh17xKfTVsR67Tvyh2CxgJbMfHE0hUvMy0gu8cq/1GgVjinhj1OFlUETB aY3RIt/fg/4Q== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 May 2020 20:33:41 -0700 IronPort-SDR: 6Oi3dGVXxH8PRgoerk4WKGC1UAawi03NxYcSSdFN3TzL8k2eGY6ooz7giyX/beEhnU3RfLAFgn Gcv4mcwAWuFg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.73,420,1583222400"; d="scan'208";a="300996064" Received: from sjchrist-coffee.jf.intel.com (HELO linux.intel.com) ([10.54.74.152]) by orsmga008.jf.intel.com with ESMTP; 21 May 2020 20:33:40 -0700 Date: Thu, 21 May 2020 20:33:40 -0700 From: Sean Christopherson To: Jarkko Sakkinen Cc: linux-kernel@vger.kernel.org, x86@kernel.org, linux-sgx@vger.kernel.org, akpm@linux-foundation.org, dave.hansen@intel.com, nhorman@redhat.com, npmccallum@redhat.com, haitao.huang@intel.com, andriy.shevchenko@linux.intel.com, tglx@linutronix.de, kai.svahn@intel.com, bp@alien8.de, josh@joshtriplett.org, luto@kernel.org, kai.huang@intel.com, rientjes@google.com, cedric.xing@intel.com, puiterwijk@redhat.com, linux-security-module@vger.kernel.org, Suresh Siddha , Jethro Beekman , Haitao Huang , Chunyang Hui , Jordan Hand , Seth Moore Subject: Re: [PATCH v30 10/20] x86/sgx: Linux Enclave Driver Message-ID: <20200522033340.GB23459@linux.intel.com> References: <20200515004410.723949-1-jarkko.sakkinen@linux.intel.com> <20200515004410.723949-11-jarkko.sakkinen@linux.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200515004410.723949-11-jarkko.sakkinen@linux.intel.com> User-Agent: Mutt/1.5.24 (2015-08-30) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, May 15, 2020 at 03:44:00AM +0300, Jarkko Sakkinen wrote: > +static int sgx_open(struct inode *inode, struct file *file) > +{ > + struct sgx_encl *encl; > + int ret; > + > + encl = kzalloc(sizeof(*encl), GFP_KERNEL); > + if (!encl) > + return -ENOMEM; > + > + atomic_set(&encl->flags, 0); > + kref_init(&encl->refcount); > + INIT_RADIX_TREE(&encl->page_tree, GFP_KERNEL); > + mutex_init(&encl->lock); > + INIT_LIST_HEAD(&encl->mm_list); > + spin_lock_init(&encl->mm_lock); > + > + ret = init_srcu_struct(&encl->srcu); We're leaking a wee bit of memory here; enough to burn through 14gb in a few minutes with my newly resurrected EPC cgroup test. The possibility for failure should have been a dead giveaway that this allocates memory, but the "init" name threw me off. :-/ > + if (ret) { > + kfree(encl); > + return ret; > + } > + > + file->private_data = encl; > + > + return 0; > +} ... > +/** > + * sgx_encl_release - Destroy an enclave instance > + * @kref: address of a kref inside &sgx_encl > + * > + * Used together with kref_put(). Frees all the resources associated with the > + * enclave and the instance itself. > + */ > +void sgx_encl_release(struct kref *ref) > +{ > + struct sgx_encl *encl = container_of(ref, struct sgx_encl, refcount); > + > + sgx_encl_destroy(encl); > + > + if (encl->backing) > + fput(encl->backing); The above mem leak can be fixed by adding cleanup_srcu_struct(&encl->srcu); > + > + WARN_ON_ONCE(!list_empty(&encl->mm_list)); > + > + /* Detect EPC page leak's. */ > + WARN_ON_ONCE(encl->secs_child_cnt); > + WARN_ON_ONCE(encl->secs.epc_page); > + > + kfree(encl); > +} ... > +static int sgx_encl_add_page(struct sgx_encl *encl, unsigned long src, > + unsigned long offset, unsigned long length, > + struct sgx_secinfo *secinfo, unsigned long flags) > +{ ... > +err_out: > + radix_tree_delete(&encl_page->encl->page_tree, > + PFN_DOWN(encl_page->desc)); > + > +err_out_unlock: > + mutex_unlock(&encl->lock); > + up_read(¤t->mm->mmap_sem); > + > +err_out_free: > + sgx_free_page(epc_page); > + kfree(encl_page); > + > + /* > + * Destroy enclave on ENCLS failure as this means that EPC has been > + * invalidated. > + */ > + if (ret == -EIO) > + sgx_encl_destroy(encl); This needs to be called with encl->lock held to prevent racing with the reclaimer, e.g. sgx_encl_destroy() and sgx_reclaimer_write() can combine to corrupt secs_child_cnt, among other badness. It's probably worth adding a lockdep assert in sgx_encl_destroy() as well. We can either keep the lock across the above frees or retake the lock. I like retaking the lock to avoid inverting the ordering between encl->lock and mmap_sem (even though it's benign). This is an extremely rare path, no need to shave cycles. > + > + return ret; > +}