From owner-freebsd-hackers@freebsd.org Tue Feb 23 21:20:34 2021 Return-Path: Delivered-To: freebsd-hackers@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 6F7A25533AB for ; Tue, 23 Feb 2021 21:20:34 +0000 (UTC) (envelope-from asomers@gmail.com) Received: from mail-oo1-f43.google.com (mail-oo1-f43.google.com [209.85.161.43]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "smtp.gmail.com", Issuer "GTS CA 1O1" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4DlX5Z2hRJz4f1G for ; Tue, 23 Feb 2021 21:20:34 +0000 (UTC) (envelope-from asomers@gmail.com) Received: by mail-oo1-f43.google.com with SMTP id f26so4227838oog.5 for ; Tue, 23 Feb 2021 13:20:34 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=PxltA2VW4YcjXh7HVedjX5XRSW8KohZcPV3CUciH1jk=; b=s3G7ghg697Frw5jRbzl3P70EwPAbbZh+PY6flGZPSN4G/7Ioaof7a5+vCkhAPpxtVi iBFrI7Mts+luTXFsmL8zUNe4b9PgsutAY+EIwCq1Nyzv0jdY7G2WXJL07CcmtDWDeFvP JSIZ7PSEmcUjxvDKgbYEpnNuarGqKxncGfntjh4eW7jOx+BMlh3o1lv3Jtu2hkN4KuhL Rr3aq+cbbrq1HfSN2FO1RyTQna71uPUH5+gur6vbeAnmpBlozfqXx4QkPRcXBIJg1/cr 7atnW8CI91Sfi6rt5Upp4iNgJqnga3nMm35N/6DkSYr6S9HjVTVoNZUPumxCeuT2Exqc kZTw== X-Gm-Message-State: AOAM531MzQbLwUBbtBCs7OATliV1uNqoCQ0tpRlw9obrs7Gn9U3fcffy ckVgsKD3YB3poShprOb8bygR0TOg9mH8RzxQi+qrDxYiV+0= X-Google-Smtp-Source: ABdhPJxcisB2buG6fehMGQOpQB9LcPxelee54CPKLZPpaCOr/FA40Cpt5dIPBZPgDhXiJbeZBslljeAGxsg12H0d/X0= X-Received: by 2002:a4a:e9a6:: with SMTP id t6mr19758623ood.74.1614115233154; Tue, 23 Feb 2021 13:20:33 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Alan Somers Date: Tue, 23 Feb 2021 14:20:21 -0700 Message-ID: Subject: Re: The out-of-swap killer makes poor choices To: Konstantin Belousov Cc: FreeBSD Hackers X-Rspamd-Queue-Id: 4DlX5Z2hRJz4f1G X-Spamd-Bar: ---- Authentication-Results: mx1.freebsd.org; none X-Spamd-Result: default: False [-4.00 / 15.00]; REPLY(-4.00)[] Content-Type: text/plain; charset="UTF-8" X-Content-Filtered-By: Mailman/MimeDel 2.1.34 X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Technical discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 23 Feb 2021 21:20:34 -0000 On Tue, Feb 23, 2021 at 2:11 PM Konstantin Belousov wrote: > On Tue, Feb 23, 2021 at 01:49:49PM -0700, Alan Somers wrote: > > To me it's always seemed like the out-of-swap killer kills the wrong > > process. Oh, it does the right thing with a trivial while(1) {malloc()} > > test program, but not with real workloads. To summarize the logic in > > vm_pageout_oom: > > > > * Don't kill system, protected, or killed processes > > * Don't kill processes with a thread that isn't running or suspended > > * Kill whichever process is using the most swap or swap + ram, depending > on > > the shortage variable. On ties, kill the newest one. > > > > This algorithm probably made sense in the days when computers had much > more > > swap than RAM. But now it leads to several problems: > > > > * It's almost guaranteed to do the wrong thing when shortage == > > VM_OOM_SWAPZ and there is little or no swap configured. If no swap is > > configured, it will kill the newest running or suspended process. If a > > little bit is configured, it will probably kill some idle process, like > > zfsd, that is swapped out because it doesn't run very often. > > > > * Even if multiple GB of swap are configured, the OOM killer is still > > biased towards killing idle processes when shortage == VM_OOM_SWAPZ. > Most > > often, the process responsible for an out-of-memory condition is not > idle, > > and is consuming large amounts of RAM. > > > > * It ignores RLIMIT_RSS. We consider that rlimit when deciding whether > to > > move a process from RAM to swap. > > > > * The "out of swap space" kernel message doesn't specify whether the > > process was killed because of insufficient swap or RAM (the shortage > > variable) > > > > I propose the following changes: > > > > * Incorporate shortage into the "out of swap space" message. > ok with me, not sure if users could make any action based on discretion > > > * When walking the process list, if any process exceeds its RLIMIT_RSS, > > choose it immediately, without bothering to compare it to older > processes. > RSS was never supposed to be a limit on how many pages are resident. > It only provided some preference for more aggressive paging out process' > pages. > > Or put it differently, RSS is not supposed to be the working set size > in VMS/NT sense. > Sure, but given that we must kill _something_, preferentially killing a process that was specifically limited sounds better than killing a process that wasn't, won't you agree? > > > * Always consider the sum of a process's RAM + swap, regardless of the > > shortage variable. > > > > Does this make sense? Am I missing something about shortage == > > VM_OOM_SWAPZ? I don't understand why you would ever want to exclude > > processes' RAM usage. That logic was added in revision > > 2025d69ba7a68a5af173007a8072c45ad797ea23, but I don't understand the > > rationale. > > SWAPZ means that swap zone is exhausted. In this case, killing a process > that does not use swap, would not free any space in the zone. Similarly, > we should select a process with largest swap (== metadata kept in swap > zone) > use to free something in swap zone. > But killing a process that does not use swap could reduce the need for more swap by other processes. How many cases are there where a process needs more SWAP and won't settle for RAM instead? > > In other words, such kill could be not enough and really require more and > more rounds of OOM, esp. on machine with very small swap configured. >