From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp2 ([2001:41d0:2:bcc0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms0.migadu.com with LMTPS id cF3hHh51eWGDPQAAgWs5BA (envelope-from ) for ; Wed, 27 Oct 2021 17:49:50 +0200 Received: from aspmx1.migadu.com ([2001:41d0:2:bcc0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp2 with LMTPS id kIyWGh51eWFnawAAB5/wlQ (envelope-from ) for ; Wed, 27 Oct 2021 15:49:50 +0000 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id EC2FA37ACD for ; Wed, 27 Oct 2021 17:49:49 +0200 (CEST) Received: from localhost ([::1]:40132 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mflBU-0004W4-Vg for larch@yhetil.org; Wed, 27 Oct 2021 11:49:49 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57884) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mfkZ1-0004Qy-9S for guix-patches@gnu.org; Wed, 27 Oct 2021 11:10:03 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:38650) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mfkZ0-00078B-Tq for guix-patches@gnu.org; Wed, 27 Oct 2021 11:10:02 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1mfkZ0-0000PN-QE for guix-patches@gnu.org; Wed, 27 Oct 2021 11:10:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#51346] [PATCH v2 2/4] gnu: system: Add swap flags. Resent-From: Josselin Poiret Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Wed, 27 Oct 2021 15:10:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 51346 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: Tobias Geerinckx-Rice Cc: Josselin Poiret , 51346@debbugs.gnu.org Received: via spool by 51346-submit@debbugs.gnu.org id=B51346.16353473881515 (code B ref 51346); Wed, 27 Oct 2021 15:10:02 +0000 Received: (at 51346) by debbugs.gnu.org; 27 Oct 2021 15:09:48 +0000 Received: from localhost ([127.0.0.1]:50190 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mfkYm-0000OM-7S for submit@debbugs.gnu.org; Wed, 27 Oct 2021 11:09:48 -0400 Received: from jpoiret.xyz ([206.189.101.64]:34742) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mfkYh-0000Nq-E7 for 51346@debbugs.gnu.org; Wed, 27 Oct 2021 11:09:43 -0400 Received: from authenticated-user (jpoiret.xyz [206.189.101.64]) by jpoiret.xyz (Postfix) with ESMTPA id 961D9184BC9; Wed, 27 Oct 2021 15:09:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jpoiret.xyz; s=dkim; t=1635347382; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=PAKY3EBJZaeHuIoMZ/xxWuXzRdhV65m7BJSYz1AAK+U=; b=m7Gg12GToY/14D5tF3bAR17Oj0gMCiyG4Ljv41HSsaSt+xO/3K6dM4Js0BmUlNBn1iw7Wz KxRA+dIAir8S50FSm9U02QIGvAzo16tlrBdqlrA2bJHkHv0RiwRtU05uU+bA6pjhuWLatM CHR5B5lAaZsoywx6VEk2BuPMOgCpoCYDdogDU+2dOnvs9n3eeTe3mME2PdAMuWH1f4BZv7 tDLR1RWTBMTFFJY9FHRXzklodHCziQDZ6oF+8PAIv/mTyv0Aa4nK1PMsPJ+8IJGJlNGHsQ OnZCpLVG1b/ILBPzkrSDLFb8wee88+0f6dQpN5wtiapMrgn58+aN57Uhn6kCYg== Date: Wed, 27 Oct 2021 15:09:11 +0000 Message-Id: <20211027150913.6038-3-dev@jpoiret.xyz> In-Reply-To: <20211027150913.6038-1-dev@jpoiret.xyz> References: <87tuh6ifwe.fsf@nckx> <20211027150913.6038-1-dev@jpoiret.xyz> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spamd-Bar: / X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+larch=yhetil.org@gnu.org Sender: "Guix-patches" Reply-to: Josselin Poiret X-ACL-Warn: , Josselin Poiret via Guix-patches From: Josselin Poiret via Guix-patches via X-Migadu-Flow: FLOW_IN ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1635349790; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding:resent-cc: resent-from:resent-sender:resent-message-id:in-reply-to:in-reply-to: references:references:list-id:list-help:list-unsubscribe: list-subscribe:list-post:dkim-signature; bh=PAKY3EBJZaeHuIoMZ/xxWuXzRdhV65m7BJSYz1AAK+U=; b=dznkEEQRuAW3oc41vfI2K5GWtIP9De01ydjGa96Iq0I5Gk/iXwZBlmuKrBIDBse85haCjB t5wP2tQlFbAlE2Q1RuzKUKKMOOlQEVGXr871XayqRMBXeWr4OfIGQUYeKeVIHwvO24QwOB 1LX4HI992FCj73ddX/P+w2Ktpg2k2f3YJRW3sZEGyjy79T72J+Am9VxwKPLLape0tmNLgq e9splAuceqmhIDQ2qmSFNsc99lgpqHVkBJJX9rOOIh1wTX1fS2rsbDM9YGZSWDHM8I04Z3 fmQF9UPLD6+SRYBfvwZImpOUv9pt/aV5ttg+3LV7kw9OFq5yS3RIs1D8s7Ltrg== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1635349790; a=rsa-sha256; cv=none; b=c23xR0ljwbQYeUkX6TdIEMK+TAhAH2G9lJGcEKNRcdAAps160ImLuz6ekGGN2qhGrusFH7 CfH3EsLZJo2nozeASVWj1RySAfVpxS1m7C8GdPDSMCWU0toZTkwszkMT7litamPmew+HnW oXRfnr4eZAdksUXLAjaiewQT0XhJBrT8oo/ccZJ3r9lQygXr3gMEq1vEHsJm4b2Pum9soW /0WkQv0dOMeo0PKHHsMlH+IOCzmd0YaAQm3iSPjT4wpPZ1hCeCy0FkBYQ8YNS4NXh89vxY ygrU4XCwmiiaZ0ipXxq477rH8uBTbI5Kc9Gd+Mnx0AIQsaiSiGUafrfvwk8Shw== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=jpoiret.xyz header.s=dkim header.b=m7Gg12GT; spf=pass (aspmx1.migadu.com: domain of guix-patches-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=guix-patches-bounces@gnu.org X-Migadu-Spam-Score: 2.57 Authentication-Results: aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=jpoiret.xyz header.s=dkim header.b=m7Gg12GT; dmarc=pass (policy=none) header.from=gnu.org; spf=pass (aspmx1.migadu.com: domain of guix-patches-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=guix-patches-bounces@gnu.org X-Migadu-Queue-Id: EC2FA37ACD X-Spam-Score: 2.57 X-Migadu-Scanner: scn0.migadu.com X-TUID: Nw2kluxCMNUi * gnu/system/file-systems.scm (swap-space)[priority, discard?]: Add them. * guix/build/syscalls.scm (SWAP_FLAG_PREFER, SWAP_FLAG_PRIO_MASK, SWAP_FLAG_PRIO_SHIFT, SWAP_FLAG_DISCARD): Add them. * gnu/build/file-systems.scm (swap-space->flags-bit-mask): Add it. * gnu/services/base.scm (swap-service-type): Use it. --- gnu/build/file-systems.scm | 35 ++++++++++++++++++++++++++++++++++- gnu/services/base.scm | 7 +++++-- gnu/system/file-systems.scm | 10 ++++++++-- guix/build/syscalls.scm | 12 ++++++++++++ 4 files changed, 59 insertions(+), 5 deletions(-) diff --git a/gnu/build/file-systems.scm b/gnu/build/file-systems.scm index d8a5ddf1e5..39a408e8c1 100644 --- a/gnu/build/file-systems.scm +++ b/gnu/build/file-systems.scm @@ -29,6 +29,8 @@ (define-module (gnu build file-systems) #:use-module (guix build bournish) #:use-module ((guix build syscalls) #:hide (file-system-type)) + #:use-module (guix diagnostics) + #:use-module (guix i18n) #:use-module (rnrs io ports) #:use-module (rnrs bytevectors) #:use-module (ice-9 match) @@ -54,7 +56,9 @@ (define-module (gnu build file-systems) mount-flags->bit-mask check-file-system - mount-file-system)) + mount-file-system + + swap-space->flags-bit-mask)) ;;; Commentary: ;;; @@ -227,6 +231,35 @@ (define (linux-swap-superblock-volume-name sblock) "Return the label of Linux-swap superblock SBLOCK as a string." (null-terminated-latin1->string (sub-bytevector sblock (+ 1024 4 4 4 16) 16))) + +(define (swap-space->flags-bit-mask swap) + "Return the number suitable for the 'flags' argument of 'mount' +that corresponds to the swap-space SWAP." + (define prio-flag + (let ((p (swap-space-priority swap)) + (max (ash SWAP_FLAG_PRIO_MASK (- SWAP_FLAG_PRIO_SHIFT)))) + (if p + (logior SWAP_FLAG_PREFER + (ash (cond + ((< p 0) + (begin (warning + (G_ "Given swap priority ~a is negative, +defaulting to 0.~%") p) + 0)) + ((> p max) + (begin (warning + (G_ "Limiting swap priority ~a to ~a.~%") + p max) + max)) + (else p)) + SWAP_FLAG_PRIO_SHIFT)) + 0))) + (define delayed-flag + (if (swap-space-discard? swap) + SWAP_FLAG_DISCARD + 0)) + (logior prio-flag delayed-flag)) + ;;; diff --git a/gnu/services/base.scm b/gnu/services/base.scm index c816381198..cf43a78fd0 100644 --- a/gnu/services/base.scm +++ b/gnu/services/base.scm @@ -58,7 +58,8 @@ (define-module (gnu services base) #:use-module (gnu packages linux) #:use-module (gnu packages terminals) #:use-module ((gnu build file-systems) - #:select (mount-flags->bit-mask)) + #:select (mount-flags->bit-mask + swap-space->flags-bit-mask)) #:use-module (guix gexp) #:use-module (guix records) #:use-module (guix modules) @@ -2223,7 +2224,9 @@ (define device-lookup (let ((device #$device-lookup)) (and device (begin - (restart-on-EINTR (swapon device)) + (restart-on-EINTR (swapon device + #$(swap-space->flags-bit-mask + swap))) #t))))) (stop #~(lambda _ (let ((device #$device-lookup)) diff --git a/gnu/system/file-systems.scm b/gnu/system/file-systems.scm index 7aa19069a1..fba4ebf65d 100644 --- a/gnu/system/file-systems.scm +++ b/gnu/system/file-systems.scm @@ -101,7 +101,9 @@ (define-module (gnu system file-systems) swap-space swap-space? swap-space-target - swap-space-dependencies)) + swap-space-dependencies + swap-space-priority + swap-space-discard?)) ;;; Commentary: ;;; @@ -685,6 +687,10 @@ (define-record-type* swap-space make-swap-space this-swap-space (target swap-space-target) (dependencies swap-space-dependencies - (default '()))) + (default '())) + (priority swap-space-priority + (default #f)) + (discard? swap-space-discard? + (default #f))) ;;; file-systems.scm ends here diff --git a/guix/build/syscalls.scm b/guix/build/syscalls.scm index 99a3b45004..f2b18abf5a 100644 --- a/guix/build/syscalls.scm +++ b/guix/build/syscalls.scm @@ -71,6 +71,11 @@ (define-module (guix build syscalls) mounts mount-points + SWAP_FLAG_PREFER + SWAP_FLAG_PRIO_MASK + SWAP_FLAG_PRIO_SHIFT + SWAP_FLAG_DISCARD + swapon swapoff @@ -677,6 +682,13 @@ (define (mount-points) "Return the mounts points for currently mounted file systems." (map mount-point (mounts))) +;; Pulled from glibc's sysdeps/unix/sysv/linux/sys/swap.h + +(define SWAP_FLAG_PREFER #x8000) ;; Set if swap priority is specified. +(define SWAP_FLAG_PRIO_MASK #x7fff) +(define SWAP_FLAG_PRIO_SHIFT 0) +(define SWAP_FLAG_DISCARD #x10000) ;; Discard swap cluster after use. + (define swapon (let ((proc (syscall->procedure int "swapon" (list '* int)))) (lambda* (device #:optional (flags 0)) -- 2.33.1