From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f198.google.com (mail-pl1-f198.google.com [209.85.214.198]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7187A3BB682 for ; Thu, 6 Aug 2026 01:12:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.198 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785978779; cv=none; b=pWxwQrOZvDq1pCR8dQMvym3Evx68FKrsAa0BjlselAhtj6SGS30tte/A4w20YqP8C6Oza1gDxIIeBuB+M9DVFKV1V9n3UYwalQFeITMhDLNXW107MYI/PDTunXlECg6hYlLUPTy5ankduDrxE5XGOpTcf7ZCzW9ef5++gXJrB3Y= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785978779; c=relaxed/simple; bh=TW8Az3zfcJFGFsisfgpEYY9q5sNJPNcDx1rQfEc6Rig=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=d4WoUCpmzZWmso6Jeqi3OMZ590hv6lDCj0EuHWzrlUJwCjhivIa1fb+KnXTcR7DqACXGbD7CFPCgxVAYNyR/7ZulX+uh2uf9/l2Urkjv3OYAy0JueU5JKRlDumvZSpi7DUs/WovyFQfFp5So836HQsXTbEglTrUZeNHBYqtuNt4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jyescas.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=sm6/owJb; arc=none smtp.client-ip=209.85.214.198 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jyescas.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="sm6/owJb" Received: by mail-pl1-f198.google.com with SMTP id d9443c01a7336-2cc88e22f92so34686095ad.1 for ; Wed, 05 Aug 2026 18:12:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1785978776; x=1786583576; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=qT2U8sjoXnH+lrEkvLzdxmLHnjH1gE3vQbkqTT4aFL8=; b=sm6/owJbTiJ/1qDEQkhATZiHe2UGOUEmt+s6u94t+slc//eWAlF3lg0qBuIy7gi6G2 XOKJ3XuOilu/mKhG7QY3a430T4SofZcPaKhR7xR5sOPMMS9+9n8Klb126acPJ8INuY4Y WDKDr30XMgflTJyoFKIZ/Mk2Mqd13cvaN/A2wbWtV2enWbStzzUJQAhtpxbTsMdF6MmJ uDQ8E6JOo8Yi1UOY3oR+7RnIJy3d1qADOVBfRa8B96IJrnNpg/6LOyf7lRUIugvzQ2Av R7BZfmqTB4LryhDejNKYQB5IMz9m6INxfQg7+sxdw8iNEJkkLLSRLxQ0DcCDhESM3ygf ju5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785978776; x=1786583576; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=qT2U8sjoXnH+lrEkvLzdxmLHnjH1gE3vQbkqTT4aFL8=; b=ZwIVR7bpKOkUUB51vHH5R0kyXU14Np63C8PoQnnCShB6ODS32qbQ9Qus3idveD/i70 aqZV67SOop3gm9Gq4E3GpthvvH+uuBvMKmfCy2d+zXAupaZm1gOarrDnjB0WdUt2SoOi 2g1akgts1thCqc49EbwXS8Ttu5+PVa0eAWhZ4iWhx6xC1wNxCgi6uFFcAeyivT49Tqtf HpIizsVFagV1wgN7i6jfoQIbQ3ZKO0Ziut6bLVDuFNsW+eELkt+bHGN49d5oZU3g2sWv iSSPinXY1Bn2GJ3oFZ4qOWU47+t3dZeHn7NmwI3Y3GpYhmIHrTP/MPQyhDeg0WNx+47/ 07qA== X-Forwarded-Encrypted: i=1; AHgh+Rp/NSYth9HkgaDoR/Gv8lVcGUqAPpZBzCsHoPSoTBO+bL9lI+IywuKwma12Vdt3uzJY2aeKe7aSCx8e0/0=@vger.kernel.org X-Gm-Message-State: AOJu0YwrnRnIQWHO9j0OLozKeMQFAMFy5tdBEAnW02gvQ2j7/kcy5JIH T6t8i9qyELSQWZc9bySXoONNVjwO2AIY+LUftwJUm4DxtlRlsoyaNrKgzGOfkpeca4rqyf7BwXb Rx5KKz6UyTA== X-Received: from dyctg10.prod.google.com ([2002:a05:693c:3b0a:b0:311:6294:c296]) (user=jyescas job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:41ce:b0:2ce:9c48:22d3 with SMTP id d9443c01a7336-2d0ca75151dmr122948145ad.11.1785978776189; Wed, 05 Aug 2026 18:12:56 -0700 (PDT) Date: Wed, 5 Aug 2026 18:09:13 -0700 In-Reply-To: <20260806011048.517229-1-jyescas@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260806011048.517229-1-jyescas@google.com> X-Mailer: git-send-email 2.55.0.629.g250fe7f194-goog Message-ID: <20260806011048.517229-13-jyescas@google.com> Subject: [RFC PATCH v2 12/16] mm: Define the make_alloc() function From: Juan Yescas To: Andrew Morton , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , open list , "open list:MEMORY MANAGEMENT - CORE" Cc: jyescas@google.com, android-mm@google.com, ljs@kernel.org, fvdl@google.com, tkjos@google.com, minchan@google.com, dskiba@google.com, open list , "open list:MEMORY MANAGEMENT - CORE" Content-Type: text/plain; charset="UTF-8" The make_alloc() is the core function that performs the allocation. The tasks of this function are: - Call the Buddy allocator to make the allocation. - Store the details of the allocation in struct page_alloc. - Insert the struct page_alloc in an xarray using the allocation id as key. If something fails during the allocation, the pages and cache will be freed. Signed-off-by: Juan Yescas --- Changes in v2: - Once that page_alloc is initialized, insert it in xarray. - Use 20 characters for the file name instead of 12. - Fix use-after-free issue while releasing the pages. mm/page_alloc_hogger.c | 72 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/mm/page_alloc_hogger.c b/mm/page_alloc_hogger.c index 4b72acfbc087..e22247b7bd98 100644 --- a/mm/page_alloc_hogger.c +++ b/mm/page_alloc_hogger.c @@ -96,6 +96,18 @@ static struct dentry *mmdir; +/** + * atomic_long_t allocs_file_seq - Represents the naming sequence used for + * allocation files. + */ +static atomic_long_t allocs_file_seq = ATOMIC_INIT(0); + +/** + * allocs_xa - Represent the xarray that contains the actual allocations perform + * by this driver. + */ +static DEFINE_XARRAY(allocs_xa); + /** * struct req_alloc - Represents the requested allocation * @node_idx: The Node index to allocate from. @@ -173,6 +185,47 @@ static inline void set_migrate_type_to_alloc_from(int migrate_type, gfp_t *flags } } +static int make_alloc(struct req_alloc *req, + gfp_t flags, unsigned long *alloc_id) +{ + struct page_alloc *pa; + struct page *page; + char new_alloc_name[20]; + int ret; + + page = alloc_pages_node_noprof(req->node_idx, flags, req->order); + if (page) { + pa = kmem_cache_alloc(page_alloc_cache, GFP_KERNEL); + if (!pa) { + ret = -ENOMEM; + goto free_pages; + } + + *alloc_id = atomic_long_inc_return(&allocs_file_seq); + snprintf(new_alloc_name, sizeof(new_alloc_name), "%lu", + *alloc_id); + + pa->req_alloc = req; + pa->page = page; + + ret = xa_insert(&allocs_xa, *alloc_id, pa, GFP_KERNEL); + if (ret) + goto free_page_alloc; + } else { + return -ENOMEM; + } + + return 0; + +free_page_alloc: + kmem_cache_free(page_alloc_cache, pa); + +free_pages: + __free_pages(page, req->order); + + return ret; +} + /** * req_page_alloc_write() - Allocates the pages on the requested node, zone, * order and migrate type. Once the allocation is performed, a file is created @@ -182,10 +235,12 @@ static ssize_t req_page_alloc_write(struct file *file, const char __user *ubuf, size_t cnt, loff_t *ppos) { struct req_alloc *req = file->private_data; + unsigned long alloc_id; unsigned long nr_pages_allocs; unsigned long *allocs_ids; gfp_t flags = 0; int ret; + int i; ret = kstrtoul_from_user(ubuf, cnt, 10, &nr_pages_allocs); if (ret) @@ -199,9 +254,26 @@ static ssize_t req_page_alloc_write(struct file *file, const char __user *ubuf, set_zone_to_alloc_from(req->zone_idx, &flags); set_migrate_type_to_alloc_from(req->migrate_type, &flags); + for (i = 0; i < nr_pages_allocs; i++) { + ret = make_alloc(req, flags, &alloc_id); + if (ret) + goto free_allocs; + + allocs_ids[i] = alloc_id; + } + kfree(allocs_ids); return cnt; + +free_allocs: + /* + * A proper clean up of the pages and page_alloc allocations will + * be done in a follow up patch of this topic. + */ + kfree(allocs_ids); + + return ret; } static const struct file_operations req_page_alloc_fops = { -- 2.55.0.629.g250fe7f194-goog