Received: by 10.223.185.116 with SMTP id b49csp3856943wrg; Tue, 6 Mar 2018 06:12:41 -0800 (PST) X-Google-Smtp-Source: AG47ELsM0T5mtoMb5VqPKA7CObbK81uKdZ7pekvR3K++o1fBduQSUF43chBjC4rcVh56BWydzFCL X-Received: by 10.98.232.6 with SMTP id c6mr18897005pfi.242.1520345561875; Tue, 06 Mar 2018 06:12:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1520345561; cv=none; d=google.com; s=arc-20160816; b=wYCpOKsvriKkHQCBtO6MG5lCv+uNz0TOotlNHhsAG+Ilt4FfuKyxZHZX2Nd2VdPU14 rB8zZNdN6bFr19c3wmhV8zFqzcGwULV4mCz4ydULrJvI0zEo+/yi+2+T1WsJs3b/VgAh CQdAtas4a7MaIhnZ46uOcKTzfta+KfuXFDyews9QSBtI5omczP1BAAyF/A5bS34E/49/ 4Io/neTBQxTszeAVgA2OzDGCJlgnAgGhDe6tweUW/O1HSdv9ytpc1qObV1MRVcYw2vRp TiIXjqOfTnmJ8hEGqlWXVXE2+vAJdvlIjc9XfqNc9923A+SXBRSdQOKqEQ6M5MdMa3ar qUXw== 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:dkim-signature:arc-authentication-results; bh=th9Sp3o5IQSn4wlvyP0N2Bybd0wAyr/nTty04d6dSrI=; b=ImgFxirk3Ei7zzvVsCbM0fojvperq0jHh1cYdws7nYx2A3N1F8JJbHDz51whWzjCRP b+TXHc9WRAoqU8KQE1IxOwsRoBJb6TJx9oBRcLYWiaecmrw5PyU/hC9+q8D3NZtoxzbW 0gKl4ObzK5N+ijVlFH9oS9Q9KQPJoosD//Y6KSwI4lPOhCpKO4bjJtnSdWNczZCZclvk AX0zduvYO6vRuho88AubhFGRn30QbFpcPe8MtizpTIIMMoCmjESkAGhFs8vms3JDg5gL PQ5VQKhiY5xzqyR9enWw3ddZrA4H6VHrEk/s0Kb3XxGdBpoZCoAo42HrU4lRN8jIR2nN 0Bfw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=SREgEfgQ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n11-v6si10948993pls.727.2018.03.06.06.12.27; Tue, 06 Mar 2018 06:12:41 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=SREgEfgQ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933011AbeCFOKy (ORCPT + 99 others); Tue, 6 Mar 2018 09:10:54 -0500 Received: from bombadil.infradead.org ([198.137.202.133]:59678 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932147AbeCFOKw (ORCPT ); Tue, 6 Mar 2018 09:10:52 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.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:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=th9Sp3o5IQSn4wlvyP0N2Bybd0wAyr/nTty04d6dSrI=; b=SREgEfgQn0TOUYt/xmBpaKDHK OE1l7ko1/V41eseyw7BUUkdz+anK7Kzv5G4VhNRKT6uGrU6CUhGp97Eq7tYtaGkhBz5O3+RWQF1NW 5GgiztPzrNYbYywYgxEx2+TiD9XLtg8d+0IkoLAVFPUIenYZEdfn1A53FrDcIcPcevzfLbXb5QIEz KqQPUrhH0TwryTi8oY+SGziTRNGdbxKK5S7jo31OvSimqpGAHSIwQ+0GKYnYoFVDGZaEYImiJuVyv 4aKFhF4U7M/qXf4bFZu3mttXfgoSwuARawQsJo+bPDS8i470ukav6WZXVIuvlmV6xGBZ5opTqTs8W lH7k9GdKw==; Received: from willy by bombadil.infradead.org with local (Exim 4.89 #1 (Red Hat Linux)) id 1etDIm-0005Pm-3F; Tue, 06 Mar 2018 14:10:48 +0000 Date: Tue, 6 Mar 2018 06:10:47 -0800 From: Matthew Wilcox To: Igor Stoppa Cc: david@fromorbit.com, keescook@chromium.org, mhocko@kernel.org, labbott@redhat.com, linux-security-module@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel-hardening@lists.openwall.com Subject: Re: [PATCH 1/7] genalloc: track beginning of allocations Message-ID: <20180306141047.GB13722@bombadil.infradead.org> References: <20180228200620.30026-1-igor.stoppa@huawei.com> <20180228200620.30026-2-igor.stoppa@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20180228200620.30026-2-igor.stoppa@huawei.com> User-Agent: Mutt/1.9.2 (2017-12-15) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Feb 28, 2018 at 10:06:14PM +0200, Igor Stoppa wrote: > + * Encoding of the bitmap tracking the allocations > + * ----------------------------------------------- > + * > + * The bitmap is composed of units of allocations. > + * > + * Each unit of allocation is represented using 2 consecutive bits. > + * > + * This makes it possible to encode, for each unit of allocation, > + * information about: > + * - allocation status (busy/free) > + * - beginning of a sequennce of allocation units (first / successive) > + * > + * > + * Dictionary of allocation units (msb to the left, lsb to the right): > + * > + * 11: first allocation unit in the allocation > + * 10: any subsequent allocation unit (if any) in the allocation > + * 00: available allocation unit > + * 01: invalid > + * > + * Example, using the same notation as above - MSb.......LSb: > + * > + * ...000010111100000010101011 <-- Read in this direction. > + * \__|\__|\|\____|\______| > + * | | | | \___ 4 used allocation units > + * | | | \___________ 3 empty allocation units > + * | | \_________________ 1 used allocation unit > + * | \___________________ 2 used allocation units > + * \_______________________ 2 empty allocation units > + * > + * The encoding allows for lockless operations, such as: > + * - search for a sufficiently large range of allocation units > + * - reservation of a selected range of allocation units > + * - release of a specific allocation > + * > + * The alignment at which to perform the research for sequence of empty > + * allocation units (marked as zeros in the bitmap) is 2^1. > + * > + * This means that an allocation can start only at even places > + * (bit 0, bit 2, etc.) in the bitmap. > + * > + * Therefore, the number of zeroes to look for must be twice the number > + * of desired allocation units. > + * > + * When it's time to free the memory associated to an allocation request, > + * it's a matter of checking if the corresponding allocation unit is > + * really the beginning of an allocation (both bits are set to 1). > + * > + * Looking for the ending can also be performed locklessly. > + * It's sufficient to identify the first mapped allocation unit > + * that is represented either as free (00) or busy (11). > + * Even if the allocation status should change in the meanwhile, it > + * doesn't matter, since it can only transition between free (00) and > + * first-allocated (11). This seems unnecessarily complicated. Why not handle it like this: - Double the bitmap in size (as you have done) but - The first half of the bits are unchanged from the existing implementation - The second half of the bits are used for determining the length On allocation, you look for a sufficiently-large string of 0 bits in the first-half. When you find it, you set all of them to 1, and set one bit in the second-half to indicate where the tail of the allocation is (you might actually want to use an rbtree or something to handle this ... using all these bits seems pretty inefficient).