From patchwork Fri Jan 4 10:05:17 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Ivanov X-Patchwork-Id: 1020668 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:e::133; helo=bombadil.infradead.org; envelope-from=linux-um-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=cambridgegreys.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="k44BWlms"; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 43WL5M3hKjz9rxp for ; Fri, 4 Jan 2019 21:05:43 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Message-Id:Date: Subject:To:From:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To: References:List-Owner; bh=OxDoz44Fq6WlMxJUZh+mn+jwTw4N9tE9fFfVV5ud1Kk=; b=k44 BWlmsQfQujQd3YKQ20xTQlOoJWlKYUI3kUalz9Ct4WyVL/De+VAoCGWJTnPWTqQu3S3o9Cj4tVwn1 jtB4S+lF/OFYJu6YxGTPpwChR6zwgytOVtXZwxCEKNfKbxfMynZWg1t0MP/lI9jdjQtPI6IaOPGt0 suXvjxV1qFUNP8jrYxnStsNcpuFTueyTfRxrj08Bt6TVWdE3MYuleCo3TZRXthPRizI/uqEgRxofJ GYhj7D82CkpR6BcNE55DKuYSs2UT0U6yEDfG6rTsfyfUd3eNgrywODfcksZqv+UYDJpJxOGp6xao+ 0hypmoiWsq0MgiEoyO1AEUICL8pAg1A==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1gfMMB-0002yq-1y; Fri, 04 Jan 2019 10:05:35 +0000 Received: from ivanoab6.miniserver.com ([5.153.251.140] helo=www.kot-begemot.co.uk) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1gfMM7-0002yP-G0 for linux-um@lists.infradead.org; Fri, 04 Jan 2019 10:05:33 +0000 Received: from [192.168.17.6] (helo=smaug.kot-begemot.co.uk) by www.kot-begemot.co.uk with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1gfMM3-0007N8-NQ; Fri, 04 Jan 2019 10:05:28 +0000 Received: from amistad.kot-begemot.co.uk ([192.168.3.89]) by smaug.kot-begemot.co.uk with esmtp (Exim 4.89) (envelope-from ) id 1gfMLw-0006lA-SJ; Fri, 04 Jan 2019 10:05:22 +0000 From: anton.ivanov@cambridgegreys.com To: linux-um@lists.infradead.org Subject: [PATCH] um: Try to avoid kmalloc in signal handling Date: Fri, 4 Jan 2019 10:05:17 +0000 Message-Id: <20190104100517.9809-1-anton.ivanov@cambridgegreys.com> X-Mailer: git-send-email 2.11.0 X-Spam-Score: -1.0 X-Spam-Score: -1.0 X-Clacks-Overhead: GNU Terry Pratchett X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20190104_020531_669492_3DE854FA X-CRM114-Status: GOOD ( 13.43 ) X-Spam-Score: 0.0 (/) X-Spam-Report: SpamAssassin version 3.4.2 on bombadil.infradead.org summary: Content analysis details: (0.0 points) pts rule name description ---- ---------------------- -------------------------------------------------- X-BeenThere: linux-um@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: richard@nod.at, Anton Ivanov MIME-Version: 1.0 Sender: "linux-um" Errors-To: linux-um-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org From: Anton Ivanov Signal handling (which maps to interrupt handling in UML) needs to pass current registers to the relevant handlers and was allocating a structure for that using kmalloc. It is possible to avoid this kmalloc by using a small "signal register stack". A depth of 4 suffices for normal use. If it is exceeded further sets of registers are allocated as before via kmalloc. The end result is >10% performance increase in networking as measured by iperf and >5% across the board. Signed-off-by: Anton Ivanov --- arch/um/include/shared/os.h | 4 ++++ arch/um/kernel/signal.c | 31 +++++++++++++++++++++++++++++++ arch/um/os-Linux/signal.c | 16 ++++------------ 3 files changed, 39 insertions(+), 12 deletions(-) diff --git a/arch/um/include/shared/os.h b/arch/um/include/shared/os.h index ebf23012a59b..4ec1bc63213b 100644 --- a/arch/um/include/shared/os.h +++ b/arch/um/include/shared/os.h @@ -319,4 +319,8 @@ extern unsigned long os_get_top_address(void); long syscall(long number, ...); +/* signal.c */ +extern struct uml_pt_regs *get_save_register_state(void); +extern void release_save_register_state(struct uml_pt_regs *r); + #endif diff --git a/arch/um/kernel/signal.c b/arch/um/kernel/signal.c index 57acbd67d85d..558ac7e4df97 100644 --- a/arch/um/kernel/signal.c +++ b/arch/um/kernel/signal.c @@ -6,11 +6,18 @@ #include #include #include +#include #include #include #include #include #include +#include + +static atomic_t reg_depth; + +static struct uml_pt_regs reg_file[4]; +#define REG_SWITCH_TO_KMALLOC_DEPTH 3 EXPORT_SYMBOL(block_signals); EXPORT_SYMBOL(unblock_signals); @@ -111,3 +118,27 @@ void do_signal(struct pt_regs *regs) if (!handled_sig) restore_saved_sigmask(); } + +struct uml_pt_regs *get_save_register_state(void) +{ + struct uml_pt_regs *r; + int depth = atomic_inc_return(®_depth); + + if (depth > REG_SWITCH_TO_KMALLOC_DEPTH) { + WARN(1, "Exceeded uml save register space, should not occur"); + r = kmalloc(sizeof(struct uml_pt_regs), GFP_ATOMIC); + if (!r) + panic("Cannot allocate extra save register space"); + } else { + r = ®_file[depth - 1]; + } + return r; +} + +void release_save_register_state(struct uml_pt_regs *r) +{ + if (atomic_dec_return(®_depth) > REG_SWITCH_TO_KMALLOC_DEPTH) + kfree(r); +} + + diff --git a/arch/um/os-Linux/signal.c b/arch/um/os-Linux/signal.c index bf0acb8aad8b..81765abe1c15 100644 --- a/arch/um/os-Linux/signal.c +++ b/arch/um/os-Linux/signal.c @@ -31,13 +31,9 @@ void (*sig_info[NSIG])(int, struct siginfo *, struct uml_pt_regs *) = { static void sig_handler_common(int sig, struct siginfo *si, mcontext_t *mc) { - struct uml_pt_regs *r; + struct uml_pt_regs *r = get_save_register_state(); int save_errno = errno; - r = uml_kmalloc(sizeof(struct uml_pt_regs), UM_GFP_ATOMIC); - if (!r) - panic("out of memory"); - r->is_user = 0; if (sig == SIGSEGV) { /* For segfaults, we want the data from the sigcontext. */ @@ -53,7 +49,7 @@ static void sig_handler_common(int sig, struct siginfo *si, mcontext_t *mc) errno = save_errno; - free(r); + release_save_register_state(r); } /* @@ -91,17 +87,13 @@ void sig_handler(int sig, struct siginfo *si, mcontext_t *mc) static void timer_real_alarm_handler(mcontext_t *mc) { - struct uml_pt_regs *regs; - - regs = uml_kmalloc(sizeof(struct uml_pt_regs), UM_GFP_ATOMIC); - if (!regs) - panic("out of memory"); + struct uml_pt_regs *regs = get_save_register_state(); if (mc != NULL) get_regs_from_mc(regs, mc); timer_handler(SIGALRM, NULL, regs); - free(regs); + release_save_register_state(regs); } void timer_alarm_handler(int sig, struct siginfo *unused_si, mcontext_t *mc)