Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp661352pxb; Fri, 16 Apr 2021 15:09:30 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz3JH9hYYFRZts4uqPohmXWBJ9+dqP9S4KswfsqF/7gjpS+lfggHKhLiNfsz2nHU4qPyc38 X-Received: by 2002:a17:906:5fc2:: with SMTP id k2mr10568992ejv.354.1618610969818; Fri, 16 Apr 2021 15:09:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618610969; cv=none; d=google.com; s=arc-20160816; b=QVhJ1FOjDujg97ypI6gO2sL+rPfQkcp+7laKnzqA039KH528wqLn3CpfTZMoX9VvLw iFCMaF/4XxyaVAZeRLefh4JctQYDOtDXTg5Ox21dbvG3yyQzv2CigaQO4ixr2cUctez4 tyL0GtLWKEUqNQu9+ie6svHQB0sTZ/4qRJUAKUhwn1beMZgMHHMF3VfPvpSIgR8odFf8 34GXAayOFtI/Dlbrmrjvbky5+hMGBBTEw8m+Pck8g/Y2zVFvbjzzSzgQCYlL5Td471Vx gNQL0XsI33KG3WcqTAn+qbpRdX1OCAmb2Pg6JJi4VOUC6XUAl89WyJibyU9uRcskgTWa Z30g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=rVOjIab3/UmOkpea85Hbp6lL2pKC2eJzDok4el+NCvk=; b=YqqPpr5WsaJvskgB93JPfStVnyjDI79QmULeWDi4DXGA2z7RLIrCmRrpZYTY3ME9Uk an7A1O7ZjYpSSPA9yZVI85QGPLGNwuYyv04FKNBwl10w10kjLnmWttbJ/Kqel9e0Pire n/g1HW+Ab9g5Jz/LxPgig96528RhXXi96mjZTVLRMIEaQ1921mx+GxFQARa8Fo6f19mR e7qf9LwuKdPvPonpycsODKENKSqb6VJOfcga7lJFsA12XMju7g54oBgtNXUqfwtDx7XW LhQk4U82KA+eeTYa3DS0GxD35ge5pJsPSuZt16342NVFyixgWT891+UJrjLZCc0ujJjD EJ5g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@fb.com header.s=facebook header.b=fGHhAwBk; 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=pass (p=REJECT sp=REJECT dis=NONE) header.from=fb.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id o5si5406783edc.249.2021.04.16.15.09.06; Fri, 16 Apr 2021 15:09:29 -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=pass header.i=@fb.com header.s=facebook header.b=fGHhAwBk; 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=pass (p=REJECT sp=REJECT dis=NONE) header.from=fb.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344231AbhDPVWI (ORCPT + 99 others); Fri, 16 Apr 2021 17:22:08 -0400 Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:42096 "EHLO mx0b-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344192AbhDPVVt (ORCPT ); Fri, 16 Apr 2021 17:21:49 -0400 Received: from pps.filterd (m0148460.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.0.43/8.16.0.43) with SMTP id 13GL9oXa017484 for ; Fri, 16 Apr 2021 14:18:41 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : cc : subject : date : message-id : mime-version : content-type : content-transfer-encoding; s=facebook; bh=rVOjIab3/UmOkpea85Hbp6lL2pKC2eJzDok4el+NCvk=; b=fGHhAwBkAjvPON+1T8V2z03F0KZQAd7tUn+lMThzBcJZ6UAb3jz667jW2Bgwc5/GAK7M pxYZBNkmxspNk6D49MI5EcOrGr/XM+nsriowgeaW5GEZQ5morkAoXdYhBzYV3bwrYNCm ig/wuAkZNuP5jB+0Sre3mDec0h+nfJXRnSw= Received: from maileast.thefacebook.com ([163.114.130.16]) by mx0a-00082601.pphosted.com with ESMTP id 37yb5e2nhj-9 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Fri, 16 Apr 2021 14:18:41 -0700 Received: from intmgw002.06.ash9.facebook.com (2620:10d:c0a8:1b::d) by mail.thefacebook.com (2620:10d:c0a8:82::c) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2176.2; Fri, 16 Apr 2021 14:18:39 -0700 Received: from devvm1945.atn0.facebook.com (localhost [127.0.0.1]) by devvm1945.atn0.facebook.com (Postfix) with ESMTP id 8BEDA5921F57; Fri, 16 Apr 2021 14:18:38 -0700 (PDT) Received: (from saravanand@localhost) by devvm1945.atn0.facebook.com (8.15.2/8.15.2/Submit) id 13GLIbun4003249; Fri, 16 Apr 2021 14:18:37 -0700 X-Authentication-Warning: devvm1945.atn0.facebook.com: saravanand set sender to saravanand@fb.com using -f From: Saravanan D To: , , CC: , , , Saravanan D , Calvin Owens Subject: [PATCH] brd: expose number of allocated pages in debugfs Date: Fri, 16 Apr 2021 14:18:29 -0700 Message-ID: <20210416211829.4002947-1-saravanand@fb.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-FB-Internal: Safe X-Proofpoint-GUID: KBIPVKMf4L-MJQPzXTarglZY0Q1LbTR6 X-Proofpoint-ORIG-GUID: KBIPVKMf4L-MJQPzXTarglZY0Q1LbTR6 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.391,18.0.761 definitions=2021-04-16_09:2021-04-16,2021-04-16 signatures=0 X-Proofpoint-Spam-Details: rule=fb_default_notspam policy=fb_default score=0 bulkscore=0 adultscore=0 mlxlogscore=999 spamscore=0 clxscore=1015 malwarescore=0 priorityscore=1501 lowpriorityscore=0 impostorscore=0 phishscore=0 suspectscore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2104060000 definitions=main-2104160150 X-FB-Internal: deliver Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Calvin Owens While the maximum size of each ramdisk is defined either as a module parameter, or compile time default, it's impossible to know how many pages have currently been allocated by each ram%d device, since they're allocated when used and never freed. This patch creates a new directory at this location: =C2=BB /sys/kernel/debug/ramdisk_pages/ ...which will contain a file named "ram%d" for each instantiated ramdisk on the system. The file is read-only, and read() will output the number of pages currently held by that ramdisk. Signed-off-by: Calvin Owens [cleaned up the !CONFIG_DEBUG_FS case and API changes for HEAD] Signed-off-by: Kyle McMartin [rebased] Signed-off-by: Saravanan D --- drivers/block/brd.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/drivers/block/brd.c b/drivers/block/brd.c index 18bf99906662..6e622c1327ee 100644 --- a/drivers/block/brd.c +++ b/drivers/block/brd.c @@ -22,6 +22,7 @@ #include #include #include +#include =20 #include =20 @@ -48,6 +49,7 @@ struct brd_device { */ spinlock_t brd_lock; struct radix_tree_root brd_pages; + u64 brd_nr_pages; }; =20 /* @@ -116,6 +118,8 @@ static struct page *brd_insert_page(struct brd_device= *brd, sector_t sector) page =3D radix_tree_lookup(&brd->brd_pages, idx); BUG_ON(!page); BUG_ON(page->index !=3D idx); + } else { + brd->brd_nr_pages++; } spin_unlock(&brd->brd_lock); =20 @@ -365,11 +369,13 @@ __setup("ramdisk_size=3D", ramdisk_size); */ static LIST_HEAD(brd_devices); static DEFINE_MUTEX(brd_devices_mutex); +static struct dentry *brd_debugfs_dir; =20 static struct brd_device *brd_alloc(int i) { struct brd_device *brd; struct gendisk *disk; + char buf[DISK_NAME_LEN]; =20 brd =3D kzalloc(sizeof(*brd), GFP_KERNEL); if (!brd) @@ -382,6 +388,11 @@ static struct brd_device *brd_alloc(int i) if (!brd->brd_queue) goto out_free_dev; =20 + snprintf(buf, DISK_NAME_LEN, "ram%d", i); + if (!IS_ERR_OR_NULL(brd_debugfs_dir)) + debugfs_create_u64(buf, 0444, brd_debugfs_dir, + &brd->brd_nr_pages); + /* This is so fdisk will align partitions on 4k, because of * direct_access API needing 4k alignment, returning a PFN * (This is only a problem on very small devices <=3D 4M, @@ -397,7 +408,7 @@ static struct brd_device *brd_alloc(int i) disk->fops =3D &brd_fops; disk->private_data =3D brd; disk->flags =3D GENHD_FL_EXT_DEVT; - sprintf(disk->disk_name, "ram%d", i); + strlcpy(disk->disk_name, buf, DISK_NAME_LEN); set_capacity(disk, rd_size * 2); =20 /* Tell the block layer that this is not a rotational device */ @@ -495,6 +506,8 @@ static int __init brd_init(void) =20 brd_check_and_reset_par(); =20 + brd_debugfs_dir =3D debugfs_create_dir("ramdisk_pages", NULL); + mutex_lock(&brd_devices_mutex); for (i =3D 0; i < rd_nr; i++) { brd =3D brd_alloc(i); @@ -519,6 +532,8 @@ static int __init brd_init(void) return 0; =20 out_free: + debugfs_remove_recursive(brd_debugfs_dir); + list_for_each_entry_safe(brd, next, &brd_devices, brd_list) { list_del(&brd->brd_list); brd_free(brd); @@ -534,6 +549,8 @@ static void __exit brd_exit(void) { struct brd_device *brd, *next; =20 + debugfs_remove_recursive(brd_debugfs_dir); + list_for_each_entry_safe(brd, next, &brd_devices, brd_list) brd_del_one(brd); =20 --=20 2.30.2