From nobody Fri Jan 2 19:06:29 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 11485E95A91 for ; Mon, 9 Oct 2023 11:07:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346165AbjJILHW (ORCPT ); Mon, 9 Oct 2023 07:07:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40854 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346015AbjJILHU (ORCPT ); Mon, 9 Oct 2023 07:07:20 -0400 Received: from mail-ej1-x631.google.com (mail-ej1-x631.google.com [IPv6:2a00:1450:4864:20::631]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2065494 for ; Mon, 9 Oct 2023 04:07:18 -0700 (PDT) Received: by mail-ej1-x631.google.com with SMTP id a640c23a62f3a-991c786369cso731086466b.1 for ; Mon, 09 Oct 2023 04:07:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696849636; x=1697454436; darn=vger.kernel.org; h=content-disposition:mime-version:message-id:subject:cc:to:from:date :from:to:cc:subject:date:message-id:reply-to; bh=94GrlV6BpXzh9G3J0ul8d01iN4TaQgOhd/Tfg+JX6og=; b=mP6FAKp7VpXGO/fULV1JVkxA+MiPiUfMkfkGCSMWfJU02Po0NOcv8jmtVIo5I/K1UD +svSl8CVudsIRn19Gz3lG7KK37cH5w0ESvCCWP2AGcdxE92Qo1s84k1DXscnrHJszPWf Ayt27IvJRqtJVcE8NvI5Sa435N89iOhaqSfB3oouyVX6T4gPJ0wR0bqwP0QIrbXbyubt hXP62syheD/4xayygaSFhp+aDsN4bYDqikGKAwxhMULEW5qsQNaPL7n5aL9n2HdUBYjX nYIGBv5U9TGbuuQsucvuutfi+xihBfpio5aktffnb/WAVUFQRqJDOU1nlIuedH6pPXpS 7ZQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696849636; x=1697454436; h=content-disposition:mime-version:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=94GrlV6BpXzh9G3J0ul8d01iN4TaQgOhd/Tfg+JX6og=; b=TIkCgUIxjTvIl8GHuVRAnDUDfE6aerDyx69kAsEcV675TYX+MFVPU6I9BqYwLM1B+J JrmMEe01KXnRn9eafoORB5UkCdrTIQmlR/rd1m4pMt6Rz5CtMU+rEJfLiwUvA0hcgfvG jUhx/wZbrGxIHUeWkYFMqOIab8G2SINwDibX5c+zVjflSFyKquBE6VECtBIaulg9GN0B 4m5jfdZthcDccliwB67p90ssMeWZ7dtEUXSP2qOn9vlaKyc5pTjzsbGyL2nX7BRnsEKC IyDga6V0nMU0PMDbh5KOEqLU+oJ5/IQhB0tn9a4ib2tPSzVyFNvyx6mEa7A8NMxDsOjK zs8w== X-Gm-Message-State: AOJu0YxsPFRt71/V9mCbP8QWO2oSOa8ZzW8AYzi3nWmvCZHh7Q121DIW VAMFv2aFfeTHqdWTTJ6IT0LNcE+j6Q== X-Google-Smtp-Source: AGHT+IHAu4nCcMbixG78LVCeC4CkKiFDMQ3xEvc+tqxg+jBzuKhd3fvPDY4cethnaS72EsBu2hlLbw== X-Received: by 2002:a17:906:209:b0:9a9:e5bb:eddc with SMTP id 9-20020a170906020900b009a9e5bbeddcmr14496587ejd.16.1696849636330; Mon, 09 Oct 2023 04:07:16 -0700 (PDT) Received: from p183 ([46.53.254.83]) by smtp.gmail.com with ESMTPSA id r11-20020a170906364b00b009a1a5a7ebacsm6506570ejb.201.2023.10.09.04.07.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Oct 2023 04:07:15 -0700 (PDT) Date: Mon, 9 Oct 2023 14:07:14 +0300 From: Alexey Dobriyan To: akpm@linux-foundation.org Cc: linux-kernel@vger.kernel.org, Hugh Dickins , Shuah Khan Subject: [PATCH v3] proc: test /proc/${pid}/statm Message-ID: <0721ca69-7bb4-40aa-8d01-0c5f91e5f363@p183> MIME-Version: 1.0 Content-Disposition: inline Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Swarup Laxman Kotiaklapudi [more or less rewritten. --adobriyan] My original comment lied, output can be "0 A A B 0 0 0\n" (see comment in the code). I don't quite understand why get_mm_counter(mm, MM_FILEPAGES) + get_mm_counter(mm, MM_SHMEMPAGES) can stay positive but get_mm_counter(mm, MM_ANONPAGES) is always 0 after everything is unmapped but that's just me. Signed-off-by: Swarup Laxman Kotiaklapudi Signed-off-by: Alexey Dobriyan --- tools/testing/selftests/proc/proc-empty-vm.c | 97 ++++++++++++++++++++++= +++-- 1 file changed, 92 insertions(+), 5 deletions(-) --- a/tools/testing/selftests/proc/proc-empty-vm.c +++ b/tools/testing/selftests/proc/proc-empty-vm.c @@ -303,6 +303,95 @@ static int test_proc_pid_smaps_rollup(pid_t pid) } } =20 +static const char *parse_u64(const char *p, const char *const end, uint64_= t *rv) +{ + *rv =3D 0; + for (; p !=3D end; p +=3D 1) { + if ('0' <=3D *p && *p <=3D '9') { + assert(!__builtin_mul_overflow(*rv, 10, rv)); + assert(!__builtin_add_overflow(*rv, *p - '0', rv)); + } else { + break; + } + } + assert(p !=3D end); + return p; +} + +/* + * There seems to be 2 types of valid output: + * "0 A A B 0 0 0\n" for dynamic exeuctables, + * "0 0 0 B 0 0 0\n" for static executables. + */ +static int test_proc_pid_statm(pid_t pid) +{ + char buf[4096]; + snprintf(buf, sizeof(buf), "/proc/%u/statm", pid); + int fd =3D open(buf, O_RDONLY); + if (fd =3D=3D -1) { + perror("open /proc/${pid}/statm"); + return EXIT_FAILURE; + } + + ssize_t rv =3D read(fd, buf, sizeof(buf)); + close(fd); + + assert(rv >=3D 0); + assert(rv <=3D sizeof(buf)); + if (0) { + write(1, buf, rv); + } + + const char *p =3D buf; + const char *const end =3D p + rv; + + /* size */ + assert(p !=3D end && *p++ =3D=3D '0'); + assert(p !=3D end && *p++ =3D=3D ' '); + + uint64_t resident; + p =3D parse_u64(p, end, &resident); + assert(p !=3D end && *p++ =3D=3D ' '); + + uint64_t shared; + p =3D parse_u64(p, end, &shared); + assert(p !=3D end && *p++ =3D=3D ' '); + + uint64_t text; + p =3D parse_u64(p, end, &text); + assert(p !=3D end && *p++ =3D=3D ' '); + + assert(p !=3D end && *p++ =3D=3D '0'); + assert(p !=3D end && *p++ =3D=3D ' '); + + /* data */ + assert(p !=3D end && *p++ =3D=3D '0'); + assert(p !=3D end && *p++ =3D=3D ' '); + + assert(p !=3D end && *p++ =3D=3D '0'); + assert(p !=3D end && *p++ =3D=3D '\n'); + + assert(p =3D=3D end); + + /* + * "text" is "mm->end_code - mm->start_code" at execve(2) time. + * munmap() doesn't change it. It can be anything (just link + * statically). It can't be 0 because executing to this point + * implies at least 1 page of code. + */ + assert(text > 0); + + /* + * These two are always equal. Always 0 for statically linked + * executables and sometimes 0 for dynamically linked executables. + * There is no way to tell one from another without parsing ELF + * which is too much for this test. + */ + assert(resident =3D=3D shared); + + return EXIT_SUCCESS; +} + int main(void) { int rv =3D EXIT_SUCCESS; @@ -389,11 +478,9 @@ int main(void) if (rv =3D=3D EXIT_SUCCESS) { rv =3D test_proc_pid_smaps_rollup(pid); } - /* - * TODO test /proc/${pid}/statm, task_statm() - * ->start_code, ->end_code aren't updated by munmap(). - * Output can be "0 0 0 2 0 0 0\n" where "2" can be anything. - */ + if (rv =3D=3D EXIT_SUCCESS) { + rv =3D test_proc_pid_statm(pid); + } =20 /* Cut the rope. */ int wstatus;