Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp721267ybt; Mon, 6 Jul 2020 21:40:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwgkjXxG4WaSuGv293qdkwkDPG8IRrH/MAnasJdXXyar3wSkRR5BvrKaJozN45CNECFUXko X-Received: by 2002:a05:6402:542:: with SMTP id i2mr59712698edx.318.1594096822440; Mon, 06 Jul 2020 21:40:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594096822; cv=none; d=google.com; s=arc-20160816; b=WMZwKX/oUatEWWGxMQ76xUyW35sWdZ437sJeKNWqfkXwlyOZZgsOjvNdnZL8V46tuh JYrSUuifnsuryolhRs05sloi7CjFslWLKLksi7qXVJhYmYUDeQC+1ZtjeBWvhjwtJlWt LeLo3vDlD5u5u/iLCMqcsVxYnlhfrEiKzKNXYwTfvK38WFzvJGJHWh8X1ocZTSJZNzOo nHOZIrqCKcHqTQnTbXHazqz2EtLf/pSUKJcujbHQebYEX1M8XUyN4W14u4HnVPifeKy2 OJ6+AA/ujFVzgr5VTVAgF1YBQJOLUO5H39JkrfdcxP1zY8Y3vEkyRauGjQ/xOhVZqpyU pUBA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=9Fn0Sg12AGqPvCet6/i3cv1zgohdxMq/STaY/sGGKBM=; b=F2p2Swul5BE7FgJeHq9ZAxImQGMokdaBQe0Fj21sBhhngxvud8zvqda2CMyAII+Wio CGQVhWzcbO75VKWE++4yeMA5LjMMnCwa0ciE2hR4HAwrlp7TVT1PLTqrckPqTycwgKYr q217IgyDKG2/21F3dIke/3V5VQi981jDwLqkL7gtZSeTSxTdd4drqiGRvuyy46i44fn7 VSoIZ+wYopsEhCapRb+gN7cJCxVUQbSSlY+TlZV/Rvium6Z0ARJaqtixSxkNoSiYqjen 7GY9e6VLXELkktC+IYKxUKGhw0bzm+QWYu75Ve+Xs/TikQ79MJrY7dq5bjBguC+iA6d7 cU5Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=casper.20170209 header.b=L5Hcz9Nf; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p7si13731723ejb.486.2020.07.06.21.39.59; Mon, 06 Jul 2020 21:40:22 -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; dkim=fail header.i=@infradead.org header.s=casper.20170209 header.b=L5Hcz9Nf; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727003AbgGGEjU (ORCPT + 99 others); Tue, 7 Jul 2020 00:39:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45878 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726805AbgGGEjU (ORCPT ); Tue, 7 Jul 2020 00:39:20 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 18BDCC061755; Mon, 6 Jul 2020 21:39:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=9Fn0Sg12AGqPvCet6/i3cv1zgohdxMq/STaY/sGGKBM=; b=L5Hcz9Nfo/DNgz2qozusOJmZHe 6yxUrH93tLHZbhy98UmvJDnpk9Y24OKFN4qqQ/Td8wKicNYtqRomjKM9dOGxzQod0nHXGvuRzvlCz MIwmIylU70LmPziuuj+aRsoMlk5qRDcAjDOVW9UscpZidiktVdQFFIQgRFTnV0p7EjipJ0RONLfoW yb3LvUHaViGZLc+ewmuBdz8E/NU30C+049ft+YFzTtGvpfxduQN0iz94zTXnfMgjiWkT/KvNit0Nz ZD1Z9Yea+wyvH6LJDU7aAM6cFYfi/ZAZoxuUUTfTzjaPCSDEY+edAUIdBbCEa95nA0OAczWB4vVgD 3E4+MfRw==; Received: from willy by casper.infradead.org with local (Exim 4.92.3 #3 (Red Hat Linux)) id 1jsfNo-0006OJ-8J; Tue, 07 Jul 2020 04:39:04 +0000 Date: Tue, 7 Jul 2020 05:39:04 +0100 From: Matthew Wilcox To: Sean Christopherson Cc: Jarkko Sakkinen , x86@kernel.org, linux-sgx@vger.kernel.org, linux-kernel@vger.kernel.org, linux-security-module@vger.kernel.org, linux-mm@kvack.org, Andrew Morton , Jethro Beekman , Haitao Huang , Chunyang Hui , Jordan Hand , Nathaniel McCallum , Seth Moore , Suresh Siddha , andriy.shevchenko@linux.intel.com, asapek@google.com, bp@alien8.de, cedric.xing@intel.com, chenalexchen@google.com, conradparker@google.com, cyhanish@google.com, dave.hansen@intel.com, haitao.huang@intel.com, josh@joshtriplett.org, kai.huang@intel.com, kai.svahn@intel.com, kmoy@google.com, ludloff@google.com, luto@kernel.org, nhorman@redhat.com, puiterwijk@redhat.com, rientjes@google.com, tglx@linutronix.de, yaozhangx@google.com Subject: Re: [PATCH v34 11/24] x86/sgx: Add SGX enclave driver Message-ID: <20200707043904.GJ25523@casper.infradead.org> References: <20200707030204.126021-1-jarkko.sakkinen@linux.intel.com> <20200707030204.126021-12-jarkko.sakkinen@linux.intel.com> <20200707033617.GF25523@casper.infradead.org> <20200707041151.GE143804@linux.intel.com> <20200707042904.GD5208@linux.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200707042904.GD5208@linux.intel.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Jul 06, 2020 at 09:29:04PM -0700, Sean Christopherson wrote: > > > > + idx_start = PFN_DOWN(start); > > > > + idx_end = PFN_DOWN(end - 1); > > > > + > > > > + for (idx = idx_start; idx <= idx_end; ++idx) { > > > > + mutex_lock(&encl->lock); > > > > + page = radix_tree_lookup(&encl->page_tree, idx); > > > > + mutex_unlock(&encl->lock); > > > > + > > > > + if (!page || (~page->vm_max_prot_bits & vm_prot_bits)) > > > > + return -EACCES; > > > > > > You should really use an iterator here instead of repeated lookups. > > > xas_for_each() will probably be what you want. > > > > Thank you for your remarks. I'll look into using xarray for this. > > Question for Matthew: > > To enforce the "page must be populated" rule, is there a clean way to retrieve > the index of the current entry? Our entries/pages don't have information > about their index. Or should we just count the number of entries and check > 'em at the end? E.g. > > xas_for_each(...) { > if (~page->vm_max_prot_bits & vm_prot_bits) > return -EACCES; > nr_entries++; > } > > if (nr_entries != (end_index - start_index)) > return -EACCES; Probably best just to steal the implementation from here: pgoff_t page_cache_next_miss(struct address_space *mapping, pgoff_t index, unsigned long max_scan) { XA_STATE(xas, &mapping->i_pages, index); while (max_scan--) { void *entry = xas_next(&xas); if (!entry || xa_is_value(entry)) break; if (xas.xa_index == 0) break; } return xas.xa_index; } although I think you have a simpler task. XA_STATE(xas, ..., start_index); for (;;) { struct page *page = xas_next(&xas); if (!page || (~page->vm_max_prot_bits & vm_prot_bits)) return -EACCES; } return 0; should do the trick, I think.