From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f46.google.com (mail-wm1-f46.google.com [209.85.128.46]) (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 1049340DB36 for ; Thu, 23 Jul 2026 08:45:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.46 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784796329; cv=none; b=ZlXMi8jBe3C5EedE/U61NXNR4HgWRjPhwNJydtrChVgGtgXzVfDu80DlEaxiaNNUilOXD698G6+WKkzr5b9pXjVd57sHcYlotVak0JkcGq+HxBeG6huFunTenqYqpvg+Q37dCZUjEdD/YWlzl/4bAAIdiQJMrDAVp6zcsjNhV/g= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784796329; c=relaxed/simple; bh=4SksbWu6UNIXwiN5dy3kr5/CrJlxiJFuiolDqe8ixm4=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=KLWQ3NOqxe0VJkZQtbW1tJr+pkoLHXQBdrmq3bGBVlR3WtqFYImmruqSoYJXnBmALZ6Xn2YoXLBhNo5wQa/CSpJ7dkdZghG1WDe5oWIezpuolr12LjCiTHSx3mAozpNw8YBYAQgMFy2fBF0HaT6JCz0qXM5/QYbv8E7TGAKIZUg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=j5hBeJ+i; arc=none smtp.client-ip=209.85.128.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="j5hBeJ+i" Received: by mail-wm1-f46.google.com with SMTP id 5b1f17b1804b1-495590dde14so4092925e9.0 for ; Thu, 23 Jul 2026 01:45:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1784796325; x=1785401125; darn=vger.kernel.org; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:subject:cc:to:from:date:from:to:cc:subject :date:message-id:reply-to:content-type; bh=QNSCWeKhrxGWAlQghBfhaL555IVsMmUGtowa8Jgc3Rs=; b=j5hBeJ+i3ElW8hPjErXWLhMQ4N//pGLR1nEuNKw1eej6yVzHho8z8nZJBsjBjFqO3B dR9N2SqObaTBnta6fsgKtWBBEco+0kXLMsHH4V6bmeQsAYGZ/IM5YfKly+U4MrJLtHUo gjXkm86R5TZjz0X9REUOJWEC9AqpHlQJ7gQeQr1PC4BRTiYB58huUjhJGBUmGDp2dj08 o4Ssq/Bc7wkTwbQn2737UPAxq7cbe7sHtziSqqgj/zuGwofH8utxoptYFvrqAn0Z/lht LYCHeco+kgSNeHhCUM8XixvQgP1WdflCj/Vjm9kj5oVxsuykXXL1SX1Q2Nj4Kag1LwxT iOwQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784796325; x=1785401125; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:subject:cc:to:from:date:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=QNSCWeKhrxGWAlQghBfhaL555IVsMmUGtowa8Jgc3Rs=; b=ISCdSWNCCGgAgH5Qcjvdr2BuPwrNghVSzGbV8dN886aFz9NLGWfkC8WivljM1ke4z0 jor4bw5E0HaZRCzg7cBTcZ+3gpdKyl0v/z/Esoieby6MzxeTuxemZ/5iWaXkbE6VXTL8 4TI+478InsGoFTOpv/Qo2+8yVxzCbcJcZsf0zVoUGLhMuS18aNVgsfewgXYygnfMFIZs GsNqv1nsefu8PuamrJwb8i3GJQWarUR0K954vcm7Y4jysI5JAjwZSSLI+hZjichNpQYc miYZcYJRZ6WDK+SkcN9zaLTeIwo4nheEWlFnli+GyjPSKigsC9XQKbZ5tLPUPS8SX0nU qRHw== X-Forwarded-Encrypted: i=1; AHgh+RobZ+xDW3jXU44hfhmCm/axjFBqW1Nuyq6a1Zy7qDJJ3Z89/oAcwEvPsaEgX5sXGwGrBlA0Z8tIzEU936w=@vger.kernel.org X-Gm-Message-State: AOJu0YxRY9YeJKJb1Pdx8pEECNVja5BPgBfUX6r9lZX7pJjTR0OtFZmC jt82TS7yJp7aGc+J8wQQ4oqtz5F/Mb8NFRrwgWHd9C8B/gBHsABJQKIl X-Gm-Gg: AR+sD10cAzTsm0m2xyvaNPWfCLzXeiyF20DKgflJLmSJrGmYUC8miNBFPIVoXlhtZXW t3c8mZiXhAVBJocHaQCF9r33moKJ5yX6B1x9P/eTwbZ5Yi6h9ksYnXhGEJZZ7WF5XkeWkxidh3L 0+nfCnlBv7Pzo1R7yMxgPFiYWk/C8u2gJL5WjKd4GCIDQBFjjlqPn2dMzVstli+0UUxn0S3b6Yd P0I0vF/IOiZSQB1kcvbUeK52Xu7hbr0OjCQWh+udekliKysgLzB/uqbi8Kdyxf/MrcIE1SEEz3j XNxfUFY2+5xSdZgjdAZS8Jo9rDOBRopVkAIs4OdpryaIfn+pee3HYKtZe2f5kogJr3KW1r/Gaqk IYtZQWBq+2E7i50mHoxWYL0HEOgvZR8FobuNVNy/keK23t4aHxQBgUz6b+oUtCDlVKtffJm+7yO 5L2HK69bkdhiwf/qJFNG4fBFQBwmlEVXl0KHJMCCM= X-Received: by 2002:a05:600c:1914:b0:495:5e86:4e59 with SMTP id 5b1f17b1804b1-49573cf6226mr22983005e9.22.1784796324550; Thu, 23 Jul 2026 01:45:24 -0700 (PDT) Received: from pumpkin (82-69-66-36.dsl.in-addr.zen.co.uk. [82.69.66.36]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-495653bfabesm234783595e9.10.2026.07.23.01.45.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Jul 2026 01:45:24 -0700 (PDT) Date: Thu, 23 Jul 2026 09:45:22 +0100 From: David Laight To: Mahad Ibrahim Cc: Kees Cook , Arnd Bergmann , Greg Kroah-Hartman , Mike Rapoport , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] lkdtm: use kmalloc() instead of __get_free_page Message-ID: <20260723094522.535b284c@pumpkin> In-Reply-To: <20260722230246.2869-1-mahad.ibrahim.dev@gmail.com> References: <20260722230246.2869-1-mahad.ibrahim.dev@gmail.com> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.38; arm-unknown-linux-gnueabihf) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Wed, 22 Jul 2026 23:02:46 +0000 Mahad Ibrahim wrote: > lkdtm_debugfs_entry and direct_entry use __get_free_page to allocate a > temporary buffer, perform copy_from_user to get the crashtype name, > strim() to strip whitespace and find_crashtype to find the corresponding > crashtype that is being requested. > > The lkdtm_debugfs_read uses __get_free_page to allocate a temporary > buffer to store all the available crashtypes, and then copy it to > userspace. > > The buffers that are allocated can be allocated with kmalloc as there is > nothing special that requires a struct page, or the page allocator. > > kmalloc() additionally provides a better API that doesn't require ugly > casts which obfuscate the code and kfree does not need to know the size > of the freed object. > > Replace use of __get_free_page() with kmalloc(). None of those buffers need to be PAGE_SIZE. Even the sanity limit for overlong requests could be 4k. The longest 'crashtype->name' is probably about 32 characters, so could probably go on stack. And can't this code use the sysfs/kernfs wrappers? David > > Signed-off-by: Mahad Ibrahim > --- > drivers/misc/lkdtm/core.c | 16 ++++++++-------- > 1 file changed, 8 insertions(+), 8 deletions(-) > > diff --git a/drivers/misc/lkdtm/core.c b/drivers/misc/lkdtm/core.c > index ededa32d6744..01bebcb33bd4 100644 > --- a/drivers/misc/lkdtm/core.c > +++ b/drivers/misc/lkdtm/core.c > @@ -236,11 +236,11 @@ static ssize_t lkdtm_debugfs_entry(struct file *f, > if (count >= PAGE_SIZE) > return -EINVAL; > > - buf = (char *)__get_free_page(GFP_KERNEL); > + buf = kmalloc(PAGE_SIZE, GFP_KERNEL); > if (!buf) > return -ENOMEM; > if (copy_from_user(buf, user_buf, count)) { > - free_page((unsigned long) buf); > + kfree(buf); > return -EFAULT; > } > /* NULL-terminate and remove enter */ > @@ -248,7 +248,7 @@ static ssize_t lkdtm_debugfs_entry(struct file *f, > strim(buf); > > crashtype = find_crashtype(buf); > - free_page((unsigned long)buf); > + kfree(buf); > > if (!crashtype) > return -EINVAL; > @@ -271,7 +271,7 @@ static ssize_t lkdtm_debugfs_read(struct file *f, char __user *user_buf, > ssize_t out; > char *buf; > > - buf = (char *)__get_free_page(GFP_KERNEL); > + buf = kmalloc(PAGE_SIZE, GFP_KERNEL); > if (buf == NULL) > return -ENOMEM; > > @@ -290,7 +290,7 @@ static ssize_t lkdtm_debugfs_read(struct file *f, char __user *user_buf, > > out = simple_read_from_buffer(user_buf, count, off, > buf, n); > - free_page((unsigned long) buf); > + kfree(buf); > > return out; > } > @@ -313,11 +313,11 @@ static ssize_t direct_entry(struct file *f, const char __user *user_buf, > if (count < 1) > return -EINVAL; > > - buf = (char *)__get_free_page(GFP_KERNEL); > + buf = kmalloc(PAGE_SIZE, GFP_KERNEL); > if (!buf) > return -ENOMEM; > if (copy_from_user(buf, user_buf, count)) { > - free_page((unsigned long) buf); > + kfree(buf); > return -EFAULT; > } > /* NULL-terminate and remove enter */ > @@ -325,7 +325,7 @@ static ssize_t direct_entry(struct file *f, const char __user *user_buf, > strim(buf); > > crashtype = find_crashtype(buf); > - free_page((unsigned long) buf); > + kfree(buf); > if (!crashtype) > return -EINVAL; >